diff --git a/app/components/village/Recruit.vue b/app/components/village/Recruit.vue
new file mode 100644
index 0000000..6b3976c
--- /dev/null
+++ b/app/components/village/Recruit.vue
@@ -0,0 +1,204 @@
+
+
+
+
+
diff --git a/app/components/village/TroopsPanel.vue b/app/components/village/TroopsPanel.vue
new file mode 100644
index 0000000..fdb70c2
--- /dev/null
+++ b/app/components/village/TroopsPanel.vue
@@ -0,0 +1,39 @@
+
+
+
+
+
+
+
{{ $t('village.troops.title') }}
+
+
+ {{ $t('village.troops.training', { count: training }) }}
+
+
+
+
+ -
+
+ {{ $t(`units.${unit}`) }}
+ {{ count }}
+
+
+
{{ $t('village.troops.none') }}
+
+
{{ $t('village.troops.hint') }}
+
+
+
diff --git a/app/composables/useVillage.ts b/app/composables/useVillage.ts
index 48a7727..425cfe7 100644
--- a/app/composables/useVillage.ts
+++ b/app/composables/useVillage.ts
@@ -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 }),
}
}
diff --git a/app/mock/catalog.ts b/app/mock/catalog.ts
index c5b0c2f..2ec4d10 100644
--- a/app/mock/catalog.ts
+++ b/app/mock/catalog.ts
@@ -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
= {
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>
+}
+
+export const UNITS: Record = {
+ 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[]
diff --git a/app/mock/server.ts b/app/mock/server.ts
index b78f8d1..dded76a 100644
--- a/app/mock/server.ts
+++ b/app/mock/server.ts
@@ -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>
+ 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 = {
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,
diff --git a/app/pages/village/[id].vue b/app/pages/village/[id].vue
index 7671b3f..ae7b93d 100644
--- a/app/pages/village/[id].vue
+++ b/app/pages/village/[id].vue
@@ -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(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' }))
+}
@@ -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"
/>
diff --git a/app/types/game.ts b/app/types/game.ts
index 606f89d..75e0505 100644
--- a/app/types/game.ts
+++ b/app/types/game.ts
@@ -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.