diff --git a/src/components/ControllerPage.vue b/src/components/ControllerPage.vue index c7963ce..c2cfee3 100644 --- a/src/components/ControllerPage.vue +++ b/src/components/ControllerPage.vue @@ -78,15 +78,16 @@ - +
@@ -250,6 +251,11 @@ export default { if (guest >= soglia && guest - home >= 2) return 'guest' return null }, + isPartitaFinita() { + if (!this.setVintoTeam) return false + const setsToWin = this.state.modalitaPartita === '2/3' ? 2 : 3 + return this.state.sp.set[this.setVintoTeam] + 1 >= setsToWin + }, cambiValid() { let hasComplete = false let allValid = true diff --git a/src/gameState.js b/src/gameState.js index 8ea3cb2..cfe56cd 100644 --- a/src/gameState.js +++ b/src/gameState.js @@ -33,6 +33,11 @@ export function checkVittoria(state) { return false } +export function checkVittoriaPartita(state) { + const setsToWin = state.modalitaPartita === "2/3" ? 2 : 3 + return state.sp.set.home >= setsToWin || state.sp.set.guest >= setsToWin +} + export function applyAction(state, action) { const s = structuredClone(state) @@ -91,7 +96,10 @@ export function applyAction(state, action) { case "nuovoSet": { const team = action.team if (team !== 'home' && team !== 'guest') break + if (checkVittoriaPartita(s)) break + const setsToWin = s.modalitaPartita === "2/3" ? 2 : 3 s.sp.set[team]++ + if (s.sp.set[team] >= setsToWin) break s.sp.punt.home = 0 s.sp.punt.guest = 0 s.sp.servHome = team === 'home' diff --git a/tests/unit/gameState.test.js b/tests/unit/gameState.test.js index fa08306..0b671d0 100644 --- a/tests/unit/gameState.test.js +++ b/tests/unit/gameState.test.js @@ -1,5 +1,5 @@ import { describe, it, expect, beforeEach } from 'vitest' -import { createInitialState, applyAction, checkVittoria } from '../../src/gameState.js' +import { createInitialState, applyAction, checkVittoria, checkVittoriaPartita } from '../../src/gameState.js' describe('Game Logic (gameState.js)', () => { let state @@ -271,6 +271,66 @@ describe('Game Logic (gameState.js)', () => { expect(s.sp.set.home).toBe(0) expect(s.sp.set.guest).toBe(0) }) + + it('in 2/3 dovrebbe registrare il set vincente senza resettare il punteggio', () => { + state.modalitaPartita = '2/3' + state.sp.set.home = 1 + state.sp.punt.home = 25 + state.sp.punt.guest = 18 + const s = applyAction(state, { type: 'nuovoSet', team: 'home' }) + expect(s.sp.set.home).toBe(2) + expect(s.sp.punt.home).toBe(25) + expect(s.sp.punt.guest).toBe(18) + }) + + it('in 3/5 dovrebbe registrare il set vincente senza resettare il punteggio', () => { + state.modalitaPartita = '3/5' + state.sp.set.home = 2 + state.sp.punt.home = 25 + state.sp.punt.guest = 20 + const s = applyAction(state, { type: 'nuovoSet', team: 'home' }) + expect(s.sp.set.home).toBe(3) + expect(s.sp.punt.home).toBe(25) + expect(s.sp.punt.guest).toBe(20) + }) + + it('dovrebbe ignorare nuovoSet se la partita è già finita', () => { + state.modalitaPartita = '2/3' + state.sp.set.home = 2 + const s = applyAction(state, { type: 'nuovoSet', team: 'home' }) + expect(s.sp.set.home).toBe(2) + }) + }) + + // ============================================= + // VITTORIA PARTITA (checkVittoriaPartita) + // ============================================= + describe('checkVittoriaPartita', () => { + it('in 2/3 restituisce false se nessuno ha 2 set', () => { + state.modalitaPartita = '2/3' + state.sp.set.home = 1 + state.sp.set.guest = 0 + expect(checkVittoriaPartita(state)).toBe(false) + }) + + it('in 2/3 restituisce true se home ha 2 set', () => { + state.modalitaPartita = '2/3' + state.sp.set.home = 2 + expect(checkVittoriaPartita(state)).toBe(true) + }) + + it('in 3/5 restituisce false se nessuno ha 3 set', () => { + state.modalitaPartita = '3/5' + state.sp.set.home = 2 + state.sp.set.guest = 2 + expect(checkVittoriaPartita(state)).toBe(false) + }) + + it('in 3/5 restituisce true se guest ha 3 set', () => { + state.modalitaPartita = '3/5' + state.sp.set.guest = 3 + expect(checkVittoriaPartita(state)).toBe(true) + }) }) // =============================================