game-village-politics-frontend/app/components/quest/Book.vue

239 lines
10 KiB
Vue
Raw Permalink Normal View History

<script setup lang="ts">
import type { BuildingType, Cost, Quest, QuestGoal, QuestLine, QuestScreen, Resources } from '~/types/game'
const open = useQuestBookOpen()
const v = useVillageContext()
const q = useQuests()
const { t, te } = useI18n()
const dialog = ref<HTMLDialogElement | null>(null)
const tab = ref<'quests' | 'rewards'>('quests')
const lineKey = ref<QuestLine['line'] | null>(null)
// The native dialog keeps focus inside and closes on Esc; the page behind stays as it is.
async function show() {
await q.load()
// Open on a line with something to claim, else on the one last looked at.
const ready = q.book.value?.lines.find((l) => l.quest?.complete)
if (ready) lineKey.value = ready.line
lineKey.value ??= q.book.value?.lines[0]?.line ?? null
if (open.value && dialog.value && !dialog.value.open) dialog.value.showModal()
}
watch(open, (isOpen) => (isOpen ? show() : dialog.value?.close()))
onMounted(() => {
if (open.value) show()
})
const lines = computed(() => q.book.value?.lines ?? [])
const line = computed(() => lines.value.find((l) => l.line === lineKey.value) ?? null)
const rewards = computed(() => q.book.value?.rewards ?? [])
const village = computed(() => v.village.value!)
const SCREEN_PATHS: Record<Exclude<QuestScreen, 'map' | 'reports'>, string> = {
overview: '',
recruit: '/recruit',
rally: '/rally',
workforce: '/workforce',
}
/** Where a goal's "go there" leads: a building's row on the headquarters screen, or a screen. */
function hintTo(goal: QuestGoal) {
const base = `/village/${village.value.id}`
if (goal.hint === 'map') return `/map/${village.value.world_id}`
if (goal.hint === 'reports') return `/reports/${village.value.world_id}`
if (goal.hint in SCREEN_PATHS) return `${base}${SCREEN_PATHS[goal.hint as keyof typeof SCREEN_PATHS]}`
return { path: `${base}/build`, query: { hint: goal.hint } }
}
function goalText(goal: QuestGoal) {
if (goal.kind === 'building') return t('quests.goal.building', { building: t(`buildings.${goal.subject}`), level: goal.need })
if (goal.kind === 'instances') return t('quests.goal.instances', { building: t(`buildings.${goal.subject}`), count: goal.need })
const key = `quests.counter.${goal.subject.replace('.', '_')}`
return te(key) ? t(key, { count: goal.need }) : goal.subject
}
const title = (key: string) => (te(`quests.q.${key}.title`) ? t(`quests.q.${key}.title`) : key)
const story = (key: string) => (te(`quests.q.${key}.text`) ? t(`quests.q.${key}.text`) : '')
/** The first goal still open, which is where "go there" points. */
const nextGoal = (quest: Quest) => quest.goals.find((g) => !g.met) ?? null
const asCost = (r: Partial<Resources>): Cost => ({ wood: r.wood ?? 0, clay: r.clay ?? 0, iron: r.iron ?? 0 })
/** True when the stock plus these resources would not fit the storage. */
function overflows(r: Partial<Resources>) {
const have = v.resources.value
if (!have) return false
return (['wood', 'clay', 'iron'] as const).some((k) => have[k] + (r[k] ?? 0) > village.value.storage_cap)
}
const rewardTotal = computed(() =>
rewards.value.reduce<Cost>((sum, r) => ({ wood: sum.wood + (r.reward.wood ?? 0), clay: sum.clay + (r.reward.clay ?? 0), iron: sum.iron + (r.reward.iron ?? 0) }), { wood: 0, clay: 0, iron: 0 }),
)
async function go(goal: QuestGoal) {
open.value = false
await navigateTo(hintTo(goal))
}
</script>
<!--
The quest book: quest lines on the left, the current quest of the chosen line on the
right, and a second tab with the level rewards. A plain framed dialog with no veil.
-->
<template>
<dialog
ref="dialog"
class="quest-book m-auto w-[min(46rem,calc(100vw-1rem))] border border-wood-900 bg-parchment-100 p-0 float text-ink-900"
:aria-label="$t('quests.title')"
@close="open = false"
>
<div class="panel-head">
<span class="panel-title">{{ $t('quests.title') }}</span>
<button type="button" class="btn btn-ghost px-1" :title="$t('quests.close')" @click="open = false">
<IconUi name="close" class="size-4" />
<span class="sr-only">{{ $t('quests.close') }}</span>
</button>
</div>
<div class="space-y-2 p-2.5">
<UiSubTabs
v-model="tab"
:tabs="[
{ key: 'quests', label: $t('quests.tab_quests'), count: village.quests.claimable },
{ key: 'rewards', label: $t('quests.tab_rewards'), count: rewards.length },
]"
/>
<p v-if="q.error.value" class="alert" role="alert">{{ q.error.value }}</p>
<div v-if="tab === 'quests'" class="grid gap-2 sm:grid-cols-[12rem_minmax(0,1fr)]">
<nav class="self-start border border-wood-700 bg-parchment-200 text-sm" :aria-label="$t('quests.lines_label')">
<button
v-for="l in lines"
:key="l.line"
type="button"
class="flex w-full items-center justify-between gap-2 border-b border-parchment-300 px-2 py-1 text-left font-semibold last:border-b-0"
:class="l.line === lineKey ? 'bg-wood-800 text-gold-400' : 'text-wood-950 hover:bg-parchment-100 hover:text-blood-700'"
:aria-current="l.line === lineKey ? 'true' : undefined"
@click="lineKey = l.line"
>
<span>{{ $t(`quests.line.${l.line}`) }}</span>
<span class="tabular-nums" :class="l.quest?.complete && l.line !== lineKey ? 'text-blood-700' : ''">
<template v-if="l.quest?.complete">!</template>
{{ l.done }}/{{ l.total }}
</span>
</button>
</nav>
<section v-if="line" class="panel">
<template v-if="line.quest">
<div class="panel-head">
<span class="font-bold text-wood-950">{{ title(line.quest.key) }}</span>
<span class="text-xs text-ink-500 tabular-nums">{{ $t('quests.step', { n: line.done + 1, total: line.total }) }}</span>
</div>
<div class="panel-body space-y-2.5 text-sm">
<p v-if="story(line.quest.key)" class="text-ink-700">{{ story(line.quest.key) }}</p>
<table class="grid-table">
<thead>
<tr>
<th>{{ $t('quests.goals') }}</th>
<th class="w-28">{{ $t('quests.progress') }}</th>
</tr>
</thead>
<tbody>
<tr v-for="(goal, i) in line.quest.goals" :key="i">
<td :class="goal.met ? 'text-moss-700' : ''">
<span v-if="goal.met" aria-hidden="true">✓ </span>{{ goalText(goal) }}
</td>
<td>
<div class="flex items-center gap-1.5">
<UiBar :value="goal.have / goal.need" :tone="goal.met ? 'emerald' : 'amber'" />
<span class="text-xs tabular-nums">{{ goal.have }}/{{ goal.need }}</span>
</div>
</td>
</tr>
</tbody>
</table>
<div class="flex flex-wrap items-center justify-between gap-2 border-t border-parchment-300 pt-2">
<span class="inline-flex items-center gap-2">
<span class="font-semibold text-ink-700">{{ $t('quests.reward') }}</span>
<UiCost :cost="asCost(line.quest.reward)" />
</span>
<button
v-if="line.quest.complete"
type="button"
class="btn btn-primary"
:disabled="q.busy.value"
@click="q.claim(line.quest.key)"
>
{{ $t('quests.claim') }}
</button>
<button v-else-if="nextGoal(line.quest)" type="button" class="btn btn-outline" @click="go(nextGoal(line.quest)!)">
{{ $t('quests.go') }}
</button>
</div>
<p v-if="line.quest.complete && overflows(line.quest.reward)" class="text-xs text-blood-700">{{ $t('quests.overflow') }}</p>
</div>
</template>
<p v-else class="panel-body text-sm text-ink-500">{{ $t('quests.line_done') }}</p>
</section>
</div>
<div v-else class="space-y-2 text-sm">
<p class="text-ink-700">{{ $t('quests.rewards_about', { share: Math.round((q.book.value?.share ?? 0) * 100) }) }}</p>
<section class="panel overflow-x-auto">
<table class="grid-table min-w-[28rem]">
<thead>
<tr>
<th>{{ $t('quests.building') }}</th>
<th>{{ $t('quests.reached_in') }}</th>
<th>{{ $t('quests.reward') }}</th>
<th class="text-right" />
</tr>
</thead>
<tbody>
<tr v-for="r in rewards" :key="r.id">
<td class="font-semibold">{{ $t('quests.level_of', { building: $t(`buildings.${r.type}`), level: r.level }) }}</td>
<td class="text-ink-500">{{ r.village.name }}</td>
<td><UiCost :cost="asCost(r.reward)" nowrap /></td>
<td class="text-right">
<button type="button" class="btn btn-outline" :disabled="q.busy.value" @click="q.collect([r.id])">{{ $t('quests.collect') }}</button>
</td>
</tr>
<tr v-if="!rewards.length">
<td colspan="4" class="text-ink-500">{{ $t('quests.no_rewards') }}</td>
</tr>
</tbody>
</table>
</section>
<div v-if="rewards.length" class="flex flex-wrap items-center justify-end gap-2">
<span v-if="overflows(rewardTotal)" class="text-xs text-blood-700">{{ $t('quests.overflow') }}</span>
<UiCost :cost="rewardTotal" nowrap />
<button type="button" class="btn btn-primary" :disabled="q.busy.value" @click="q.collect()">{{ $t('quests.collect_all') }}</button>
</div>
<section v-if="q.book.value?.upcoming.length" class="panel overflow-x-auto">
<div class="panel-head"><span class="font-semibold">{{ $t('quests.upcoming') }}</span></div>
<table class="grid-table min-w-[22rem]">
<tbody>
<tr v-for="u in q.book.value.upcoming" :key="u.type">
<td>{{ $t('quests.level_of', { building: $t(`buildings.${u.type as BuildingType}`), level: u.level }) }}</td>
<td class="text-right"><UiCost :cost="asCost(u.reward)" nowrap /></td>
</tr>
</tbody>
</table>
</section>
</div>
</div>
</dialog>
</template>
<style scoped>
/* No veil behind the book: the page stays visible and untouched. */
.quest-book::backdrop {
background: transparent;
}
</style>