Public: anyone can read this; the crew can change it.
Migrated from github.com/Trevato/SO-ARM100
workbenchthis machine
Mobile queue and turns: Join in one tap, "you're next" alert, a ring countdown on your turn, a Ready check so idle phones don't hold the arm #57
Check · Review · Land done · Live — Landed; nothing deploys from this branch, so this is the end of its path.
smith/arm:smith/52-mobile-queue-and-turns-join-in-one-tap-y → main 3 commits fork 2 threads · 2 unresolved
8 files
changed +267
−23
Software/README.md +10
| @@ -315,6 +315,14 @@ its live pose and the cameras. Driving needs a name and a turn. | ||
| 315 | 315 | agent is kept with each ban to read, not to block on: too many people |
| 316 | 316 | share one browser for it to tell them apart. |
| 317 | 317 | |
| 318 | **The page's turn cues.** The status strip shows who drives and your place. | |
| 319 | When you are first in line it turns amber and the phone buzzes and plays a | |
| 320 | short tone (a checkbox under the queue mutes the tone). When your turn is | |
| 321 | called, **Ready** appears and must be tapped within 15 s or the turn passes | |
| 322 | on. While you drive, a ring around the seconds counts the time left, and | |
| 323 | **+30 s** adds time while nobody waits. The countdown text is not read out | |
| 324 | by screen readers; the call is announced once. | |
| 325 | ||
| 318 | 326 | **The terminal.** The room also has a terminal door (`src/tty.ts`, the |
| 319 | 327 | `tty` in `forge.json`). Connect with the exact command the forge gives in |
| 320 | 328 | `FORGE_APP_SSH`; when the forge has |
| @@ -324,6 +332,8 @@ is the owner, any other account is a signed-in person, and an unknown key | ||
| 324 | 332 | watches. A line you type is said in the chat; commands start with `/`: |
| 325 | 333 | |
| 326 | 334 | - `/join`, `/leave`: ask to drive (the owner takes over at once), hand back. |
| 335 | - `/ready`, `/extend`: confirm you are there when your turn is called (15 s, | |
| 336 | or it passes on), add 30 s to your turn while nobody waits. | |
| 327 | 337 | - `/end [name]`, owner: end that turn (no name: whoever drives), or take |
| 328 | 338 | them out of the line. |
| 329 | 339 | - `/mute <name>`, `/unmute <name>`, owner. |
Software/so101/src/lobby.test.ts +43
| @@ -23,6 +23,7 @@ const as = (id: string, name: string, role = "user") => { | ||
| 23 | 23 | return { "x-forge-identity": identity, "x-forge-signature": sign(identity, KEY) }; |
| 24 | 24 | }; |
| 25 | 25 | const ALICE = as("u-alice", "alice"); |
| 26 | const CAROL = as("u-carol", "carol"); | |
| 26 | 27 | const BOB = as("u-bob", "bob"); |
| 27 | 28 | const OWNER = as("u-owner", "trevato", "admin"); |
| 28 | 29 | |
| @@ -137,6 +138,48 @@ describe("lobby", () => { | ||
| 137 | 138 | expect(bob.heard.some((m) => m.type === "said" && m.message.from === null && /alice's turn ended \(handed back\); bob has control/.test(m.message.text))).toBe(true); |
| 138 | 139 | }); |
| 139 | 140 | |
| 141 | test("two clients: the turn passes to the one in line, who confirms Ready; one who does not loses it", async () => { | |
| 142 | const port = await start(new Turns({ readyMs: 150 })); | |
| 143 | const alice = await join(port, ALICE); | |
| 144 | const bob = await join(port, BOB); | |
| 145 | alice.send({ type: "join" }); | |
| 146 | await bob.next(drivingIs("alice")); | |
| 147 | bob.send({ type: "join" }); | |
| 148 | await alice.next((m) => m.type === "state" && m.queue.length === 1); | |
| 149 | alice.send({ type: "extend" }); | |
| 150 | expect((await alice.next((m) => m.type === "error")).error).toContain("no more time"); | |
| 151 | alice.send({ type: "leave" }); | |
| 152 | const called = await bob.next(drivingIs("bob")); | |
| 153 | expect(called.driver.readyBy).toBeGreaterThan(called.now); | |
| 154 | bob.send({ type: "ready" }); | |
| 155 | const ready = await bob.next((m) => m.type === "state" && m.driver?.readyBy === null); | |
| 156 | expect(ready.driver.name).toBe("bob"); | |
| 157 | await sleep(250); | |
| 158 | expect(lobby!.turns.driver?.name).toBe("bob"); | |
| 159 | bob.send({ type: "extend" }); | |
| 160 | await bob.next((m) => m.type === "state" && m.driver?.endsAt > ready.driver.endsAt); | |
| 161 | ||
| 162 | bob.send({ type: "leave" }); | |
| 163 | await alice.next(drivingIs(null)); | |
| 164 | alice.send({ type: "join" }); | |
| 165 | await alice.next(drivingIs("alice")); | |
| 166 | bob.send({ type: "join" }); | |
| 167 | alice.send({ type: "leave" }); | |
| 168 | await bob.next(drivingIs("bob")); | |
| 169 | await bob.next(drivingIs(null)); // never confirmed: the turn passes on (to nobody) | |
| 170 | ||
| 171 | // with a second person in line, the unconfirmed turn passes to them | |
| 172 | const carol = await join(port, CAROL); | |
| 173 | alice.send({ type: "join" }); | |
| 174 | await alice.next(drivingIs("alice")); | |
| 175 | bob.send({ type: "join" }); | |
| 176 | carol.send({ type: "join" }); | |
| 177 | await alice.next((m) => m.type === "state" && m.queue.length === 2); | |
| 178 | alice.send({ type: "leave" }); | |
| 179 | await carol.next(drivingIs("bob")); | |
| 180 | await carol.next(drivingIs("carol")); | |
| 181 | }); | |
| 182 | ||
| 140 | 183 | test("an idle driver loses the turn to the next in line, and their stream is closed", async () => { |
| 141 | 184 | const port = await start(new Turns({ idleMs: 300 })); |
| 142 | 185 | const alice = await join(port, ALICE); |
Software/so101/src/lobby.ts +7 −2
| @@ -6,6 +6,8 @@ | ||
| 6 | 6 | * |
| 7 | 7 | * Client → server, one JSON object per frame: |
| 8 | 8 | * {"type": "join"} ask to drive (the owner takes over at once) |
| 9 | * {"type": "ready"} confirm you are there when your turn comes (15 s) | |
| 10 | * {"type": "extend"} +30 s on your turn, while nobody waits | |
| 9 | 11 | * {"type": "leave"} hand back the turn, or leave the line |
| 10 | 12 | * {"type": "post", "text": "…"} say something |
| 11 | 13 | * {"type": "end", "id": "<user id>"} owner: end that turn, or take them out of the line |
| @@ -277,8 +279,11 @@ export class Lobby { | ||
| 277 | 279 | case "join": |
| 278 | 280 | if (this.takeControl(who) === "full") this.send(seat, { type: "error", error: `the line is full (${this.turns.maxQueue}); try again in a little while` }); |
| 279 | 281 | return; |
| 280 | case "leave": | |
| 281 | this.turns.leave(who.id); | |
| 282 | case "ready": | |
| 283 | this.turns.ready(who.id); | |
| 284 | return; | |
| 285 | case "extend": | |
| 286 | if (!this.turns.extend(who.id)) this.send(seat, { type: "error", error: "no more time: someone is waiting, or you have had the most allowed" }); | |
| 282 | 287 | return; |
| 283 | 288 | case "post": { |
| 284 | 289 | if (!who.owner && typeof m.text === "string" && this.filter.blocks(m.text)) throw new ChatError("that word is not allowed here"); |
Software/so101/src/terminal.test.ts +37 −2
| @@ -73,6 +73,8 @@ describe("terminal lines", () => { | ||
| 73 | 73 | const room = emptyRoom(); |
| 74 | 74 | hear(room, { type: "state", driver: { id: "u-alice", name: "alice" }, queue: [{ id: "u-bob", name: "bob" }], paused: false }); |
| 75 | 75 | expect(typed(room, "hello <b>there</b>")).toEqual({ send: { type: "post", text: "hello <b>there</b>" } }); |
| 76 | expect(typed(room, "/ready")).toEqual({ send: { type: "ready" } }); | |
| 77 | expect(typed(room, "/extend")).toEqual({ send: { type: "extend" } }); | |
| 76 | 78 | expect(typed(room, "/end")).toEqual({ send: { type: "end", id: "u-alice" } }); |
| 77 | 79 | expect(typed(room, "/end Bob")).toEqual({ send: { type: "end", id: "u-bob" } }); |
| 78 | 80 | expect(typed(room, "/mute alice")).toEqual({ send: { type: "mute", id: "u-alice", on: true } }); |
| @@ -103,8 +105,8 @@ describe("terminal door", () => { | ||
| 103 | 105 | relay = lobby = undefined; |
| 104 | 106 | }); |
| 105 | 107 | |
| 106 | const start = async () => { | |
| 107 | lobby = new Lobby({ identityKey: KEY, turns: new Turns(), chat: new Chat({ rateMs: 0 }), admin: new Admin(), accounts: new Accounts() }); | |
| 108 | const start = async (turns = new Turns()) => { | |
| 109 | lobby = new Lobby({ identityKey: KEY, turns, chat: new Chat({ rateMs: 0 }), admin: new Admin(), accounts: new Accounts() }); | |
| 108 | 110 | relay = await startRelay({ port: 0, simulation: `http://127.0.0.1:${simulation.port}`, lobby }); |
| 109 | 111 | return relay.port; |
| 110 | 112 | }; |
| @@ -167,6 +169,39 @@ describe("terminal door", () => { | ||
| 167 | 169 | await page.heardOne((m) => m.type === "said" && /alice's turn ended \(handed back\)/.test(m.message.text), "the hand-back on the page"); |
| 168 | 170 | }); |
| 169 | 171 | |
| 172 | test("a terminal is told once when it is next and once when it is called", () => { | |
| 173 | const room = emptyRoom(); | |
| 174 | hear(room, { type: "hello", you: { id: "u-bob", name: "bob" }, signin: true }); | |
| 175 | const alice = { id: "u-alice", name: "alice" }, bob = { id: "u-bob", name: "bob" }; | |
| 176 | const state = (driver: unknown, queue: unknown[]) => hear(room, { type: "state", driver, queue, paused: false }); | |
| 177 | expect(state(alice, [{ id: "u-carol", name: "carol" }, bob])).toEqual([]); | |
| 178 | expect(state(alice, [bob])).toEqual(["you are next"]); | |
| 179 | expect(state(alice, [bob])).toEqual([]); | |
| 180 | expect(state({ ...bob, readyBy: 123 }, [])).toEqual(["your turn: type /ready within 15 s or it passes on"]); | |
| 181 | expect(state({ ...bob, readyBy: 123 }, [])).toEqual([]); | |
| 182 | expect(state({ ...bob, readyBy: null }, [])).toEqual([]); | |
| 183 | }); | |
| 184 | ||
| 185 | test("a terminal driver called from the line confirms /ready and keeps the turn", async () => { | |
| 186 | const port = await start(new Turns({ readyMs: 300 })); | |
| 187 | const alice = await terminal(port, ttyEnv("u-alice", "alice", "user")); | |
| 188 | const bob = await terminal(port, ttyEnv("u-bob", "bob", "user")); | |
| 189 | const page = await viewer(port); | |
| 190 | alice.door.input("/join"); | |
| 191 | await page.heardOne((m) => m.type === "state" && m.driver?.id === "u-alice", "alice driving"); | |
| 192 | bob.door.input("/join"); | |
| 193 | await page.heardOne((m) => m.type === "state" && m.queue.length === 1, "bob in line"); | |
| 194 | alice.door.input("/leave"); | |
| 195 | await page.heardOne((m) => m.type === "state" && m.driver?.id === "u-bob" && m.driver.readyBy > 0, "bob called"); | |
| 196 | bob.door.input("/ready"); | |
| 197 | await page.heardOne((m) => m.type === "state" && m.driver?.id === "u-bob" && m.driver.readyBy === null, "bob ready"); | |
| 198 | await new Promise((r) => setTimeout(r, 450)); | |
| 199 | expect(lobby!.turns.driver?.name).toBe("bob"); | |
| 200 | const before = lobby!.turns.driver!.endsAt; | |
| 201 | bob.door.input("/extend"); | |
| 202 | await page.heardOne((m) => m.type === "state" && m.driver?.id === "u-bob" && m.driver.endsAt > before, "bob has more time"); | |
| 203 | }); | |
| 204 | ||
| 170 | 205 | test("mute: only the owner's terminal may; the muted person's page hears it, and their terminal cannot chat", async () => { |
| 171 | 206 | const port = await start(); |
| 172 | 207 | const alicePage = await viewer(port, web("u-alice", "alice")); |
Software/so101/src/terminal.ts +22 −3
| @@ -11,6 +11,7 @@ | ||
| 11 | 11 | * Typed lines: |
| 12 | 12 | * <text> say it |
| 13 | 13 | * /join /leave ask to drive (the owner takes over), or hand back / leave the line |
| 14 | * /ready /extend confirm you are there when called (15 s), or add 30 s to your turn | |
| 14 | 15 | * /end [who] owner: end that turn (none: whoever drives), or take them out of the line |
| 15 | 16 | * /mute <who> /unmute <who> owner |
| 16 | 17 | * /who /help /quit |
| @@ -63,11 +64,13 @@ export interface Room { | ||
| 63 | 64 | driver: Someone | null; |
| 64 | 65 | queue: Someone[]; |
| 65 | 66 | paused: boolean; |
| 67 | /** Whether the call to confirm, and the "you are next" cue, were already printed (each is said once). */ | |
| 68 | told: { called: boolean; next: boolean }; | |
| 66 | 69 | /** Everyone seen by name, most recent last: to turn a typed name into an id. */ |
| 67 | 70 | seen: Map<string, Someone>; |
| 68 | 71 | } |
| 69 | 72 | |
| 70 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, seen: new Map() }); | |
| 73 | export const emptyRoom = (): Room => ({ you: null, muted: false, banned: false, driver: null, queue: [], paused: false, told: { called: false, next: false }, seen: new Map() }); | |
| 71 | 74 | |
| 72 | 75 | const someone = (x: unknown): Someone | null => { |
| 73 | 76 | if (typeof x !== "object" || x === null) return null; |
| @@ -87,7 +90,21 @@ const said = (m: unknown): string => { | ||
| 87 | 90 | return from ? `${plain(from.name)}: ${plain(message?.text)}` : `· ${plain(message?.text)}`; |
| 88 | 91 | }; |
| 89 | 92 | |
| 90 | export const HELP = "type to chat · /join /leave · /end [who] /mute <who> /unmute <who> (owner) · /who · /quit"; | |
| 93 | export const HELP = "type to chat · /join /leave /ready /extend · /end [who] /mute <who> /unmute <who> (owner) · /who · /quit"; | |
| 94 | ||
| 95 | /** Once each: "you are called, confirm" and "you are next", so a terminal driver gets what the page's strip shows. */ | |
| 96 | function cues(room: Room, driver: unknown): string[] { | |
| 97 | const out: string[] = []; | |
| 98 | const me = room.you?.id; | |
| 99 | const readyBy = (driver as { readyBy?: unknown } | null)?.readyBy; | |
| 100 | const called = Boolean(me && room.driver?.id === me && typeof readyBy === "number"); | |
| 101 | if (called && !room.told.called) out.push("your turn: type /ready within 15 s or it passes on"); | |
| 102 | room.told.called = called; | |
| 103 | const next = Boolean(me && !room.paused && room.queue[0]?.id === me); | |
| 104 | if (next && !room.told.next) out.push("you are next"); | |
| 105 | room.told.next = next; | |
| 106 | return out; | |
| 107 | } | |
| 91 | 108 | |
| 92 | 109 | /** Fold one server frame into the room; what to print for it, if anything. */ |
| 93 | 110 | export function hear(room: Room, frame: Frame): string[] { |
| @@ -112,7 +129,7 @@ export function hear(room: Room, frame: Frame): string[] { | ||
| 112 | 129 | room.paused = frame.paused === true; |
| 113 | 130 | see(room, room.driver); |
| 114 | 131 | for (const p of room.queue) see(room, p); |
| 115 | return []; | |
| 132 | return cues(room, frame.driver); | |
| 116 | 133 | } |
| 117 | 134 | case "chat": { |
| 118 | 135 | const messages = Array.isArray(frame.messages) ? frame.messages : []; |
| @@ -155,6 +172,8 @@ export function typed(room: Room, input: string): Typed { | ||
| 155 | 172 | switch (verb.toLowerCase()) { |
| 156 | 173 | case "join": |
| 157 | 174 | case "leave": |
| 175 | case "ready": | |
| 176 | case "extend": | |
| 158 | 177 | return { send: { type: verb.toLowerCase() } }; |
| 159 | 178 | case "end": { |
| 160 | 179 | if (!arg && !room.driver) return { print: "nobody has control" }; |
Software/so101/src/turns.test.ts +49
| @@ -190,3 +190,52 @@ describe("turns", () => { | ||
| 190 | 190 | expect(turns.driver).toBeNull(); |
| 191 | 191 | }); |
| 192 | 192 | }); |
| 193 | ||
| 194 | describe("ready check and extra time", () => { | |
| 195 | test("someone called from the line who does not confirm in 15 s loses the turn to the next", () => { | |
| 196 | const events = make(); | |
| 197 | turns.join(alice); | |
| 198 | turns.join(bob); | |
| 199 | turns.join(carol); | |
| 200 | advance(5_000); | |
| 201 | turns.leave("a"); | |
| 202 | expect(turns.snapshot().driver).toMatchObject({ id: "b", readyBy: clock + 15_000 }); | |
| 203 | advance(14_000); | |
| 204 | expect(turns.driver?.id).toBe("b"); | |
| 205 | advance(1_100); | |
| 206 | expect(turns.driver?.id).toBe("c"); | |
| 207 | expect(events.find((e) => e.kind === "turn" && e.ended?.reason === "not ready")).toBeTruthy(); | |
| 208 | advance(16_000); | |
| 209 | expect(turns.driver).toBeNull(); // nobody left to pass it to | |
| 210 | }); | |
| 211 | ||
| 212 | test("ready, or any command, keeps the turn; asking for an idle arm needs no check", () => { | |
| 213 | make(); | |
| 214 | expect(turns.join(alice)).toBe("driving"); | |
| 215 | expect(turns.snapshot().driver?.readyBy).toBeNull(); | |
| 216 | turns.join(bob); | |
| 217 | turns.join(carol); | |
| 218 | turns.leave("a"); | |
| 219 | expect(turns.ready("b")).toBe(true); | |
| 220 | advance(16_000); | |
| 221 | turns.activity("b"); | |
| 222 | expect(turns.driver?.id).toBe("b"); | |
| 223 | turns.leave("b"); | |
| 224 | turns.activity("c"); | |
| 225 | advance(16_000); | |
| 226 | expect(turns.driver?.id).toBe("c"); | |
| 227 | }); | |
| 228 | ||
| 229 | test("+30 s only while nobody waits, and not without limit", () => { | |
| 230 | make(); | |
| 231 | turns.join(alice); | |
| 232 | expect(turns.extend("a")).toBe(true); | |
| 233 | expect(turns.driver?.endsAt).toBe(1_000_000 + 210_000); | |
| 234 | turns.join(bob); | |
| 235 | expect(turns.extend("a")).toBe(false); | |
| 236 | expect(turns.extend("b")).toBe(false); | |
| 237 | turns.leave("b"); | |
| 238 | for (let i = 0; i < 10; i++) turns.extend("a"); | |
| 239 | expect(turns.driver!.endsAt - turns.driver!.startedAt).toBe(360_000); | |
| 240 | }); | |
| 241 | }); |
Software/so101/src/turns.ts +45 −10
| @@ -15,9 +15,13 @@ export interface Turn extends Person { | ||
| 15 | 15 | startedAt: number; |
| 16 | 16 | endsAt: number; |
| 17 | 17 | lastActive: number; |
| 18 | /** Until this, someone taken from the line must confirm they are there; null once they have (or never needed to). */ | |
| 19 | readyBy: number | null; | |
| 20 | /** Time added on request when nobody waits. */ | |
| 21 | bonusMs: number; | |
| 18 | 22 | } |
| 19 | 23 | |
| 20 | export type EndReason = "handed back" | "time is up" | "idle" | "disconnected" | "ended by the owner" | "taken over by the owner" | "paused for maintenance"; | |
| 24 | export type EndReason = "handed back" | "time is up" | "idle" | "not ready" | "disconnected" | "ended by the owner" | "taken over by the owner" | "paused for maintenance"; | |
| 21 | 25 | |
| 22 | 26 | export type TurnEvent = { kind: "turn"; ended: { who: Person; reason: EndReason } | null; started: Person | null } | { kind: "queue" }; |
| 23 | 27 | |
| @@ -27,13 +31,14 @@ export interface Snapshot { | ||
| 27 | 31 | idleMs: number; |
| 28 | 32 | paused: boolean; |
| 29 | 33 | maxQueue: number; |
| 30 | driver: { id: string; name: string; owner: boolean; endsAt: number; idleEndsAt: number } | null; | |
| 34 | driver: { id: string; name: string; owner: boolean; endsAt: number; idleEndsAt: number; readyBy: number | null } | null; | |
| 31 | 35 | queue: Array<{ id: string; name: string; place: number; waitMs: number }>; |
| 32 | 36 | } |
| 33 | 37 | |
| 34 | 38 | export interface TurnsOptions { |
| 35 | 39 | turnMs?: number; |
| 36 | 40 | idleMs?: number; |
| 41 | readyMs?: number; | |
| 37 | 42 | graceMs?: number; |
| 38 | 43 | maxQueue?: number; |
| 39 | 44 | paused?: boolean; |
| @@ -44,6 +49,8 @@ export class Turns { | ||
| 44 | 49 | turnMs: number; |
| 45 | 50 | idleMs: number; |
| 46 | 51 | readonly graceMs: number; |
| 52 | /** How long someone called from the line has to say they are there. */ | |
| 53 | readyMs: number; | |
| 47 | 54 | maxQueue: number; |
| 48 | 55 | paused: boolean; |
| 49 | 56 | private readonly now: () => number; |
| @@ -59,6 +66,7 @@ export class Turns { | ||
| 59 | 66 | this.turnMs = options.turnMs ?? 180_000; |
| 60 | 67 | this.idleMs = options.idleMs ?? 30_000; |
| 61 | 68 | this.graceMs = options.graceMs ?? 10_000; |
| 69 | this.readyMs = options.readyMs ?? 15_000; | |
| 62 | 70 | this.maxQueue = options.maxQueue ?? Infinity; |
| 63 | 71 | this.paused = options.paused ?? false; |
| 64 | 72 | this.now = options.now ?? Date.now; |
| @@ -85,7 +93,7 @@ export class Turns { | ||
| 85 | 93 | if ((this.driver || this.paused) && this.queue.length >= this.maxQueue) return "full"; |
| 86 | 94 | this.queue.push({ ...who }); |
| 87 | 95 | } |
| 88 | if (!this.driver && !this.paused) this.handOver(null); | |
| 96 | if (!this.driver && !this.paused) this.handOver(null, undefined, false); | |
| 89 | 97 | else this.emit({ kind: "queue" }); |
| 90 | 98 | return this.isDriver(who.id) ? "driving" : "queued"; |
| 91 | 99 | } |
| @@ -111,7 +119,7 @@ export class Turns { | ||
| 111 | 119 | if (limits.turnMs !== undefined) this.turnMs = limits.turnMs; |
| 112 | 120 | if (limits.idleMs !== undefined) this.idleMs = limits.idleMs; |
| 113 | 121 | if (limits.maxQueue !== undefined) this.maxQueue = limits.maxQueue; |
| 114 | if (this.driver) this.driver.endsAt = this.driver.startedAt + this.turnMs; | |
| 122 | if (this.driver) this.driver.endsAt = this.driver.startedAt + this.turnMs + this.driver.bonusMs; | |
| 115 | 123 | this.check(); |
| 116 | 124 | this.emit({ kind: "queue" }); |
| 117 | 125 | } |
| @@ -128,9 +136,32 @@ export class Turns { | ||
| 128 | 136 | if (this.remove(id)) this.emit({ kind: "queue" }); |
| 129 | 137 | } |
| 130 | 138 | |
| 131 | /** The driver did something: the idle clock starts over. */ | |
| 139 | /** The driver did something: the idle clock starts over, and they are plainly there. */ | |
| 132 | 140 | activity(id: string): void { |
| 133 | if (this.driver?.id === id) this.driver.lastActive = this.now(); | |
| 141 | if (this.driver?.id === id) { | |
| 142 | this.driver.lastActive = this.now(); | |
| 143 | this.driver.readyBy = null; | |
| 144 | } | |
| 145 | } | |
| 146 | ||
| 147 | /** The driver says they are there. */ | |
| 148 | ready(id: string): boolean { | |
| 149 | if (!this.driver || this.driver.id !== id) return false; | |
| 150 | this.driver.readyBy = null; | |
| 151 | this.driver.lastActive = this.now(); | |
| 152 | this.emit({ kind: "queue" }); | |
| 153 | return true; | |
| 154 | } | |
| 155 | ||
| 156 | /** The driver asks for more time: allowed only while nobody waits, and at most one more turn's worth. */ | |
| 157 | extend(id: string, ms = 30_000): boolean { | |
| 158 | const d = this.driver; | |
| 159 | if (!d || d.id !== id || this.queue.length || d.bonusMs + ms > this.turnMs) return false; | |
| 160 | d.bonusMs += ms; | |
| 161 | d.endsAt = d.startedAt + this.turnMs + d.bonusMs; | |
| 162 | this.schedule(); | |
| 163 | this.emit({ kind: "queue" }); | |
| 164 | return true; | |
| 134 | 165 | } |
| 135 | 166 | |
| 136 | 167 | connect(id: string): void { |
| @@ -159,6 +190,7 @@ export class Turns { | ||
| 159 | 190 | } |
| 160 | 191 | const d = this.driver; |
| 161 | 192 | if (d && now >= d.endsAt) this.handOver("time is up"); |
| 193 | else if (d && d.readyBy !== null && now >= d.readyBy) this.handOver("not ready"); | |
| 162 | 194 | else if (d && now - d.lastActive >= this.idleMs) this.handOver("idle"); |
| 163 | 195 | if (queueChanged) this.emit({ kind: "queue" }); |
| 164 | 196 | this.schedule(); |
| @@ -174,7 +206,7 @@ export class Turns { | ||
| 174 | 206 | idleMs: this.idleMs, |
| 175 | 207 | paused: this.paused, |
| 176 | 208 | maxQueue: Number.isFinite(this.maxQueue) ? this.maxQueue : -1, |
| 177 | driver: d ? { id: d.id, name: d.name, owner: d.owner, endsAt: d.endsAt, idleEndsAt: Math.min(d.endsAt, d.lastActive + this.idleMs) } : null, | |
| 209 | driver: d ? { id: d.id, name: d.name, owner: d.owner, endsAt: d.endsAt, idleEndsAt: Math.min(d.endsAt, d.lastActive + this.idleMs), readyBy: d.readyBy } : null, | |
| 178 | 210 | // At most: a turn can end early, never late. |
| 179 | 211 | queue: this.queue.map((p, i) => ({ id: p.id, name: p.name, place: i + 1, waitMs: left + i * this.turnMs })), |
| 180 | 212 | }; |
| @@ -193,13 +225,15 @@ export class Turns { | ||
| 193 | 225 | } |
| 194 | 226 | |
| 195 | 227 | /** End the current turn (if a reason is given) and start the next: `next`, or the head of the line. */ |
| 196 | private handOver(reason: EndReason | null, next?: Person): void { | |
| 228 | private handOver(reason: EndReason | null, next?: Person, confirm = true): void { | |
| 197 | 229 | const ended = this.driver && reason ? { who: { id: this.driver.id, name: this.driver.name, owner: this.driver.owner }, reason } : null; |
| 198 | 230 | if (reason) this.driver = null; |
| 199 | 231 | if (this.driver) return; |
| 200 | const who = next ?? (this.paused ? undefined : this.queue.shift()); | |
| 232 | const called = next ? undefined : this.paused ? undefined : this.queue.shift(); | |
| 233 | const who = next ?? called; | |
| 201 | 234 | const now = this.now(); |
| 202 | this.driver = who ? { ...who, startedAt: now, endsAt: now + this.turnMs, lastActive: now } : null; | |
| 235 | // Only someone called out of the line has to confirm; a person who asked for an idle arm is looking at it. | |
| 236 | this.driver = who ? { ...who, startedAt: now, endsAt: now + this.turnMs, lastActive: now, readyBy: called && confirm ? now + this.readyMs : null, bonusMs: 0 } : null; | |
| 203 | 237 | this.emit({ kind: "turn", ended, started: who ?? null }); |
| 204 | 238 | this.schedule(); |
| 205 | 239 | } |
| @@ -212,6 +246,7 @@ export class Turns { | ||
| 212 | 246 | clearTimeout(this.timer); |
| 213 | 247 | const deadlines = [...this.gone.values()].map((since) => since + this.graceMs); |
| 214 | 248 | if (this.driver) deadlines.push(this.driver.endsAt, this.driver.lastActive + this.idleMs); |
| 249 | if (this.driver?.readyBy != null) deadlines.push(this.driver.readyBy); | |
| 215 | 250 | if (!deadlines.length) return; |
| 216 | 251 | // The idle deadline moves with every command; waking early just reschedules. |
| 217 | 252 | const wait = Math.max(0, Math.min(...deadlines) - this.now()); |
Software/so101/src/viewer.html +54 −6
| @@ -90,6 +90,14 @@ | ||
| 90 | 90 | .estop { flex: none; min-width: 72px; min-height: 52px; border: 0; border-radius: 12px; background: #d1242f; color: #fff; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; box-shadow: 0 0 0 3px rgba(209,36,47,.25), 0 2px 8px rgba(0,0,0,.35); } |
| 91 | 91 | .estop:hover { background: #e5323d; } .estop:active { transform: scale(.97); } |
| 92 | 92 | #sheetToggle { flex: none; } |
| 93 | /* the turn ring: time left as a sweep around the seconds; the driver's name over the video for everyone */ | |
| 94 | #ring { flex: none; width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center; background: conic-gradient(var() var(, 0%), var() 0); font-size: 13px; font-weight: 700; font-variant-numeric: tabular-nums; position: relative; } | |
| 95 | #ring::before { content: ""; position: absolute; inset: 5px; border-radius: 50%; background: var(); } #ring span { position: relative; } | |
| 96 | #ring.low { background: conic-gradient(var() var(, 0%), var() 0); color: var(); } | |
| 97 | #strip.next { background: color-mix(in srgb, var() 22%, var()); box-shadow: inset 0 0 0 2px var(); } | |
| 98 | #strip.ready { box-shadow: inset 0 0 0 2px var(); } #turnActions .ready { min-height: 48px; padding: 0 20px; font-weight: 800; } | |
| 99 | @media (prefers-reduced-motion: no-preference) { #turnActions .ready { animation: pulse 1s ease-in-out infinite; } @keyframes pulse { 50% { opacity: .65; } } } | |
outdated
This hides the plain Stop on phones. Keep it unless the issue asked for it. Also wrap the pulse animation in |
||
| 100 | #driverTag { position: absolute; z-index: 2; left: calc(10px + var()); bottom: 10px; max-width: 70%; padding: 4px 10px; border-radius: 999px; background: rgba(0,0,0,.6); color: #fff; font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none; } #driverTag:empty { display: none; } | |
| 93 | 101 | |
| 94 | 102 | /* the sheet: one panel at a time, the tabs under the thumb */ |
| 95 | 103 | #sheet { grid-area: sheet; min-height: 0; display: flex; flex-direction: column; background: var(); } |
| @@ -240,12 +248,14 @@ | ||
| 240 | 248 | <span class="info"><span id="link" hidden></span><span id="live" class="label"></span><span class="dot" id="dot"></span><span id="state">…</span></span> |
| 241 | 249 | <button id="fs" title="F">Fullscreen</button> |
| 242 | 250 | </div> |
| 251 | <div id="driverTag" aria-hidden="true"></div> | |
| 243 | 252 | <div id="banner" role="alert"></div> |
| 244 | 253 | <div id="msg" role="status"></div> |
| 245 | 254 | </div> |
| 246 | 255 | |
| 247 | 256 | <section id="strip" aria-label="Who drives" aria-live="polite"> |
| 248 | <div class="who"><div id="driverLine" class="none">Connecting…</div><div id="clock"></div><div id="capLine"></div></div> | |
| 257 | <div id="ring" hidden aria-hidden="true"><span id="ringN"></span></div> | |
| 258 | <div class="who"><div id="driverLine" class="none">Connecting…</div><div id="clock" aria-live="off"></div><div id="capLine"></div></div> | |
| 249 | 259 | <div id="turnActions"></div> |
| 250 | 260 | <button id="sheetToggle" hidden aria-controls="sheet" aria-expanded="true">Hide</button> |
| 251 | 261 | <button id="stop" class="danger" hidden title="End what the arm is doing and hold (space)">Stop</button> |
| @@ -262,6 +272,7 @@ | ||
| 262 | 272 | <div class="tab" id="tab-queue" role="tabpanel" aria-labelledby="t-queue" hidden> |
| 263 | 273 | <h2>Driving</h2> |
| 264 | 274 | <div class="row" id="queueActions"></div> |
| 275 | <label class="row" style="font-size:13px"><input type="checkbox" id="quiet"> Mute the "you're next" tone</label> | |
| 265 | 276 | <form id="joinForm" hidden><input id="joinName" type="text" required minlength="3" maxlength="20" pattern="[A-Za-z0-9_\-]{3,20}" autocomplete="nickname" autocapitalize="off" spellcheck="false" enterkeyhint="go" placeholder="Pick a name" aria-label="Your name" title="3-20 letters, digits, _ or -"><button class="go">Join</button></form> |
| 266 | 277 | <p id="joinErr" role="alert"></p> |
| 267 | 278 | <a id="forgeSignin" hidden>Have a forge account? Sign in instead</a> |
| @@ -1299,13 +1310,41 @@ | ||
| 1299 | 1310 | if (!lobby.state || !lobby.hello?.signin) return void (c.textContent = lobby.hello && !lobby.hello.signin ? "Local: no sign-in, every visitor drives." : ""); |
| 1300 | 1311 | const mine = me ? lobby.state.queue.find((x) => x.id === me.id) : null; |
| 1301 | 1312 | const place = mine ? ` · you're ${ordinal(mine.place)} in line, ≤ ${Math.max(1, Math.ceil(mine.waitMs / 60000))} min` : ""; |
| 1313 | if (!d) $("ring").hidden = true; | |
| 1302 | 1314 | if (!d) { c.className = ""; c.textContent = lobby.state.paused ? `No new turns until the owner resumes${place || "; you can still join the line"}.` : "Free: take control to drive."; return; } |
| 1303 | 1315 | const now = serverNow(), idle = d.idleEndsAt - now; |
| 1304 | 1316 | const idleSoon = idle < 15000 && d.idleEndsAt < d.endsAt; |
| 1305 | c.className = idleSoon ? "idle" : ""; | |
| 1317 | c.className = idleSoon || d.readyBy ? "idle" : ""; | |
| 1318 | tickRing(d, me, now); | |
| 1319 | if (d.readyBy && me && d.id === me.id) return void (c.textContent = `Tap Ready within ${Math.max(0, Math.ceil((d.readyBy - now) / 1000))} s or the turn passes on`); | |
| 1306 | 1320 | c.textContent = idleSoon ? `Idle: the turn ends in ${Math.max(0, Math.ceil(idle / 1000))} s unless the arm moves` : `${minutes(d.endsAt - now)} left${place}`; |
| 1307 | 1321 | } |
| 1322 | /** The ring is the driver's own: time left on the turn, in the seconds around it. */ | |
| 1323 | function tickRing(d, me, now) { | |
| 1324 | const ring = $("ring"), mine = Boolean(me && d.id === me.id), st = lobby.state; | |
| 1325 | ring.hidden = !mine; | |
| 1326 | if (!mine) return; | |
| 1327 | const left = Math.max(0, d.endsAt - now); | |
| 1328 | ring.style.setProperty("--p", `${Math.min(100, (left / Math.max(st.turnMs, 1)) * 100)}%`); | |
| 1329 | ring.classList.toggle("low", left < 15000); | |
| 1330 | $("ringN").textContent = left >= 60000 ? `${Math.ceil(left / 60000)}m` : `${Math.ceil(left / 1000)}`; | |
| 1331 | } | |
| 1308 | 1332 | setInterval(tickClock, 250); |
| 1333 | $("quiet").onchange = (e) => { try { localStorage.setItem("so101.quiet", e.target.checked ? "1" : "0"); } catch {} }; | |
| 1334 | /** "You're next": a tone and a buzz (Android) and an amber strip; no web push, so only while the page is open. */ | |
| 1335 | let wasNext = false; | |
| 1336 | const quiet = () => { try { return localStorage.getItem("so101.quiet") === "1"; } catch { return false; } }; | |
| 1337 | function alertNext(next) { | |
| 1338 | $("strip").classList.toggle("next", next); | |
| 1339 | if (next && !wasNext) { | |
| 1340 | navigator.vibrate?.([200, 100, 200]); | |
| 1341 | if (!quiet()) try { | |
| 1342 | const ctx = new (window.AudioContext || window.webkitAudioContext)(), o = ctx.createOscillator(), g = ctx.createGain(); | |
| 1343 | o.frequency.value = 880; g.gain.value = 0.08; o.connect(g); g.connect(ctx.destination); o.start(); o.stop(ctx.currentTime + 0.25); o.onended = () => ctx.close(); | |
| 1344 | } catch { /* autoplay refused: the amber strip is the alert */ } | |
| 1345 | } | |
| 1346 | wasNext = next; | |
| 1347 | } | |
| 1309 | 1348 | const button = (textContent, className, onclick) => el("button", { textContent, className, type: "button", onclick }); |
| 1310 | 1349 | /** The one thing to do about the turn, short enough for the strip. */ |
| 1311 | 1350 | function turnButtons() { |
| @@ -1313,20 +1352,29 @@ | ||
| 1313 | 1352 | if (!st || !lobby.hello?.signin) return []; // a run on one's own machine: everyone drives, nobody queues |
| 1314 | 1353 | if (!me) return [button("Join to drive", "go", () => { showTab("queue"); $("joinName").focus(); })]; |
| 1315 | 1354 | if (lobby.hello.banned) return []; // banned: watching only |
| 1316 | if (d?.id === me.id) return [button("Hand back", "", () => say({ type: "leave" }))]; | |
| 1317 | if (st.queue.some((x) => x.id === me.id)) return [button("Leave line", "", () => say({ type: "leave" }))]; | |
| 1318 | return [button(me.owner && d ? "Take over" : d || (st.paused && !me.owner) ? "Join line" : "Take control", "go", () => say({ type: "join" }))]; | |
| 1355 | if (d?.id === me.id) return [...(d.readyBy ? [button("Ready", "go ready", () => say({ type: "ready" }))] : []), ...(!st.queue.length && !d.readyBy ? [button("+30 s", "", () => say({ type: "extend" }))] : []), button("Hand back", "", () => say({ type: "leave" }))]; | |
| 1356 | if (st.queue.some((x) => x.id === me.id)) return [button("Leave", "", () => say({ type: "leave" }))]; | |
| 1357 | return [button(me.owner && d ? "Take over" : d || (st.paused && !me.owner) ? "Join queue" : "Take control", "go", () => say({ type: "join" }))]; | |
| 1319 | 1358 | } |
| 1320 | 1359 | function renderTurn() { |
| 1321 | 1360 | const st = lobby.state, me = lobby.hello?.you ?? null; |
| 1322 | 1361 | if (!st || !lobby.hello) return; |
| 1323 | 1362 | const d = st.driver; |
| 1324 | 1363 | $("driverLine").className = st.paused ? "paused" : d ? "" : "none"; |
| 1325 | $("driverLine").textContent = (st.paused ? "Paused for maintenance" + (d ? " · " : "") : "") + (d ? (me && d.id === me.id ? "You have control" : `${d.name} has control`) : st.paused ? "" : "Nobody has control"); | |
| 1364 | const mine = me ? st.queue.find((x) => x.id === me.id) : null, driving = Boolean(me && d?.id === me.id); | |
| 1365 | // The strip in words: watching, queued, next, your turn. | |
| 1366 | const wait = Math.max(1, Math.ceil((st.queue.length ? st.queue.at(-1).waitMs + st.turnMs : d ? Math.max(0, d.endsAt - st.now) : 0) / 60000)); | |
| 1367 | $("driverLine").textContent = (st.paused ? "Paused for maintenance" + (d ? " · " : "") : "") + | |
| 1368 | (driving ? (d.readyBy ? "Your turn · confirm you're here" : "Your turn") : mine ? (mine.place === 1 ? "You're next" : `#${mine.place} · ~${Math.max(1, Math.ceil(mine.waitMs / 60000))} min`) : | |
| 1369 | d ? (lobby.hello.signin && !lobby.hello.banned ? `Join queue · ${st.queue.length} waiting · ~${wait} min` : `${d.name} has control`) : st.paused ? "" : "Nobody has control"); | |
| 1370 | $("driverTag").textContent = d ? `🎮 ${d.name}${driving ? " (you)" : ""}` : ""; | |
| 1371 | $("strip").classList.toggle("ready", Boolean(driving && d.readyBy)); | |
| 1372 | alertNext(Boolean(mine && mine.place === 1 && !st.paused)); | |
| 1326 | 1373 | $("notice").hidden = !st.announcement; |
| 1327 | 1374 | $("notice").textContent = st.announcement ?? ""; |
| 1328 | 1375 | $("capLine").textContent = st.maxSpeedPercent < 100 ? `Speed limited to ${st.maxSpeedPercent}% of full` : ""; |
| 1329 | 1376 | $("turnActions").replaceChildren(...turnButtons()); |
| 1377 | $("quiet").checked = quiet(); | |
| 1330 | 1378 | $("queueActions").replaceChildren(el("span", { textContent: d ? (me && d.id === me.id ? "You" : d.name) : "Nobody", style: "flex:1;font-weight:600" }), ...turnButtons()); |
| 1331 | 1379 | const joining = lobby.hello.signin && !me; |
| 1332 | 1380 | $("joinForm").hidden = $("forgeSignin").hidden = !joining; |
new above
offline
The
case "leave": this.turns.leave(who.id)branch was replaced instead of kept, soleavenow falls through and nothing hands back the turn or leaves the line. Restore it.