diff --git a/app/components/village/BuildForm.vue b/app/components/village/BuildForm.vue new file mode 100644 index 0000000..b3e2489 --- /dev/null +++ b/app/components/village/BuildForm.vue @@ -0,0 +1,55 @@ + + + diff --git a/app/components/village/BuildQueue.vue b/app/components/village/BuildQueue.vue new file mode 100644 index 0000000..fa743ef --- /dev/null +++ b/app/components/village/BuildQueue.vue @@ -0,0 +1,49 @@ + + + diff --git a/app/components/village/BuildingTable.vue b/app/components/village/BuildingTable.vue new file mode 100644 index 0000000..8e0c830 --- /dev/null +++ b/app/components/village/BuildingTable.vue @@ -0,0 +1,73 @@ + + + diff --git a/app/components/village/Grid.vue b/app/components/village/Grid.vue new file mode 100644 index 0000000..e6d9794 --- /dev/null +++ b/app/components/village/Grid.vue @@ -0,0 +1,92 @@ + + + + + diff --git a/app/components/village/PopulationPanel.vue b/app/components/village/PopulationPanel.vue new file mode 100644 index 0000000..e8daa2c --- /dev/null +++ b/app/components/village/PopulationPanel.vue @@ -0,0 +1,39 @@ + + + diff --git a/app/components/village/ResourceBar.vue b/app/components/village/ResourceBar.vue new file mode 100644 index 0000000..458a2c4 --- /dev/null +++ b/app/components/village/ResourceBar.vue @@ -0,0 +1,30 @@ + + + diff --git a/app/composables/useNow.ts b/app/composables/useNow.ts new file mode 100644 index 0000000..a4bd8ff --- /dev/null +++ b/app/composables/useNow.ts @@ -0,0 +1,8 @@ +const now = ref(Date.now()) +let timer: ReturnType | undefined + +/** A shared clock for projections and countdowns, ticking four times a second. */ +export function useNow() { + timer ??= setInterval(() => (now.value = Date.now()), 250) + return readonly(now) +} diff --git a/app/composables/useVillage.ts b/app/composables/useVillage.ts new file mode 100644 index 0000000..16b83bb --- /dev/null +++ b/app/composables/useVillage.ts @@ -0,0 +1,84 @@ +import type { BuildingType, Cost, Resource, Resources, VillageState, VillageUpdated } from '~/types/game' + +const RESOURCES: Resource[] = ['wood', 'clay', 'iron', 'food'] + +/** + * One village: the last server snapshot, resources projected to now, and the intents. + * Call it from a page's setup(), since it listens for pushes while that page is mounted. + */ +export function useVillage(villageId: number) { + const village = ref(null) + const error = ref(null) + const busy = ref(false) + const errorText = useErrorText() + const clock = useNow() + + // Server time minus client time, so projections use the server's clock. + const offset = ref(0) + const now = computed(() => clock.value + offset.value) + + async function run(event: string, data: object = {}) { + busy.value = true + error.value = null + try { + const state = await ws.send(event, { village_id: villageId, ...data }) + offset.value = Date.parse(state.server_time) - Date.now() + village.value = state + } catch (e) { + error.value = errorText(e) + } finally { + busy.value = false + } + } + + const refresh = () => run('village.show') + + /** stored + rate × elapsed, kept between 0 and the storage cap. */ + const resources = computed(() => { + const v = village.value + if (!v) return null + const hours = (now.value - Date.parse(v.resources_at)) / 3_600_000 + const project = (r: Resource) => Math.min(v.storage_cap, Math.max(0, v.resources[r] + v.rates[r] * hours)) + return Object.fromEntries(RESOURCES.map((r) => [r, project(r)])) as Resources + }) + + const canAfford = (cost: Cost) => + !!resources.value && + resources.value.wood >= cost.wood && + resources.value.clay >= cost.clay && + resources.value.iron >= cost.iron + + /** Whole seconds until a server timestamp, never below zero. */ + const secondsUntil = (iso: string) => Math.max(0, Math.ceil((Date.parse(iso) - now.value) / 1000)) + + // Refetch once when the next order's countdown runs out. + const nextDue = computed(() => { + const times = (village.value?.queue ?? []).flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])) + return times.length ? Math.min(...times) : null + }) + watch( + () => nextDue.value !== null && now.value >= nextDue.value, + (due) => { + if (due) refresh() + }, + ) + + ws.listenWhileMounted('village:updated', null, (data) => { + if (data.village_id === villageId) refresh() + }) + + return { + village, + resources, + error, + busy, + canAfford, + secondsUntil, + refresh, + build: (type: BuildingType, at: { x: number; y: number } | null) => run('village.build', { type, ...at }), + upgrade: (buildingId: number) => run('village.upgrade', { building_id: buildingId }), + demolish: (buildingId: number) => run('village.demolish', { building_id: buildingId }), + cancel: (orderId: number) => run('village.cancel', { order_id: orderId }), + setPriority: (buildingId: number, priority: number) => run('village.staff', { building_id: buildingId, priority }), + } +} diff --git a/app/pages/village/[id].vue b/app/pages/village/[id].vue new file mode 100644 index 0000000..8e38f89 --- /dev/null +++ b/app/pages/village/[id].vue @@ -0,0 +1,52 @@ + + + diff --git a/app/utils/format.ts b/app/utils/format.ts new file mode 100644 index 0000000..4e63b4e --- /dev/null +++ b/app/utils/format.ts @@ -0,0 +1,16 @@ +import type { Cost } from '~/types/game' + +const pad = (n: number) => String(n).padStart(2, '0') + +/** 3725 → "1:02:05" */ +export function formatDuration(totalSeconds: number) { + const s = Math.max(0, Math.round(totalSeconds)) + return `${Math.floor(s / 3600)}:${pad(Math.floor((s % 3600) / 60))}:${pad(s % 60)}` +} + +export const formatAmount = (n: number) => String(Math.floor(n)) + +/** 12.5 → "+12.5", -3 → "-3" */ +export const formatRate = (n: number) => `${n >= 0 ? '+' : ''}${Math.round(n * 10) / 10}` + +export const formatCost = (cost: Cost) => `${cost.wood} / ${cost.clay} / ${cost.iron}`