feat: instant village actions: build, upgrade, demolish, cancel and priority show at once and confirm in the background; refreshes never lock controls; older answers never replace newer ones. The upgrade card names its building
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
5ad3087326
commit
dfc02b1e5d
|
|
@ -157,6 +157,11 @@ const badges = computed(() => {
|
||||||
// The upgrade button's card: target level, cost against the stock, build time.
|
// The upgrade button's card: target level, cost against the stock, build time.
|
||||||
const tip = ref<{ id: number; x: number; y: number } | null>(null)
|
const tip = ref<{ id: number; x: number; y: number } | null>(null)
|
||||||
const tipOffer = computed(() => (tip.value ? (badges.value.find((p) => p.b.id === tip.value!.id)?.offer ?? null) : null))
|
const tipOffer = computed(() => (tip.value ? (badges.value.find((p) => p.b.id === tip.value!.id)?.offer ?? null) : null))
|
||||||
|
/** Which building the card is about: "Hiding place 2" when there are several. */
|
||||||
|
const tipName = computed(() => {
|
||||||
|
const b = tip.value ? props.village.buildings.find((x) => x.id === tip.value!.id) : null
|
||||||
|
return b ? buildingName(props.village, b, t) : ''
|
||||||
|
})
|
||||||
function showTip(id: number, e: Event) {
|
function showTip(id: number, e: Event) {
|
||||||
const r = (e.currentTarget as Element).getBoundingClientRect()
|
const r = (e.currentTarget as Element).getBoundingClientRect()
|
||||||
tip.value = { id, x: r.left + r.width / 2, y: r.top }
|
tip.value = { id, x: r.left + r.width / 2, y: r.top }
|
||||||
|
|
@ -359,7 +364,7 @@ const timers = computed(() => {
|
||||||
:style="{ left: `${tip.x}px`, top: `${tip.y - 6}px` }"
|
:style="{ left: `${tip.x}px`, top: `${tip.y - 6}px` }"
|
||||||
role="tooltip"
|
role="tooltip"
|
||||||
>
|
>
|
||||||
<p class="mb-1 font-semibold whitespace-nowrap text-wood-900">{{ $t('village.diorama.upgrade_to', { level: tipOffer.target_level }) }}</p>
|
<p class="mb-1 font-semibold whitespace-nowrap text-wood-900">{{ $t('village.diorama.upgrade_to', { name: tipName, from: tipOffer.target_level - 1, level: tipOffer.target_level }) }}</p>
|
||||||
<div class="flex items-center gap-3 whitespace-nowrap">
|
<div class="flex items-center gap-3 whitespace-nowrap">
|
||||||
<UiCost :cost="tipOffer.cost" :have="resources ?? undefined" nowrap />
|
<UiCost :cost="tipOffer.cost" :have="resources ?? undefined" nowrap />
|
||||||
<span class="inline-flex items-center gap-1 text-ink-700 tabular-nums"><IconUi name="clock" class="size-3.5" />{{ formatDuration(tipOffer.seconds) }}</span>
|
<span class="inline-flex items-center gap-1 text-ink-700 tabular-nums"><IconUi name="clock" class="size-3.5" />{{ formatDuration(tipOffer.seconds) }}</span>
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import type { BuildingType, Cost, GameError, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game'
|
import type { BuildingType, BuildOrder, Cost, GameError, MarchPlan, MovementType, Resource, Resources, TravelMode, Units, UnitType, VillageState, VillageUpdated } from '~/types/game'
|
||||||
import type { InjectionKey } from 'vue'
|
import { toRaw, type InjectionKey } from 'vue'
|
||||||
|
|
||||||
const RESOURCES: Resource[] = ['wood', 'clay', 'iron', 'food']
|
const RESOURCES: Resource[] = ['wood', 'clay', 'iron', 'food']
|
||||||
|
|
||||||
|
|
@ -17,14 +17,37 @@ export function useVillage(villageId: number) {
|
||||||
const { now, sync } = useServerClock()
|
const { now, sync } = useServerClock()
|
||||||
const { forget } = useCurrentWorld()
|
const { forget } = useCurrentWorld()
|
||||||
|
|
||||||
async function run(event: string, data: object = {}) {
|
/** Server time of the snapshot shown: an answer that left the server earlier is older and never replaces it. */
|
||||||
busy.value = true
|
let shownAt = 0
|
||||||
error.value = null
|
/** Changes in flight that lock their forms (sending troops, recruiting); quick actions never do. */
|
||||||
try {
|
let blocking = 0
|
||||||
const state = await ws.send<VillageState>(event, { village_id: villageId, ...data })
|
|
||||||
|
function apply(state: VillageState) {
|
||||||
|
const at = Date.parse(state.server_time)
|
||||||
|
if (at < shownAt) return
|
||||||
|
shownAt = at
|
||||||
sync(state.server_time)
|
sync(state.server_time)
|
||||||
village.value = state
|
village.value = state
|
||||||
unreadReports.value = state.unread_reports
|
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
|
||||||
|
error.value = null
|
||||||
|
const before = village.value
|
||||||
|
if (opts.optimistic && before) {
|
||||||
|
const copy = JSON.parse(JSON.stringify(toRaw(before))) as VillageState
|
||||||
|
opts.optimistic(copy)
|
||||||
|
village.value = copy
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
apply(await ws.send<VillageState>(event, { village_id: villageId, ...data }))
|
||||||
return true
|
return true
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// The village is gone (the world was reset): forget it and pick a world again.
|
// The village is gone (the world was reset): forget it and pick a world again.
|
||||||
|
|
@ -33,14 +56,41 @@ export function useVillage(villageId: number) {
|
||||||
await navigateTo('/worlds')
|
await navigateTo('/worlds')
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
if (opts.optimistic) refresh()
|
||||||
error.value = errorText(e)
|
error.value = errorText(e)
|
||||||
return false
|
return false
|
||||||
} finally {
|
} finally {
|
||||||
busy.value = false
|
if (block) busy.value = --blocking > 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const refresh = () => run('village.show')
|
// 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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 })
|
||||||
|
|
||||||
/** stored + rate × elapsed, kept between 0 and the storage cap. */
|
/** stored + rate × elapsed, kept between 0 and the storage cap. */
|
||||||
const resources = computed<Resources | null>(() => {
|
const resources = computed<Resources | null>(() => {
|
||||||
|
|
@ -96,11 +146,50 @@ export function useVillage(villageId: number) {
|
||||||
canAfford,
|
canAfford,
|
||||||
secondsUntil,
|
secondsUntil,
|
||||||
refresh,
|
refresh,
|
||||||
build: (type: BuildingType) => run('village.build', { type }),
|
build: (type: BuildingType) =>
|
||||||
upgrade: (buildingId: number) => run('village.upgrade', { building_id: buildingId }),
|
run('village.build', { type }, {
|
||||||
demolish: (buildingId: number) => run('village.demolish', { building_id: buildingId }),
|
optimistic: (v) => {
|
||||||
cancel: (orderId: number) => run('village.cancel', { order_id: orderId }),
|
const o = v.buildable.find((b) => b.type === type)
|
||||||
setPriority: (buildingId: number, priority: number) => run('village.staff', { building_id: buildingId, priority }),
|
if (!o) return
|
||||||
|
pay(v, o.cost)
|
||||||
|
queueOrder(v, { building_id: null, type, action: 'build', target_level: 1, space: o.space, cost: o.cost })
|
||||||
|
v.space.used += o.space
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
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)
|
||||||
|
queueOrder(v, { building_id: b.id, type: b.type, action: 'upgrade', target_level: b.upgrade.target_level, space: b.space, cost: b.upgrade.cost })
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
demolish: (buildingId: number) =>
|
||||||
|
run('village.demolish', { building_id: buildingId }, {
|
||||||
|
optimistic: (v) => {
|
||||||
|
const b = v.buildings.find((x) => x.id === buildingId)
|
||||||
|
if (!b?.demolish) return
|
||||||
|
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 } })
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
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
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
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
|
||||||
|
},
|
||||||
|
}),
|
||||||
/** One order per unit type, sent one after another; stops at the first refusal. */
|
/** One order per unit type, sent one after another; stops at the first refusal. */
|
||||||
recruit: async (buildingId: number, orders: { unit: UnitType; count: number }[]) => {
|
recruit: async (buildingId: number, orders: { unit: UnitType; count: number }[]) => {
|
||||||
for (const o of orders) {
|
for (const o of orders) {
|
||||||
|
|
|
||||||
|
|
@ -285,7 +285,7 @@ export default {
|
||||||
title: 'Village overview',
|
title: 'Village overview',
|
||||||
label: '{name}, level {level}',
|
label: '{name}, level {level}',
|
||||||
planned: 'Under construction',
|
planned: 'Under construction',
|
||||||
upgrade_to: 'Upgrade to level {level}',
|
upgrade_to: '{name}: level {from} → {level}',
|
||||||
upgrade_aria: '{name}: upgrade to level {level}',
|
upgrade_aria: '{name}: upgrade to level {level}',
|
||||||
plot: '{name}: not built yet',
|
plot: '{name}: not built yet',
|
||||||
plot_another: '{name}: room for another',
|
plot_another: '{name}: room for another',
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue