Split The code is public; the app answers only to the crew.

A whole match on a phone: fire while aiming, weapon switch, scoreboard and a way to leave #25

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

vesper/cs:vesper/20-a-whole-match-on-a-phone-fire-while-aimi → main 1 commit fork

  • Merged. vesper/20-a-whole-match-on-a-phone-fire-while-aimi was merged into main as 97a2585.

Review

Sign in to review this change.
8 files changed +425 −96
README.md +22 −3
@@ -71,9 +71,28 @@ The bundle in `static/` is committed, so a fresh clone plays without building;
7171 <kbd>Shift</kbd> walk, mouse aims and fires, <kbd>R</kbd> reload,
7272 <kbd>E</kbd> plants or defuses, <kbd>B</kbd> opens the buy menu in the freeze
7373 time, <kbd>1</kbd>/<kbd>2</kbd>/<kbd>3</kbd> switch guns, <kbd>Tab</kbd> shows
74 the scoreboard, <kbd>Esc</kbd> pauses. On a touch screen the left of the view
75 is a thumb stick, the right looks around, and the buttons along the bottom do
76 the rest.
74 the scoreboard, <kbd>Esc</kbd> pauses.
75
76 On a touch screen the same actions are a pad, and the view is the aim:
77
78 | what | key | thumb |
79 | --- | --- | --- |
80 | move | <kbd>WASD</kbd> | the left third of the view is a stick |
81 | aim | mouse | drag anywhere on the rest of the view |
82 | fire | mouse | **a second finger on the right of the view**, or the fire bar |
83 | crouch, jump, use, reload | <kbd>Ctrl</kbd> <kbd>Space</kbd> <kbd>E</kbd> <kbd>R</kbd> | the pad under the view |
84 | guns | <kbd>1</kbd>/<kbd>2</kbd>/<kbd>3</kbd> | `1 rifle`, `2 pistol`, `3 knife` |
85 | scoreboard | <kbd>Tab</kbd> | hold `scores` |
86 | buy | <kbd>B</kbd> | `buy`, and `Close` when you are done |
87 | pause, leave the match | <kbd>Esc</kbd> | `menu` |
88
89 Firing is a second finger rather than a button under your aiming thumb because
90 a touch keeps the target it started on for its whole life: the finger already
91 dragging the view goes on steering while another one anywhere on the right
92 holds the trigger, and the trigger finger's own movement is thrown away. The
93 pad lives under the view rather than on it — on a phone the view is 4:3 and
94 the HUD already owns all four of its corners, so a button on the glass would
95 be sitting on the radar, the health or the ammo.
7796
7897 When you die the camera moves to a teammate who is still up and the HUD becomes
7998 theirs — their health, armour, ammo and gun, no crosshair — over a line naming
src/client/input.test.ts added +202
@@ -0,0 +1,202 @@
1 /**
2 * Input against a canvas and a document that are three lines of fake each: no
3 * browser, just the events one would send. What is worth checking here is the
4 * touch build, because that is where two fingers have to mean two different
5 * things at the same moment — the one that steers and the one that fires.
6 */
7
8 import { afterEach, expect, test } from "bun:test";
9 import { attachButtons, createControls, type Controls } from "./input.ts";
10 import { downAt } from "./press.ts";
11
12 type Listener = (event: unknown) => void;
13
14 /** An EventTarget that remembers its listeners so a test can fire them. */
15 function target(extra: Record<string, unknown> = {}): Record<string, unknown> & {
16 fire(type: string, event?: Record<string, unknown>): void;
17 } {
18 const listeners = new Map<string, Set<Listener>>();
19 return {
20 ...extra,
21 addEventListener(type: string, fn: Listener): void {
22 const set = listeners.get(type) ?? new Set<Listener>();
23 set.add(fn);
24 listeners.set(type, set);
25 },
26 removeEventListener(type: string, fn: Listener): void {
27 listeners.get(type)?.delete(fn);
28 },
29 fire(type: string, event: Record<string, unknown> = {}): void {
30 for (const fn of Array.from(listeners.get(type) ?? [])) {
31 fn({ preventDefault: () => {}, ...event });
32 }
33 },
34 };
35 }
36
37 type Canvas = ReturnType<typeof target>;
38
39 const globals = globalThis as unknown as Record<string, unknown>;
40 const before = { window: globals.window, document: globals.document };
41
42 // The fakes are this file's and nobody else's: the rest of the suite runs in
43 // the same process, and hud.ts asks whether `document` exists.
44 afterEach(() => {
45 globals.window = before.window;
46 globals.document = before.document;
47 });
48
49 /** A canvas 1000 wide: the stick is x < 380, the look and the trigger are past it. */
50 function harness(): { canvas: Canvas; controls: Controls } {
51 const canvas = target({ clientWidth: 1000, clientHeight: 600 });
52 globals.window = target();
53 globals.document = target({ pointerLockElement: null });
54 const controls = createControls(canvas as unknown as HTMLCanvasElement, 2.2);
55 return { canvas, controls };
56 }
57
58 /** One touch point, as the browser would hand it over. */
59 const at = (identifier: number, clientX: number, clientY = 300): Record<string, unknown> =>
60 ({ identifier, clientX, clientY });
61
62 const touch = (canvas: Canvas, type: string, ...points: Record<string, unknown>[]): void =>
63 canvas.fire(type, { changedTouches: points });
64
65 /** Is `control` down on the first step of a frame of `steps` steps? */
66 const firstStep = (controls: Controls, control: string, steps = 2): boolean =>
67 downAt(controls.take(steps), control, 0);
68
69 test("a second finger on the looking side fires, and the first keeps aiming", () => {
70 const { canvas, controls } = harness();
71 touch(canvas, "touchstart", at(1, 700));
72 touch(canvas, "touchmove", at(1, 740));
73 const [turned] = controls.takeLook();
74 expect(turned).toBeGreaterThan(0);
75
76 // The trigger lands while the aiming finger is still down.
77 touch(canvas, "touchstart", at(2, 900, 500));
78 expect(firstStep(controls, "fire")).toBe(true);
79
80 // ...and the aiming finger goes on steering, from where it actually is.
81 touch(canvas, "touchmove", at(1, 780));
82 const [again] = controls.takeLook();
83 expect(again).toBeCloseTo(turned, 6);
84 expect(firstStep(controls, "fire")).toBe(true); // still held
85
86 touch(canvas, "touchend", at(2, 900, 500));
87 expect(firstStep(controls, "fire")).toBe(false);
88 touch(canvas, "touchmove", at(1, 820));
89 expect(controls.takeLook()[0]).toBeCloseTo(turned, 6);
90 });
91
92 test("the finger that fires does not steer", () => {
93 const { canvas, controls } = harness();
94 touch(canvas, "touchstart", at(1, 700));
95 controls.takeLook();
96 touch(canvas, "touchstart", at(2, 900));
97 touch(canvas, "touchmove", at(2, 500, 100)); // a trigger finger twitching
98 expect(controls.takeLook()).toEqual([0, 0]);
99 });
100
101 test("a cancelled trigger releases: no stuck fire on a phone", () => {
102 const { canvas, controls } = harness();
103 touch(canvas, "touchstart", at(1, 700));
104 touch(canvas, "touchstart", at(2, 900));
105 controls.take(1);
106 touch(canvas, "touchcancel", at(2, 900));
107 expect(controls.take(1).held.has("fire")).toBe(false);
108 });
109
110 test("two trigger fingers are one trigger: the last one up releases it", () => {
111 const { canvas, controls } = harness();
112 touch(canvas, "touchstart", at(1, 700));
113 touch(canvas, "touchstart", at(2, 850), at(3, 950));
114 touch(canvas, "touchend", at(2, 850));
115 expect(controls.take(1).held.has("fire")).toBe(true);
116 touch(canvas, "touchend", at(3, 950));
117 expect(controls.take(1).held.has("fire")).toBe(false);
118 });
119
120 test("the left third is still the stick, and a second finger there does not fire", () => {
121 const { canvas, controls } = harness();
122 touch(canvas, "touchstart", at(1, 100));
123 touch(canvas, "touchmove", at(1, 160));
124 const frame = controls.take(1);
125 expect(controls.inputAt(frame, 0).side).toBeCloseTo(1, 6);
126 expect(frame.held.has("fire")).toBe(false);
127 touch(canvas, "touchend", at(1, 160));
128 expect(controls.inputAt(controls.take(1), 0).side).toBe(0);
129 });
130
131 // --- the buttons on the pad, which are the same action names as the keys
132
133 /** A fake `[data-action]` button, and a root that hands it to attachButtons. */
134 function pad(...actions: string[]): {
135 root: ParentNode;
136 press(action: string): void;
137 release(action: string): void;
138 classes(action: string): string[];
139 } {
140 const buttons = new Map<string, Canvas>();
141 for (const action of actions) {
142 const classes: string[] = [];
143 buttons.set(action, target({
144 dataset: { action },
145 classList: {
146 add: (name: string) => { if (!classes.includes(name)) classes.push(name); },
147 remove: (name: string) => {
148 const at = classes.indexOf(name);
149 if (at >= 0) classes.splice(at, 1);
150 },
151 names: classes,
152 },
153 }));
154 }
155 const root = { querySelectorAll: () => Array.from(buttons.values()) };
156 return {
157 root: root as unknown as ParentNode,
158 press: (action) => buttons.get(action)?.fire("pointerdown"),
159 release: (action) => buttons.get(action)?.fire("pointerup"),
160 classes: (action) => ((buttons.get(action)?.classList as { names: string[] }).names),
161 };
162 }
163
164 test("the menu button pauses without pointer lock", () => {
165 const { controls } = harness();
166 const buttons = pad("menu");
167 attachButtons(buttons.root, controls);
168 let paused = 0;
169 controls.onPause = () => { paused += 1; };
170 expect(controls.locked).toBe(false);
171 buttons.press("menu");
172 expect(paused).toBe(1);
173 buttons.release("menu");
174 expect(paused).toBe(1); // the release is not a second pause
175 });
176
177 test("the pad's slot, scoreboard and buy buttons are the keyboard's actions", () => {
178 const { controls } = harness();
179 const buttons = pad("slot1", "slot2", "slot3", "scoreboard", "buy");
180 attachButtons(buttons.root, controls);
181 let buys = 0;
182 controls.onBuy = () => { buys += 1; };
183
184 expect(controls.slotWanted).toBe(null);
185 for (const [action, slot] of [["slot1", 1], ["slot2", 2], ["slot3", 3]] as const) {
186 buttons.press(action);
187 expect(controls.slotWanted).toBe(slot);
188 buttons.release(action);
189 }
190 expect(controls.slotWanted).toBe(null);
191
192 expect(controls.scoreboard).toBe(false);
193 buttons.press("scoreboard");
194 expect(controls.scoreboard).toBe(true); // held, not a toggle
195 expect(buttons.classes("scoreboard")).toEqual(["on"]); // and it looks held
196 buttons.release("scoreboard");
197 expect(controls.scoreboard).toBe(false);
198 expect(buttons.classes("scoreboard")).toEqual([]);
199
200 buttons.press("buy");
201 expect(buys).toBe(1);
202 });
src/client/input.ts +41 −9
@@ -6,6 +6,10 @@
66 * Events are edges, and the loop only looks once a frame, so press.ts holds
77 * the edges in between: a tap that starts and ends between two frames still
88 * fires, exactly once.
9 *
10 * `lookX`/`lookY` accumulate in exactly two places — a locked mouse move and a
11 * touch whose role is `look` — and are drained once a frame by `takeLook`.
12 * Buttons never touch them; they only ever name an action.
913 */
1014
1115 import { clamp } from "./math.ts";
@@ -78,7 +82,12 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co
7882 setAction: (action, down) => {
7983 if (down) press.down(action);
8084 else press.up(action);
81 if (down && action === "buy") controls.onBuy();
85 if (!down) return;
86 // The two actions that are a menu rather than a movement. `menu` is how a
87 // touch screen reaches the pause overlay at all: pointer lock is the only
88 // other thing that calls onPause, and a finger never takes the lock.
89 if (action === "buy") controls.onBuy();
90 else if (action === "menu") controls.onPause();
8291 },
8392 onBuy: () => {},
8493 onPause: () => {},
@@ -132,14 +141,28 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co
132141 press.clear();
133142 });
134143
135 // --- touch: the left third is a stick, the rest of the canvas looks around
136 const touches = new Map<number, { startX: number; startY: number; x: number; y: number; stick: boolean }>();
144 // --- touch: the left third is a stick, the rest of the canvas looks around,
145 // and a second finger on the looking side is the trigger. That is the whole
146 // of firing while aiming: the touch that steers keeps its own target for its
147 // whole life, so a shot never costs the aim the thumb already has. A touch
148 // that fires does not steer — a trigger finger twitches, and the aim should
149 // not follow it.
150 type Role = "stick" | "look" | "fire";
151 type Held = { startX: number; startY: number; x: number; y: number; role: Role };
152 const touches = new Map<number, Held>();
153 const anyRole = (role: Role): boolean => {
154 for (const held of touches.values()) if (held.role === role) return true;
155 return false;
156 };
137157 canvas.addEventListener("touchstart", (event) => {
138158 for (const touch of Array.from(event.changedTouches)) {
139 const isStick = touch.clientX < canvas.clientWidth * 0.38;
159 const role: Role = touch.clientX < canvas.clientWidth * 0.38
160 ? "stick"
161 : anyRole("look") ? "fire" : "look";
140162 touches.set(touch.identifier, {
141 startX: touch.clientX, startY: touch.clientY, x: touch.clientX, y: touch.clientY, stick: isStick,
163 startX: touch.clientX, startY: touch.clientY, x: touch.clientX, y: touch.clientY, role,
142164 });
165 if (role === "fire") controls.setAction("fire", true);
143166 }
144167 event.preventDefault();
145168 }, { passive: false });
@@ -147,10 +170,10 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co
147170 for (const touch of Array.from(event.changedTouches)) {
148171 const state = touches.get(touch.identifier);
149172 if (state === undefined) continue;
150 if (state.stick) {
173 if (state.role === "stick") {
151174 stick.x = clamp((touch.clientX - state.startX) / 60, -1, 1);
152175 stick.y = clamp((state.startY - touch.clientY) / 60, -1, 1);
153 } else {
176 } else if (state.role === "look") {
154177 lookX += (touch.clientX - state.x) * controls.sensitivity * 0.06;
155178 lookY += (touch.clientY - state.y) * controls.sensitivity * 0.06;
156179 }
@@ -162,11 +185,15 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co
162185 const endTouch = (event: TouchEvent): void => {
163186 for (const touch of Array.from(event.changedTouches)) {
164187 const state = touches.get(touch.identifier);
165 if (state?.stick === true) {
188 touches.delete(touch.identifier);
189 if (state === undefined) continue;
190 if (state.role === "stick") {
166191 stick.x = 0;
167192 stick.y = 0;
168193 }
169 touches.delete(touch.identifier);
194 // The last finger off the trigger is the one that releases it: a stuck
195 // trigger on a phone is unrecoverable without reloading the page.
196 if (state.role === "fire" && !anyRole("fire")) controls.setAction("fire", false);
170197 }
171198 };
172199 canvas.addEventListener("touchend", endTouch);
@@ -183,12 +210,17 @@ export function createControls(canvas: HTMLCanvasElement, sensitivity = 2.2): Co
183210 export function attachButtons(root: ParentNode, controls: Controls): void {
184211 for (const element of Array.from(root.querySelectorAll<HTMLElement>("[data-action]"))) {
185212 const action = element.dataset.action as string;
213 // preventDefault keeps the browser from making a second event out of a
214 // touch, which also costs the button its :active state — so the class is
215 // the one that says "held", and a held control looks held.
186216 const down = (event: Event): void => {
187217 event.preventDefault();
218 element.classList.add("on");
188219 controls.setAction(action, true);
189220 };
190221 const up = (event: Event): void => {
191222 event.preventDefault();
223 element.classList.remove("on");
192224 controls.setAction(action, false);
193225 };
194226 element.addEventListener("pointerdown", down);
src/client/main.ts +42 −10
@@ -71,6 +71,25 @@ function boot(): void {
7171 element.hidden = !on;
7272 };
7373
74 // A touch screen has no pointer lock — asking for it there is a promise that
75 // rejects — so the phone build never asks, and the look comes off the glass.
76 const coarse = matchMedia("(pointer: coarse)").matches;
77 const lock = (): void => {
78 if (!coarse) void scene.requestPointerLock();
79 };
80 /**
81 * The touch pad is up only while a match is being played and nothing is over
82 * the view: an overlay is a dialog, and a jump button still live underneath
83 * one is a player moving where they cannot see.
84 */
85 const refreshPad = (): void => {
86 show(touchPanel, coarse && running);
87 // Hidden, not removed: an overlay comes and goes every freeze time, and a
88 // pad that left the flow would drag the page under it up and down with it.
89 touchPanel.classList.toggle(
90 "blocked", !(pause.hidden && buyPanel.hidden && summaryPanel.hidden));
91 };
92
7493 // --- the buy menu, rebuilt whenever it opens so the prices are honest
7594 function renderBuy(): void {
7695 if (world === null) return;
@@ -83,7 +102,9 @@ function boot(): void {
83102 ];
84103 buyPanel.innerHTML =
85104 `<h2>Buy <small>$${me.money}</small></h2><div class="grid"></div>` +
86 `<p class="hint">B or Escape closes. You can only buy in the freeze time.</p>`;
105 `<div class="row"><button type="button" id="buy-close">Close</button></div>` +
106 `<p class="hint">B, Escape or Close closes. You can only buy in the freeze time.</p>`;
107 $("buy-close").addEventListener("click", () => openBuy(false));
87108 const grid = buyPanel.querySelector(".grid") as HTMLElement;
88109 for (const [item, label] of items) {
89110 const price = priceOf(item, me);
@@ -104,18 +125,23 @@ function boot(): void {
104125 if (world === null) return;
105126 if (open && !canBuy(world, world.me)) return;
106127 show(buyPanel, open);
128 refreshPad();
107129 if (open) {
108130 renderBuy();
109131 document.exitPointerLock();
110132 (buyPanel.querySelector("button") as HTMLButtonElement | null)?.focus();
111133 } else if (running) {
112 void scene.requestPointerLock();
134 lock();
113135 }
114136 }
115137
116138 controls.onBuy = () => openBuy(buyPanel.hidden);
117139 controls.onPause = () => {
118 if (running && buyPanel.hidden) show(pause, true);
140 if (running && buyPanel.hidden) {
141 show(pause, true);
142 refreshPad();
143 $("resume").focus();
144 }
119145 };
120146
121147 // --- results, sent as the match goes rather than only at the end
@@ -179,24 +205,27 @@ function boot(): void {
179205 show(menu, false);
180206 show(pause, false);
181207 show(summaryPanel, false);
182 show(touchPanel, matchMedia("(pointer: coarse)").matches);
208 show(buyPanel, false);
209 refreshPad();
183210 audio.resume();
184 void scene.requestPointerLock();
211 lock();
185212 localStorage.setItem("cs.name", name);
186213 }
187214
188215 $("play").addEventListener("click", start);
189216 $("resume").addEventListener("click", () => {
190217 show(pause, false);
191 void scene.requestPointerLock();
218 refreshPad();
219 lock();
192220 });
193221 const toMenu = (): void => {
194222 running = false;
195223 world = null;
196224 show(pause, false);
197225 show(summaryPanel, false);
226 show(buyPanel, false);
198227 show(menu, true);
199 show(touchPanel, false);
228 refreshPad();
200229 $("play").focus();
201230 };
202231 $("quit").addEventListener("click", toMenu);
@@ -243,7 +272,10 @@ function boot(): void {
243272
244273 const me = world.me;
245274 const [dx, dy] = controls.takeLook();
246 if (me.alive && controls.locked) {
275 // Whatever look has accumulated is aim: the mouse only adds to it under
276 // lock and a finger only adds to it on the canvas, so there is nothing to
277 // gate here — and gating on the lock is what left a phone unable to turn.
278 if (me.alive) {
247279 me.yaw -= dx;
248280 me.pitch = clamp(me.pitch + dy, -89, 89);
249281 }
@@ -263,7 +295,7 @@ function boot(): void {
263295 for (let step = 0; step < steps; step += 1) {
264296 const input = controls.inputAt(pressed, step);
265297 input.switchTo = switchTo;
266 if (!buyPanel.hidden) input.fire = false;
298 if (!buyPanel.hidden || !pause.hidden) input.fire = false;
267299 if (!world.me.alive && input.fire) cycle = true;
268300 update(world, TICK, input);
269301 accumulator -= TICK;
@@ -283,7 +315,7 @@ function boot(): void {
283315 posted = played;
284316 document.exitPointerLock();
285317 show(buyPanel, false);
286 show(touchPanel, false);
318 refreshPad();
287319 // The last post is the only one that says the match finished, and the
288320 // score it carries is the one the overlay is about to show.
289321 const summary = summarize(world);
src/pages/game.ts +48 −16
@@ -51,7 +51,10 @@ small.commit { color:var(--dim); font-size:.75rem; }
5151 #scene, #hud { position:absolute; inset:0; width:100%; height:100%; display:block; }
5252 #hud { pointer-events:none; }
5353 .overlay { position:absolute; inset:0; display:flex; flex-direction:column; align-items:center; justify-content:center;
54 gap:.75rem; background:rgba(8,10,14,.82); text-align:center; padding:1rem; overflow:auto; }
54 gap:.75rem; background:rgba(8,10,14,.82); text-align:center; padding:1rem; overflow:auto;
55 /* #stage is touch-action:none for the look drag; an overlay taller
56 than the stage still has to scroll under a thumb. */
57 touch-action:pan-y; overscroll-behavior:contain; }
5558 .overlay[hidden] { display:none; }
5659 .overlay h2 { margin:0; font-size:1.3rem; }
5760 .overlay p { margin:.2rem 0; color:var(--dim); max-width:34rem; }
@@ -70,10 +73,26 @@ button.primary { background:#2563eb; border-color:#2563eb; font-weight:600; }
7073 #summary h2 { font-size:1.8rem; }
7174 #summary #summary-line { color:var(--text); font-size:.95rem; }
7275 #fps { position:absolute; right:.5rem; bottom:.4rem; font-size:.7rem; color:#8ea; opacity:.7; pointer-events:none; }
73 #touch { position:absolute; inset:auto 0 0 0; display:flex; justify-content:space-between; padding:.6rem; pointer-events:none; }
76 /* The pad is under the view, not over it: the stage is 4:3 on a phone and the
77 HUD already owns all four of its corners, so a button on the glass would sit
78 on the radar, the health or the ammo. Aim, move and fire stay on the canvas
79 — the pad is only the discrete actions. */
80 #touch { max-width:34rem; margin:0 auto 1.5rem; display:flex; flex-direction:column; gap:.4rem;
81 touch-action:manipulation; -webkit-user-select:none; user-select:none; -webkit-touch-callout:none; }
7482 #touch[hidden] { display:none; }
75 #touch .pad { display:flex; gap:.4rem; flex-wrap:wrap; pointer-events:auto; }
76 #touch button { opacity:.72; background:#0f131acc; }
83 #touch.blocked { visibility:hidden; }
84 #touch .pad { display:grid; grid-template-columns:repeat(auto-fit,minmax(3.4rem,1fr)); gap:.4rem; }
85 #touch .pad.wide { grid-template-columns:1fr; }
86 #touch button { display:flex; flex-direction:column; align-items:center; justify-content:center; gap:.05rem;
87 min-height:48px; min-width:44px; padding:.4rem .25rem; font-size:.82rem; line-height:1.15; }
88 #touch button b { color:var(--dim); font-size:.68rem; font-weight:600; }
89 #touch button.fire { background:#7f1d1d; border-color:#8f2222; font-weight:600; }
90 /* A thumb has no hover, so held has to look held — and the "on" class is put
91 there by the same handler that presses the control, since preventDefault
92 costs a touched button its :active. */
93 #touch button.on { background:#33405a; border-color:#4a5c7d; }
94 #touch button.fire.on { background:#b02626; border-color:#c33; }
95 #touch .hint { margin:0; text-align:center; color:var(--dim); font-size:.74rem; }
7796 main section { margin:1.5rem 0; }
7897 table { width:100%; border-collapse:collapse; font-size:.9rem; }
7998 th, td { text-align:left; padding:.35rem .5rem; border-bottom:1px solid var(--line); }
@@ -114,18 +133,6 @@ export function gamePage(db: Database, options: PageOptions): string {
114133 <canvas id="scene"></canvas>
115134 <canvas id="hud"></canvas>
116135 <div id="fps"></div>
117 <div id="touch" hidden>
118 <div class="pad">
119 <button type="button" data-action="duck">crouch</button>
120 <button type="button" data-action="jump">jump</button>
121 <button type="button" data-action="use">use</button>
122 </div>
123 <div class="pad">
124 <button type="button" data-action="reload">reload</button>
125 <button type="button" data-action="buy">buy</button>
126 <button type="button" data-action="fire">fire</button>
127 </div>
128 </div>
129136 <div class="overlay" id="menu">
130137 <h2>Defuse or detonate</h2>
131138 <p>Five against five on <b>de_forge</b>. Bomb rounds, CS movement and economy, bots that push the sites.</p>
@@ -139,6 +146,9 @@ export function gamePage(db: Database, options: PageOptions): string {
139146 <p class="hint"><kbd>WASD</kbd> move · <kbd>Space</kbd> jump · <kbd>Ctrl</kbd> crouch · <kbd>Shift</kbd> walk ·
140147 mouse to aim and fire · <kbd>R</kbd> reload · <kbd>E</kbd> plant or defuse · <kbd>B</kbd> buy ·
141148 <kbd>1</kbd><kbd>2</kbd><kbd>3</kbd> guns · <kbd>Tab</kbd> scores · <kbd>Esc</kbd> pause</p>
149 <p class="hint">On a phone: the left of the view is a thumb stick, the right aims, a second
150 finger on the right fires, and the pad under the view does the rest — guns, scores, buy
151 and a menu you can leave the match from.</p>
142152 </div>
143153 <div class="overlay" id="pause" hidden>
144154 <h2>Paused</h2>
@@ -157,6 +167,28 @@ export function gamePage(db: Database, options: PageOptions): string {
157167 <div class="overlay" id="buy" hidden></div>
158168 </div>
159169
170 <div id="touch" hidden>
171 <div class="pad">
172 <button type="button" data-action="slot1"><b>1</b>rifle</button>
173 <button type="button" data-action="slot2"><b>2</b>pistol</button>
174 <button type="button" data-action="slot3"><b>3</b>knife</button>
175 <button type="button" data-action="scoreboard">scores</button>
176 <button type="button" data-action="menu">menu</button>
177 </div>
178 <div class="pad">
179 <button type="button" data-action="duck">crouch</button>
180 <button type="button" data-action="jump">jump</button>
181 <button type="button" data-action="use">use</button>
182 <button type="button" data-action="reload">reload</button>
183 <button type="button" data-action="buy">buy</button>
184 </div>
185 <div class="pad wide">
186 <button type="button" class="fire" data-action="fire">fire</button>
187 </div>
188 <p class="hint">Left of the view is the stick, the right aims — and a second
189 finger anywhere on the right fires without moving your aim.</p>
190 </div>
191
160192 <section id="stats">${statsHtml(db)}</section>
161193
162194 <section>
src/server.test.ts +12
@@ -34,6 +34,18 @@ test("serves the game page with the leaderboard in the first paint", async () =>
3434 expect(body).toMatch(/<script type="module" src="\/static\/app\.[0-9a-f]+\.js">/);
3535 });
3636
37 test("the touch pad is in the first paint, and it names every action a match needs", async () => {
38 // Markup asks for behaviour: the CSP forbids inline script, so a control is
39 // a [data-action] in the page and nothing else. A pad missing one of these
40 // is a phone that cannot switch guns, see the scores or leave the match.
41 const body = await (await fetch(base + "/")).text();
42 const pad = body.slice(body.indexOf(`<div id="touch"`), body.indexOf("</div>\n\n <section"));
43 for (const action of ["slot1", "slot2", "slot3", "scoreboard", "menu", "duck", "jump", "use", "reload", "buy", "fire"])
44 expect(pad).toContain(`data-action="${action}"`);
45 // ...and it is under the view, not on it: the HUD owns the corners of #stage.
46 expect(body.indexOf(`<div id="touch"`)).toBeGreaterThan(body.indexOf(`<div class="overlay" id="buy"`));
47 });
48
3749 test("a first visit is given a session cookie that javascript cannot read", async () => {
3850 const response = await fetch(base + "/");
3951 const cookie = response.headers.get("set-cookie") ?? "";
static/app.642d83d9b8.js deleted −58

Diff not shown — a line 62,866 characters wide (limit 2,000). −58

View file
static/app.942a1ec39a.js added +58

Diff not shown — a line 63,210 characters wide (limit 2,000). +58

View file

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs