// ── Dangerous Treasures (protocol 14, PLAN_REDESIGNS §14.3.4) ────────────── // // nivex's Dangerous Treasures opens a guarded, burning treasure chest somewhere // on the map. Its events reach this module as three kinds: `dt.started` // (public), `dt.won` (public) and `dt.ended` (staff's). The chest emptied is // *Treasure Hunter*'s count, from the tally's `treasuresWon` (D375). A run's step // opens one (`eventDangerousTreasures.js`). // // ── Who may read what ────────────────────────────────────────────────────── // // As a world event is read (`worldEvents.js`, D353, D376): the event and its // grid are public; the winner's name shows only to the operator's presence // audience, so with presence left at staff a signed-out visitor reads "The // treasure was won at K12"; Steam ids, the invaders' among them, and the // position are staff's. How many fought for it is a count, and names nobody. // The step's key, when a step opened it, is staff's too. const { gridOf } = require('./worldEvents') const KINDS = Object.freeze(['dt.started', 'dt.won', 'dt.ended']) /** DT's three shipped difficulties, as `dtevent ` takes them. */ const LEVELS = Object.freeze([ { value: 0, label: 'Easy' }, { value: 1, label: 'Medium' }, { value: 2, label: 'Hard' }, ]) const STEAM_ID = /^\d{17}$/ function isKind(kind) { return KINDS.includes(kind) } /** * One `dt.*` frame for one viewer: `{ staff, presence, geometry }`. Passes the * envelope, adds `grid`; a win adds `invaderCount`, and `name` for the presence * audience. Staff also get `x`, `z`, the step's `key`, the winner's `steamId` and * the `invaders`. Any other frame is returned as is. */ function shapeFor(frame, { staff = false, presence = false, geometry = null } = {}) { if (!frame || !isKind(frame.kind)) return frame const out = {} for (const key of ['kind', 'type', 't', 'serverId', 'wipeId']) { if (frame[key] !== undefined) out[key] = frame[key] } const grid = gridOf(geometry, frame.x, frame.z) if (grid) out.grid = grid if (frame.kind === 'dt.won') { const invaders = Array.isArray(frame.invaders) ? frame.invaders : [] const count = Number(frame.invaderCount) out.invaderCount = Number.isInteger(count) && count >= 0 ? count : invaders.length if ((presence || staff) && typeof frame.name === 'string' && frame.name) out.name = frame.name if (staff) { if (STEAM_ID.test(String(frame.steamId || ''))) out.steamId = String(frame.steamId) out.invaders = invaders.map(String).filter((id) => STEAM_ID.test(id)) } } if (staff) { for (const key of ['x', 'z', 'key']) { if (frame[key] !== undefined) out[key] = frame[key] } } return out } /** * A server's last report about Dangerous Treasures and its console, from * `servers.db.dtReportFor`: `{ dangerousTreasures, consolePlugins }`, each null * where the plugin never said (no state yet, or a plugin before protocol 14). * `dangerousTreasures` is `{ loaded, version }`. */ function reportOf(row) { const parse = (value) => { if (value === null || value === undefined) return null if (typeof value !== 'string') return value try { return JSON.parse(value) } catch { return null } } const dt = row ? parse(row.dangerousTreasures) : null const probe = row ? parse(row.console) : null return { dangerousTreasures: dt && typeof dt === 'object' ? { loaded: dt.loaded === true, version: typeof dt.version === 'string' ? dt.version : null } : null, consolePlugins: probe && typeof probe === 'object' && typeof probe.plugins === 'boolean' ? probe.plugins : null, } } module.exports = { KINDS, LEVELS, isKind, shapeFor, reportOf }