feat: one game shell: menu bar, sticky village bar with storage meters and screen tabs, phone tab bar

The village bar now stays on top of every screen in a world, the map and reports
included. Phones get a bottom tab bar instead of an overflowing header.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-29 00:38:54 +01:00
parent d12443bf18
commit 6ff529955b
21 changed files with 462 additions and 262 deletions

View File

@ -10,7 +10,7 @@ This is the Nuxt client for a browser strategy game in the style of Tribal Wars.
- **Light Tailwind UI** (Tailwind 4, theme and component classes like `.panel`, `.btn` in `app/assets/css/main.css`) with simple hand-written inline SVG graphics (`app/components/icon/`, colours in `app/utils/palette.ts`). No paid, hosted or licensed assets: no icon packs, web fonts or images from outside the repo.
- **The village is a ground-level land view** (`app/components/village/Diorama.vue`, geometry in `app/diorama/`): isometric low-poly, drawn as inline SVG, no WebGL. The land fills the frame edge to edge (no island, no sky), cut to the container's shape; narrow screens scroll sideways. There are no tiles or positions: the server only sends each building's `space` and the village's `space.used/max`, and the client lays everything out deterministically in id order. The village core sits in the middle (headquarters on the plaza, storage, hiding places and rally point around it, inside the wall ring when there is one, a dirt edge without). Resource sites stand outside in their zones: iron mines at the rocky hill (upper left), clay pits on clay ground (lower left), timber camps at the forest edge (lower right), farms with fields (right); dirt roads lead from the gates to each zone and forest fills the rest. Levels start at 1. Each type has a model per level tier (1–4, 5–14, 15+); the wall goes stakes → timber → stone with towers. Buildings are focusable buttons that open their details card; a type that doesn't stand yet keeps an invisible empty plot that opens its build card. Buildings never move or light up on hover: only a click outlines the ground under them.
- **Flat medieval colours only**, in the diorama and everywhere else: no gradients (CSS or SVG), no blur or backdrop-blur, no SVG filters or glows, no translucent overlays. Depth comes from the three flat shades per face in `app/diorama/iso.ts`, solid borders and hard shadows. Keep components small and typed, with state in composables (`useVillage`, `useWorld`).
- **The village page is laid out like Die Stämme**: a compact strip with the stock and the screens (Overview, Headquarters, Recruit, Rally point, Workforce) and a red badge for incoming attacks, each screen a child route of `pages/village/[id].vue` sharing one `useVillage()` through `useVillageContext()`. The overview stays clean: the village and a narrow column of boxes (incoming attacks, production, units, population, construction); tables live on the building screens. Reports are a world-level page (`pages/reports/[worldId]`) linked from the header with the unread count.
- **One game shell, like Die Stämme.** Pages inside a world (village screens, map, reports) use `layouts/game.vue`: the dark menu bar (`GameMenuBar`: village, map, reports with the unread count, server clock, account menu), then the sticky village bar (`GameVillageBar`: which village, stock with storage meters, incoming troops, and the village's screens as tabs: Overview, Headquarters, Recruit, Rally point, Workforce). On phones the screens move to a bottom tab bar (`GameTabBar`). The bar's village is the URL's on village screens and the last opened one elsewhere; `GameVillageFrame` holds its one `useVillage()` and shares it through `useVillageContext()`, so the map and reports keep the village on top. Pages outside a world use `layouts/default.vue` (menu bar only). The overview stays clean: the village and a narrow column of boxes (incoming attacks, production, units, population, construction); tables live on the building screens.
- All user-facing text goes through i18n keys, with `en` as the first locale. Sources are `i18n/locales/` and `i18n/pages/` (`@blax-software/i18n` layout); register a new page file in `i18n/messageloader.ts`.
## Mock server

View File

@ -1,24 +1,6 @@
<script setup lang="ts">
const { t } = useI18n()
const { user, logout } = useAuth()
const notices = useNotices()
const route = useRoute()
const { current } = useCurrentWorld()
const unreadReports = useUnreadReports()
/** Inside a world (village, map or reports) the header is that world's navigation. */
const inWorld = computed(() => !!current.value && /^\/(village|map|reports)\//.test(route.path))
const home = computed(() =>
inWorld.value && current.value?.villageId ? `/village/${current.value.villageId}` : '/worlds',
)
const connection = computed(() =>
isMockServer
? { text: t('app.mock'), tone: 'badge-gold' }
: ws.is_opened.value
? { text: t('app.connected'), tone: 'badge-moss' }
: { text: t('app.offline'), tone: 'badge-blood' },
)
const toastTone = {
success: 'border-moss-700 bg-parchment-50 text-moss-700',
@ -30,98 +12,29 @@ const toastTone = {
useHead({ title: () => t('app.title') })
</script>
<!-- Pages pick their frame: `game` inside a world (village, map, reports), `default` outside. -->
<template>
<div class="min-h-dvh">
<!-- A dark wooden bar, like a tavern sign board. -->
<header class="border-b-4 border-wood-950 bg-wood-900 text-parchment-100">
<div class="flex items-center gap-2 px-4 py-2 sm:gap-3 sm:px-6">
<NuxtLink :to="home" class="flex min-w-0 items-center gap-2">
<span class="grid size-8 shrink-0 place-items-center rounded-sm border border-gold-600 bg-blood-700 text-gold-400">
<IconBuilding type="headquarters" class="size-5" />
</span>
<span class="hidden font-display text-lg font-semibold text-gold-400 sm:inline" style="font-variant: small-caps">
{{ t('app.title') }}
</span>
</NuxtLink>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
<nav v-if="user && inWorld && current" class="ml-1 flex items-center gap-1">
<NuxtLink
v-if="current.villageId"
:to="`/village/${current.villageId}`"
class="nav-link"
active-class="nav-link-active"
>
<IconBuilding type="headquarters" class="size-4" />
{{ t('app.village') }}
</NuxtLink>
<NuxtLink :to="`/map/${current.worldId}`" class="nav-link" active-class="nav-link-active">
<IconUi name="pin" class="size-4" />
{{ t('app.map') }}
</NuxtLink>
<NuxtLink :to="`/reports/${current.worldId}`" class="nav-link" active-class="nav-link-active">
<IconUi name="report" class="size-4" />
<span class="hidden sm:inline">{{ t('app.reports') }}</span>
<span v-if="unreadReports" class="badge badge-gold px-1.5 py-0" :title="t('app.unread_reports', { count: unreadReports })">
{{ unreadReports }}
</span>
</NuxtLink>
</nav>
<div class="ml-auto flex min-w-0 items-center gap-2">
<!-- The socket opens with the first request after login, so before that there is nothing to show. -->
<span v-if="user" class="badge" :class="connection.tone">
<span class="size-1.5 rounded-full bg-current" />
{{ connection.text }}
</span>
<template v-if="user">
<span class="hidden truncate text-sm text-parchment-300 md:inline">{{ user.name }}</span>
<!-- Leaving the world is a quiet way out, not a tab. -->
<NuxtLink v-if="inWorld" to="/worlds" class="nav-link px-2" :title="t('app.change_world')">
<IconUi name="globe" class="size-4" />
<span class="hidden lg:inline">{{ t('app.change_world') }}</span>
</NuxtLink>
<button type="button" class="nav-link px-2" :title="t('app.logout')" @click="logout()">
<IconUi name="logout" class="size-4" />
<span class="hidden sm:inline">{{ t('app.logout') }}</span>
</button>
</template>
</div>
</div>
</header>
<main>
<NuxtPage />
</main>
<div class="pointer-events-none fixed inset-x-4 bottom-4 z-50 flex flex-col items-end gap-2">
<TransitionGroup
enter-from-class="translate-y-2 opacity-0"
leave-to-class="opacity-0"
enter-active-class="transition duration-200"
leave-active-class="transition duration-200"
<div class="pointer-events-none fixed inset-x-4 bottom-20 z-50 flex flex-col items-end gap-2 md:bottom-4">
<TransitionGroup
enter-from-class="translate-y-2 opacity-0"
leave-to-class="opacity-0"
enter-active-class="transition duration-200"
leave-active-class="transition duration-200"
>
<button
v-for="n in notices.list.value"
:key="n.id"
type="button"
class="pointer-events-auto w-full max-w-sm rounded-sm border-2 px-3 py-2 text-left text-sm shadow-hard"
:class="toastTone[n.type]"
@click="notices.dismiss(n.id)"
>
<button
v-for="n in notices.list.value"
:key="n.id"
type="button"
class="pointer-events-auto w-full max-w-sm rounded-sm border-2 px-3 py-2 text-left text-sm shadow-hard"
:class="toastTone[n.type]"
@click="notices.dismiss(n.id)"
>
{{ n.text }}
</button>
</TransitionGroup>
</div>
{{ n.text }}
</button>
</TransitionGroup>
</div>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.nav-link {
@apply inline-flex items-center gap-1.5 rounded-sm border border-transparent px-3 py-1.5 text-sm font-semibold text-parchment-100 transition-colors hover:border-wood-600 hover:bg-wood-800 focus-visible:outline-2 focus-visible:outline-gold-500;
}
.nav-link-active {
@apply border-gold-600 bg-wood-800 text-gold-400;
}
</style>

View File

@ -0,0 +1,118 @@
<script setup lang="ts">
const props = defineProps<{
/** Inside a world: show the world's menu (village, map, reports) and its clock. */
inWorld?: boolean
}>()
const { t } = useI18n()
const { user, logout } = useAuth()
const { current } = useCurrentWorld()
const { worlds, refresh } = useWorld()
const unread = useUnreadReports()
const { now } = useServerClock()
const route = useRoute()
const world = computed(() => (props.inWorld && current.value ? (worlds.value?.find((w) => w.id === current.value!.worldId) ?? null) : null))
if (props.inWorld && !worlds.value) refresh()
const home = computed(() => (props.inWorld && current.value?.villageId ? `/village/${current.value.villageId}` : '/worlds'))
const links = computed(() => {
const c = current.value
if (!props.inWorld || !c) return []
return [
...(c.villageId ? [{ key: 'village', to: `/village/${c.villageId}`, icon: 'home' as const, active: route.path.startsWith('/village/') }] : []),
{ key: 'map', to: `/map/${c.worldId}`, icon: 'pin' as const, active: route.path.startsWith('/map/') },
{ key: 'reports', to: `/reports/${c.worldId}`, icon: 'report' as const, active: route.path.startsWith('/reports/') },
]
})
const connection = computed(() =>
isMockServer
? { text: t('app.mock'), tone: 'bg-gold-400' }
: ws.is_opened.value
? { text: t('app.connected'), tone: 'bg-moss-500' }
: { text: t('app.offline'), tone: 'bg-blood-600' },
)
const clock = computed(() => new Date(now.value).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false }))
// The account menu closes when a link in it is followed.
const menu = ref<HTMLDetailsElement | null>(null)
watch(() => route.fullPath, () => menu.value?.removeAttribute('open'))
</script>
<!-- The dark wooden bar on top of every page: the game's main menu, the server clock and the account. -->
<template>
<header class="border-b-4 border-wood-950 bg-wood-900 text-parchment-100">
<div class="mx-auto flex h-12 max-w-[96rem] items-center gap-1 px-2 sm:gap-2 sm:px-4">
<NuxtLink :to="home" class="mr-1 shrink-0 items-center gap-2" :class="links.length ? 'hidden sm:flex' : 'flex'" :title="t('app.title')">
<span class="grid size-8 place-items-center rounded-sm border border-gold-600 bg-blood-700 text-gold-400">
<IconBuilding type="headquarters" class="size-5" />
</span>
<span class="hidden font-display text-lg font-semibold text-gold-400 lg:inline" style="font-variant: small-caps">{{ t('app.title') }}</span>
</NuxtLink>
<nav v-if="links.length" class="flex items-center gap-0.5" :aria-label="t('app.menu')">
<NuxtLink
v-for="l in links"
:key="l.key"
:to="l.to"
class="menu-link"
:class="{ 'menu-link-active': l.active }"
:aria-current="l.active ? 'page' : undefined"
>
<span class="relative">
<IconUi :name="l.icon" class="size-5 sm:size-4" />
<span
v-if="l.key === 'reports' && unread"
class="absolute -top-2 -right-2.5 min-w-4 rounded-full border border-wood-950 bg-gold-400 px-1 text-center text-[10px] leading-3.5 font-bold text-wood-950 sm:hidden"
>
{{ unread }}
</span>
</span>
<span class="hidden sm:inline">{{ t(`app.${l.key}`) }}</span>
<span v-if="l.key === 'reports' && unread" class="hidden rounded-sm bg-gold-400 px-1.5 text-xs font-bold text-wood-950 sm:inline" :title="t('app.unread_reports', { count: unread })">
{{ unread }}
</span>
</NuxtLink>
</nav>
<div class="ml-auto flex min-w-0 items-center gap-2 sm:gap-3">
<span v-if="world" class="hidden truncate text-sm text-parchment-300 md:inline">{{ world.name }}</span>
<span v-if="user" class="flex items-center gap-1.5 text-sm tabular-nums text-parchment-200" :title="`${t('app.server_time')} · ${connection.text}`">
<span class="size-2 rounded-full" :class="connection.tone" />
{{ clock }}
</span>
<details v-if="user" ref="menu" class="relative">
<summary class="menu-link list-none" :title="t('app.account')">
<IconUi name="users" class="size-4" />
<span class="hidden max-w-32 truncate md:inline">{{ user.name }}</span>
</summary>
<div class="absolute right-0 z-50 mt-2 w-52 rounded-sm border-2 border-wood-950 bg-parchment-50 py-1 text-sm text-ink-900 shadow-hard">
<p class="truncate border-b border-parchment-300 px-3 py-1.5 font-semibold">{{ user.name }}</p>
<NuxtLink to="/worlds" class="account-item">
<IconUi name="globe" class="size-4" />
{{ t('app.change_world') }}
</NuxtLink>
<button type="button" class="account-item w-full" @click="logout()">
<IconUi name="logout" class="size-4" />
{{ t('app.logout') }}
</button>
</div>
</details>
</div>
</div>
</header>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.menu-link {
@apply inline-flex h-9 items-center gap-1.5 rounded-sm border border-transparent px-2.5 text-sm font-semibold text-parchment-100 transition-colors hover:border-wood-600 hover:bg-wood-800 focus-visible:outline-2 focus-visible:outline-gold-500;
}
.menu-link-active {
@apply border-gold-600 bg-wood-800 text-gold-400;
}
.account-item {
@apply flex items-center gap-2 px-3 py-1.5 text-left hover:bg-parchment-200;
}
</style>

View File

@ -0,0 +1,41 @@
<script setup lang="ts">
import type { Resource } from '~/types/game'
const props = defineProps<{
resource: Resource
amount: number
cap: number
/** Per hour; food can be negative. */
rate: number
}>()
const { t } = useI18n()
const share = computed(() => Math.max(0, Math.min(1, props.amount / props.cap)))
const full = computed(() => props.amount >= props.cap)
/** Seconds until the storage is full at this rate; null when it won't fill. */
const fullIn = computed(() => (props.rate > 0 && !full.value ? ((props.cap - props.amount) / props.rate) * 3600 : null))
const title = computed(() =>
[
`${t(`resources.${props.resource}`)}: ${formatAmount(props.amount)} / ${props.cap}`,
t('village.resources.per_hour', { rate: formatRate(props.rate) }),
full.value ? t('village.resources.full') : fullIn.value !== null ? t('village.resources.full_in', { time: formatDuration(fullIn.value) }) : null,
]
.filter(Boolean)
.join(' · '),
)
</script>
<!-- One resource in the village bar: icon, amount, and a thin meter of how full the storage is. -->
<template>
<span class="inline-flex min-w-0 items-center gap-1.5" :title="title">
<IconResource :resource="resource" class="size-5 shrink-0" />
<span class="flex min-w-0 flex-col">
<span class="text-sm leading-tight font-semibold tabular-nums" :class="full ? 'text-blood-700' : rate < 0 ? 'text-blood-700' : 'text-ink-900'">
{{ formatAmount(amount) }}
</span>
<span class="mt-0.5 h-1 w-full min-w-10 overflow-hidden rounded-[1px] bg-parchment-300" aria-hidden="true">
<span class="block h-full" :class="full ? 'bg-blood-700' : share > 0.85 ? 'bg-gold-500' : 'bg-moss-600'" :style="{ width: `${share * 100}%` }" />
</span>
</span>
</span>
</template>

View File

@ -0,0 +1,26 @@
<script setup lang="ts">
const props = defineProps<{ villageId: number }>()
const route = useRoute()
const base = computed(() => `/village/${props.villageId}`)
const isActive = (path: string) => route.path === `${base.value}${path}`
</script>
<!-- On phones the village's screens sit in a bar at the bottom, in thumb reach. -->
<template>
<nav
class="fixed inset-x-0 bottom-0 z-40 grid grid-cols-5 border-t-4 border-wood-950 bg-wood-900 pb-[env(safe-area-inset-bottom)] md:hidden"
:aria-label="$t('village.screens.label')"
>
<NuxtLink
v-for="s in VILLAGE_SCREENS"
:key="s.name"
:to="`${base}${s.path}`"
class="flex flex-col items-center gap-0.5 px-1 pt-1.5 pb-1 text-[11px] leading-tight font-semibold"
:class="isActive(s.path) ? 'bg-wood-800 text-gold-400' : 'text-parchment-200'"
:aria-current="isActive(s.path) ? 'page' : undefined"
>
<IconBuilding :type="s.icon" class="size-5" />
<span class="w-full truncate text-center">{{ $t(`village.screens.short.${s.name}`) }}</span>
</NuxtLink>
</nav>
</template>

View File

@ -0,0 +1,125 @@
<script setup lang="ts">
import type { Resources, VillageState } from '~/types/game'
const props = defineProps<{ village: VillageState; resources: Resources }>()
const v = useVillageContext()
const route = useRoute()
const base = computed(() => `/village/${props.village.id}`)
const incoming = computed(() => props.village.movements.filter((m) => m.direction === 'in'))
const attacks = computed(() => incoming.value.filter((m) => m.type === 'attack'))
const nextAttack = computed(() => attacks.value[0] ?? null)
const support = computed(() => incoming.value.length - attacks.value.length)
const isActive = (path: string) => route.path === `${base.value}${path}`
/** The new name while the village is being renamed. */
const renaming = ref<string | null>(null)
async function rename() {
if (renaming.value !== null && (await v.rename(renaming.value))) renaming.value = null
}
</script>
<!--
The village bar under the menu, on every screen of the world: which village, its
stock with storage meters, incoming troops, and its screens as tabs (on phones the
tabs move to the bottom bar).
-->
<template>
<div class="sticky top-0 z-30 border-b-2 border-wood-700 bg-parchment-200">
<div class="mx-auto flex max-w-[96rem] flex-wrap items-center gap-x-6 gap-y-2 px-3 pt-2 pb-2 sm:px-6 md:pb-0">
<!-- Which village. -->
<div class="flex min-w-0 flex-1 items-center gap-2 md:flex-none md:pb-2">
<form v-if="renaming !== null" class="flex items-center gap-1.5" @submit.prevent="rename">
<input v-model="renaming" type="text" maxlength="32" class="input w-44 px-2 py-0.5" :aria-label="$t('village.rename')" @keydown.esc="renaming = null" />
<button type="submit" class="btn btn-primary px-2 py-0.5" :disabled="v.busy.value || !renaming.trim()">{{ $t('village.rename_save') }}</button>
<button type="button" class="btn btn-ghost px-2 py-0.5" @click="renaming = null">{{ $t('village.rename_cancel') }}</button>
</form>
<template v-else>
<div class="min-w-0">
<div class="flex min-w-0 items-center gap-1.5">
<NuxtLink :to="base" class="truncate font-display text-lg leading-tight font-semibold text-wood-900 hover:text-blood-700" style="font-variant: small-caps">
{{ village.name }}
</NuxtLink>
<button type="button" class="shrink-0 text-ink-400 hover:text-blood-700" :title="$t('village.rename')" @click="renaming = village.name">
<IconUi name="edit" class="size-3.5" />
<span class="sr-only">{{ $t('village.rename') }}</span>
</button>
</div>
<div class="flex items-center gap-2 text-xs text-ink-500">
<NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: village.x, y: village.y } }"
class="tabular-nums hover:text-blood-700 hover:underline"
:title="$t('village.on_map')"
>
{{ village.x }}|{{ village.y }} {{ village.continent }}
</NuxtLink>
<span>·</span>
<span class="tabular-nums">{{ $t('village.points', { points: village.points }) }}</span>
</div>
</div>
</template>
</div>
<!-- Incoming troops, red for attacks, with the next arrival. -->
<NuxtLink
v-if="incoming.length"
:to="`${base}/rally`"
class="order-2 flex items-center gap-2 rounded-sm border-2 px-2 py-1 text-sm font-semibold md:order-3 md:mb-2"
:class="attacks.length ? 'border-blood-800 bg-blood-700 text-parchment-50' : 'border-moss-700 bg-moss-600 text-parchment-50'"
:title="$t('village.incoming', { count: attacks.length })"
>
<span v-if="attacks.length" class="inline-flex items-center gap-1"><IconUi name="attack" class="size-4" />{{ attacks.length }}</span>
<span v-if="support" class="inline-flex items-center gap-1"><IconUi name="support" class="size-4" />{{ support }}</span>
<span v-if="nextAttack" class="hidden tabular-nums sm:inline">{{ formatDuration(v.secondsUntil(nextAttack.arrives_at)) }}</span>
</NuxtLink>
<!-- The stock: four resources with their storage meters, the storage cap and the villagers. -->
<div class="order-3 grid w-full grid-cols-[repeat(4,minmax(0,1fr))_auto] items-center gap-x-3 md:order-2 md:mb-2 md:ml-auto md:flex md:w-auto md:gap-x-4">
<GameResource
v-for="r in (['wood', 'clay', 'iron', 'food'] as const)"
:key="r"
:resource="r"
:amount="resources[r]"
:cap="village.storage_cap"
:rate="village.rates[r]"
/>
<span class="hidden items-center gap-1.5 text-sm text-ink-700 tabular-nums lg:inline-flex" :title="$t('village.resources.cap')">
<IconBuilding type="storage" class="size-4" />{{ village.storage_cap }}
</span>
<NuxtLink
:to="`${base}/workforce`"
class="inline-flex items-center gap-1.5 text-sm text-ink-700 tabular-nums hover:text-blood-700"
:title="`${$t('village.population.villagers')}: ${village.population.villagers} · ${$t('village.population.upkeep')}: ${village.population.upkeep}/${village.population.food_budget}`"
>
<IconUi name="users" class="size-4" />{{ village.population.villagers }}
</NuxtLink>
</div>
<!-- The village's screens as tabs that sit on the page below. -->
<nav class="order-4 -mb-0.5 hidden w-full items-end gap-1 md:flex" :aria-label="$t('village.screens.label')">
<NuxtLink
v-for="s in VILLAGE_SCREENS"
:key="s.name"
:to="`${base}${s.path}`"
class="tab"
:class="{ 'tab-active': isActive(s.path) }"
:aria-current="isActive(s.path) ? 'page' : undefined"
>
<IconBuilding :type="s.icon" class="size-4" />
{{ $t(`village.screens.${s.name}`) }}
</NuxtLink>
</nav>
</div>
</div>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.tab {
@apply inline-flex items-center gap-1.5 rounded-t-sm border-2 border-b-0 border-transparent px-3 pt-1.5 pb-2 text-sm font-semibold text-wood-800 hover:bg-parchment-100 hover:text-blood-700;
}
.tab-active {
@apply border-wood-700 bg-parchment-100 text-blood-700;
}
</style>

