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 <noreply@anthropic.com>
This commit is contained in:
Shinuwa 2026-11-21 12:27:52 +01:00
parent 2ba3e10005
commit 2f02c761a8
4 changed files with 48 additions and 0 deletions

View file

@ -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;
}

View file

@ -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));