quill/val Public Public: anyone can read this; nobody can change it.
forked from benjivers/val
workbenchthis machine
Files a07c6a614323699908d79cb29213e15bff012908
| 1 | /** |
| 2 | * The page. The first paint is the match as it stands right now — score, |
| 3 | * round, phase, scoreboard — server-rendered HTML, so somebody arriving with |
| 4 | * a cold cache or no JavaScript still sees a live match. The client script |
| 5 | * then takes the same state over a socket and draws it. |
| 6 | */ |
| 7 | |
| 8 | import { AGENTS } from "../abilities.ts"; |
| 9 | import type { Result } from "../db.ts"; |
| 10 | import type { MatchState } from "../state.ts"; |
| 11 | import { WEAPONS, weaponIds } from "../weapons.ts"; |
| 12 | |
| 13 | export const escape = (s: string): string => |
| 14 | s.replace(/[&<>"']/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c] ?? c); |
| 15 | |
| 16 | const phaseWord = (state: MatchState): string => |
| 17 | state.phase === "buy" |
| 18 | ? "buy phase" |
| 19 | : state.phase === "live" |
| 20 | ? state.spike.planted |
| 21 | ? `spike down · ${Math.ceil(state.spike.fuse)}s` |
| 22 | : "live" |
| 23 | : state.phase === "end" |
| 24 | ? "round over" |
| 25 | : "match over"; |
| 26 | |
| 27 | export function gamePage(opts: { |
| 28 | state: MatchState; |
| 29 | script: string; |
| 30 | name: string; |
| 31 | env: string; |
| 32 | commit: string; |
| 33 | ssh: string | undefined; |
| 34 | results: Result[]; |
| 35 | }): string { |
| 36 | const { state } = opts; |
| 37 | const board = [...state.players].sort((a, b) => b.kills - a.kills); |
| 38 | const row = (side: "atk" | "def"): string => |
| 39 | board |
| 40 | .filter((p) => p.side === side) |
| 41 | .map( |
| 42 | (p) => |
| 43 | `<tr><td>${escape(p.name)}${p.bot ? " <small>bot</small>" : ""}</td><td>${escape(p.agent)}</td><td>${p.kills}</td><td>${p.deaths}</td></tr>`, |
| 44 | ) |
| 45 | .join(""); |
| 46 | |
| 47 | return `<!doctype html> |
| 48 | <html lang="en"> |
| 49 | <meta charset="utf-8"> |
| 50 | <meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover"> |
| 51 | <title>val — attack, defend, plant the spike</title> |
| 52 | <link rel="icon" href="data:,"> |
| 53 | <style> |
| 54 | :root { color-scheme: dark; --atk: #e35d5b; --def: #4fb3d9; --ink: #e8e6e3; } |
| 55 | * { box-sizing: border-box; } |
| 56 | body { margin: 0; background: #0e0f12; color: var(--ink); |
| 57 | font: 15px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; } |
| 58 | .wrap { max-width: 1100px; margin: 0 auto; padding: .6rem; } |
| 59 | h1 { font-size: 1.1rem; margin: 0; letter-spacing: .12em; text-transform: uppercase; } |
| 60 | header.top { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; } |
| 61 | .env { font-size: .7rem; padding: .1rem .5rem; border-radius: 99px; background: #2a2d34; } |
| 62 | #scoreline { display: flex; align-items: center; justify-content: center; gap: .6rem; |
| 63 | font-variant-numeric: tabular-nums; margin: .4rem 0; font-size: 1.05rem; } |
| 64 | .red { color: var(--atk); font-weight: 700; } .blue { color: var(--def); font-weight: 700; } |
| 65 | #clock { font-size: 1.5rem; font-weight: 700; min-width: 3ch; text-align: center; } |
| 66 | #phase { opacity: .75; font-size: .85rem; } |
| 67 | #stage { position: relative; width: 100%; aspect-ratio: 30 / 19; background: #14161b; |
| 68 | border: 1px solid #262a33; border-radius: .6rem; overflow: hidden; touch-action: none; } |
| 69 | #view { width: 100%; height: 100%; display: block; } |
| 70 | #flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; } |
| 71 | #dead { position: absolute; inset: 0; display: none; place-items: center; background: #0008; |
| 72 | font-size: 1.1rem; letter-spacing: .1em; text-transform: uppercase; } |
| 73 | #hud { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin: .5rem 0; |
| 74 | font-variant-numeric: tabular-nums; } |
| 75 | .chip { background: #1b1e25; border: 1px solid #2a2f3a; border-radius: .4rem; padding: .3rem .55rem; |
| 76 | font-size: .85rem; } |
| 77 | .kit { display: flex; gap: .4rem; flex-wrap: wrap; } |
| 78 | .kit button, .buys button, .agents button { min-height: 44px; min-width: 44px; padding: .4rem .6rem; |
| 79 | background: #1b1e25; color: var(--ink); border: 1px solid #333a47; border-radius: .45rem; |
| 80 | font: inherit; font-size: .8rem; cursor: pointer; } |
| 81 | .kit button[disabled], .buys button[disabled] { opacity: .35; cursor: not-allowed; } |
| 82 | .kit b { display: block; font-size: .7rem; opacity: .7; } |
| 83 | #buy { display: none; margin: .5rem 0; padding: .6rem; background: #151820; border: 1px solid #2a2f3a; |
| 84 | border-radius: .6rem; } |
| 85 | #buy.on { display: block; } |
| 86 | .buys, .agents { display: flex; gap: .4rem; flex-wrap: wrap; margin: .35rem 0; } |
| 87 | #log { margin: .5rem 0; font-size: .82rem; opacity: .8; min-height: 3.2em; } |
| 88 | #log div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; } |
| 89 | table { width: 100%; border-collapse: collapse; font-size: .85rem; } |
| 90 | th, td { text-align: left; padding: .25rem .4rem; border-bottom: 1px solid #23262e; } |
| 91 | td small { opacity: .5; } |
| 92 | .boards { display: grid; gap: .8rem; grid-template-columns: 1fr; } |
| 93 | @media (min-width: 720px) { .boards { grid-template-columns: 1fr 1fr; } } |
| 94 | footer { margin: 1.2rem 0 3rem; font-size: .82rem; opacity: .72; } |
| 95 | code { background: #1b1e25; padding: .1rem .35rem; border-radius: .3rem; } |
| 96 | form.name { display: flex; gap: .4rem; margin: .5rem 0; } |
| 97 | form.name input { flex: 1; min-height: 44px; padding: .4rem .6rem; background: #1b1e25; color: var(--ink); |
| 98 | border: 1px solid #333a47; border-radius: .45rem; font: inherit; } |
| 99 | form.name button { min-height: 44px; padding: 0 1rem; background: #3a4356; color: var(--ink); |
| 100 | border: 0; border-radius: .45rem; font: inherit; cursor: pointer; } |
| 101 | #touch { display: none; } |
| 102 | @media (pointer: coarse) { #touch { display: block; } } |
| 103 | </style> |
| 104 | <div class="wrap"> |
| 105 | <header class="top"> |
| 106 | <h1>val</h1> |
| 107 | <span class="env">${escape(opts.env)}${opts.commit ? " @ " + escape(opts.commit) : ""}</span> |
| 108 | <span class="env" id="conn">connecting…</span> |
| 109 | </header> |
| 110 | |
| 111 | <div id="scoreline"> |
| 112 | <span class="red">red ${state.score.red}</span> |
| 113 | <span id="clock">${Math.max(0, Math.ceil(state.clock))}</span> |
| 114 | <span class="blue">${state.score.blue} blue</span> |
| 115 | </div> |
| 116 | <div id="phase">round ${state.round} · ${escape(phaseWord(state))} · ${escape(state.attackers)} attacking${state.result ? ` · ${escape(state.result)}` : ""}</div> |
| 117 | |
| 118 | <div id="stage"> |
| 119 | <canvas id="view" width="900" height="570"></canvas> |
| 120 | <div id="flash"></div> |
| 121 | <div id="dead">waiting for the next round</div> |
| 122 | </div> |
| 123 | |
| 124 | <div id="hud"> |
| 125 | <span class="chip" id="hp">100 hp</span> |
| 126 | <span class="chip" id="gun">Classic 12</span> |
| 127 | <span class="chip" id="creds">800 cr</span> |
| 128 | <span class="chip" id="ultmeter">ult 0/6</span> |
| 129 | <span class="kit" id="kit"></span> |
| 130 | <button id="buytoggle" class="chip" type="button">buy (B)</button> |
| 131 | </div> |
| 132 | |
| 133 | <div id="buy"> |
| 134 | <div><strong>Buy phase</strong> — credits are refused server-side if you cannot afford them.</div> |
| 135 | <div class="agents" id="agents">${AGENTS.map( |
| 136 | (a) => `<button type="button" data-agent="${escape(a.id)}" title="${escape(a.blurb)}">${escape(a.name)} <small>${escape(a.role)}</small></button>`, |
| 137 | ).join("")}</div> |
| 138 | <div class="buys" id="guns">${weaponIds() |
| 139 | .map((id) => `<button type="button" data-buy="${id}">${escape(WEAPONS[id].name)} <b>${WEAPONS[id].cost}</b></button>`) |
| 140 | .join("")}</div> |
| 141 | <div class="buys" id="gear"> |
| 142 | <button type="button" data-buy="light">Light shield <b>400</b></button> |
| 143 | <button type="button" data-buy="heavy">Heavy shield <b>1000</b></button> |
| 144 | <button type="button" data-buy="c">Ability C</button> |
| 145 | <button type="button" data-buy="q">Ability Q</button> |
| 146 | </div> |
| 147 | </div> |
| 148 | |
| 149 | <div id="log">${state.events |
| 150 | .slice(0, 3) |
| 151 | .map((e) => `<div>${escape(e.text)}</div>`) |
| 152 | .join("")}</div> |
| 153 | |
| 154 | <form class="name" method="post" action="/join"> |
| 155 | <input name="name" maxlength="16" placeholder="Your name" value="${escape(opts.name)}" aria-label="Your name"> |
| 156 | <button>Take a seat</button> |
| 157 | </form> |
| 158 | |
| 159 | <div class="boards"> |
| 160 | <div><h2 class="env">attackers</h2><table><thead><tr><th>player</th><th>agent</th><th>k</th><th>d</th></tr></thead><tbody id="atkboard">${row("atk")}</tbody></table></div> |
| 161 | <div><h2 class="env">defenders</h2><table><thead><tr><th>player</th><th>agent</th><th>k</th><th>d</th></tr></thead><tbody id="defboard">${row("def")}</tbody></table></div> |
| 162 | </div> |
| 163 | |
| 164 | <footer> |
| 165 | <p><strong>Controls.</strong> WASD move · mouse aims · click fires · R reloads · |
| 166 | <strong>F</strong> plants and defuses (hold it) · C / Q / E / X cast · B opens the buy. |
| 167 | On a phone: left thumb walks, right thumb aims and fires, the buttons under the map do the rest.</p> |
| 168 | <p>Five attackers carry the spike, five defenders stop them. 100s round, 4s plant, 45s fuse, |
| 169 | 7s defuse with a checkpoint at half. First to 13, sides swap at 12. Bots fill every seat |
| 170 | nobody is in, and they use their kit.</p> |
| 171 | <p>Terminal: ${opts.ssh ? `<code>${escape(opts.ssh)}</code>` : "off in this environment."}</p> |
| 172 | ${opts.results.length ? `<p>Last matches: ${opts.results.map((r) => `${escape(r.summary)} <small>(${escape(r.ended_at)})</small>`).join(" · ")}</p>` : ""} |
| 173 | </footer> |
| 174 | </div> |
| 175 | <script src="${escape(opts.script)}" defer></script> |
| 176 | </html> |
| 177 | `; |
| 178 | } |