feat: recruit units: a tab per military building with its queue and a unit table, troops panel
Like the Die Stämme barracks: amount fields with (max), one Recruit button for every filled-in row, the training queue with next-unit and all-done countdowns and cancel. The village refetches when the next unit is due, so trained troops show up at once. The mock server trains units too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
3e96a70f69
commit
18232e9c59
|
|
@ -0,0 +1,72 @@
|
|||
<script setup lang="ts">
|
||||
import type { UnitType } from '~/types/game'
|
||||
|
||||
defineProps<{ unit: UnitType }>()
|
||||
</script>
|
||||
|
||||
<!-- Line glyphs in currentColor, one per unit type. -->
|
||||
<template>
|
||||
<svg
|
||||
viewBox="0 0 24 24"
|
||||
fill="none"
|
||||
stroke="currentColor"
|
||||
stroke-width="1.7"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
aria-hidden="true"
|
||||
class="shrink-0"
|
||||
>
|
||||
<g v-if="unit === 'spear'">
|
||||
<path d="M4 20L15.5 8.5" />
|
||||
<path d="M15.5 8.5l1-4.5 3.5-1-1 3.5z" />
|
||||
<path d="M13 8l3 3" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'sword'">
|
||||
<path d="M20 4L9.5 14.5" />
|
||||
<path d="M20 4l-4.5.8M20 4l-.8 4.5" />
|
||||
<path d="M7 12l5 5" />
|
||||
<path d="M9.5 14.5L5.5 18.5" />
|
||||
<path d="M4 20l1.5-1.5" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'archer'">
|
||||
<path d="M8 3a14 14 0 0 1 0 18" />
|
||||
<path d="M8 3v18" />
|
||||
<path d="M3 12h16" />
|
||||
<path d="M19 12l-3-2.5M19 12l-3 2.5" />
|
||||
<path d="M3 12l-1-1.5M3 12l-1 1.5" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'light_cavalry'">
|
||||
<path d="M9 21c0-3-.8-4.8-2.6-5.8L4 13.8l1-3 3.5-2.2L10 4l2 2.6c4.3.8 7 4.4 7 8.6V21" />
|
||||
<path d="M9.5 10h.01" />
|
||||
<path d="M13 7.5c1.5.5 2.5 1.6 3 3" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'heavy_cavalry'">
|
||||
<path d="M9 21c0-3-.8-4.8-2.6-5.8L4 13.8l1-3 3.5-2.2L10 4l2 2.6c4.3.8 7 4.4 7 8.6V21" />
|
||||
<path d="M9.5 10h.01" />
|
||||
<path d="M6.5 13l5-3.5 2 4.5-6 3" />
|
||||
<path d="M11 3l1-2 1.5 2" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'ram'">
|
||||
<path d="M5 11l6-5 6 5" />
|
||||
<path d="M2.5 14h16l2.5 1.5-2.5 1.5h-16z" />
|
||||
<circle cx="7" cy="19.5" r="1.8" />
|
||||
<circle cx="15" cy="19.5" r="1.8" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'catapult'">
|
||||
<path d="M3 18h15" />
|
||||
<path d="M6 18l3-5h4l3 5" />
|
||||
<path d="M11 13l7-8.5" />
|
||||
<path d="M16.5 3.2a2.2 2.2 0 1 0 3.1 3.1" />
|
||||
<circle cx="6" cy="20" r="1.5" />
|
||||
<circle cx="15" cy="20" r="1.5" />
|
||||
</g>
|
||||
<g v-else-if="unit === 'nobleman'">
|
||||
<circle cx="12" cy="11" r="3.5" />
|
||||
<path d="M5 21c0-4 3-6.5 7-6.5s7 2.5 7 6.5" />
|
||||
<path d="M8.5 6.5L9 3.5l1.5 1.5L12 3l1.5 2L15 3.5l.5 3z" />
|
||||
</g>
|
||||
<g v-else>
|
||||
<path d="M12 3l7 3v5c0 5-3 8.5-7 10-4-1.5-7-5-7-10V6z" />
|
||||
</g>
|
||||
</svg>
|
||||
</template>
|
||||
|
|
@ -6,11 +6,14 @@ defineProps<{
|
|||
resources: Resources
|
||||
canAfford: (cost: Cost) => boolean
|
||||
busy: boolean
|
||||
/** Whether it trains units, so the card links to its recruit tab. */
|
||||
trains?: boolean
|
||||
}>()
|
||||
const emit = defineEmits<{
|
||||
upgrade: [buildingId: number]
|
||||
demolish: [buildingId: number]
|
||||
priority: [buildingId: number, priority: number]
|
||||
recruit: [buildingId: number]
|
||||
close: []
|
||||
}>()
|
||||
</script>
|
||||
|
|
@ -74,6 +77,10 @@ const emit = defineEmits<{
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<button v-if="trains" type="button" class="btn btn-outline w-full" @click="emit('recruit', building.id)">
|
||||
{{ $t('village.buildings.recruit') }}
|
||||
</button>
|
||||
|
||||
<div v-if="building.upgrade" class="space-y-1.5 rounded-sm border border-wood-600/50 bg-parchment-100 p-3">
|
||||
<div class="flex items-center justify-between gap-2 text-xs text-ink-500">
|
||||
<UiCost :cost="building.upgrade.cost" :have="resources" />
|
||||
|
|
|
|||
|
|
@ -0,0 +1,204 @@
|
|||
<script setup lang="ts">
|
||||
import type { MilitaryBuilding, RecruitOption, RecruitOrder, Resources, UnitType } from '~/types/game'
|
||||
|
||||
const props = defineProps<{
|
||||
military: MilitaryBuilding[]
|
||||
troops: Partial<Record<UnitType, number>>
|
||||
/** Villagers left to train; every unit takes `upkeep` of them. */
|
||||
villagers: number
|
||||
resources: Resources
|
||||
secondsUntil: (iso: string) => number
|
||||
busy: boolean
|
||||
/** Sends the orders; resolves false when the server refused one. */
|
||||
recruit: (buildingId: number, orders: { unit: UnitType; count: number }[]) => Promise<boolean>
|
||||
}>()
|
||||
const emit = defineEmits<{ cancel: [orderId: number] }>()
|
||||
/** Building whose tab is open, by id. */
|
||||
const selected = defineModel<number | null>('selected', { default: null })
|
||||
const { t } = useI18n()
|
||||
|
||||
// Like the Die Stämme barracks: the queue on top, then one row per unit with an amount
|
||||
// field and its (max), and one button that recruits every filled-in row. Each military
|
||||
// building instance has its own tab, since each one trains in its own queue.
|
||||
const current = computed(() => props.military.find((m) => m.building_id === selected.value) ?? props.military[0] ?? null)
|
||||
|
||||
function tabName(m: MilitaryBuilding) {
|
||||
const same = props.military.filter((x) => x.type === m.type)
|
||||
const name = t(`buildings.${m.type}`)
|
||||
return same.length > 1 ? t('village.build.nth', { name, n: same.indexOf(m) + 1 }) : name
|
||||
}
|
||||
|
||||
const amounts = reactive<Partial<Record<UnitType, number>>>({})
|
||||
watch(
|
||||
() => current.value?.building_id,
|
||||
() => Object.keys(amounts).forEach((k) => delete amounts[k as UnitType]),
|
||||
)
|
||||
|
||||
/** Most of this unit the projected resources and the villagers allow right now. */
|
||||
function max(o: RecruitOption) {
|
||||
if (o.blocked) return 0
|
||||
const byResources = (['wood', 'clay', 'iron'] as const).map((r) =>
|
||||
o.cost[r] > 0 ? Math.floor(props.resources[r] / o.cost[r]) : Infinity,
|
||||
)
|
||||
return Math.max(0, Math.min(Math.floor(props.villagers / o.upkeep), ...byResources))
|
||||
}
|
||||
|
||||
const orders = computed(() =>
|
||||
(current.value?.units ?? [])
|
||||
.map((o) => ({ unit: o.unit, count: Math.floor(Number(amounts[o.unit]) || 0) }))
|
||||
.filter((o) => o.count > 0),
|
||||
)
|
||||
|
||||
async function submit() {
|
||||
if (!current.value || !orders.value.length) return
|
||||
if (await props.recruit(current.value.building_id, orders.value)) {
|
||||
Object.keys(amounts).forEach((k) => delete amounts[k as UnitType])
|
||||
}
|
||||
}
|
||||
|
||||
/** Units of an order still to come and the wait for the next one, from its finish time. */
|
||||
function progress(o: RecruitOrder) {
|
||||
const remaining = props.secondsUntil(o.finishes_at)
|
||||
const left = Math.min(o.count - o.done, Math.ceil(remaining / o.seconds_per_unit))
|
||||
return { done: o.count - left, next: left > 0 ? remaining - (left - 1) * o.seconds_per_unit : 0, remaining }
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section v-if="current" id="recruit" class="panel scroll-mt-20 overflow-hidden">
|
||||
<div class="panel-head">
|
||||
<div class="flex flex-wrap items-center gap-1" role="tablist">
|
||||
<button
|
||||
v-for="m in military"
|
||||
:key="m.building_id"
|
||||
type="button"
|
||||
role="tab"
|
||||
:aria-selected="m === current"
|
||||
class="inline-flex items-center gap-1.5 rounded-sm border px-2.5 py-1 font-display text-base font-semibold"
|
||||
:class="m === current ? 'border-wood-700 bg-parchment-50 text-wood-900' : 'border-transparent text-ink-500 hover:text-wood-900'"
|
||||
style="font-variant: small-caps"
|
||||
@click="selected = m.building_id"
|
||||
>
|
||||
<IconBuilding :type="m.type" class="size-4" />
|
||||
{{ tabName(m) }}
|
||||
<span class="font-sans text-xs font-normal text-ink-500" style="font-variant: normal">{{ m.level }}</span>
|
||||
</button>
|
||||
</div>
|
||||
<span class="badge badge-plain">
|
||||
<IconUi name="users" class="size-3.5" />
|
||||
{{ $t('village.recruit.villagers', { count: villagers }) }}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div v-if="current.queue.length" class="overflow-x-auto border-b-2 border-wood-700">
|
||||
<table class="w-full min-w-[30rem] border-collapse text-sm">
|
||||
<thead>
|
||||
<tr class="border-b border-wood-700 bg-parchment-100 text-left text-xs font-semibold whitespace-nowrap text-ink-700">
|
||||
<th class="px-3 py-1.5">{{ $t('village.recruit.training') }}</th>
|
||||
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.next') }}</th>
|
||||
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.all_done') }}</th>
|
||||
<th class="w-10 px-3 py-1.5"><span class="sr-only">{{ $t('village.recruit.cancel') }}</span></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="(o, i) in current.queue" :key="o.id" class="border-b border-parchment-300 last:border-b-0" :class="i === 0 ? 'bg-parchment-50' : 'bg-parchment-100'">
|
||||
<td class="px-3 py-1">
|
||||
<div class="flex items-center gap-2">
|
||||
<IconUnit :unit="o.unit" class="size-5 text-wood-800" />
|
||||
<span class="font-semibold">{{ $t('village.recruit.order', { count: o.count, unit: $t(`units.${o.unit}`) }) }}</span>
|
||||
<span class="text-xs text-ink-500 tabular-nums">{{ $t('village.recruit.done', { done: progress(o).done, count: o.count }) }}</span>
|
||||
</div>
|
||||
<UiBar v-if="i === 0" class="mt-1 mb-0.5" :value="progress(o).done / o.count" />
|
||||
</td>
|
||||
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(progress(o).next) }}</td>
|
||||
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(progress(o).remaining) }}</td>
|
||||
<td class="px-3 py-1 text-right">
|
||||
<button
|
||||
type="button"
|
||||
class="btn btn-danger p-1"
|
||||
:title="$t('village.recruit.cancel')"
|
||||
:aria-label="$t('village.recruit.cancel')"
|
||||
:disabled="busy"
|
||||
@click="emit('cancel', o.id)"
|
||||
>
|
||||
<IconUi name="close" class="size-3.5" />
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<form class="overflow-x-auto" @submit.prevent="submit">
|
||||
<table class="w-full min-w-[36rem] border-collapse text-sm">
|
||||
<thead>
|
||||
<tr class="border-b-2 border-wood-700 bg-parchment-100 text-left text-xs font-semibold whitespace-nowrap text-ink-700">
|
||||
<th class="px-3 py-1.5">{{ $t('village.recruit.unit') }}</th>
|
||||
<th class="px-2 py-1.5">{{ $t('village.recruit.cost') }}</th>
|
||||
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.upkeep') }}</th>
|
||||
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.time') }}</th>
|
||||
<th class="px-2 py-1.5 text-right">{{ $t('village.recruit.at_home') }}</th>
|
||||
<th class="px-3 py-1.5 text-right">{{ $t('village.recruit.amount') }}</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="o in current.units" :key="o.unit" class="border-b border-parchment-300 last:border-b-0 even:bg-parchment-100">
|
||||
<td class="px-3 py-1">
|
||||
<div class="flex items-center gap-2" :class="o.blocked ? 'opacity-60' : ''">
|
||||
<IconUnit :unit="o.unit" class="size-6 text-wood-800" />
|
||||
<span class="font-semibold whitespace-nowrap">{{ $t(`units.${o.unit}`) }}</span>
|
||||
</div>
|
||||
</td>
|
||||
<template v-if="!o.blocked">
|
||||
<td class="px-2 py-1"><UiCost :cost="o.cost" :have="resources" nowrap /></td>
|
||||
<td class="px-2 py-1 text-right">
|
||||
<span class="inline-flex items-center gap-1 text-xs tabular-nums whitespace-nowrap">
|
||||
<IconResource resource="food" class="size-4" />
|
||||
{{ $t('village.recruit.per_hour', { value: o.upkeep }) }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="px-2 py-1 text-right tabular-nums whitespace-nowrap">{{ formatDuration(o.seconds) }}</td>
|
||||
<td class="px-2 py-1 text-right tabular-nums">{{ troops[o.unit] ?? 0 }}</td>
|
||||
<td class="px-3 py-1">
|
||||
<div class="flex items-center justify-end gap-1.5">
|
||||
<input
|
||||
v-model.number="amounts[o.unit]"
|
||||
type="number"
|
||||
min="0"
|
||||
:max="max(o)"
|
||||
inputmode="numeric"
|
||||
class="input w-16 px-1.5 py-0.5 text-right tabular-nums"
|
||||
:aria-label="$t('village.recruit.amount_of', { unit: $t(`units.${o.unit}`) })"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="w-12 text-left text-xs font-semibold tabular-nums"
|
||||
:class="max(o) ? 'text-blood-700 hover:underline' : 'text-ink-400'"
|
||||
:title="$t('village.recruit.max_title')"
|
||||
:disabled="!max(o)"
|
||||
@click="amounts[o.unit] = max(o)"
|
||||
>
|
||||
({{ max(o) }})
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</template>
|
||||
<td v-else colspan="5" class="px-2 py-1 text-ink-500">
|
||||
<span class="mr-2 text-xs">{{ $t('village.build.requires') }}</span>
|
||||
<span v-for="r in o.requires" :key="r.type" class="mr-3 inline-flex items-center gap-1 whitespace-nowrap">
|
||||
<IconBuildingTile :type="r.type" class="size-5" />
|
||||
{{ $t('village.build.requirement', { type: $t(`buildings.${r.type}`), level: r.level }) }}
|
||||
</span>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
<div class="flex items-center justify-end gap-3 border-t-2 border-wood-700 bg-parchment-100 px-3 py-2">
|
||||
<button type="submit" class="btn btn-primary min-w-[7rem]" :disabled="busy || !orders.length">
|
||||
<IconUnit unit="spear" class="size-4" />
|
||||
{{ $t('village.recruit.submit') }}
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
<script setup lang="ts">
|
||||
import type { MilitaryBuilding, UnitType } from '~/types/game'
|
||||
|
||||
const props = defineProps<{
|
||||
troops: Partial<Record<UnitType, number>>
|
||||
military: MilitaryBuilding[]
|
||||
training: number
|
||||
}>()
|
||||
const emit = defineEmits<{ recruit: [] }>()
|
||||
|
||||
const home = computed(() => (Object.entries(props.troops) as [UnitType, number][]).filter(([, n]) => n > 0))
|
||||
</script>
|
||||
|
||||
<!-- Own units in the village, like the Die Stämme overview's unit box. -->
|
||||
<template>
|
||||
<section class="panel">
|
||||
<div class="panel-head">
|
||||
<h2 class="panel-title">{{ $t('village.troops.title') }}</h2>
|
||||
<span v-if="training" class="badge badge-plain">
|
||||
<IconUi name="clock" class="size-3.5" />
|
||||
{{ $t('village.troops.training', { count: training }) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="panel-body">
|
||||
<ul v-if="home.length" class="divide-y divide-parchment-300">
|
||||
<li v-for="[unit, count] in home" :key="unit" class="flex items-center gap-2 py-1 text-sm">
|
||||
<IconUnit :unit="unit" class="size-5 text-wood-800" />
|
||||
<span class="flex-1">{{ $t(`units.${unit}`) }}</span>
|
||||
<span class="font-semibold tabular-nums">{{ count }}</span>
|
||||
</li>
|
||||
</ul>
|
||||
<p v-else class="py-2 text-center text-sm text-ink-500">{{ $t('village.troops.none') }}</p>
|
||||
<button v-if="military.length" type="button" class="btn btn-outline mt-3 w-full" @click="emit('recruit')">
|
||||
{{ $t('village.troops.recruit') }}
|
||||
</button>
|
||||
<p v-else class="mt-1 text-center text-xs text-ink-500">{{ $t('village.troops.hint') }}</p>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
import type { BuildingType, Cost, Resource, Resources, VillageState, VillageUpdated } from '~/types/game'
|
||||
import type { BuildingType, Cost, Resource, Resources, UnitType, VillageState, VillageUpdated } from '~/types/game'
|
||||
|
||||
const RESOURCES: Resource[] = ['wood', 'clay', 'iron', 'food']
|
||||
|
||||
|
|
@ -51,9 +51,17 @@ export function useVillage(villageId: number) {
|
|||
/** 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.
|
||||
// Refetch once when the next build order or the next unit in training is due, so
|
||||
// new buildings and trained troops show up without waiting for a push.
|
||||
const nextDue = computed(() => {
|
||||
const times = (village.value?.queue ?? []).flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : []))
|
||||
const v = village.value
|
||||
if (!v) return null
|
||||
const times = [
|
||||
...v.queue.flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])),
|
||||
...v.military.flatMap((m) =>
|
||||
m.queue.filter((o) => o.done < o.count).map((o) => Date.parse(o.starts_at) + (o.done + 1) * o.seconds_per_unit * 1000),
|
||||
),
|
||||
]
|
||||
return times.length ? Math.min(...times) : null
|
||||
})
|
||||
watch(
|
||||
|
|
@ -80,5 +88,14 @@ export function useVillage(villageId: number) {
|
|||
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 }),
|
||||
/** One order per unit type, sent one after another; stops at the first refusal. */
|
||||
recruit: async (buildingId: number, orders: { unit: UnitType; count: number }[]) => {
|
||||
for (const o of orders) {
|
||||
await run('village.recruit', { building_id: buildingId, unit: o.unit, count: o.count })
|
||||
if (error.value) return false
|
||||
}
|
||||
return true
|
||||
},
|
||||
cancelRecruit: (orderId: number) => run('village.cancelRecruit', { order_id: orderId }),
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
import type { BuildingType, Cost, Resource, Resources } from '~/types/game'
|
||||
import type { BuildingType, Cost, Resource, Resources, UnitType } from '~/types/game'
|
||||
|
||||
// Placeholder numbers so the PoC has something to show. They scale linearly with the
|
||||
// level on purpose: the real values and formulas belong to the backend
|
||||
|
|
@ -44,3 +44,26 @@ export const CATALOG: Record<BuildingType, CatalogEntry> = {
|
|||
export const BUILDING_TYPES = Object.keys(CATALOG) as BuildingType[]
|
||||
|
||||
export const atLevel = (base: number, level: number) => base * (level + 1)
|
||||
|
||||
export interface UnitEntry {
|
||||
building: BuildingType
|
||||
cost: Cost
|
||||
/** Food per hour, and villagers taken per unit. */
|
||||
upkeep: number
|
||||
/** Seconds per unit, short so the mock shows progress. */
|
||||
seconds: number
|
||||
requires: Partial<Record<BuildingType, number>>
|
||||
}
|
||||
|
||||
export const UNITS: Record<UnitType, UnitEntry> = {
|
||||
spear: { building: 'barracks', cost: { wood: 50, clay: 30, iron: 10 }, upkeep: 1, seconds: 10, requires: {} },
|
||||
sword: { building: 'barracks', cost: { wood: 30, clay: 30, iron: 70 }, upkeep: 1, seconds: 15, requires: {} },
|
||||
archer: { building: 'barracks', cost: { wood: 100, clay: 30, iron: 60 }, upkeep: 1, seconds: 18, requires: { barracks: 3 } },
|
||||
light_cavalry: { building: 'stable', cost: { wood: 125, clay: 100, iron: 250 }, upkeep: 3, seconds: 18, requires: {} },
|
||||
heavy_cavalry: { building: 'stable', cost: { wood: 200, clay: 150, iron: 600 }, upkeep: 4, seconds: 36, requires: { stable: 5 } },
|
||||
ram: { building: 'workshop', cost: { wood: 300, clay: 200, iron: 200 }, upkeep: 4, seconds: 48, requires: {} },
|
||||
catapult: { building: 'workshop', cost: { wood: 320, clay: 400, iron: 100 }, upkeep: 6, seconds: 72, requires: { workshop: 2 } },
|
||||
nobleman: { building: 'academy', cost: { wood: 40000, clay: 50000, iron: 50000 }, upkeep: 10, seconds: 108, requires: {} },
|
||||
}
|
||||
|
||||
export const UNIT_TYPES = Object.keys(UNITS) as UnitType[]
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@ import type {
|
|||
BuildingType,
|
||||
Cost,
|
||||
GameError,
|
||||
MilitaryBuilding,
|
||||
Offer,
|
||||
Resource,
|
||||
Resources,
|
||||
UnitType,
|
||||
VillageState,
|
||||
WorldSummary,
|
||||
} from '~/types/game'
|
||||
|
|
@ -31,6 +33,8 @@ import {
|
|||
MAX_SPACE,
|
||||
MAX_LEVEL,
|
||||
START_RESOURCES,
|
||||
UNITS,
|
||||
UNIT_TYPES,
|
||||
VILLAGER_UPKEEP,
|
||||
atLevel,
|
||||
} from './catalog'
|
||||
|
|
@ -67,6 +71,22 @@ interface MockVillage {
|
|||
resources_at: number
|
||||
buildings: MockBuilding[]
|
||||
orders: MockOrder[]
|
||||
/** Missing in states stored before recruiting existed. */
|
||||
troops?: Partial<Record<UnitType, number>>
|
||||
recruits?: MockRecruit[]
|
||||
}
|
||||
|
||||
interface MockRecruit {
|
||||
id: number
|
||||
building_id: number
|
||||
unit: UnitType
|
||||
count: number
|
||||
done: number
|
||||
seconds_per_unit: number
|
||||
/** Per unit. */
|
||||
cost: Cost
|
||||
starts_at: number
|
||||
finishes_at: number
|
||||
}
|
||||
|
||||
interface MockState {
|
||||
|
|
@ -131,8 +151,14 @@ function foodBudget(v: MockVillage) {
|
|||
.reduce((sum, b) => sum + atLevel(CATALOG.farm.production!.per_hour, b.level), 0)
|
||||
}
|
||||
|
||||
/** Food the troops and the units in training eat; it comes out of the villagers' share. */
|
||||
function soldierUpkeep(v: MockVillage) {
|
||||
const home = Object.entries(v.troops ?? {}).reduce((sum, [unit, n]) => sum + UNITS[unit as UnitType].upkeep * (n ?? 0), 0)
|
||||
return home + (v.recruits ?? []).reduce((sum, o) => sum + UNITS[o.unit].upkeep * (o.count - o.done), 0)
|
||||
}
|
||||
|
||||
function villagers(v: MockVillage) {
|
||||
return Math.floor(foodBudget(v) / VILLAGER_UPKEEP)
|
||||
return Math.max(0, Math.floor((foodBudget(v) - soldierUpkeep(v)) / VILLAGER_UPKEEP))
|
||||
}
|
||||
|
||||
/** Farms first, then by priority, then oldest first. */
|
||||
|
|
@ -171,7 +197,7 @@ function rates(v: MockVillage): Resources {
|
|||
const p = production(b, assigned)
|
||||
if (p) r[p.resource] += p.per_hour
|
||||
}
|
||||
r.food -= villagers(v) * VILLAGER_UPKEEP
|
||||
r.food -= villagers(v) * VILLAGER_UPKEEP + soldierUpkeep(v)
|
||||
return r
|
||||
}
|
||||
|
||||
|
|
@ -297,7 +323,74 @@ function finish(state: MockState, v: MockVillage, o: MockOrder) {
|
|||
refund(v, mapCost(levelCost(b.type, b.level), (n) => Math.floor(n * DEMOLISH_REFUND)))
|
||||
}
|
||||
b.level = o.target_level
|
||||
if (b.level < 0) v.buildings = v.buildings.filter((x) => x !== b)
|
||||
if (b.level < 0) {
|
||||
v.buildings = v.buildings.filter((x) => x !== b)
|
||||
// Units still in training there go back to work, refunded.
|
||||
for (const r of (v.recruits ?? []).filter((r) => r.building_id === b.id)) {
|
||||
refund(v, mapCost(r.cost, (n) => n * (r.count - r.done)))
|
||||
}
|
||||
v.recruits = (v.recruits ?? []).filter((r) => r.building_id !== b.id)
|
||||
}
|
||||
}
|
||||
|
||||
// Recruiting: each military building trains its own queue, one unit after another.
|
||||
|
||||
const doneAt = (o: MockRecruit, at: number) =>
|
||||
Math.min(o.count, Math.max(o.done, Math.floor((at - o.starts_at) / (o.seconds_per_unit * 1000))))
|
||||
|
||||
/** Move units trained by `at` into the village and drop finished orders. */
|
||||
function settleRecruits(v: MockVillage, at: number) {
|
||||
v.troops ??= {}
|
||||
for (const o of v.recruits ?? []) {
|
||||
const done = doneAt(o, at)
|
||||
v.troops[o.unit] = (v.troops[o.unit] ?? 0) + done - o.done
|
||||
o.done = done
|
||||
}
|
||||
v.recruits = (v.recruits ?? []).filter((o) => o.done < o.count)
|
||||
}
|
||||
|
||||
function missingRequirements(v: MockVillage, unit: UnitType) {
|
||||
return Object.entries(UNITS[unit].requires)
|
||||
.filter(([type, level]) => !v.buildings.some((b) => b.type === type && b.level >= (level ?? 0)))
|
||||
.map(([type, level]) => ({ type: type as BuildingType, level: level ?? 0 }))
|
||||
}
|
||||
|
||||
const secondsPerUnit = (b: MockBuilding, unit: UnitType) => Math.max(1, Math.ceil(UNITS[unit].seconds / (1 + 0.05 * b.level)))
|
||||
|
||||
function military(v: MockVillage, at: number): MilitaryBuilding[] {
|
||||
const pop = villagers(v)
|
||||
return v.buildings
|
||||
.filter((b) => UNIT_TYPES.some((u) => UNITS[u].building === b.type))
|
||||
.map((b) => ({
|
||||
building_id: b.id,
|
||||
type: b.type,
|
||||
level: b.level,
|
||||
units: UNIT_TYPES.filter((u) => UNITS[u].building === b.type).map((unit) => {
|
||||
const { cost, upkeep } = UNITS[unit]
|
||||
const requires = missingRequirements(v, unit)
|
||||
const byResources = Math.min(...(['wood', 'clay', 'iron'] as const).map((r) => Math.floor(v.resources[r] / cost[r])))
|
||||
return {
|
||||
unit,
|
||||
cost,
|
||||
seconds: secondsPerUnit(b, unit),
|
||||
upkeep,
|
||||
max: requires.length ? 0 : Math.max(0, Math.min(byResources, Math.floor(pop / upkeep))),
|
||||
blocked: requires.length ? ('requires' as const) : null,
|
||||
requires,
|
||||
}
|
||||
}),
|
||||
queue: (v.recruits ?? [])
|
||||
.filter((o) => o.building_id === b.id)
|
||||
.map((o) => ({
|
||||
id: o.id,
|
||||
unit: o.unit,
|
||||
count: o.count,
|
||||
done: doneAt(o, at),
|
||||
seconds_per_unit: o.seconds_per_unit,
|
||||
starts_at: iso(o.starts_at),
|
||||
finishes_at: iso(o.finishes_at),
|
||||
})),
|
||||
}))
|
||||
}
|
||||
|
||||
/** Process every order that finished before `now`, in order, then settle to `now`. */
|
||||
|
|
@ -309,9 +402,11 @@ function catchUp(state: MockState, v: MockVillage, now: number) {
|
|||
if (!due) break
|
||||
settle(v, due.finishes_at!)
|
||||
v.orders = v.orders.filter((o) => o !== due)
|
||||
settleRecruits(v, due.finishes_at!)
|
||||
finish(state, v, due)
|
||||
startWaiting(v, due.finishes_at!)
|
||||
}
|
||||
settleRecruits(v, now)
|
||||
settle(v, now)
|
||||
}
|
||||
|
||||
|
|
@ -338,11 +433,14 @@ function view(v: MockVillage, now: number): VillageState {
|
|||
storage_cap: storageCap(v),
|
||||
population: {
|
||||
food_budget: foodBudget(v),
|
||||
upkeep: pop * VILLAGER_UPKEEP,
|
||||
upkeep: pop * VILLAGER_UPKEEP + soldierUpkeep(v),
|
||||
villagers: pop,
|
||||
workers: [...assigned.values()].reduce((a, b) => a + b, 0),
|
||||
soldiers: 0,
|
||||
soldiers: Object.values(v.troops ?? {}).reduce((a, b) => a + (b ?? 0), 0),
|
||||
training: (v.recruits ?? []).reduce((sum, o) => sum + o.count - o.done, 0),
|
||||
},
|
||||
troops: Object.fromEntries(Object.entries(v.troops ?? {}).filter(([, n]) => n)),
|
||||
military: military(v, now),
|
||||
builders: BUILDERS,
|
||||
buildings: v.buildings.map(
|
||||
(b): Building => {
|
||||
|
|
@ -544,6 +642,46 @@ const handlers: Record<string, Handler> = {
|
|||
return view(v, now)
|
||||
},
|
||||
|
||||
'village.recruit': (state, user, data, now) => {
|
||||
const v = ownVillage(state, user, data.village_id, now)
|
||||
const b = ownBuilding(v, data.building_id)
|
||||
const unit = data.unit as UnitType
|
||||
const entry = UNITS[unit]
|
||||
if (!entry || entry.building !== b.type) fail('not_found')
|
||||
const count = Math.floor(Number(data.count) || 0)
|
||||
if (count < 1) fail('count')
|
||||
if (missingRequirements(v, unit).length) fail('requires')
|
||||
if (count * entry.upkeep > villagers(v)) fail('villagers')
|
||||
pay(v, mapCost(entry.cost, (n) => n * count))
|
||||
const seconds = secondsPerUnit(b, unit)
|
||||
const last = (v.recruits ?? []).filter((o) => o.building_id === b.id).at(-1)
|
||||
const starts = last && last.finishes_at > now ? last.finishes_at : now
|
||||
v.recruits = [
|
||||
...(v.recruits ?? []),
|
||||
{ id: nextId(state), building_id: b.id, unit, count, done: 0, seconds_per_unit: seconds, cost: entry.cost, starts_at: starts, finishes_at: starts + seconds * count * 1000 },
|
||||
]
|
||||
return view(v, now)
|
||||
},
|
||||
|
||||
// Refunds the untrained units; later orders at the same building move up.
|
||||
'village.cancelRecruit': (state, user, data, now) => {
|
||||
const v = ownVillage(state, user, data.village_id, now)
|
||||
const order = (v.recruits ?? []).find((o) => o.id === Number(data.order_id)) ?? fail('not_found')
|
||||
refund(v, mapCost(order.cost, (n) => n * (order.count - order.done)))
|
||||
v.recruits = (v.recruits ?? []).filter((o) => o !== order)
|
||||
let from = now
|
||||
for (const o of v.recruits.filter((o) => o.building_id === order.building_id)) {
|
||||
if (o.id < order.id) {
|
||||
from = Math.max(now, o.finishes_at)
|
||||
continue
|
||||
}
|
||||
o.starts_at = from
|
||||
o.finishes_at = from + o.seconds_per_unit * o.count * 1000
|
||||
from = o.finishes_at
|
||||
}
|
||||
return view(v, now)
|
||||
},
|
||||
|
||||
'mock.reset': (state, user) => {
|
||||
state.villages = state.villages.filter((v) => v.user_id !== user.id)
|
||||
return true
|
||||
|
|
@ -561,7 +699,7 @@ export function createMockServer(): { ws: GameWs; auth: GameAuth } {
|
|||
function schedule(state: MockState) {
|
||||
for (const v of state.villages) {
|
||||
clearTimeout(timers.get(v.id))
|
||||
const next = Math.min(...v.orders.map((o) => o.finishes_at ?? Infinity))
|
||||
const next = Math.min(...v.orders.map((o) => o.finishes_at ?? Infinity), ...(v.recruits ?? []).map((o) => o.finishes_at))
|
||||
if (next === Infinity) continue
|
||||
timers.set(
|
||||
v.id,
|
||||
|
|
|
|||
|
|
@ -23,6 +23,15 @@ function showInVillage(id: number) {
|
|||
selected.value = id
|
||||
window.scrollTo({ top: 0, behavior: 'smooth' })
|
||||
}
|
||||
|
||||
/** Military building whose recruit tab is open. */
|
||||
const recruitAt = ref<number | null>(null)
|
||||
const trains = (buildingId: number) => !!village.value?.military.some((m) => m.building_id === buildingId)
|
||||
|
||||
function openRecruit(buildingId?: number) {
|
||||
if (buildingId !== undefined) recruitAt.value = buildingId
|
||||
nextTick(() => document.getElementById('recruit')?.scrollIntoView({ behavior: 'smooth', block: 'start' }))
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
|
|
@ -76,9 +85,11 @@ function showInVillage(id: number) {
|
|||
:resources="resources"
|
||||
:can-afford="v.canAfford"
|
||||
:busy="busy"
|
||||
:trains="trains(selectedBuilding.id)"
|
||||
@upgrade="v.upgrade"
|
||||
@demolish="v.demolish"
|
||||
@priority="v.setPriority"
|
||||
@recruit="openRecruit"
|
||||
@close="selected = null"
|
||||
/>
|
||||
</div>
|
||||
|
|
@ -88,6 +99,7 @@ function showInVillage(id: number) {
|
|||
<p v-if="error" class="alert" role="alert">{{ error }}</p>
|
||||
|
||||
<div class="grid gap-6 lg:grid-cols-[minmax(0,1fr)_22rem] 2xl:grid-cols-[minmax(0,1fr)_26rem]">
|
||||
<div class="min-w-0 space-y-6">
|
||||
<VillageBuildForm
|
||||
:options="village.buildable"
|
||||
:buildings="village.buildings"
|
||||
|
|
@ -100,6 +112,18 @@ function showInVillage(id: number) {
|
|||
@demolish="v.demolish"
|
||||
@select="showInVillage"
|
||||
/>
|
||||
<VillageRecruit
|
||||
v-model:selected="recruitAt"
|
||||
:military="village.military"
|
||||
:troops="village.troops"
|
||||
:villagers="village.population.villagers"
|
||||
:resources="resources"
|
||||
:seconds-until="v.secondsUntil"
|
||||
:busy="busy"
|
||||
:recruit="v.recruit"
|
||||
@cancel="v.cancelRecruit"
|
||||
/>
|
||||
</div>
|
||||
<div class="space-y-6">
|
||||
<VillageBuildQueue
|
||||
:queue="village.queue"
|
||||
|
|
@ -109,6 +133,12 @@ function showInVillage(id: number) {
|
|||
@cancel="v.cancel"
|
||||
/>
|
||||
<VillagePopulationPanel :population="village.population" />
|
||||
<VillageTroopsPanel
|
||||
:troops="village.troops"
|
||||
:military="village.military"
|
||||
:training="village.population.training"
|
||||
@recruit="openRecruit()"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,16 @@ export type BuildingType =
|
|||
| 'workshop'
|
||||
| 'academy'
|
||||
|
||||
export type UnitType =
|
||||
| 'spear'
|
||||
| 'sword'
|
||||
| 'archer'
|
||||
| 'light_cavalry'
|
||||
| 'heavy_cavalry'
|
||||
| 'ram'
|
||||
| 'catapult'
|
||||
| 'nobleman'
|
||||
|
||||
/** Why the server refuses an offer. The client shows `village.blocked.<reason>`; no_space means not enough free building space. */
|
||||
export type BlockedReason = 'max_per_village' | 'no_space' | 'max_level' | 'requires'
|
||||
|
||||
|
|
@ -64,6 +74,10 @@ export interface VillageState {
|
|||
rates: Resources
|
||||
storage_cap: number
|
||||
population: Population
|
||||
/** Own units standing in the village. */
|
||||
troops: Partial<Record<UnitType, number>>
|
||||
/** One entry per building that trains units, in building order. */
|
||||
military: MilitaryBuilding[]
|
||||
builders: number
|
||||
buildings: Building[]
|
||||
queue: BuildOrder[]
|
||||
|
|
@ -81,6 +95,42 @@ export interface Population {
|
|||
/** Villagers assigned to buildings. */
|
||||
workers: number
|
||||
soldiers: number
|
||||
/** Units queued but not trained yet; they already eat instead of working. */
|
||||
training: number
|
||||
}
|
||||
|
||||
/** A barracks, stable, workshop or academy: what it trains and its own queue. */
|
||||
export interface MilitaryBuilding {
|
||||
building_id: number
|
||||
type: BuildingType
|
||||
level: number
|
||||
units: RecruitOption[]
|
||||
queue: RecruitOrder[]
|
||||
}
|
||||
|
||||
export interface RecruitOption {
|
||||
unit: UnitType
|
||||
/** Per unit. */
|
||||
cost: Cost
|
||||
/** Per unit at this building, from its level and staffing. */
|
||||
seconds: number
|
||||
/** Food per hour one unit eats. */
|
||||
upkeep: number
|
||||
/** Most units that resources and villagers allowed when the snapshot was taken. */
|
||||
max: number
|
||||
blocked: 'requires' | null
|
||||
requires: { type: BuildingType; level: number }[]
|
||||
}
|
||||
|
||||
export interface RecruitOrder {
|
||||
id: number
|
||||
unit: UnitType
|
||||
count: number
|
||||
/** Units trained by the snapshot; the rest arrive every seconds_per_unit after starts_at. */
|
||||
done: number
|
||||
seconds_per_unit: number
|
||||
starts_at: string
|
||||
finishes_at: string
|
||||
}
|
||||
|
||||
export interface Building {
|
||||
|
|
|
|||
|
|
@ -34,6 +34,16 @@ export default {
|
|||
workshop: 'Workshop',
|
||||
academy: 'Academy',
|
||||
},
|
||||
units: {
|
||||
spear: 'Spear fighter',
|
||||
sword: 'Swordsman',
|
||||
archer: 'Archer',
|
||||
light_cavalry: 'Light cavalry',
|
||||
heavy_cavalry: 'Heavy cavalry',
|
||||
ram: 'Ram',
|
||||
catapult: 'Catapult',
|
||||
nobleman: 'Nobleman',
|
||||
},
|
||||
errors: {
|
||||
unknown: 'Something went wrong.',
|
||||
unauthenticated: 'Please log in again.',
|
||||
|
|
@ -46,6 +56,8 @@ export default {
|
|||
resources: 'Not enough resources.',
|
||||
core_building: 'Core buildings cannot go below level 0.',
|
||||
requires: 'Build its requirements first.',
|
||||
villagers: 'Not enough villagers to train that many.',
|
||||
count: 'Recruit at least one unit.',
|
||||
world_full: 'This world is full.',
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,6 +36,33 @@ export default {
|
|||
demolish: 'Demolish',
|
||||
refund: 'Refund',
|
||||
show: 'Show in the village',
|
||||
recruit: 'Recruit units',
|
||||
},
|
||||
recruit: {
|
||||
villagers: '{count} villagers free',
|
||||
training: 'Training',
|
||||
next: 'Next unit',
|
||||
all_done: 'All done in',
|
||||
cancel: 'Cancel',
|
||||
order: '{count} × {unit}',
|
||||
done: '{done}/{count}',
|
||||
unit: 'Unit',
|
||||
cost: 'Cost',
|
||||
upkeep: 'Upkeep',
|
||||
per_hour: '{value}/h',
|
||||
time: 'Time',
|
||||
at_home: 'In village',
|
||||
amount: 'Recruit',
|
||||
amount_of: 'How many {unit}',
|
||||
max_title: 'The most you can recruit now',
|
||||
submit: 'Recruit',
|
||||
},
|
||||
troops: {
|
||||
title: 'Troops',
|
||||
training: '{count} in training',
|
||||
none: 'No troops in the village.',
|
||||
recruit: 'Recruit',
|
||||
hint: 'Build barracks to recruit units.',
|
||||
},
|
||||
diorama: {
|
||||
title: 'Village overview',
|
||||
|
|
|
|||
Loading…
Reference in New Issue