fix(gameState): striscia simmetrica + structuredClone + cambioPalla dedup
- 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.
This commit is contained in:
+6
-15
@@ -10,7 +10,7 @@ export function createInitialState() {
|
|||||||
visuStriscia: true,
|
visuStriscia: true,
|
||||||
modalitaPartita: "3/5",
|
modalitaPartita: "3/5",
|
||||||
sp: {
|
sp: {
|
||||||
striscia: { home: [0], guest: [" "] },
|
striscia: { home: [0], guest: [0] },
|
||||||
servHome: true,
|
servHome: true,
|
||||||
punt: { home: 0, guest: 0 },
|
punt: { home: 0, guest: 0 },
|
||||||
set: { home: 0, guest: 0 },
|
set: { home: 0, guest: 0 },
|
||||||
@@ -51,19 +51,15 @@ export function checkVittoria(state) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function applyAction(state, action) {
|
export function applyAction(state, action) {
|
||||||
// Esegue un deep clone per evitare mutazioni indesiderate dello stato lato server.
|
const s = structuredClone(state)
|
||||||
// Restituisce sempre un nuovo oggetto di stato.
|
|
||||||
const s = JSON.parse(JSON.stringify(state))
|
|
||||||
|
|
||||||
switch (action.type) {
|
switch (action.type) {
|
||||||
case "incPunt": {
|
case "incPunt": {
|
||||||
const team = action.team
|
const team = action.team
|
||||||
if (checkVittoria(s)) break
|
if (checkVittoria(s)) break
|
||||||
|
|
||||||
s.sp.storicoServizio.push({
|
const cambioPalla = (team === "home") !== s.sp.servHome
|
||||||
servHome: s.sp.servHome,
|
s.sp.storicoServizio.push({ servHome: s.sp.servHome, cambioPalla })
|
||||||
cambioPalla: (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome),
|
|
||||||
})
|
|
||||||
|
|
||||||
s.sp.punt[team]++
|
s.sp.punt[team]++
|
||||||
if (team === "home") {
|
if (team === "home") {
|
||||||
@@ -74,7 +70,6 @@ export function applyAction(state, action) {
|
|||||||
s.sp.striscia.home.push(" ")
|
s.sp.striscia.home.push(" ")
|
||||||
}
|
}
|
||||||
|
|
||||||
const cambioPalla = (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome)
|
|
||||||
if (cambioPalla) {
|
if (cambioPalla) {
|
||||||
s.sp.form[team].push(s.sp.form[team].shift())
|
s.sp.form[team].push(s.sp.form[team].shift())
|
||||||
}
|
}
|
||||||
@@ -118,9 +113,7 @@ export function applyAction(state, action) {
|
|||||||
case "cambiaPalla": {
|
case "cambiaPalla": {
|
||||||
if (s.sp.punt.home === 0 && s.sp.punt.guest === 0) {
|
if (s.sp.punt.home === 0 && s.sp.punt.guest === 0) {
|
||||||
s.sp.servHome = !s.sp.servHome
|
s.sp.servHome = !s.sp.servHome
|
||||||
s.sp.striscia = s.sp.servHome
|
s.sp.striscia = { home: [0], guest: [0] }
|
||||||
? { home: [0], guest: [" "] }
|
|
||||||
: { home: [" "], guest: [0] }
|
|
||||||
}
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -135,9 +128,7 @@ export function applyAction(state, action) {
|
|||||||
home: ["1", "2", "3", "4", "5", "6"],
|
home: ["1", "2", "3", "4", "5", "6"],
|
||||||
guest: ["1", "2", "3", "4", "5", "6"],
|
guest: ["1", "2", "3", "4", "5", "6"],
|
||||||
}
|
}
|
||||||
s.sp.striscia = s.sp.servHome
|
s.sp.striscia = { home: [0], guest: [0] }
|
||||||
? { home: [0], guest: [" "] }
|
|
||||||
: { home: [" "], guest: [0] }
|
|
||||||
s.sp.storicoServizio = []
|
s.sp.storicoServizio = []
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user