tools: name players through player.setname, and the character groups and four fault classes (#23)

This commit is contained in:
2026-11-15 12:01:30 +01:00
parent f134534c6c
commit c4b97ec16c
10 changed files with 405 additions and 132 deletions
+26 -27
View File
@@ -6,7 +6,6 @@ import crypto from 'node:crypto'
import fs from 'node:fs'
import net from 'node:net'
import path from 'node:path'
import { DatabaseSync } from 'node:sqlite'
import { fileURLToPath } from 'node:url'
export const sleep = ms => new Promise(r => setTimeout(r, ms))
@@ -411,32 +410,32 @@ export async function collectAcks(client, ids, timeoutMs = 5000) {
}
// ---------------------------------------------------------------------------------------------
// Slice 5 (#20) fixtures: naming a player out of band.
// Slice 8 (#23): naming a player through the product's own intent.
//
// DESIGN 3.2 gives a player their display name at first sign-in, through `player.setname` - and
// `player.setname` is slice 8 (#23). Until then every non-GM connection is *unnamed*, and 6.1/6.5
// refuse an unnamed player everything but `conn.ping`: no scene block, no resync, no intents. The
// player-side acceptance of this slice (leak test A and B, WS probing) therefore cannot be reached
// through the product's own surface yet, so the harness writes the name into the DB before the
// connection is opened. That is a fixture, not a product path: the unnamed-player gate itself is
// pinned by tools/tokens.test.mjs (hello without a scene block, `not-allowed` for every intent).
export async function namePlayer(stateDir, playerId, name) {
const dbPath = path.join(stateDir, 'state.db')
let lastError
for (let attempt = 0; attempt < 40; attempt++) {
const db = new DatabaseSync(dbPath)
try {
db.exec('PRAGMA busy_timeout=5000')
db.prepare('UPDATE player SET name = ? WHERE id = ?').run(name, playerId)
const row = db.prepare('SELECT name FROM player WHERE id = ?').get(playerId)
if (!row || row.name !== name) throw new Error(`naming player ${playerId} failed`)
return
} catch (e) {
lastError = e
} finally {
try { db.close() } catch {}
}
await sleep(25) // SQLITE_BUSY while the server holds the write lock: back off and retry
// DESIGN 3.2 gives a player their display name at first sign-in through `player.setname`, which
// slice 8 builds. The harness therefore names a player the way a browser does: sign in with the
// player key, open a connection, send `player.setname`, wait for the ack and for the second `hello`
// (6.1: the player's connections get `hello` + a full resync), then close the naming connection.
// Nothing here writes to the DB out of band: the old `node:sqlite` fixture is gone, so a harness can
// only reach a named player through the wire.
export async function namePlayer(origin, key, name) {
const signed = await join(origin, key)
if (signed.status !== 303 || !signed.cookie) {
throw new Error(`sign-in for namePlayer failed: ${signed.status}`)
}
throw lastError
const ws = await openWs(origin, signed.cookie)
await ws.waitText('hello')
const from = ws.messages.length
const ack = await ws.request({ t: 'player.setname', name })
if (ack.ok !== true) {
ws.close()
throw new Error(`player.setname refused for "${name}": ${JSON.stringify(ack)}`)
}
// 6.1/6.3: `hello` again with `me.name`, then `roster` and the resync. Waiting for the second
// hello pins the effect, not just the ack.
const hello = await ws.waitFor((m, i) => i >= from && m.t === 'hello')
await ws.waitFor((m, i) => i >= from && m.t === 'roster')
ws.close()
await ws.closed
return { hello, ack, cookie: signed.cookie }
}