View File

@ -0,0 +1,31 @@
<script setup lang="ts">
const props = defineProps<{ villageId: number }>()
// One village state for every screen in the world: the village bar, the village's own
// screens, the map and the reports all read it through useVillageContext().
const v = useVillage(props.villageId)
provideVillage(v)
v.refresh()
const { enter } = useCurrentWorld()
watch(v.village, (state) => {
if (state) enter(state.world_id, state.id)
})
</script>
<template>
<div>
<GameVillageBar v-if="v.village.value && v.resources.value" :village="v.village.value" :resources="v.resources.value" />
<VillageFoodWarning
v-if="v.village.value"
:starvation="v.village.value.starvation"
:levy="v.village.value.levy"
:seconds-until="v.secondsUntil"
/>
<p v-if="v.error.value" class="alert mx-4 mt-3 sm:mx-6" role="alert">{{ v.error.value }}</p>
<main>
<slot />
</main>
<GameTabBar :village-id="villageId" />
</div>
</template>

View File

@ -16,6 +16,7 @@ export type UiIconName =
| 'report'
| 'eye'
| 'edit'
| 'home'
defineProps<{ name: UiIconName }>()
</script>
@ -72,6 +73,10 @@ defineProps<{ name: UiIconName }>()
<path d="M6 3h9l4 4v14H6z" />
<path d="M14 3v5h5M9 12h7M9 16h7" />
</g>
<g v-else-if="name === 'home'">
<path d="M3 11l9-7 9 7" />
<path d="M5 10v10h14V10M10 20v-6h4v6" />
</g>
<path v-else-if="name === 'edit'" d="M4 20l1-4L16 5l3 3L8 19zM14 7l3 3" />
<g v-else-if="name === 'eye'">
<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z" />

