diff --git a/app/components/icon/Unit.vue b/app/components/icon/Unit.vue index a52cb50..02ce40f 100644 --- a/app/components/icon/Unit.vue +++ b/app/components/icon/Unit.vue @@ -1,7 +1,8 @@ @@ -65,6 +66,11 @@ defineProps<{ unit: UnitType }>() + + + + + diff --git a/app/components/village/PopulationPanel.vue b/app/components/village/PopulationPanel.vue index 0332a21..e74a6bd 100644 --- a/app/components/village/PopulationPanel.vue +++ b/app/components/village/PopulationPanel.vue @@ -1,12 +1,16 @@ diff --git a/app/components/village/TroopsPanel.vue b/app/components/village/TroopsPanel.vue index fdb70c2..0ef7c18 100644 --- a/app/components/village/TroopsPanel.vue +++ b/app/components/village/TroopsPanel.vue @@ -1,17 +1,31 @@ - + diff --git a/app/composables/useVillage.ts b/app/composables/useVillage.ts index 425cfe7..89d3d16 100644 --- a/app/composables/useVillage.ts +++ b/app/composables/useVillage.ts @@ -58,6 +58,7 @@ export function useVillage(villageId: number) { if (!v) return null const times = [ ...v.queue.flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])), + ...(v.levy.until ? [Date.parse(v.levy.until)] : []), ...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), ), @@ -97,5 +98,7 @@ export function useVillage(villageId: number) { return true }, cancelRecruit: (orderId: number) => run('village.cancelRecruit', { order_id: orderId }), + levy: (count: number) => run('village.levy', { count }), + endLevy: () => run('village.endLevy'), } } diff --git a/app/mock/server.ts b/app/mock/server.ts index dded76a..8227b30 100644 --- a/app/mock/server.ts +++ b/app/mock/server.ts @@ -74,8 +74,13 @@ interface MockVillage { /** Missing in states stored before recruiting existed. */ troops?: Partial> recruits?: MockRecruit[] + militia?: number + levy_until?: number | null } +/** Mock levy length; the backend's comes from the world config. */ +const LEVY_SECONDS = 120 + interface MockRecruit { id: number building_id: number @@ -161,9 +166,12 @@ function villagers(v: MockVillage) { return Math.max(0, Math.floor((foodBudget(v) - soldierUpkeep(v)) / VILLAGER_UPKEEP)) } +/** Drafted villagers, never more than there are. */ +const militia = (v: MockVillage) => Math.min(v.militia ?? 0, villagers(v)) + /** Farms first, then by priority, then oldest first. */ function assignWorkers(v: MockVillage) { - let free = villagers(v) + let free = villagers(v) - militia(v) const assigned = new Map() const order = [...v.buildings].sort( (a, b) => Number(b.type === 'farm') - Number(a.type === 'farm') || b.priority - a.priority || a.id - b.id, @@ -358,7 +366,7 @@ function missingRequirements(v: MockVillage, unit: UnitType) { 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) + const pop = villagers(v) - militia(v) return v.buildings .filter((b) => UNIT_TYPES.some((u) => UNITS[u].building === b.type)) .map((b) => ({ @@ -399,6 +407,13 @@ function catchUp(state: MockState, v: MockVillage, now: number) { const due = v.orders .filter((o) => o.finishes_at !== null && o.finishes_at <= now) .sort((a, b) => a.finishes_at! - b.finishes_at! || a.id - b.id)[0] + const levyEnds = v.levy_until != null && v.levy_until <= now ? v.levy_until : null + if (levyEnds !== null && (!due || levyEnds <= due.finishes_at!)) { + settle(v, levyEnds) + v.militia = 0 + v.levy_until = null + continue + } if (!due) break settle(v, due.finishes_at!) v.orders = v.orders.filter((o) => o !== due) @@ -439,6 +454,7 @@ function view(v: MockVillage, now: number): VillageState { soldiers: Object.values(v.troops ?? {}).reduce((a, b) => a + (b ?? 0), 0), training: (v.recruits ?? []).reduce((sum, o) => sum + o.count - o.done, 0), }, + levy: { militia: militia(v), until: v.levy_until ? iso(v.levy_until) : null, seconds: LEVY_SECONDS }, troops: Object.fromEntries(Object.entries(v.troops ?? {}).filter(([, n]) => n)), military: military(v, now), builders: BUILDERS, @@ -651,7 +667,7 @@ const handlers: Record = { 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') + if (count * entry.upkeep > villagers(v) - militia(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) @@ -682,6 +698,25 @@ const handlers: Record = { return view(v, now) }, + 'village.levy': (state, user, data, now) => { + const v = ownVillage(state, user, data.village_id, now) + const count = Math.floor(Number(data.count) || 0) + if (v.militia) fail('levy_active') + if (count < 1) fail('count') + if (count > villagers(v)) fail('villagers') + v.militia = count + v.levy_until = now + LEVY_SECONDS * 1000 + return view(v, now) + }, + + 'village.endLevy': (state, user, data, now) => { + const v = ownVillage(state, user, data.village_id, now) + if (!v.militia) fail('not_found') + v.militia = 0 + v.levy_until = null + return view(v, now) + }, + 'mock.reset': (state, user) => { state.villages = state.villages.filter((v) => v.user_id !== user.id) return true @@ -699,7 +734,11 @@ 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), ...(v.recruits ?? []).map((o) => o.finishes_at)) + const next = Math.min( + ...v.orders.map((o) => o.finishes_at ?? Infinity), + ...(v.recruits ?? []).map((o) => o.finishes_at), + v.levy_until ?? Infinity, + ) if (next === Infinity) continue timers.set( v.id, diff --git a/app/pages/village/[id].vue b/app/pages/village/[id].vue index ae7b93d..777cbf4 100644 --- a/app/pages/village/[id].vue +++ b/app/pages/village/[id].vue @@ -116,7 +116,7 @@ function openRecruit(buildingId?: number) { v-model:selected="recruitAt" :military="village.military" :troops="village.troops" - :villagers="village.population.villagers" + :villagers="village.population.villagers - village.levy.militia" :resources="resources" :seconds-until="v.secondsUntil" :busy="busy" @@ -132,12 +132,18 @@ function openRecruit(buildingId?: number) { :busy="busy" @cancel="v.cancel" /> - + diff --git a/app/types/game.ts b/app/types/game.ts index 75e0505..1e6303a 100644 --- a/app/types/game.ts +++ b/app/types/game.ts @@ -74,6 +74,8 @@ export interface VillageState { rates: Resources storage_cap: number population: Population + /** Villagers drafted as militia: they eat but don't work until `until`. */ + levy: Levy /** Own units standing in the village. */ troops: Partial> /** One entry per building that trains units, in building order. */ @@ -99,6 +101,15 @@ export interface Population { training: number } +export interface Levy { + /** 0 when no levy is under way. */ + militia: number + /** When the militia go back to work on their own. */ + until: string | null + /** How long a new levy would last. */ + seconds: number +} + /** A barracks, stable, workshop or academy: what it trains and its own queue. */ export interface MilitaryBuilding { building_id: number diff --git a/i18n/locales/en.ts b/i18n/locales/en.ts index 57d0a85..1926f7d 100644 --- a/i18n/locales/en.ts +++ b/i18n/locales/en.ts @@ -57,7 +57,8 @@ export default { 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.', + count: 'Pick at least one.', + levy_active: 'A levy is already under way.', world_full: 'This world is full.', }, } diff --git a/i18n/pages/village.ts b/i18n/pages/village.ts index 40c00f2..1d83b3e 100644 --- a/i18n/pages/village.ts +++ b/i18n/pages/village.ts @@ -64,6 +64,15 @@ export default { recruit: 'Recruit', hint: 'Build barracks to recruit units.', }, + levy: { + title: 'Levy', + militia: 'Militia', + all: 'Draft every villager', + submit: 'Draft', + hint: 'Drafted villagers defend the village and stop working for {duration}, then go back to work.', + returns_in: 'Back to work in', + end: 'End levy', + }, diorama: { title: 'Village overview', hint: 'Select a building to see what it does and upgrade it.',