/** * One line of advice, in the player's own words. * * A pure function of the snapshot the server sent plus two things the client * works out from the map it was given: whether you are standing on a site, * and whether you are within reach of the spike. Never more than one prompt — * the most urgent thing wins, because two instructions at once is none. */ /** Within this many units the server lets you pick up, plant or defuse. */ export const REACH = 46; const secs = (n) => Math.max(0, Math.ceil(n)); /** "Spike defused" the way the log says it, lower-cased to sit mid-sentence. */ export const lower = (text) => String(text).charAt(0).toLowerCase() + String(text).slice(1); /** * How the round just ended, from the viewer's side of it. The server says who * took it and why; a person wants to know whether that was them. A viewer * with no team (watching) is told the team. */ export function roundOutcome(snap) { const last = snap && snap.last; if (!last) return null; const team = snap.me && snap.me.team; const title = team ? (last.winner === team ? "Round won" : "Round lost") : `${last.winner} take the round`; return { title, why: lower(last.why), winner: last.winner }; } export function choosePrompt(snap, world = {}) { if (!snap) return ""; const me = snap.me; const spike = snap.spike; const onSite = world.onSite === true; const nearSpike = world.nearSpike === true; // The page is played with a mouse and the terminal with keys; one line // names the input, so it says the thing the reader can actually do. const buyWith = world.input === "keys" ? "Press b and buy a gun you can afford." : "Click a gun you can afford."; if (snap.phase === "over") return `Match over — ${snap.result ?? "done"} · a new match starts in ${secs(snap.clock)}s`; if (me.blind > 0.05) return "You are blind — back off until it clears"; if (snap.phase === "end") { const outcome = roundOutcome(snap); const next = `next round in ${secs(snap.clock)}s`; return outcome ? `${outcome.title} — ${outcome.why} · ${next}` : `Round over · ${next}`; } if (!me.alive) return snap.phase === "buy" ? "Buy phase — you are back next round" : "Waiting for the next round — you're in it"; if (snap.phase === "buy") { const side = me.side === "atk" ? "You attack: plant the spike on A or B." : "You defend: hold A and B."; return `Buy phase — ${secs(snap.clock)}s left. ${side} ${buyWith}`; } if (me.planting > 0) return `Planting — hold F for ${secs(4 - me.planting)}s more`; if (spike.planted) { if (me.side === "def") { if (spike.defuse > 0) return `Defusing — keep holding F, ${secs(7 - spike.defuse)}s`; return nearSpike ? `Spike down, ${secs(spike.fuse)}s — hold F to defuse` : `Spike down, ${secs(spike.fuse)}s — get to it and hold F`; } return `Spike down, ${secs(spike.fuse)}s — keep them off it`; } if (me.side === "atk") { if (me.spike) return onSite ? "Hold F to plant" : "You have the spike — get to site A or B"; if (spike.carried) return "Your team has the spike — go with them and take a site"; if (nearSpike) return "Walk onto the spike to pick it up"; return "Somebody has to carry the spike — it is on the ground"; } return `Defending — hold A and B, ${secs(snap.clock)}s`; }