View File

@ -1,31 +0,0 @@
<script setup lang="ts">
import type { Resource, Resources, VillageState } from '~/types/game'
defineProps<{ village: VillageState; resources: Resources }>()
const list: Resource[] = ['wood', 'clay', 'iron', 'food']
</script>
<!-- Stock in storage, one line like the Die Stämme header; rates live in the overview's production box. -->
<template>
<div class="flex flex-wrap items-center gap-x-4 gap-y-1 text-sm tabular-nums">
<span
v-for="r in list"
:key="r"
class="inline-flex items-center gap-1.5 font-semibold"
:class="resources[r] >= village.storage_cap ? 'text-blood-700' : 'text-ink-900'"
:title="`${$t(`resources.${r}`)}: ${$t('village.resources.per_hour', { rate: formatRate(village.rates[r]) })}`"
>
<IconResource :resource="r" class="size-5" />
{{ formatAmount(resources[r]) }}
</span>
<span class="inline-flex items-center gap-1.5 text-ink-700" :title="$t('village.resources.cap')">
<IconBuilding type="storage" class="size-4" />
{{ village.storage_cap }}
</span>
<span class="inline-flex items-center gap-1.5 text-ink-700" :title="$t('village.population.villagers')">
<IconUi name="users" class="size-4" />
{{ village.population.villagers }}
</span>
</div>
</template>

