refactor(striscia): nuova struttura array-di-set, elimina storicoServizio
La striscia diventa un array di set: ogni elemento è { serv, r[] }
dove r è la sequenza di scorer ('home'|'guest') del set.
- Un rally = un elemento in r: minimo non-derivabile
- Tutti i set (passati e corrente) sono conservati nell'array
- Dal set corrente si derivano: punteggio, servizio, cambio palla, rotazione
- Dal set completo si derivano: vincitore (r.at(-1)), score finale (count)
- storicoServizio eliminato: l'undo legge l'entry precedente di r
DisplayPage calcola le strip visive (home/guest) tramite computed
da striscia.at(-1).r senza dati ridondanti nel modello.
This commit is contained in:
+24
-51
@@ -1,8 +1,3 @@
|
||||
/**
|
||||
* Logica di gioco condivisa per il segnapunti.
|
||||
* Utilizzata sia dal server WebSocket sia dal client per l'anteprima locale.
|
||||
*/
|
||||
|
||||
export function createInitialState() {
|
||||
return {
|
||||
order: true,
|
||||
@@ -10,7 +5,7 @@ export function createInitialState() {
|
||||
visuStriscia: true,
|
||||
modalitaPartita: "3/5",
|
||||
sp: {
|
||||
striscia: { home: [0], guest: [0] },
|
||||
striscia: [{ serv: 'home', r: [] }],
|
||||
servHome: true,
|
||||
punt: { home: 0, guest: 0 },
|
||||
set: { home: 0, guest: 0 },
|
||||
@@ -19,7 +14,6 @@ export function createInitialState() {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
},
|
||||
storicoServizio: [],
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -31,22 +25,11 @@ export function checkVittoria(state) {
|
||||
const setGuest = state.sp.set.guest
|
||||
const totSet = setHome + setGuest
|
||||
|
||||
let isSetDecisivo = false
|
||||
if (state.modalitaPartita === "2/3") {
|
||||
isSetDecisivo = totSet >= 2
|
||||
} else {
|
||||
isSetDecisivo = totSet >= 4
|
||||
}
|
||||
|
||||
const isSetDecisivo = state.modalitaPartita === "2/3" ? totSet >= 2 : totSet >= 4
|
||||
const punteggioVittoria = isSetDecisivo ? 15 : 25
|
||||
|
||||
if (puntHome >= punteggioVittoria && puntHome - puntGuest >= 2) {
|
||||
return true
|
||||
}
|
||||
if (puntGuest >= punteggioVittoria && puntGuest - puntHome >= 2) {
|
||||
return true
|
||||
}
|
||||
|
||||
if (puntHome >= punteggioVittoria && puntHome - puntGuest >= 2) return true
|
||||
if (puntGuest >= punteggioVittoria && puntGuest - puntHome >= 2) return true
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -59,16 +42,8 @@ export function applyAction(state, action) {
|
||||
if (checkVittoria(s)) break
|
||||
|
||||
const cambioPalla = (team === "home") !== s.sp.servHome
|
||||
s.sp.storicoServizio.push({ servHome: s.sp.servHome, cambioPalla })
|
||||
|
||||
s.sp.punt[team]++
|
||||
if (team === "home") {
|
||||
s.sp.striscia.home.push(s.sp.punt.home)
|
||||
s.sp.striscia.guest.push(" ")
|
||||
} else {
|
||||
s.sp.striscia.guest.push(s.sp.punt.guest)
|
||||
s.sp.striscia.home.push(" ")
|
||||
}
|
||||
s.sp.striscia.at(-1).r.push(team)
|
||||
|
||||
if (cambioPalla) {
|
||||
s.sp.form[team].push(s.sp.form[team].shift())
|
||||
@@ -79,23 +54,22 @@ export function applyAction(state, action) {
|
||||
}
|
||||
|
||||
case "decPunt": {
|
||||
if (s.sp.storicoServizio.length > 0) {
|
||||
const tmpHome = s.sp.striscia.home.pop()
|
||||
s.sp.striscia.guest.pop()
|
||||
const statoServizio = s.sp.storicoServizio.pop()
|
||||
const currentSet = s.sp.striscia.at(-1)
|
||||
if (currentSet.r.length === 0) break
|
||||
|
||||
if (tmpHome === " ") {
|
||||
s.sp.punt.guest--
|
||||
if (statoServizio.cambioPalla) {
|
||||
s.sp.form.guest.unshift(s.sp.form.guest.pop())
|
||||
}
|
||||
} else {
|
||||
s.sp.punt.home--
|
||||
if (statoServizio.cambioPalla) {
|
||||
s.sp.form.home.unshift(s.sp.form.home.pop())
|
||||
}
|
||||
}
|
||||
s.sp.servHome = statoServizio.servHome
|
||||
const lastScorer = currentSet.r[currentSet.r.length - 1]
|
||||
const prevServer = currentSet.r.length >= 2
|
||||
? currentSet.r[currentSet.r.length - 2]
|
||||
: currentSet.serv
|
||||
|
||||
const wasCambioPalla = lastScorer !== prevServer
|
||||
|
||||
currentSet.r.pop()
|
||||
s.sp.punt[lastScorer]--
|
||||
s.sp.servHome = prevServer === 'home'
|
||||
|
||||
if (wasCambioPalla) {
|
||||
s.sp.form[lastScorer].unshift(s.sp.form[lastScorer].pop())
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -116,8 +90,8 @@ export function applyAction(state, action) {
|
||||
s.sp.set[team]++
|
||||
s.sp.punt.home = 0
|
||||
s.sp.punt.guest = 0
|
||||
s.sp.striscia = { home: [0], guest: [0] }
|
||||
s.sp.storicoServizio = []
|
||||
s.sp.servHome = team === 'home'
|
||||
s.sp.striscia.push({ serv: team, r: [] })
|
||||
s.sp.form = {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
@@ -128,7 +102,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 = { home: [0], guest: [0] }
|
||||
s.sp.striscia.at(-1).serv = s.sp.servHome ? 'home' : 'guest'
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -143,8 +117,7 @@ export function applyAction(state, action) {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
}
|
||||
s.sp.striscia = { home: [0], guest: [0] }
|
||||
s.sp.storicoServizio = []
|
||||
s.sp.striscia = [{ serv: s.sp.servHome ? 'home' : 'guest', r: [] }]
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user