From 2f02c761a8b536c3e93037df84b858c03e456556 Mon Sep 17 00:00:00 2001 From: Shinuwa Date: Sat, 21 Nov 2026 12:27:52 +0100 Subject: [PATCH] Read the expedition settings once per TCG state The settings getter reread and remerged the SQL row on every access, seven times per getState. getState now builds the state inside a settings snapshot scoped to that synchronous build and released even on error, so an administrator's change still shows in the next state. An empty account now prepares 42 statements instead of 48. Co-Authored-By: Claude Opus 5.5 --- .beads/interactions.jsonl | 1 + services/cards/expedition-service.js | 17 +++++++++++++ services/cards/service.js | 5 ++++ .../nebula-cards-expedition-service.test.mjs | 25 +++++++++++++++++++ 4 files changed, 48 insertions(+) diff --git a/.beads/interactions.jsonl b/.beads/interactions.jsonl index 2587524..c5aff79 100644 --- a/.beads/interactions.jsonl +++ b/.beads/interactions.jsonl @@ -441,3 +441,4 @@ {"id":"int-48d5d2077d92a245d65c3b067eabf51b","kind":"field_change","created_at":"2026-11-20T18:21:36.31133937Z","actor":"Shinuwa","issue_id":"sokko-g-eecb.6","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"AdminPanel chargé à la demande : chunk CardsPage 311,6 → 191,8 ko (77,6 → 47,8 ko gzip). Séparation configuration/catalogue non faite : 30 ko gzip une fois la compression active (sokko-g-6vjw)."}} {"id":"int-94dfd02acf23d34d6e65722cbb2386e4","kind":"field_change","created_at":"2026-11-21T09:02:20.762620584Z","actor":"Shinuwa","issue_id":"sokko-g-9r25","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"dev-auth limit is now the devAuthRateLimit option (default 10); the cards E2E server raises it like apiRateLimit. Full cards suite 14/14 locally."}} {"id":"int-6e97e6d3763a00e700221d758dc65309","kind":"field_change","created_at":"2026-11-21T10:43:10.046418077Z","actor":"Shinuwa","issue_id":"sokko-g-65n7","extra":{"field":"status","new_value":"closed","old_value":"open","reason":"service_env_value keeps a value a later file does not define; rollback uses rollback_commit instead of redeclaring the readonly previous_commit. Both covered by new tests that fail on the old script."}} +{"id":"int-0ca290cd706fed413576380e6e7b9501","kind":"field_change","created_at":"2026-11-21T11:22:57.988315482Z","actor":"Shinuwa","issue_id":"sokko-g-eecb.9","extra":{"field":"status","new_value":"closed","old_value":"in_progress","reason":"getState wraps its build in expeditions.withSettingsSnapshot: expedition settings read 7 -> 1, prepares 48 -> 42 on an empty account. Snapshot is scoped to the synchronous build, cleared on error; outside it every access rereads. Test added."}} diff --git a/services/cards/expedition-service.js b/services/cards/expedition-service.js index ad89bc8..50a81b5 100644 --- a/services/cards/expedition-service.js +++ b/services/cards/expedition-service.js @@ -30,6 +30,7 @@ export class ExpeditionService { // `alerts` : { available } si le bot Discord est configuré, et `publicOrigin` pour le lien des messages. constructor(cards, destinations, alerts = {}) { this.cards = cards; + this.settingsSnapshot = null; this.alertsAvailable = Boolean(alerts.available); this.publicOrigin = alerts.publicOrigin || null; this.db = cards.db; @@ -47,10 +48,26 @@ export class ExpeditionService { // Réglages en vigueur : un champ stocké invalide reprend sa valeur par défaut, pour que le jeu reste jouable. get settings() { + return this.settingsSnapshot || this.readSettings(); + } + + readSettings() { const row = this.db.prepare("SELECT settings_json FROM expedition_settings WHERE id = 1").get(); return mergeExpeditionSettings(parse(row?.settings_json) || {}, this.destinationIds, (path) => console.error(`Invalid stored expedition setting ${path}; using its default.`)); } + // Une construction d'état consulte les réglages dans chacune de ses sous-vues : `fn` les lit une seule fois. `fn` doit être + // synchrone (aucune autre requête ne s'intercale) et ne pas modifier les réglages ; hors de `fn`, chaque accès relit la base. + withSettingsSnapshot(fn) { + if (this.settingsSnapshot) return fn(); + this.settingsSnapshot = this.readSettings(); + try { + return fn(); + } finally { + this.settingsSnapshot = null; + } + } + settingsUpdatedAt() { return this.db.prepare("SELECT updated_at FROM expedition_settings WHERE id = 1").get()?.updated_at ?? 0; } diff --git a/services/cards/service.js b/services/cards/service.js index 53fee01..15dc7eb 100644 --- a/services/cards/service.js +++ b/services/cards/service.js @@ -511,6 +511,11 @@ export class CardsService { } getState(playerId) { + // Les sous-vues lisent toutes les réglages des Expéditions : une seule lecture pour tout l'état. + return this.expeditions.withSettingsSnapshot(() => this.buildState(playerId)); + } + + buildState(playerId) { // Groupes arrivés au bout de leur inscription : départ ou dissolution avant de lire l'historique et le résumé. this.expeditions.settleForming(); const recharge = this.database.transaction(() => this.refreshRecharge(playerId)); diff --git a/tests/nebula-cards-expedition-service.test.mjs b/tests/nebula-cards-expedition-service.test.mjs index 5a89c88..4c57d83 100644 --- a/tests/nebula-cards-expedition-service.test.mjs +++ b/tests/nebula-cards-expedition-service.test.mjs @@ -51,6 +51,31 @@ function goSolo(service, playerId, body, key) { return { ...departed, expedition: service.expeditions.expeditionView(group.id, playerId) }; } +test("a state reads the expedition settings once, and the next state sees an administrator's change", () => { + const { service, player, open } = expeditionService(); + const settingsQuery = "SELECT settings_json FROM expedition_settings WHERE id = 1"; + const prepare = service.db.prepare.bind(service.db); + let reads = 0; + service.db.prepare = (sql) => { + if (sql === settingsQuery) reads += 1; + return prepare(sql); + }; + + assert.equal(service.getState(player.id).expeditions.enabled, false); + assert.equal(reads, 1); + open(); + reads = 0; + assert.equal(service.getState(player.id).expeditions.enabled, true); + assert.equal(reads, 1); + // Hors d'une construction d'état, chaque accès relit la base, même après un état interrompu par une erreur. + assert.throws(() => service.getState("missing-player"), (error) => error.code === "PLAYER_NOT_FOUND"); + assert.equal(service.expeditions.settingsSnapshot, null); + reads = 0; + void service.expeditions.settings; + void service.expeditions.settings; + assert.equal(reads, 2); +}); + test("expeditions stay closed until an administrator opens them", () => { const { service, player, open, rares } = expeditionService(); const closed = service.expeditions.getExpeditions(player.id);