View File

@ -0,0 +1,14 @@
/**
* The server's clock: the client clock plus the offset the last server snapshot showed.
* Shared, so the header clock and every countdown agree.
*/
export function useServerClock() {
const offset = useState<number>('clock.offset', () => 0)
const clock = useNow()
const now = computed(() => clock.value + offset.value)
/** Call with a snapshot's `server_time`. */
const sync = (serverTime: string) => (offset.value = Date.parse(serverTime) - Date.now())
return { now, sync }
}

View File

@ -12,19 +12,16 @@ export function useVillage(villageId: number) {
const error = ref<string | null>(null)
const busy = ref(false)
const errorText = useErrorText()
const clock = useNow()
const unreadReports = useUnreadReports()
// Server time minus client time, so projections use the server's clock.
const offset = ref(0)
const now = computed(() => clock.value + offset.value)
// Projections and countdowns run on the server's clock.
const { now, sync } = useServerClock()
async function run(event: string, data: object = {}) {
busy.value = true
error.value = null
try {
const state = await ws.send<VillageState>(event, { village_id: villageId, ...data })
offset.value = Date.parse(state.server_time) - Date.now()
sync(state.server_time)
village.value = state
unreadReports.value = state.unread_reports
return true

9
app/layouts/default.vue Normal file
View File

@ -0,0 +1,9 @@
<!-- Outside a world (login, world list): the menu bar without game navigation. -->
<template>
<div class="min-h-dvh">
<GameMenuBar />
<main>
<slot />
</main>
</div>
</template>

27
app/layouts/game.vue Normal file
View File

@ -0,0 +1,27 @@
<script setup lang="ts">
const route = useRoute()
const { current } = useCurrentWorld()
/**
* The village whose bar stays on top of every screen in the world: the one in the
* URL on village screens, else the one last opened in this world (map, reports).
*/
const villageId = computed(() => {
if (route.path.startsWith('/village/')) return Number(route.params.id)
const worldId = Number(route.params.worldId)
return current.value && current.value.worldId === worldId ? current.value.villageId : null
})
</script>
<!-- Inside a world: the menu bar, then the village bar and its screens, like Die Stämme. -->
<template>
<div class="min-h-dvh pb-16 md:pb-0">
<GameMenuBar in-world />
<GameVillageFrame v-if="villageId" :key="villageId" :village-id="villageId">
<slot />
</GameVillageFrame>
<main v-else>
<slot />
</main>
</div>
</template>

View File

@ -1,6 +1,8 @@
<script setup lang="ts">
import type { MapVillage } from '~/types/map'
definePageMeta({ layout: 'game' })
const route = useRoute()
const router = useRouter()
const worldId = Number(route.params.worldId)

View File

@ -1,6 +1,8 @@
<script setup lang="ts">
import type { Report } from '~/types/game'
definePageMeta({ layout: 'game' })
const route = useRoute()
const worldId = Number(route.params.worldId)
const { error, show } = useReports(worldId)

View File

@ -1,6 +1,8 @@
<script setup lang="ts">
import type { Report } from '~/types/game'
definePageMeta({ layout: 'game' })
const route = useRoute()
const worldId = Number(route.params.worldId)
const { page, error, busy, load, remove, readAll } = useReports(worldId)

View File

@ -1,110 +1,11 @@
<script setup lang="ts">
const route = useRoute()
const id = Number(route.params.id)
const v = useVillage(id)
const { village, resources, error } = v
provideVillage(v)
const { enter } = useCurrentWorld()
watch(village, (state) => {
if (state) enter(state.world_id, state.id)
})
v.refresh()
/** The new name while the village is being renamed. */
const renaming = ref<string | null>(null)
async function rename() {
if (renaming.value !== null && (await v.rename(renaming.value))) renaming.value = null
}
const incomingAttacks = computed(() => village.value?.movements.filter((m) => m.direction === 'in' && m.type === 'attack') ?? [])
// The screens of a village, like the building links along the top in Die Stämme.
const screens = [
{ name: 'overview', path: '', icon: 'headquarters' },
{ name: 'build', path: '/build', icon: 'storage' },
{ name: 'recruit', path: '/recruit', icon: 'barracks' },
{ name: 'rally', path: '/rally', icon: 'rally_point' },
{ name: 'workforce', path: '/workforce', icon: 'farm' },
] as const
// The village bar and its tabs live in the game layout; this only waits for the
// village before its screens render.
definePageMeta({ layout: 'game' })
const v = useVillageContext()
</script>
<template>
<div v-if="village && resources">
<header class="sticky top-0 z-30 border-b-2 border-wood-700 bg-parchment-200">
<div class="flex flex-wrap items-center gap-x-5 gap-y-2 px-4 py-2 sm:px-6">
<div class="flex min-w-0 items-baseline gap-2">
<form v-if="renaming !== null" class="flex items-center gap-1.5" @submit.prevent="rename">
<input
v-model="renaming"
type="text"
maxlength="32"
class="input w-44 px-2 py-0.5"
:aria-label="$t('village.rename')"
@keydown.esc="renaming = null"
/>
<button type="submit" class="btn btn-primary px-2 py-0.5" :disabled="v.busy.value || !renaming.trim()">{{ $t('village.rename_save') }}</button>
<button type="button" class="btn btn-ghost px-2 py-0.5" @click="renaming = null">{{ $t('village.rename_cancel') }}</button>
</form>
<template v-else>
<h1 class="truncate font-display text-lg font-semibold text-wood-900" style="font-variant: small-caps">{{ village.name }}</h1>
<button type="button" class="self-center text-ink-500 hover:text-blood-700" :title="$t('village.rename')" @click="renaming = village.name">
<IconUi name="edit" class="size-3.5" />
<span class="sr-only">{{ $t('village.rename') }}</span>
</button>
</template>
<NuxtLink
:to="{ path: `/map/${village.world_id}`, query: { x: village.x, y: village.y } }"
class="text-sm whitespace-nowrap text-ink-700 tabular-nums hover:text-blood-700 hover:underline"
:title="$t('village.on_map')"
>
({{ village.x }}|{{ village.y }}) {{ village.continent }}
</NuxtLink>
<span class="hidden text-xs whitespace-nowrap text-ink-500 sm:inline">{{ $t('village.points', { points: village.points }) }}</span>
</div>
<nav class="flex items-center gap-0.5" :aria-label="$t('village.screens.label')">
<NuxtLink
v-for="s in screens"
:key="s.name"
:to="`/village/${id}${s.path}`"
class="screen-link"
:class="{ 'screen-link-active': route.path === `/village/${id}${s.path}` }"
>
<IconBuilding :type="s.icon" class="size-4" />
<span :class="s.name === 'overview' ? 'hidden sm:inline' : ''">{{ $t(`village.screens.${s.name}`) }}</span>
</NuxtLink>
</nav>
<NuxtLink
v-if="incomingAttacks.length"
:to="`/village/${id}/rally`"
class="badge badge-blood"
:title="$t('village.incoming', { count: incomingAttacks.length })"
>
<IconUi name="attack" class="size-3.5" />
{{ incomingAttacks.length }}
</NuxtLink>
<VillageResourceBar class="lg:ml-auto" :village="village" :resources="resources" />
</div>
<VillageFoodWarning :starvation="village.starvation" :levy="village.levy" :seconds-until="v.secondsUntil" />
</header>
<p v-if="error" class="alert mx-4 mt-3 sm:mx-6" role="alert">{{ error }}</p>
<NuxtPage />
</div>
<div v-else class="px-4 py-16 text-center">
<p v-if="error" class="alert inline-block" role="alert">{{ error }}</p>
<p v-else class="text-sm text-ink-500">{{ $t('common.loading') }}</p>
</div>
<NuxtPage v-if="v.village.value && v.resources.value" />
<p v-else-if="!v.error.value" class="px-4 py-16 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
</template>
<style scoped>
@reference "~/assets/css/main.css";
.screen-link {
@apply inline-flex items-center gap-1.5 rounded-sm border border-transparent px-2 py-1 text-sm font-semibold text-wood-900 hover:border-wood-600;
}
.screen-link-active {
@apply border-wood-700 bg-parchment-50 text-blood-700;
}
</style>

View File

@ -29,7 +29,7 @@ async function resetMock() {
<p v-if="!worlds" class="px-4 py-8 text-center text-sm text-ink-500">{{ $t('common.loading') }}</p>
<p v-else-if="!worlds.length" class="px-4 py-8 text-center text-sm text-ink-500">{{ $t('worlds.none') }}</p>
<ul v-else class="divide-y-2 divide-parchment-200">
<li v-for="w in worlds" :key="w.id" class="flex items-center gap-4 px-4 py-4">
<li v-for="w in worlds" :key="w.id" class="flex flex-wrap items-center gap-x-4 gap-y-3 px-4 py-4">
<span class="grid size-11 shrink-0 place-items-center rounded-sm border-2 border-wood-700 bg-moss-600 text-parchment-50">
<IconUi name="globe" class="size-6" />
</span>
@ -40,16 +40,18 @@ async function resetMock() {
<span v-if="w.village_id" class="badge badge-moss">{{ $t('worlds.joined') }}</span>
</p>
</div>
<button
type="button"
class="btn"
:class="w.village_id ? 'btn-primary' : 'btn-outline'"
:disabled="busy"
@click="enter(w.id)"
>
{{ w.village_id ? $t('worlds.open') : $t('worlds.join') }}
</button>
<NuxtLink v-if="w.village_id" :to="`/map/${w.id}`" class="btn btn-outline" @click="enterWorld(w.id, w.village_id)">{{ $t('worlds.map') }}</NuxtLink>
<div class="flex w-full gap-2 sm:w-auto">
<button
type="button"
class="btn flex-1 sm:flex-none"
:class="w.village_id ? 'btn-primary' : 'btn-outline'"
:disabled="busy"
@click="enter(w.id)"
>
{{ w.village_id ? $t('worlds.open') : $t('worlds.join') }}
</button>
<NuxtLink v-if="w.village_id" :to="`/map/${w.id}`" class="btn btn-outline" @click="enterWorld(w.id, w.village_id)">{{ $t('worlds.map') }}</NuxtLink>
</div>
</li>
</ul>
</div>

10
app/utils/screens.ts Normal file
View File

@ -0,0 +1,10 @@
import type { BuildingType } from '~/types/game'
/** A village's own screens, like the building links of Die Stämme: the bar's tabs and the phone's tab bar. */
export const VILLAGE_SCREENS: { name: 'overview' | 'build' | 'recruit' | 'rally' | 'workforce'; path: string; icon: BuildingType }[] = [
{ name: 'overview', path: '', icon: 'headquarters' },
{ name: 'build', path: '/build', icon: 'storage' },
{ name: 'recruit', path: '/recruit', icon: 'barracks' },
{ name: 'rally', path: '/rally', icon: 'rally_point' },
{ name: 'workforce', path: '/workforce', icon: 'farm' },
]

View File

@ -1,6 +1,9 @@
export default {
app: {
title: 'Village Politics',
menu: 'Main menu',
account: 'Account',
server_time: 'Server time',
map: 'Map',
reports: 'Reports',
unread_reports: '{count} unread reports',

View File

@ -15,6 +15,7 @@ export default {
recruit: 'Recruit',
rally: 'Rally point',
workforce: 'Workforce',
short: { overview: 'Village', build: 'Build', recruit: 'Recruit', rally: 'Rally', workforce: 'Workers' },
},
side: {
production: 'Production',
@ -35,6 +36,8 @@ export default {
title: 'Resources',
cap: 'Storage cap',
per_hour: '{rate}/h',
full: 'storage full',
full_in: 'full in {time}',
},
population: {
title: 'Population',