wren/val Public Public: anyone can read this; nobody can change it.
forked from benjivers/val
workbenchthis machine
Files 1c30034c602bf5aa8777f1246b20f6bc05dd22e0
| 1 | /** |
| 2 | * One line of advice, in the player's own words. |
| 3 | * |
| 4 | * A pure function of the snapshot the server sent plus two things the client |
| 5 | * works out from the map it was given: whether you are standing on a site, |
| 6 | * and whether you are within reach of the spike. Never more than one prompt — |
| 7 | * the most urgent thing wins, because two instructions at once is none. |
| 8 | */ |
| 9 | |
| 10 | /** Within this many units the server lets you pick up, plant or defuse. */ |
| 11 | export const REACH = 46; |
| 12 | |
| 13 | const secs = (n) => Math.max(0, Math.ceil(n)); |
| 14 | |
| 15 | /** "Spike defused" the way the log says it, lower-cased to sit mid-sentence. */ |
| 16 | export const lower = (text) => String(text).charAt(0).toLowerCase() + String(text).slice(1); |
| 17 | |
| 18 | /** |
| 19 | * How the round just ended, from the viewer's side of it. The server says who |
| 20 | * took it and why; a person wants to know whether that was them. A viewer |
| 21 | * with no team (watching) is told the team. |
| 22 | */ |
| 23 | export function roundOutcome(snap) { |
| 24 | const last = snap && snap.last; |
| 25 | if (!last) return null; |
| 26 | const team = snap.me && snap.me.team; |
| 27 | const title = team ? (last.winner === team ? "Round won" : "Round lost") : `${last.winner} take the round`; |
| 28 | return { title, why: lower(last.why), winner: last.winner }; |
| 29 | } |
| 30 | |
| 31 | export function choosePrompt(snap, world = {}) { |
| 32 | if (!snap) return ""; |
| 33 | const me = snap.me; |
| 34 | const spike = snap.spike; |
| 35 | const onSite = world.onSite === true; |
| 36 | const nearSpike = world.nearSpike === true; |
| 37 | // The page is played with a mouse and the terminal with keys; one line |
| 38 | // names the input, so it says the thing the reader can actually do. |
| 39 | const buyWith = world.input === "keys" ? "Press b and buy a gun you can afford." : "Click a gun you can afford."; |
| 40 | |
| 41 | if (snap.phase === "over") |
| 42 | return `Match over — ${snap.result ?? "done"} · a new match starts in ${secs(snap.clock)}s`; |
| 43 | if (me.blind > 0.05) return "You are blind — back off until it clears"; |
| 44 | if (snap.phase === "end") { |
| 45 | const outcome = roundOutcome(snap); |
| 46 | const next = `next round in ${secs(snap.clock)}s`; |
| 47 | return outcome ? `${outcome.title} — ${outcome.why} · ${next}` : `Round over · ${next}`; |
| 48 | } |
| 49 | if (!me.alive) |
| 50 | return snap.phase === "buy" |
| 51 | ? "Buy phase — you are back next round" |
| 52 | : "Waiting for the next round — you're in it"; |
| 53 | if (snap.phase === "buy") { |
| 54 | const side = me.side === "atk" ? "You attack: plant the spike on A or B." : "You defend: hold A and B."; |
| 55 | return `Buy phase — ${secs(snap.clock)}s left. ${side} ${buyWith}`; |
| 56 | } |
| 57 | |
| 58 | if (me.planting > 0) return `Planting — hold F for ${secs(4 - me.planting)}s more`; |
| 59 | if (spike.planted) { |
| 60 | if (me.side === "def") { |
| 61 | if (spike.defuse > 0) return `Defusing — keep holding F, ${secs(7 - spike.defuse)}s`; |
| 62 | return nearSpike |
| 63 | ? `Spike down, ${secs(spike.fuse)}s — hold F to defuse` |
| 64 | : `Spike down, ${secs(spike.fuse)}s — get to it and hold F`; |
| 65 | } |
| 66 | return `Spike down, ${secs(spike.fuse)}s — keep them off it`; |
| 67 | } |
| 68 | |
| 69 | if (me.side === "atk") { |
| 70 | if (me.spike) return onSite ? "Hold F to plant" : "You have the spike — get to site A or B"; |
| 71 | if (spike.carried) return "Your team has the spike — go with them and take a site"; |
| 72 | if (nearSpike) return "Walk onto the spike to pick it up"; |
| 73 | return "Somebody has to carry the spike — it is on the ground"; |
| 74 | } |
| 75 | return `Defending — hold A and B, ${secs(snap.clock)}s`; |
| 76 | } |