From 16a3fb912a0bf4f2dadbcac40622db46d8179fff Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Tue, 12 May 2026 12:22:26 +0200 Subject: [PATCH] fix(gameState): striscia simmetrica + structuredClone + cambioPalla dedup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Inizializza striscia con { home: [0], guest: [0] } invece di usare [" "] per il team non servente; corregge anche reset e cambiaPalla. - Sostituisce JSON.parse/stringify con structuredClone (nativo, più veloce). - Calcola cambioPalla una sola volta in incPunt invece di due volte. --- src/gameState.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/gameState.js b/src/gameState.js index 2065620..7fcdc30 100644 --- a/src/gameState.js +++ b/src/gameState.js @@ -10,7 +10,7 @@ export function createInitialState() { visuStriscia: true, modalitaPartita: "3/5", sp: { - striscia: { home: [0], guest: [" "] }, + striscia: { home: [0], guest: [0] }, servHome: true, punt: { home: 0, guest: 0 }, set: { home: 0, guest: 0 }, @@ -51,19 +51,15 @@ export function checkVittoria(state) { } export function applyAction(state, action) { - // Esegue un deep clone per evitare mutazioni indesiderate dello stato lato server. - // Restituisce sempre un nuovo oggetto di stato. - const s = JSON.parse(JSON.stringify(state)) + const s = structuredClone(state) switch (action.type) { case "incPunt": { const team = action.team if (checkVittoria(s)) break - s.sp.storicoServizio.push({ - servHome: s.sp.servHome, - cambioPalla: (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome), - }) + const cambioPalla = (team === "home") !== s.sp.servHome + s.sp.storicoServizio.push({ servHome: s.sp.servHome, cambioPalla }) s.sp.punt[team]++ if (team === "home") { @@ -74,7 +70,6 @@ export function applyAction(state, action) { s.sp.striscia.home.push(" ") } - const cambioPalla = (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome) if (cambioPalla) { s.sp.form[team].push(s.sp.form[team].shift()) } @@ -118,9 +113,7 @@ export function applyAction(state, action) { case "cambiaPalla": { if (s.sp.punt.home === 0 && s.sp.punt.guest === 0) { s.sp.servHome = !s.sp.servHome - s.sp.striscia = s.sp.servHome - ? { home: [0], guest: [" "] } - : { home: [" "], guest: [0] } + s.sp.striscia = { home: [0], guest: [0] } } break } @@ -135,9 +128,7 @@ export function applyAction(state, action) { home: ["1", "2", "3", "4", "5", "6"], guest: ["1", "2", "3", "4", "5", "6"], } - s.sp.striscia = s.sp.servHome - ? { home: [0], guest: [" "] } - : { home: [" "], guest: [0] } + s.sp.striscia = { home: [0], guest: [0] } s.sp.storicoServizio = [] break }