2026-10-30 12:10:22 +00:00
|
|
|
|
import type { BuildingType, BuildOrder, Cost, GameError, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game'
|
|
|
|
|
|
import { toRaw, type InjectionKey } from 'vue'
|
2026-10-28 16:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
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()
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
const unreadReports = useUnreadReports()
|
2026-10-28 23:38:54 +00:00
|
|
|
|
// Projections and countdowns run on the server's clock.
|
|
|
|
|
|
const { now, sync } = useServerClock()
|
2026-10-29 09:16:34 +00:00
|
|
|
|
const { forget } = useCurrentWorld()
|
2026-10-28 16:57:44 +00:00
|
|
|
|
|
2026-10-30 12:10:22 +00:00
|
|
|
|
/** Server time of the snapshot shown: an answer that left the server earlier is older and never replaces it. */
|
|
|
|
|
|
let shownAt = 0
|
|
|
|
|
|
/** Changes in flight that lock their forms (sending troops, recruiting); quick actions never do. */
|
|
|
|
|
|
let blocking = 0
|
|
|
|
|
|
|
|
|
|
|
|
function apply(state: VillageState) {
|
|
|
|
|
|
const at = Date.parse(state.server_time)
|
|
|
|
|
|
if (at < shownAt) return
|
|
|
|
|
|
shownAt = at
|
|
|
|
|
|
sync(state.server_time)
|
|
|
|
|
|
village.value = state
|
|
|
|
|
|
unreadReports.value = state.unread_reports
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Send an intent and show the answer. A quick action passes `optimistic`: its result shows
|
|
|
|
|
|
* at once on a copy of the village, the server's answer replaces it, and a refusal puts
|
|
|
|
|
|
* the village back. Only intents with `block` lock the controls while they run.
|
|
|
|
|
|
*/
|
|
|
|
|
|
async function run(event: string, data: object = {}, opts: { block?: boolean; optimistic?: (v: VillageState) => void } = {}) {
|
|
|
|
|
|
const block = opts.block ?? !opts.optimistic
|
|
|
|
|
|
if (block) busy.value = ++blocking > 0
|
2026-10-28 16:57:44 +00:00
|
|
|
|
error.value = null
|
2026-10-30 12:10:22 +00:00
|
|
|
|
const before = village.value
|
|
|
|
|
|
if (opts.optimistic && before) {
|
|
|
|
|
|
const copy = JSON.parse(JSON.stringify(toRaw(before))) as VillageState
|
|
|
|
|
|
opts.optimistic(copy)
|
|
|
|
|
|
village.value = copy
|
|
|
|
|
|
}
|
2026-10-28 16:57:44 +00:00
|
|
|
|
try {
|
2026-10-30 12:10:22 +00:00
|
|
|
|
apply(await ws.send<VillageState>(event, { village_id: villageId, ...data }))
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
return true
|
2026-10-28 16:57:44 +00:00
|
|
|
|
} catch (e) {
|
2026-10-29 09:16:34 +00:00
|
|
|
|
// The village is gone (the world was reset): forget it and pick a world again.
|
|
|
|
|
|
if (event === 'village.show' && (e as Partial<GameError>)?.code === 'not_found') {
|
|
|
|
|
|
forget(villageId)
|
|
|
|
|
|
await navigateTo('/worlds')
|
|
|
|
|
|
return false
|
|
|
|
|
|
}
|
2026-10-30 12:10:22 +00:00
|
|
|
|
if (opts.optimistic) refresh()
|
2026-10-28 16:57:44 +00:00
|
|
|
|
error.value = errorText(e)
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
return false
|
2026-10-28 16:57:44 +00:00
|
|
|
|
} finally {
|
2026-10-30 12:10:22 +00:00
|
|
|
|
if (block) busy.value = --blocking > 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Refreshes run quietly in the background, one at a time; a call while one runs asks for one more after it.
|
|
|
|
|
|
let refreshing: Promise<boolean> | null = null
|
|
|
|
|
|
let again = false
|
|
|
|
|
|
async function refresh(): Promise<boolean> {
|
|
|
|
|
|
if (refreshing) {
|
|
|
|
|
|
again = true
|
|
|
|
|
|
return refreshing
|
|
|
|
|
|
}
|
|
|
|
|
|
refreshing = run('village.show', {}, { block: false })
|
|
|
|
|
|
try {
|
|
|
|
|
|
return await refreshing
|
|
|
|
|
|
} finally {
|
|
|
|
|
|
refreshing = null
|
|
|
|
|
|
if (again) {
|
|
|
|
|
|
again = false
|
|
|
|
|
|
refresh()
|
|
|
|
|
|
}
|
2026-10-28 16:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-10-30 12:10:22 +00:00
|
|
|
|
// What quick actions look like before the server answers.
|
|
|
|
|
|
let placeholder = 0
|
|
|
|
|
|
const pay = (v: VillageState, cost: Cost, sign = -1) => {
|
|
|
|
|
|
for (const r of ['wood', 'clay', 'iron'] as const) v.resources[r] = Math.min(v.storage_cap, Math.max(0, v.resources[r] + sign * cost[r]))
|
|
|
|
|
|
}
|
|
|
|
|
|
const queueOrder = (v: VillageState, order: Omit<BuildOrder, 'id' | 'position' | 'builder' | 'starts_at' | 'finishes_at'>) =>
|
|
|
|
|
|
v.queue.push({ ...order, id: --placeholder, position: v.queue.length + 1, builder: null, starts_at: null, finishes_at: null })
|
2026-11-03 08:56:37 +00:00
|
|
|
|
/** The queue holds back (or, with sign 1, frees) the villagers an order needs, as the server counts them. */
|
|
|
|
|
|
const hold = (v: VillageState, villagers: number, sign = -1) => {
|
|
|
|
|
|
v.population.idle = Math.max(0, v.population.idle + sign * villagers)
|
|
|
|
|
|
v.population.reserved = Math.max(0, v.population.reserved - sign * villagers)
|
|
|
|
|
|
}
|
2026-10-28 16:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
/** 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))
|
|
|
|
|
|
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
// Refetch once when the next build order, unit in training or group under way is due,
|
|
|
|
|
|
// so new buildings, trained troops and arrivals show up without waiting for a push.
|
2026-10-28 16:57:44 +00:00
|
|
|
|
const nextDue = computed(() => {
|
2026-10-28 20:51:43 +00:00
|
|
|
|
const v = village.value
|
|
|
|
|
|
if (!v) return null
|
|
|
|
|
|
const times = [
|
|
|
|
|
|
...v.queue.flatMap((o) => (o.finishes_at ? [Date.parse(o.finishes_at)] : [])),
|
2026-10-28 21:05:45 +00:00
|
|
|
|
...[v.levy.until, v.starvation.food_empty_at, v.starvation.desertion_at].flatMap((at) => (at ? [Date.parse(at)] : [])),
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
...v.movements.map((m) => Date.parse(m.arrives_at)),
|
2026-10-28 20:51:43 +00:00
|
|
|
|
...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),
|
|
|
|
|
|
),
|
|
|
|
|
|
]
|
2026-10-28 16:57:44 +00:00
|
|
|
|
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,
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
/** Milliseconds on the server clock, ticking. */
|
|
|
|
|
|
now,
|
2026-10-28 16:57:44 +00:00
|
|
|
|
error,
|
|
|
|
|
|
busy,
|
|
|
|
|
|
canAfford,
|
|
|
|
|
|
secondsUntil,
|
|
|
|
|
|
refresh,
|
2026-10-30 12:10:22 +00:00
|
|
|
|
build: (type: BuildingType) =>
|
|
|
|
|
|
run('village.build', { type }, {
|
|
|
|
|
|
optimistic: (v) => {
|
|
|
|
|
|
const o = v.buildable.find((b) => b.type === type)
|
|
|
|
|
|
if (!o) return
|
|
|
|
|
|
pay(v, o.cost)
|
2026-11-03 08:56:37 +00:00
|
|
|
|
queueOrder(v, { building_id: null, type, action: 'build', target_level: 1, space: o.space, cost: o.cost, villagers: o.villagers })
|
2026-10-30 12:10:22 +00:00
|
|
|
|
v.space.used += o.space
|
2026-11-03 08:56:37 +00:00
|
|
|
|
hold(v, o.villagers)
|
2026-10-30 12:10:22 +00:00
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
upgrade: (buildingId: number) =>
|
|
|
|
|
|
run('village.upgrade', { building_id: buildingId }, {
|
|
|
|
|
|
optimistic: (v) => {
|
|
|
|
|
|
const b = v.buildings.find((x) => x.id === buildingId)
|
|
|
|
|
|
if (!b?.upgrade) return
|
|
|
|
|
|
pay(v, b.upgrade.cost)
|
2026-11-03 08:56:37 +00:00
|
|
|
|
queueOrder(v, { building_id: b.id, type: b.type, action: 'upgrade', target_level: b.upgrade.target_level, space: b.space, cost: b.upgrade.cost, villagers: b.upgrade.villagers })
|
|
|
|
|
|
hold(v, b.upgrade.villagers)
|
2026-10-30 12:10:22 +00:00
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
demolish: (buildingId: number) =>
|
|
|
|
|
|
run('village.demolish', { building_id: buildingId }, {
|
|
|
|
|
|
optimistic: (v) => {
|
|
|
|
|
|
const b = v.buildings.find((x) => x.id === buildingId)
|
|
|
|
|
|
if (!b?.demolish) return
|
2026-11-03 08:56:37 +00:00
|
|
|
|
queueOrder(v, { building_id: b.id, type: b.type, action: 'demolish', target_level: b.demolish.target_level, space: b.space, cost: { wood: 0, clay: 0, iron: 0 }, villagers: 0 })
|
2026-10-30 12:10:22 +00:00
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
cancel: (orderId: number) =>
|
|
|
|
|
|
run('village.cancel', { order_id: orderId }, {
|
|
|
|
|
|
optimistic: (v) => {
|
|
|
|
|
|
const o = v.queue.find((x) => x.id === orderId)
|
|
|
|
|
|
if (!o) return
|
|
|
|
|
|
v.queue = v.queue.filter((x) => x !== o)
|
|
|
|
|
|
pay(v, o.cost, 1)
|
|
|
|
|
|
if (o.action === 'build') v.space.used -= o.space
|
2026-11-03 08:56:37 +00:00
|
|
|
|
hold(v, o.villagers ?? 0, 1)
|
2026-10-30 12:10:22 +00:00
|
|
|
|
},
|
|
|
|
|
|
}),
|
|
|
|
|
|
setPriority: (buildingId: number, priority: number) =>
|
|
|
|
|
|
run('village.staff', { building_id: buildingId, priority }, {
|
|
|
|
|
|
optimistic: (v) => {
|
|
|
|
|
|
const b = v.buildings.find((x) => x.id === buildingId)
|
|
|
|
|
|
if (b) b.priority = priority
|
|
|
|
|
|
},
|
|
|
|
|
|
}),
|
2026-10-28 20:51:43 +00:00
|
|
|
|
/** 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 }),
|
2026-10-28 20:58:09 +00:00
|
|
|
|
levy: (count: number) => run('village.levy', { count }),
|
|
|
|
|
|
endLevy: () => run('village.endLevy'),
|
feat: rally point, troop movements, support, reports, scouts, battle simulator and village rename
Send attacks and support from the rally point with a live travel-time preview, see own
commands and spotted incoming attacks with countdowns and call-back, send support home
or withdraw it, read battle and support reports with an unread badge, run the battle
simulator, rename villages, and send troops straight from the map. The watchtower gets
a diorama model; the mock server answers every new intent.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 23:16:06 +00:00
|
|
|
|
/** Target, distance and travel time for the send form; null when the server refuses. */
|
|
|
|
|
|
preview: async (x: number, y: number, units: Units, mode: TravelMode) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
return await ws.send<MarchPlan>('rally.preview', { village_id: villageId, x, y, units, mode })
|
|
|
|
|
|
} catch {
|
|
|
|
|
|
return null
|
|
|
|
|
|
}
|
|
|
|
|
|
},
|
|
|
|
|
|
send: (type: Exclude<MovementType, 'return'>, x: number, y: number, units: Units, mode: TravelMode, catapultTarget: BuildingType | null) =>
|
|
|
|
|
|
run('rally.send', { type, x, y, units, mode, catapult_target: catapultTarget }),
|
|
|
|
|
|
cancelMovement: (movementId: number) => run('rally.cancel', { movement_id: movementId }),
|
|
|
|
|
|
sendHome: (homeVillageId: number) => run('rally.sendHome', { home_village_id: homeVillageId }),
|
|
|
|
|
|
withdraw: (hostVillageId: number) => run('rally.withdraw', { host_village_id: hostVillageId }),
|
|
|
|
|
|
rename: (name: string) => run('village.rename', { name }),
|
2026-10-29 09:33:26 +00:00
|
|
|
|
mint: (count: number) => run('village.mint', { count }),
|
2026-10-28 16:57:44 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-10-28 21:14:56 +00:00
|
|
|
|
|
|
|
|
|
|
export type VillageContext = ReturnType<typeof useVillage>
|
|
|
|
|
|
const VILLAGE: InjectionKey<VillageContext> = Symbol('village')
|
|
|
|
|
|
|
|
|
|
|
|
/** The village page shares its one useVillage() with every screen in it. */
|
|
|
|
|
|
export function provideVillage(context: VillageContext) {
|
|
|
|
|
|
provide(VILLAGE, context)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
export function useVillageContext(): VillageContext {
|
|
|
|
|
|
const context = inject(VILLAGE)
|
|
|
|
|
|
if (!context) throw new Error('useVillageContext() needs the village page around it.')
|
|
|
|
|
|
return context
|
|
|
|
|
|
}
|
feat: rally point tabs, map village popups, report filters, beginner protection bar, keyboard shortcuts
Ideas taken from how Die Stämme works, in our own look: the rally point splits into
commands, troops and simulator; hovering a village on the map shows its owner, points
and distance, a click offers send troops and centre; reports filter by attacks,
defence and support; a dismissible bar counts down beginner protection; one key per
screen (V, H, T, P, W, M, R); resources in the village bar lead to the workforce.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 00:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
/** The village of the bar on top, where a page may also stand without one (the map). */
|
|
|
|
|
|
export function useVillageContextOrNull(): VillageContext | null {
|
|
|
|
|
|
return inject(VILLAGE, null)
|
|
|
|
|
|
}
|