feat(persist): salva stato su .segnapunti/state.json ad ogni azione

All'avvio il server carica lo stato dal file se esiste; ad ogni azione
lo riscrive. Il riavvio del server riprende dall'ultimo punto salvato.
This commit is contained in:
2026-05-12 14:08:06 +02:00
parent 0ba49ead5d
commit 15dac9f965
4 changed files with 32 additions and 3 deletions
+3 -2
View File
@@ -5,9 +5,9 @@ import { createInitialState, applyAction } from './gameState.js'
* @param {WebSocketServer} wss - Istanza del server WebSocket.
* @returns {Object} Oggetto con metodi di gestione dello stato.
*/
export function setupWebSocketHandler(wss) {
export function setupWebSocketHandler(wss, options = {}) {
// Stato globale della partita.
let gameState = createInitialState()
let gameState = options.initialState ?? createInitialState()
// Mappa dei ruoli associati ai client connessi.
const clients = new Map() // ws -> { role: 'display' | 'controller' }
@@ -100,6 +100,7 @@ export function setupWebSocketHandler(wss) {
// Propaga il nuovo stato a tutti i client connessi.
broadcastState()
options.onStateChange?.(gameState)
}
/**