Something went wrong. Try again.
A Social Deduction / Deception Game
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233// Client-side incremental application of projected #event frames onto a wire ProjectedState.//// The subscription is the projected action log (PLAN.md §3.7): the client takes a #snapshot as a// base and folds each #event onto it. Every event folds exactly — the lobby/movement/vote actions// carry everything an observer needs, and the two state-dependent resolutions (actionResolved,// debrisInspected) carry their per-viewer public consequence in `effect` / `authentic`, computed// server-side (wire/adapt.ts). `roundEnded` folds phase/outcome and is followed by a fresh// full-reveal #snapshot from the server. `applyWireEvent` only returns `null` for an unknown// $type, which the caller treats as "advance the cursor, keep state".
import type * as Wire from '../lexgen/types/town/deception/defs.js'import type { WireAction } from './adapt.js'
type State = Wire.ProjectedState
function replacePlayer(state: State, did: string, fn: (p: Wire.ProjectedPlayer) => Wire.ProjectedPlayer): State { let found = false const players = state.players.map((p) => { if (p.did !== did) return p found = true return fn(p) }) if (!found) return state return { ...state, players }}
function replaceStation(state: State, id: string, fn: (s: Wire.ProjectedStation) => Wire.ProjectedStation): State { let found = false const stations = state.stations.map((s) => { if (s.id !== id) return s found = true return fn(s) }) if (!found) return state return { ...state, stations }}
function replaceDebris(state: State, id: string, fn: (d: Wire.ProjectedDebris) => Wire.ProjectedDebris): State { let found = false const debris = state.debris.map((d) => { if (d.id !== id) return d found = true return fn(d) }) if (!found) return state return { ...state, debris }}
/** * Fold one projected event's action onto the current state, returning a new state. Returns `null` * if the event can't be applied incrementally (the caller should request a fresh snapshot). The * caller is responsible for setting `seq` from the #event envelope. */export function applyWireEvent(state: State, action: WireAction): State | null { // every concrete action carries serverTime; the generated union also has an open `{ $type }` // fallback member, so read it defensively. const serverTime = (action as { serverTime?: number }).serverTime ?? state.now const now = Math.max(state.now, serverTime) const base = { ...state, now }
switch (action.$type) { case 'town.deception.defs#gameCreated': // The snapshot already reflects creation; nothing to fold. return base
case 'town.deception.defs#playerJoined': { const a = action as Wire.PlayerJoined const player: Wire.ProjectedPlayer = { did: a.did, handle: a.handle, status: 'alive', pos: a.pos, facing: 'south', path: [], } if (a.spriteRef != null) player.spriteRef = a.spriteRef const others = base.players.filter((p) => p.did !== a.did) return { ...base, players: [...others, player] } }
case 'town.deception.defs#playerDisconnected': { const a = action as Wire.PlayerDisconnected return replacePlayer(base, a.did, (p) => ({ ...p, status: 'disconnected' })) }
case 'town.deception.defs#playerReconnected': { const a = action as Wire.PlayerReconnected return replacePlayer(base, a.did, (p) => p.status === 'disconnected' ? { ...p, status: 'alive' } : p, ) }
case 'town.deception.defs#roundStarted': { const a = action as Wire.RoundStarted const spawnByDid = new Map(a.spawns.map((s) => [s.did, s.pos])) const roleByDid = new Map((a.roles ?? []).map((r) => [r.did, r.role])) const players = base.players.map((p) => { const next: Wire.ProjectedPlayer = { ...p, dest: undefined, path: [] } const spawn = spawnByDid.get(p.did) if (spawn) next.pos = spawn const role = roleByDid.get(p.did) if (role) next.role = role return next }) return { ...base, phase: 'round', startedAt: a.serverTime, endsAt: a.endsAt, config: a.config ?? base.config, players, } }
case 'town.deception.defs#playerMoved': { const a = action as Wire.PlayerMoved // keep the forward-path stub the server sent (full for self, 1–2 waypoints for others) so the // client can interpolate movement and drive cosmetic gates; clear dest (it's self-only and // arrives via setDestination). return replacePlayer(base, a.did, (p) => ({ ...p, pos: a.pos, facing: a.facing, dest: undefined, path: a.path ?? [], })) }
case 'town.deception.defs#setDestination': { const a = action as Wire.SetDestination // `target` is present only for the actor's own viewer; for others it's absent (the destination // is secret) and dest is left untouched. return a.target === undefined ? base : replacePlayer(base, a.did, (p) => ({ ...p, dest: a.target })) }
case 'town.deception.defs#actionStarted': { const a = action as Wire.ActionStarted const active: Wire.ProjectedActive = { stationId: a.stationId, startedAt: a.serverTime, endsAt: a.endsAt } if (a.kind != null) active.kind = a.kind return replacePlayer(base, a.did, (p) => ({ ...p, active, dest: undefined, path: [] })) }
case 'town.deception.defs#actionCancelled': { const a = action as Wire.ActionCancelled return replacePlayer(base, a.did, (p) => ({ ...p, active: undefined })) }
case 'town.deception.defs#actionResolved': { const a = action as Wire.ActionResolved let next = replacePlayer(base, a.did, (p) => ({ ...p, active: undefined })) const eff = a.effect if (eff) { if (eff.carrySet) next = replacePlayer(next, a.did, (p) => ({ ...p, carrying: eff.carrySet })) else if (eff.carryCleared) next = replacePlayer(next, a.did, (p) => ({ ...p, carrying: undefined })) if (eff.station) { const st = eff.station next = replaceStation(next, st.id, (s) => ({ ...s, state: st.state })) } if (eff.resources) next = { ...next, resources: eff.resources } // a successful callVote opened a vote as its resolution's consequence if (eff.voteOpened) { next = { ...next, vote: { startedAt: eff.voteOpened.startedAt, endsAt: eff.voteOpened.endsAt, ballots: [] } } } } return next }
case 'town.deception.defs#itemDropped': { const a = action as Wire.ItemDropped const actor = base.players.find((p) => p.did === a.did) if (!actor?.carrying) return base // debris resource type is public; authenticity is forgotten on drop for everyone const debris: Wire.ProjectedDebris = { id: a.debrisId, pos: a.pos, resourceId: actor.carrying.resourceId } const withDebris: State = { ...base, debris: [...base.debris.filter((d) => d.id !== a.debrisId), debris] } return replacePlayer(withDebris, a.did, (p) => ({ ...p, carrying: undefined })) }
case 'town.deception.defs#debrisInspected': { const a = action as Wire.DebrisInspected if (a.authentic === undefined) return base // not entitled to learn it const authentic = a.authentic return replaceDebris(base, a.debrisId, (d) => ({ ...d, authentic })) }
case 'town.deception.defs#debrisExpired': { const a = action as Wire.DebrisExpired return { ...base, debris: base.debris.filter((d) => d.id !== a.debrisId) } }
case 'town.deception.defs#voteCast': { const a = action as Wire.VoteCast const prevBallots = base.vote?.ballots ?? [] const ballots = [...prevBallots.filter((b) => b.voter !== a.voterDid), { voter: a.voterDid, target: a.targetDid }] return { ...base, vote: { startedAt: base.vote?.startedAt ?? a.serverTime, endsAt: base.vote?.endsAt ?? a.serverTime, ballots, }, } }
case 'town.deception.defs#voteEnded': { const a = action as Wire.VoteEnded let next: State = { ...base, vote: undefined } if (a.kickedDid != null) { next = replacePlayer(next, a.kickedDid, (p) => ({ ...p, status: 'kicked', active: undefined, carrying: undefined, dest: undefined, path: [], })) } return next }
case 'town.deception.defs#roundEnded': { const a = action as Wire.RoundEnded // fold phase/outcome; the server follows this with a full-reveal #snapshot return { ...base, phase: 'ended', outcome: a.outcome, vote: undefined } }
case 'town.deception.defs#tick': return base
default: return null }}