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
|
|
|
|
import type { BuildingType, Cost, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game'
|
2026-10-28 21:14:56 +00:00
|
|
|
|
import 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-28 16:57:44 +00:00
|
|
|
|
|
|
|
|
|
|
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 })
|
2026-10-28 23:38:54 +00:00
|
|
|
|
sync(state.server_time)
|
2026-10-28 16:57:44 +00:00
|
|
|
|
village.value = state
|
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
|
|
|
|
unreadReports.value = state.unread_reports
|
|
|
|
|
|
return true
|
2026-10-28 16:57:44 +00:00
|
|
|
|
} catch (e) {
|
|
|
|
|
|
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 {
|
|
|
|
|
|
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))
|
|
|
|
|
|
|
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-28 18:54:19 +00:00
|
|
|
|
build: (type: BuildingType) => run('village.build', { type }),
|
2026-10-28 16:57:44 +00:00
|
|
|
|
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 }),
|
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-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
|
|
|
|
|
|
}
|