Public: anyone can read this; the crew can change it.
Game door: the desk snapshot carries each player's allowed servers, from whoMayReach #18325
Merged 4h 15m ago, live in production
mason/forge:mason/18323-game-door-the-desk-snapshot-carries-each → main 0 commits fork
3 files
changed +133
−16
docs/features/games.md +2 −2
Diff not shown — a line 2,906 characters wide (limit 2,000). +2 −2
View filesrc/games/desk_snapshot.ts +51 −12
| @@ -10,6 +10,10 @@ | ||
| 10 | 10 | * |
| 11 | 11 | * The plugin only looks a word up. What a player may see is decided here, once, when the file is |
| 12 | 12 | * made; a word the file lacks goes to the forge as it always did. |
| 13 | * | |
| 14 | * The file also carries who may reach which server (#18323): `reach(uuid, server)`, one row for | |
| 15 | * each linked player and each server `whoMayReach` lets them on. The table is there, empty, when | |
| 16 | * nobody may reach anything, so the plugin can tell "nobody" from a file made before it. | |
| 13 | 17 | */ |
| 14 | 18 | |
| 15 | 19 | import { Database } from "bun:sqlite"; |
| @@ -20,6 +24,7 @@ import { composeServices, readCompose } from "./compose.ts"; | ||
| 20 | 24 | import { helpLines } from "./chat.ts"; |
| 21 | 25 | import { readLines } from "./forge_read.ts"; |
| 22 | 26 | import { peopleOf } from "./people.ts"; |
| 27 | import { whoMayReach } from "./reach.ts"; | |
| 23 | 28 | import { proxyOf } from "./sleep.ts"; |
| 24 | 29 | import { mayDo, roleOn, type GameBox } from "./store.ts"; |
| 25 | 30 | |
| @@ -52,7 +57,10 @@ interface Kept { | ||
| 52 | 57 | } |
| 53 | 58 | const kept = new WeakMap<App, Map<string, Kept>>(); |
| 54 | 59 | |
| 55 | /** The latest file for `box`; made again in the background when it is old. Undefined until the first is made, or when nobody linked is on. */ | |
| 60 | /** | |
| 61 | * The latest file for `box`; made again in the background when it is old. Undefined until the first | |
| 62 | * is made. | |
| 63 | */ | |
| 56 | 64 | export function snapshotFor(app: App, box: GameBox, origin: string, now = Date.now()): Made | undefined { |
| 57 | 65 | let boxes = kept.get(app); |
| 58 | 66 | if (boxes === undefined) kept.set(app, (boxes = new Map())); |
| @@ -90,7 +98,32 @@ async function rowsFor(app: App, box: GameBox, compose: string, origin: string, | ||
| 90 | 98 | return rows; |
| 91 | 99 | } |
| 92 | 100 | |
| 93 | /** The SQLite file for the players linked and on a server of `box` now. */ | |
| 101 | /** One row of the `reach` table: a linked player's UUID (as linked) and a server they may reach. */ | |
| 102 | export interface Reach { | |
| 103 | readonly uuid: string; | |
| 104 | readonly server: string; | |
| 105 | } | |
| 106 | ||
| 107 | /** The `reach` rows for `box`: `whoMayReach`'s names, each as the UUID its link holds. */ | |
| 108 | export function reachRows(db: App["db"], box: Pick<GameBox, "id">, compose: string): Reach[] { | |
| 109 | const uuids = new Map( | |
| 110 | db | |
| 111 | .query<{ player: string; uuid: string }, [string]>("SELECT player, uuid FROM game_link WHERE box_id = ?1 AND uuid != ''") | |
| 112 | .all(box.id) | |
| 113 | .map((l) => [l.player, l.uuid] as const), | |
| 114 | ); | |
| 115 | return Object.entries(whoMayReach(db, box, compose)).flatMap(([server, names]) => | |
| 116 | names.flatMap((name) => { | |
| 117 | const uuid = uuids.get(name); | |
| 118 | return uuid === undefined ? [] : [{ uuid, server }]; | |
| 119 | }), | |
| 120 | ); | |
| 121 | } | |
| 122 | ||
| 123 | /** | |
| 124 | * The SQLite file for `box`: who may reach which server, and the answers for the players linked and | |
| 125 | * on a server now. | |
| 126 | */ | |
| 94 | 127 | async function make(app: App, box: GameBox, origin: string, now: number): Promise<Made | undefined> { |
| 95 | 128 | const compose = (await readCompose(app, box))?.text; |
| 96 | 129 | if (compose === undefined) return undefined; |
| @@ -107,10 +140,9 @@ async function make(app: App, box: GameBox, origin: string, now: number): Promis | ||
| 107 | 140 | const link = links.get(bare(p.uuid)); |
| 108 | 141 | if (link !== undefined && !on.some((o) => o.uuid === link.uuid)) on.push({ uuid: link.uuid, userId: link.user_id, service, player: link.player }); |
| 109 | 142 | } |
| 110 | if (on.length === 0) return undefined; | |
| 111 | 143 | const players: PlayerRows[] = []; |
| 112 | 144 | for (const o of on.slice(0, MOST_PLAYERS)) players.push({ uuid: o.uuid, rows: await rowsFor(app, box, compose, origin, o.userId, o.service, o.player, now) }); |
| 113 | const made = pack(players, now); | |
| 145 | const made = pack(players, now, SNAPSHOT_MAX_B64, reachRows(app.db, box, compose)); | |
| 114 | 146 | if (made !== undefined && made.players < players.length) |
| 115 | 147 | app.log.warn("the snapshot for /forge holds only some players: the rest ask the forge", { box: box.name, kept: made.players, on: players.length }); |
| 116 | 148 | return made; |
| @@ -122,23 +154,30 @@ export interface PlayerRows { | ||
| 122 | 154 | } |
| 123 | 155 | |
| 124 | 156 | /** |
| 125 | * The SQLite file for `players`, as base64, never over `maxB64`: players are dropped from the end | |
| 126 | * (they ask the forge as before the file) until it fits. Undefined when none fits. | |
| 157 | * The SQLite file for `players` and `reach`, as base64, never over `maxB64`: players are dropped | |
| 158 | * from the end (they ask the forge as before the file) until it fits; `reach` is always whole. A | |
| 159 | * file with no players still carries `reach`. Undefined when nothing fits. | |
| 127 | 160 | */ |
| 128 | export function pack(players: readonly PlayerRows[], now: number, maxB64 = SNAPSHOT_MAX_B64): (Made & { players: number }) | undefined { | |
| 129 | for (let keep = players.slice(0, MOST_PLAYERS); keep.length > 0; keep = keep.slice(0, Math.floor(keep.length / 2))) { | |
| 130 | const made = build(keep, now); | |
| 161 | export function pack(players: readonly PlayerRows[], now: number, maxB64 = SNAPSHOT_MAX_B64, reach: readonly Reach[] = []): (Made & { players: number }) | undefined { | |
| 162 | for (let keep = players.slice(0, MOST_PLAYERS); ; keep = keep.slice(0, Math.floor(keep.length / 2))) { | |
| 163 | const made = build(keep, reach, now); | |
| 131 | 164 | if (made.b64.length <= maxB64) return { ...made, players: keep.length }; |
| 165 | if (keep.length === 0) return undefined; | |
| 132 | 166 | } |
| 133 | return undefined; | |
| 134 | 167 | } |
| 135 | 168 | |
| 136 | function build(players: readonly PlayerRows[], now: number): Made { | |
| 169 | function build(players: readonly PlayerRows[], reach: readonly Reach[], now: number): Made { | |
| 137 | 170 | const db = new Database(":memory:"); |
| 138 | 171 | try { |
| 139 | 172 | db.run("CREATE TABLE answer (uuid TEXT NOT NULL, words TEXT NOT NULL, text TEXT NOT NULL, ttl INTEGER NOT NULL, PRIMARY KEY (uuid, words)) WITHOUT ROWID"); |
| 140 | const put = db.prepare("INSERT OR REPLACE INTO answer (uuid, words, text, ttl) VALUES (?1, ?2, ?3, ?4)"); | |
| 173 | db.run("CREATE TABLE reach (uuid TEXT NOT NULL, server TEXT NOT NULL, PRIMARY KEY (uuid, server)) WITHOUT ROWID"); | |
| 141 | 174 | const hash = new Bun.CryptoHasher("sha256"); |
| 175 | const may = db.prepare("INSERT OR IGNORE INTO reach (uuid, server) VALUES (?1, ?2)"); | |
| 176 | for (const r of reach) { | |
| 177 | may.run(r.uuid, r.server); | |
| 178 | hash.update(`reach\0${r.uuid}\0${r.server}\0`); | |
| 179 | } | |
| 180 | const put = db.prepare("INSERT OR REPLACE INTO answer (uuid, words, text, ttl) VALUES (?1, ?2, ?3, ?4)"); | |
| 142 | 181 | for (const p of players) |
| 143 | 182 | for (const [words, answer] of p.rows) { |
| 144 | 183 | put.run(p.uuid, words, answer, SNAPSHOT_TTL_MS); |
test/game-desk-snapshot.test.ts +80 −2
| @@ -1,10 +1,20 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * The file the forge makes for the proxy's plugin (#18028): never over what the agent takes, one |
| 3 | * version for one content, and the plugin's own key for words. | |
| 3 | * version for one content, the plugin's own key for words, and who may reach which server (#18323). | |
| 4 | 4 | */ |
| 5 | 5 | import { Database } from "bun:sqlite"; |
| 6 | 6 | import { expect, test } from "bun:test"; |
| 7 | import { pack, snapshotKey, SNAPSHOT_MAX_B64, type PlayerRows } from "../src/games/desk_snapshot.ts"; | |
| 7 | import { mkdtempSync } from "node:fs"; | |
| 8 | import { rm } from "node:fs/promises"; | |
| 9 | import { tmpdir } from "node:os"; | |
| 10 | import { join } from "node:path"; | |
| 11 | import { createApp } from "../src/app.ts"; | |
| 12 | import { createUser } from "../src/core/users.ts"; | |
| 13 | import { pack, reachRows, snapshotKey, SNAPSHOT_MAX_B64, type PlayerRows } from "../src/games/desk_snapshot.ts"; | |
| 14 | import { createGameBox, grant } from "../src/games/store.ts"; | |
| 15 | import { quietLogs } from "./helpers/globals.ts"; | |
| 16 | ||
| 17 | quietLogs(); | |
| 8 | 18 | |
| 9 | 19 | const uuid = (n: number): string => `00000000-0000-4000-8000-${String(n).padStart(12, "0")}`; |
| 10 | 20 | const player = (n: number, rows: number, size: number): PlayerRows => ({ |
| @@ -47,3 +57,71 @@ test("the key the plugin looks words up by: lower case, one space, no ending mar | ||
| 47 | 57 | // Only the end loses its marks. |
| 48 | 58 | expect(snapshotKey("why? not")).toBe("why? not"); |
| 49 | 59 | }); |
| 60 | ||
| 61 | const NETWORK = (online: boolean): string => `name: game | |
| 62 | configs: | |
| 63 | velocity_toml: | |
| 64 | content: | | |
| 65 | online-mode = ${String(online)} | |
| 66 | [servers] | |
| 67 | hub = "hub:25565" | |
| 68 | services: | |
| 69 | velocity: | |
| 70 | image: itzg/mc-proxy:latest | |
| 71 | configs: | |
| 72 | - source: velocity_toml | |
| 73 | target: /config/velocity.toml | |
| 74 | environment: | |
| 75 | TYPE: VELOCITY | |
| 76 | hub: | |
| 77 | image: itzg/minecraft-server:latest | |
| 78 | mods: | |
| 79 | image: itzg/minecraft-server:latest | |
| 80 | `; | |
| 81 | ||
| 82 | test("the file carries who may reach which server: a viewer's row, none for the ungranted or the inactive", async () => { | |
| 83 | const dataDir = mkdtempSync(join(tmpdir(), "forge-desk-reach-")); | |
| 84 | const app = createApp({ dataDir, port: 0, host: "forge.test" }); | |
| 85 | try { | |
| 86 | const boxId = createGameBox(app.db, { name: "mc" }).id; | |
| 87 | const person = async (name: string, n: number): Promise<string> => { | |
| 88 | const u = await createUser(app.db, { username: name, email: `${name}@f.test`, password: "a-good-passphrase", origin: "seed", trusted: true }); | |
| 89 | if (!u.ok) throw new Error("user"); | |
| 90 | app.db | |
| 91 | .query("INSERT INTO game_link (box_id, player, user_id, uuid, created_at) VALUES (?1, ?2, ?3, ?4, ?5)") | |
| 92 | .run(boxId, name, u.value.id, uuid(n), new Date().toISOString()); | |
| 93 | return u.value.id; | |
| 94 | }; | |
| 95 | const ann = await person("ann", 1); | |
| 96 | await person("bo", 2); | |
| 97 | const cy = await person("cy", 3); | |
| 98 | grant(app.db, boxId, ann, "viewer", "hub"); | |
| 99 | grant(app.db, boxId, cy, "viewer", "hub"); | |
| 100 | app.db.query("UPDATE user SET suspended_at = ?2 WHERE id = ?1").run(cy, new Date().toISOString()); | |
| 101 | const rows = (compose: string): unknown[] => { | |
| 102 | const made = pack([], 1_700_000_000_000, SNAPSHOT_MAX_B64, reachRows(app.db, { id: boxId }, compose))!; | |
| 103 | const file = Database.deserialize(new Uint8Array(Buffer.from(made.b64, "base64"))); | |
| 104 | try { | |
| 105 | return file.query("SELECT uuid, server FROM reach ORDER BY uuid, server").all(); | |
| 106 | } finally { | |
| 107 | file.close(); | |
| 108 | } | |
| 109 | }; | |
| 110 | // Ann (viewer on hub) reaches hub alone; Bo (no grant) and Cy (suspended) reach nothing. | |
| 111 | expect(rows(NETWORK(true))).toEqual([{ uuid: uuid(1), server: "hub" }]); | |
| 112 | // An offline-mode network: nobody, and the table is still there. | |
| 113 | expect(rows(NETWORK(false))).toEqual([]); | |
| 114 | // The grant taken away, the file made again: Ann's row is gone, the table stays. | |
| 115 | grant(app.db, boxId, ann, "none", "hub"); | |
| 116 | expect(rows(NETWORK(true))).toEqual([]); | |
| 117 | } finally { | |
| 118 | app.close(); | |
| 119 | await rm(dataDir, { recursive: true, force: true }); | |
| 120 | } | |
| 121 | }); | |
| 122 | ||
| 123 | test("a grant changes the version: the agent is sent the new file", () => { | |
| 124 | const now = 1_700_000_000_000; | |
| 125 | const before = pack([], now, SNAPSHOT_MAX_B64, [{ uuid: uuid(1), server: "hub" }])!; | |
| 126 | expect(pack([], now, SNAPSHOT_MAX_B64, [])!.v).not.toBe(before.v); | |
| 127 | }); |
new above
offline