feat: plain village view with resources, grid, build queue and buildings
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
4b82bc9d45
commit
4af1f07e37
|
|
@ -0,0 +1,55 @@
|
|||
<script setup lang="ts">
|
||||
import type { BuildOption, BuildingType, Cost } from '~/types/game'
|
||||
|
||||
const props = defineProps<{
|
||||
options: BuildOption[]
|
||||
selected: { x: number; y: number } | null
|
||||
canAfford: (cost: Cost) => boolean
|
||||
busy: boolean
|
||||
}>()
|
||||
const emit = defineEmits<{ build: [type: BuildingType] }>()
|
||||
const { t } = useI18n()
|
||||
|
||||
function status(o: BuildOption) {
|
||||
if (o.blocked) return t(`village.blocked.${o.blocked}`)
|
||||
if (!props.canAfford(o.cost)) return t('village.blocked.resources')
|
||||
return ''
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.build.title') }}</h2>
|
||||
<p>
|
||||
{{ $t('village.build.position') }}:
|
||||
<template v-if="selected">{{ $t('village.build.at_cell') }} ({{ selected.x }}|{{ selected.y }})</template>
|
||||
<template v-else>{{ $t('village.build.auto') }}</template>
|
||||
</p>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ $t('village.build.type') }}</th>
|
||||
<th>{{ $t('village.build.size') }}</th>
|
||||
<th>{{ $t('village.build.cost') }} ({{ $t('resources.wood') }} / {{ $t('resources.clay') }} / {{ $t('resources.iron') }})</th>
|
||||
<th>{{ $t('village.build.time') }}</th>
|
||||
<th>{{ $t('village.build.status') }}</th>
|
||||
<th />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="o in options" :key="o.type">
|
||||
<td>{{ $t(`buildings.${o.type}`) }}</td>
|
||||
<td>{{ o.footprint[0] }}×{{ o.footprint[1] }}</td>
|
||||
<td>{{ formatCost(o.cost) }}</td>
|
||||
<td>{{ formatDuration(o.seconds) }}</td>
|
||||
<td>{{ status(o) }}</td>
|
||||
<td>
|
||||
<button type="button" :disabled="busy || !!o.blocked || !canAfford(o.cost)" @click="emit('build', o.type)">
|
||||
{{ $t('village.build.submit') }}
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<script setup lang="ts">
|
||||
import type { BuildOrder } from '~/types/game'
|
||||
|
||||
defineProps<{
|
||||
queue: BuildOrder[]
|
||||
builders: number
|
||||
secondsUntil: (iso: string) => number
|
||||
busy: boolean
|
||||
}>()
|
||||
const emit = defineEmits<{ cancel: [orderId: number] }>()
|
||||
const { t } = useI18n()
|
||||
|
||||
function describe(o: BuildOrder) {
|
||||
const type = t(`buildings.${o.type}`)
|
||||
if (o.action === 'build') return t('village.queue.build', { type })
|
||||
if (o.action === 'upgrade') return t('village.queue.upgrade', { type, level: o.target_level })
|
||||
if (o.target_level < 0) return t('village.queue.remove', { type })
|
||||
return t('village.queue.demolish', { type, level: o.target_level })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.queue.title') }} <small>{{ $t('village.queue.builders', { count: builders }) }}</small></h2>
|
||||
<p v-if="!queue.length">{{ $t('village.queue.empty') }}</p>
|
||||
<table v-else>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th>
|
||||
<th>{{ $t('village.queue.order') }}</th>
|
||||
<th>{{ $t('village.queue.builder') }}</th>
|
||||
<th>{{ $t('village.queue.remaining') }}</th>
|
||||
<th />
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="o in queue" :key="o.id">
|
||||
<td>{{ o.position }}</td>
|
||||
<td>{{ describe(o) }}</td>
|
||||
<td>{{ o.builder ?? '–' }}</td>
|
||||
<td>{{ o.finishes_at ? formatDuration(secondsUntil(o.finishes_at)) : $t('village.queue.waiting') }}</td>
|
||||
<td>
|
||||
<button type="button" :disabled="busy" @click="emit('cancel', o.id)">{{ $t('village.queue.cancel') }}</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,73 @@
|
|||
<script setup lang="ts">
|
||||
import type { Building, Cost } from '~/types/game'
|
||||
|
||||
defineProps<{
|
||||
buildings: Building[]
|
||||
canAfford: (cost: Cost) => boolean
|
||||
busy: boolean
|
||||
}>()
|
||||
const emit = defineEmits<{
|
||||
upgrade: [buildingId: number]
|
||||
demolish: [buildingId: number]
|
||||
priority: [buildingId: number, priority: number]
|
||||
}>()
|
||||
|
||||
function onPriority(b: Building, e: Event) {
|
||||
emit('priority', b.id, Number((e.target as HTMLInputElement).value))
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.buildings.title') }}</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>{{ $t('village.buildings.type') }}</th>
|
||||
<th>{{ $t('village.buildings.level') }}</th>
|
||||
<th>{{ $t('village.buildings.position') }}</th>
|
||||
<th>{{ $t('village.buildings.size') }}</th>
|
||||
<th>{{ $t('village.buildings.workers') }}</th>
|
||||
<th>{{ $t('village.buildings.staffing') }}</th>
|
||||
<th>{{ $t('village.buildings.output') }}</th>
|
||||
<th>{{ $t('village.buildings.priority') }}</th>
|
||||
<th>{{ $t('village.buildings.actions') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="b in buildings" :key="b.id">
|
||||
<td>{{ $t(`buildings.${b.type}`) }}</td>
|
||||
<td>{{ b.level }}</td>
|
||||
<td>{{ b.footprint[0] ? `${b.x}|${b.y}` : '–' }}</td>
|
||||
<td>{{ b.footprint[0] }}×{{ b.footprint[1] }}</td>
|
||||
<td>{{ b.workers.assigned }}/{{ b.workers.required }}</td>
|
||||
<td>{{ Math.round(b.staffing * 100) }}%</td>
|
||||
<td>
|
||||
<template v-if="b.production">
|
||||
{{ $t(`resources.${b.production.resource}`) }}
|
||||
{{ $t('village.resources.per_hour', { rate: formatRate(b.production.per_hour) }) }}
|
||||
</template>
|
||||
<template v-else>–</template>
|
||||
</td>
|
||||
<td>
|
||||
<input type="number" min="0" max="9" size="2" :value="b.priority" :disabled="busy" @change="onPriority(b, $event)" />
|
||||
</td>
|
||||
<td>
|
||||
<button
|
||||
v-if="b.upgrade"
|
||||
type="button"
|
||||
:disabled="busy || !canAfford(b.upgrade.cost)"
|
||||
@click="emit('upgrade', b.id)"
|
||||
>
|
||||
{{ $t('village.buildings.upgrade', { level: b.upgrade.target_level }) }}
|
||||
({{ formatCost(b.upgrade.cost) }}, {{ formatDuration(b.upgrade.seconds) }})
|
||||
</button>
|
||||
<button v-if="b.demolish" type="button" :disabled="busy" @click="emit('demolish', b.id)">
|
||||
{{ $t('village.buildings.demolish') }} ({{ $t('village.buildings.refund') }} {{ formatCost(b.demolish.refund) }})
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
<script setup lang="ts">
|
||||
import type { BuildingType, VillageState } from '~/types/game'
|
||||
|
||||
const props = defineProps<{ village: VillageState }>()
|
||||
const selected = defineModel<{ x: number; y: number } | null>('selected', { default: null })
|
||||
|
||||
interface Item {
|
||||
key: string
|
||||
type: BuildingType
|
||||
level: number
|
||||
w: number
|
||||
h: number
|
||||
planned: boolean
|
||||
}
|
||||
type Cell = { key: string; item: Item } | { key: string; item: null; x: number; y: number }
|
||||
|
||||
// A plain table: each building is one cell spanning its footprint, queued new buildings
|
||||
// show as planned, and the remaining cells can be picked as the next build position.
|
||||
const rows = computed(() => {
|
||||
const { w, h } = props.village.grid
|
||||
const slots: (Item | 'covered' | null)[][] = Array.from({ length: h }, () => Array(w).fill(null))
|
||||
const items = [
|
||||
...props.village.buildings.map((b) => ({ ...b, key: `b${b.id}`, planned: false })),
|
||||
...props.village.queue
|
||||
.filter((o) => o.action === 'build')
|
||||
.map((o) => ({ ...o, key: `o${o.id}`, level: 0, planned: true })),
|
||||
]
|
||||
for (const it of items) {
|
||||
const [fw, fh] = it.footprint
|
||||
if (fw === 0 || fh === 0) continue
|
||||
for (let dy = 0; dy < fh; dy++) for (let dx = 0; dx < fw; dx++) slots[it.y + dy]![it.x + dx] = 'covered'
|
||||
slots[it.y]![it.x] = { key: it.key, type: it.type, level: it.level, w: fw, h: fh, planned: it.planned }
|
||||
}
|
||||
return slots.map((row, y) =>
|
||||
row.flatMap((slot, x): Cell[] =>
|
||||
slot === 'covered' ? [] : slot ? [{ key: slot.key, item: slot }] : [{ key: `${x},${y}`, item: null, x, y }],
|
||||
),
|
||||
)
|
||||
})
|
||||
|
||||
const isSelected = (x: number, y: number) => selected.value?.x === x && selected.value?.y === y
|
||||
|
||||
function toggle(x: number, y: number) {
|
||||
selected.value = isSelected(x, y) ? null : { x, y }
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.grid.title') }}</h2>
|
||||
<p><small>{{ $t('village.grid.hint') }}</small></p>
|
||||
<table class="grid">
|
||||
<tbody>
|
||||
<tr v-for="(row, y) in rows" :key="y">
|
||||
<template v-for="cell in row" :key="cell.key">
|
||||
<td v-if="cell.item" :colspan="cell.item.w" :rowspan="cell.item.h" :class="{ planned: cell.item.planned }">
|
||||
{{ $t(`buildings.${cell.item.type}`) }}
|
||||
<template v-if="cell.item.planned">({{ $t('village.grid.planned') }})</template>
|
||||
<template v-else>{{ cell.item.level }}</template>
|
||||
</td>
|
||||
<td v-else class="free" :class="{ selected: isSelected(cell.x, cell.y) }" @click="toggle(cell.x, cell.y)">
|
||||
{{ cell.x }}|{{ cell.y }}
|
||||
</td>
|
||||
</template>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.grid {
|
||||
border-collapse: collapse;
|
||||
}
|
||||
.grid td {
|
||||
border: 1px solid;
|
||||
width: 5em;
|
||||
height: 2.5em;
|
||||
text-align: center;
|
||||
}
|
||||
.free {
|
||||
cursor: pointer;
|
||||
opacity: 0.5;
|
||||
}
|
||||
.selected {
|
||||
outline: 3px solid;
|
||||
opacity: 1;
|
||||
}
|
||||
.planned {
|
||||
font-style: italic;
|
||||
}
|
||||
</style>
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<script setup lang="ts">
|
||||
import type { Population } from '~/types/game'
|
||||
|
||||
defineProps<{ population: Population }>()
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.population.title') }}</h2>
|
||||
<table>
|
||||
<tbody>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.food_budget') }}</th>
|
||||
<td>{{ $t('village.population.food_per_hour', { value: population.food_budget }) }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.upkeep') }}</th>
|
||||
<td>{{ $t('village.population.food_per_hour', { value: population.upkeep }) }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.villagers') }}</th>
|
||||
<td>{{ population.villagers }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.workers') }}</th>
|
||||
<td>{{ population.workers }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.idle') }}</th>
|
||||
<td>{{ population.villagers - population.workers }}</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>{{ $t('village.population.soldiers') }}</th>
|
||||
<td>{{ population.soldiers }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<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>
|
||||
|
||||
<template>
|
||||
<section>
|
||||
<h2>{{ $t('village.resources.title') }}</h2>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th v-for="r in list" :key="r">{{ $t(`resources.${r}`) }}</th>
|
||||
<th>{{ $t('village.resources.cap') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr>
|
||||
<td v-for="r in list" :key="r">
|
||||
{{ formatAmount(resources[r]) }}
|
||||
<small>({{ $t('village.resources.per_hour', { rate: formatRate(village.rates[r]) }) }})</small>
|
||||
</td>
|
||||
<td>{{ village.storage_cap }}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
const now = ref(Date.now())
|
||||
let timer: ReturnType<typeof setInterval> | 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)
|
||||
}
|
||||
|
|
@ -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<VillageState | null>(null)
|
||||
const error = ref<string | null>(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<VillageState>(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<Resources | null>(() => {
|
||||
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<VillageUpdated>('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 }),
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,52 @@
|
|||
<script setup lang="ts">
|
||||
import type { BuildingType } from '~/types/game'
|
||||
|
||||
const route = useRoute()
|
||||
const v = useVillage(Number(route.params.id))
|
||||
const { village, resources, error, busy } = v
|
||||
|
||||
/** Grid cell the next new building goes to; null lets the server pick. */
|
||||
const selected = ref<{ x: number; y: number } | null>(null)
|
||||
|
||||
v.refresh()
|
||||
|
||||
async function build(type: BuildingType) {
|
||||
await v.build(type, selected.value)
|
||||
if (!error.value) selected.value = null
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section v-if="village && resources">
|
||||
<h1>{{ village.name }} ({{ village.x }}|{{ village.y }})</h1>
|
||||
<p v-if="error" role="alert">{{ error }}</p>
|
||||
|
||||
<VillageResourceBar :village="village" :resources="resources" />
|
||||
<VillagePopulationPanel :population="village.population" />
|
||||
<VillageBuildQueue
|
||||
:queue="village.queue"
|
||||
:builders="village.builders"
|
||||
:seconds-until="v.secondsUntil"
|
||||
:busy="busy"
|
||||
@cancel="v.cancel"
|
||||
/>
|
||||
<VillageGrid v-model:selected="selected" :village="village" />
|
||||
<VillageBuildForm
|
||||
:options="village.buildable"
|
||||
:selected="selected"
|
||||
:can-afford="v.canAfford"
|
||||
:busy="busy"
|
||||
@build="build"
|
||||
/>
|
||||
<VillageBuildingTable
|
||||
:buildings="village.buildings"
|
||||
:can-afford="v.canAfford"
|
||||
:busy="busy"
|
||||
@upgrade="v.upgrade"
|
||||
@demolish="v.demolish"
|
||||
@priority="v.setPriority"
|
||||
/>
|
||||
</section>
|
||||
<p v-else-if="error" role="alert">{{ error }}</p>
|
||||
<p v-else>{{ $t('common.loading') }}</p>
|
||||
</template>
|
||||
|
|
@ -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}`
|
||||
Loading…
Reference in New Issue