Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5621830803 | |||
| b3faf06477 | |||
| 1df239ed3d | |||
| d9e1ac751f |
@@ -1,7 +0,0 @@
|
||||
{
|
||||
"enabledPlugins": {
|
||||
"claude-md-management@claude-plugins-official": true,
|
||||
"code-simplifier@claude-plugins-official": true,
|
||||
"frontend-design@claude-plugins-official": true
|
||||
}
|
||||
}
|
||||
@@ -1,11 +0,0 @@
|
||||
node_modules
|
||||
dist
|
||||
dev-dist
|
||||
.segnapunti
|
||||
tests
|
||||
playwright-report
|
||||
test-results
|
||||
*.md
|
||||
.git
|
||||
.gitignore
|
||||
.vscode
|
||||
+3
-2
@@ -13,8 +13,6 @@ currentCommit.txt
|
||||
node_modules
|
||||
dist
|
||||
dist-ssr
|
||||
dev-dist
|
||||
.segnapunti
|
||||
*.local
|
||||
|
||||
# Editor directories and files
|
||||
@@ -37,3 +35,6 @@ dev-dist
|
||||
|
||||
# Vitest
|
||||
coverage/
|
||||
|
||||
# Database SQLite
|
||||
data/
|
||||
|
||||
@@ -7,35 +7,6 @@ e questo progetto aderisce al [Versionamento Semantico](https://semver.org/lang/
|
||||
|
||||
---
|
||||
|
||||
## [2.0.0] - 2026-05-12
|
||||
|
||||
### Aggiunto
|
||||
- Architettura client-server con WebSocket: server Express (`server.js`) + handler (`src/websocket-handler.js`) come unica fonte di verità; display e controller sono client separati sincronizzati in tempo reale
|
||||
- Interfaccia display (`/display`) e controller (`/controller`) su porta singola `:3000`
|
||||
- Robustezza connessione WebSocket: reconnect automatico con backoff esponenziale, indicatore stato connessione sul display
|
||||
- Supporto query parameter `?wsHost=` per scenari WSL2 / development remoto
|
||||
- Validazione cambi giocatori già in formazione lato client
|
||||
- Sintesi vocale inoltrata dal controller al display via WebSocket
|
||||
- Dialog set vinto sul controller al raggiungimento dei 25 punti: mostra il vincitore, permette di annullare l'ultimo punto (INDIETRO) o avanzare al set successivo con reset automatico formazioni
|
||||
- Struttura striscia ottimizzata: array di set `[{ serv, r[] }]` che registra la sequenza dei punti e preserva la storia di tutti i set; elimina `storicoServizio`
|
||||
- Persistenza stato su `.segnapunti/state.json`: salvato ad ogni azione, ricaricato all'avvio del server
|
||||
- Suite di test completa: unit (Vitest), integration, component (Vue Test Utils + Happy-DOM), stress (50+ client), E2E (Playwright su Chromium, Firefox, Mobile Chrome)
|
||||
- Dockerfile multi-stage (builder + runtime minimale) e docker-compose con volume per persistenza stato; immagine pubblicata su registro Gitea
|
||||
|
||||
### Modificato
|
||||
- `applyAction` usa `structuredClone` al posto di `JSON.parse/stringify`
|
||||
- Calcolo cambio palla deduplicato in `applyAction`
|
||||
- Undo punto (`decPunt`) ricostruisce il servizio precedente dalla storia `r[]`
|
||||
- `nuovoSet` come azione dedicata per la progressione regolare tra set
|
||||
|
||||
### Rimosso
|
||||
- Terminal controller CLI (`cli.js`)
|
||||
- Dipendenze inutilizzate: `wave-ui`, `vue-router`, `concurrently`
|
||||
- Script npm ridondanti: `preview`, `start`, `cli`, `cli:dev`
|
||||
- Asset template Vite: `vite.svg`, `vue.svg`, `serve.png`
|
||||
|
||||
---
|
||||
|
||||
## [1.0.0] - 2026-02-10
|
||||
|
||||
Rilascio iniziale di **Segnapunti Anto**, un'applicazione web Progressive Web App (PWA) professionale per il tracciamento in tempo reale dei punteggi durante partite di pallavolo.
|
||||
|
||||
@@ -1,82 +0,0 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Scopo del progetto
|
||||
|
||||
**Segnapunti Anto** è una PWA per il segnapunti pallavolo in tempo reale. Un server Express/WebSocket gestisce lo stato di gioco; due interfacce Vue separate — **display** (tabellone pubblico) e **controller** (pannello operatore) — si sincronizzano via WebSocket.
|
||||
|
||||
## Comandi
|
||||
|
||||
```bash
|
||||
npm run dev # Vite dev server — display: :5173/display, controller: :5173/controller
|
||||
npm run serve # Build + avvio produzione — display: :3000/display, controller: :3000/controller
|
||||
|
||||
npm run test # Vitest in modalità watch
|
||||
npm run test:all # Tutte le suite Vitest in una sola esecuzione
|
||||
npm run test:unit # Solo unit + integration
|
||||
npm run test:component # Test componenti Vue (happy-dom)
|
||||
npm run test:stress # Load test (50+ client concorrenti)
|
||||
npm run test:e2e # Playwright E2E (richiede npm run serve attivo)
|
||||
npm run test:e2e:ui # Playwright con UI interattiva
|
||||
```
|
||||
|
||||
Per eseguire un singolo file di test: `npx vitest run tests/unit/gameState.test.js`
|
||||
|
||||
## Architettura
|
||||
|
||||
```
|
||||
Controller (Vue) ──WebSocket──┐
|
||||
Display (Vue) ──WebSocket──┤── websocket-handler.js ── gameState.js
|
||||
│ │
|
||||
│ └── persist.js ── .segnapunti/state.json
|
||||
```
|
||||
|
||||
**Tutta la logica di gioco** è in `src/gameState.js` come tre funzioni pure esportate:
|
||||
- `createInitialState()` — restituisce lo stato iniziale
|
||||
- `applyAction(state, action)` — reducer immutabile (deep-clone via `structuredClone`)
|
||||
- `checkVittoria(state)` — condizioni di vittoria set (25 punti, vantaggio di 2; 15 punti nel set decisivo)
|
||||
|
||||
`src/websocket-handler.js` riceve i messaggi WebSocket, valida che il mittente sia un `controller` (non un `display`), chiama `applyAction`, fa il broadcast del nuovo stato a tutti i client, poi invoca `onStateChange` per persistere su disco.
|
||||
|
||||
`server.js` (Express) serve entrambe le interfacce sulla porta 3000: `/display` → `dist/index.html`, `/controller` → `dist/controller.html`. Singolo endpoint WebSocket su `/ws`.
|
||||
|
||||
In sviluppo, `vite-plugin-websocket.js` incorpora il server WebSocket dentro il dev server Vite con middleware di URL rewrite per `/display` e `/controller`.
|
||||
|
||||
## Vincoli di design
|
||||
|
||||
- **Tutta la logica sul server** — i client sono pura UI; il server è l'unica source of truth.
|
||||
- **WebSocket role-based** — i client si registrano come `display` o `controller`; solo i controller possono inviare azioni.
|
||||
- **Stato immutabile** — `applyAction` non muta mai lo stato, restituisce sempre un nuovo oggetto.
|
||||
- **Un solo controller** — il design prevede un controller e un display attivi; non esiste conflict resolution per controller simultanei.
|
||||
- **Stato persistente** — `src/persist.js` salva lo stato in `.segnapunti/state.json` dopo ogni azione e lo carica all'avvio del server.
|
||||
|
||||
## Layout dei test
|
||||
|
||||
| Suite | Percorso | Runner |
|
||||
|-------|----------|--------|
|
||||
| Unit | `tests/unit/` | Vitest + Node |
|
||||
| Integration | `tests/integration/` | Vitest + Node |
|
||||
| Component | `tests/component/` | Vitest + Happy-DOM |
|
||||
| Stress | `tests/stress/` | Vitest + Node |
|
||||
| E2E | `tests/e2e/` | Playwright (Chromium, Firefox, Mobile Chrome) |
|
||||
|
||||
I test E2E girano in serie (`workers: 1`) per evitare race condition sullo stato WebSocket. Eseguire `npm run serve` prima di `npm run test:e2e`.
|
||||
|
||||
## Deploy
|
||||
|
||||
Il progetto si distribuisce tramite Docker. L'immagine è pubblicata sul registry Gitea self-hosted (`santantonio.sytes.net`):
|
||||
|
||||
```bash
|
||||
docker compose up -d # Avvia con immagine dal registry privato (porta 3000)
|
||||
```
|
||||
|
||||
Il volume `./.segnapunti` persiste lo stato tra i riavvii del container.
|
||||
|
||||
## Istruzioni operative
|
||||
|
||||
- Per ogni nuova funzionalità: analizza come si inserisce nel flusso `action → applyAction → broadcast`, poi decidi se aggiungere un nuovo tipo di action o estendere uno esistente.
|
||||
- Qualsiasi modifica alle regole di gioco va fatta esclusivamente in `src/gameState.js`.
|
||||
- Qualsiasi modifica al protocollo WebSocket va fatta in `src/websocket-handler.js`.
|
||||
- Aggiungere test unit in `tests/unit/gameState.test.js` per ogni nuovo action type o regola di gioco.
|
||||
- Il codice deve essere scritto in italiano per commenti e nomi di variabili di dominio (es. `servHome`, `striscia`, `nomi`), ma in inglese per nomi tecnici standard (`state`, `action`, `handler`).
|
||||
+12
-16
@@ -1,22 +1,18 @@
|
||||
# Stage 1: build frontend
|
||||
FROM node:20-alpine AS builder
|
||||
WORKDIR /app
|
||||
COPY package*.json ./
|
||||
RUN npm ci
|
||||
|
||||
WORKDIR /usr/src/app
|
||||
# Copia tutto
|
||||
COPY . .
|
||||
RUN npm run build
|
||||
|
||||
# Stage 2: runtime
|
||||
FROM node:20-alpine
|
||||
WORKDIR /app
|
||||
ENV NODE_ENV=production PORT=3000
|
||||
# Aggiunge GIT ma serve solo se si vuole evidenziare un hash del commit
|
||||
RUN apk add git
|
||||
|
||||
COPY package*.json ./
|
||||
RUN npm ci --omit=dev
|
||||
# aggiunge l'ultima versione di node
|
||||
RUN npm install -g npm@latest
|
||||
# Installa tutte le dipendenze del progetto
|
||||
RUN npm install
|
||||
|
||||
COPY server.js ./
|
||||
COPY src/gameState.js src/websocket-handler.js src/server-utils.js src/persist.js ./src/
|
||||
COPY --from=builder /app/dist ./dist
|
||||
# Qui fa partire il comando...
|
||||
# Per adesso è dev perchè non ho capito bene il tutto... (Attilio)
|
||||
CMD ["npm", "run", "serve"]
|
||||
|
||||
EXPOSE 3000
|
||||
CMD ["node", "server.js"]
|
||||
|
||||
@@ -1,195 +1,208 @@
|
||||
# Segnapunti
|
||||
# Segnapunti Anto
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
Segnapunti digitale in tempo reale per partite di pallavolo. Un server centrale gestisce lo stato della partita; un **display** mostra il tabellone pubblico e un **controller** (smartphone o tablet) permette all'operatore di gestire punti, formazioni e cambi.
|
||||
Applicazione web **Progressive Web App (PWA)** per tracciare i punteggi di partite di pallavolo in tempo reale.
|
||||
|
||||
---
|
||||
|
||||
## Indice
|
||||
## Panoramica
|
||||
|
||||
- [Architettura](#architettura)
|
||||
- [Guida utente](#guida-utente)
|
||||
- [Funzionalità](#funzionalità)
|
||||
- [Deploy con Docker](#deploy-con-docker)
|
||||
- [Sviluppo](#sviluppo)
|
||||
- [Test](#test)
|
||||
**Segnapunti Anto** e un'applicazione digitale per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone.
|
||||
|
||||
L'app e composta da due interfacce:
|
||||
- **Display** (tabellone pubblico)
|
||||
- **Controller** (pannello operatore)
|
||||
|
||||
Le due interfacce condividono lo stato in tempo reale tramite WebSocket.
|
||||
|
||||
### Funzionalita Principali
|
||||
|
||||
- **Gestione partita in tempo reale**
|
||||
- Tracciamento punti home/guest
|
||||
- Gestione set
|
||||
- Indicatore servizio
|
||||
- Storico punti (striscia)
|
||||
- Blocchi logici quando il set e gia vinto
|
||||
|
||||
- **Regole pallavolo integrate**
|
||||
- Set normali: vittoria a 25 con almeno 2 punti di scarto
|
||||
- Set decisivo: vittoria a 15 con almeno 2 punti di scarto
|
||||
- Modalita partita `2/3` o `3/5`
|
||||
|
||||
- **Formazioni e cambi**
|
||||
- Gestione formazione a 6 giocatori
|
||||
- Rotazione automatica al cambio palla
|
||||
- Dialog cambi con validazioni (`IN -> OUT`)
|
||||
|
||||
- **Controlli e personalizzazione**
|
||||
- Configurazione nomi squadre
|
||||
- Toggle ordine squadre (inverti)
|
||||
- Toggle visualizzazione punteggio/formazioni
|
||||
- Toggle striscia storico
|
||||
- Sintesi vocale punteggio (Web Speech API)
|
||||
|
||||
---
|
||||
|
||||
## Architettura
|
||||
## Requisiti
|
||||
|
||||
```
|
||||
Controller (smartphone) ──WebSocket──┐
|
||||
├── Server Node.js ── gameState.js
|
||||
Display (schermo) ──WebSocket──┘ │
|
||||
└── .segnapunti/state.json
|
||||
```
|
||||
### Requisiti di Sistema
|
||||
|
||||
Il server è l'unica fonte di verità. Ogni azione del controller viene elaborata e trasmessa in broadcast a tutti i client connessi. Lo stato viene salvato su disco ad ogni azione e ricaricato all'avvio, sopravvivendo ai riavvii del server.
|
||||
#### Per Sviluppo
|
||||
- **Sistema Operativo**: Linux, macOS, Windows
|
||||
- **Node.js**: `>= 18.19.0` (consigliato `20 LTS`)
|
||||
- **npm**: `>= 9`
|
||||
- **RAM**: minimo 2GB (consigliato 4GB)
|
||||
|
||||
| Percorso | Ruolo |
|
||||
|---|---|
|
||||
| `http://<host>:3000/display` | Tabellone pubblico — sola lettura |
|
||||
| `http://<host>:3000/controller` | Pannello operatore — gestione partita |
|
||||
| `ws://<host>:3000/ws` | WebSocket endpoint |
|
||||
#### Per Esecuzione Test E2E
|
||||
- Browser Playwright installati (`chromium`, `firefox`)
|
||||
- Su Linux, eventuali dipendenze sistema per browser headless
|
||||
|
||||
---
|
||||
|
||||
## Guida utente
|
||||
|
||||
### Scenario tipico: schermo fisso + smartphone operatore
|
||||
|
||||
#### 1. Avvia il server
|
||||
Comandi utili:
|
||||
|
||||
```bash
|
||||
docker compose up -d
|
||||
node -v
|
||||
npm -v
|
||||
npx playwright install chromium firefox
|
||||
# Linux, se necessario:
|
||||
# npx playwright install --with-deps chromium firefox
|
||||
```
|
||||
|
||||
All'avvio il terminale mostra gli URL locali e di rete.
|
||||
### Requisiti Browser (Utente Finale)
|
||||
|
||||
#### 2. Apri il display
|
||||
| Requisito | Dettaglio | Necessita |
|
||||
|-----------|-----------|-----------|
|
||||
| JavaScript ES6+ | Moduli, async/await | Obbligatorio |
|
||||
| WebSocket | Sincronizzazione stato live | Obbligatorio |
|
||||
| Service Worker API | Supporto PWA offline | Consigliato |
|
||||
| Web Speech API | Annunci vocali | Opzionale |
|
||||
|
||||
Collega il PC/server allo schermo via HDMI e apri il browser a schermo intero:
|
||||
### Browser Testati e Supportati
|
||||
|
||||
```
|
||||
http://localhost:3000/display
|
||||
```
|
||||
|
||||
Se il display è su un dispositivo separato nella stessa rete:
|
||||
|
||||
```
|
||||
http://<IP-del-server>:3000/display
|
||||
```
|
||||
|
||||
> **Trovare l'IP:** il server lo stampa all'avvio. In alternativa usa `ip a` su Linux.
|
||||
|
||||
#### 3. Apri il controller sullo smartphone
|
||||
|
||||
Connetti il telefono alla stessa rete Wi-Fi e apri:
|
||||
|
||||
```
|
||||
http://<IP-del-server>:3000/controller
|
||||
```
|
||||
|
||||
> **Installazione come app:** nel browser tocca *"Aggiungi a schermata Home"* per avere il controller come icona dedicata.
|
||||
| Browser | Supporto | Note |
|
||||
|---------|----------|------|
|
||||
| Chrome/Chromium | ✅ | Completo |
|
||||
| Firefox | ✅ | Completo |
|
||||
| Mobile Chrome (Playwright Pixel 5) | ✅ | Copertura E2E mobile |
|
||||
|
||||
---
|
||||
|
||||
## Funzionalità
|
||||
## Installazione e Setup
|
||||
|
||||
### Display
|
||||
### Prerequisiti
|
||||
|
||||
- Nomi squadre con indicatore di servizio
|
||||
- Punteggio del set corrente (grande, leggibile da lontano)
|
||||
- Contatore set vinti
|
||||
- Striscia storica punti del set in corso, scorrevole verso destra
|
||||
- Modalità formazioni: posizioni dei 6 giocatori in campo
|
||||
- Indicatore connessione WebSocket (scompare quando connesso, rosso lampeggiante se disconnesso)
|
||||
- Node.js `>= 18.19.0`
|
||||
- npm `>= 9`
|
||||
|
||||
### Controller
|
||||
|
||||
- **Punti** — `+1` per casa e ospite, con annullamento dell'ultimo punto
|
||||
- **Dialog set vinto** — appare automaticamente al raggiungimento dei 25 punti (o 15 nel tie-break); permette di confermare il set o annullare l'ultimo punto
|
||||
- **Formazioni** — configura i numeri di maglia; la rotazione avviene automaticamente al cambio palla
|
||||
- **Cambi** — dialog `IN → OUT` con validazione
|
||||
- **Servizio** — cambio manuale (disponibile solo a 0-0)
|
||||
- **Visualizzazione** — alterna tra punteggio grande e formazioni in campo
|
||||
- **Striscia** — mostra/nasconde lo storico punti sul display
|
||||
- **Reset** — azzera la partita (richiede conferma)
|
||||
|
||||
### Regole pallavolo integrate
|
||||
|
||||
| Set | Condizione di vittoria |
|
||||
|---|---|
|
||||
| Set 1–4 (modalità 3/5) o 1–2 (modalità 2/3) | Primo a **25** con almeno 2 punti di scarto |
|
||||
| Set decisivo (tie-break) | Primo a **15** con almeno 2 punti di scarto |
|
||||
|
||||
---
|
||||
|
||||
|
||||
## Deploy con Docker
|
||||
|
||||
### Prima installazione
|
||||
|
||||
```bash
|
||||
docker compose up -d
|
||||
```
|
||||
|
||||
Lo stato viene salvato nel volume Docker `segnapunti-state` e sopravvive ai riavvii del container.
|
||||
|
||||
### Aggiornamento a nuova versione
|
||||
|
||||
```bash
|
||||
docker compose pull && docker compose up -d
|
||||
```
|
||||
|
||||
### Build e pubblicazione immagine
|
||||
|
||||
```bash
|
||||
docker build \
|
||||
-t santantonio.sytes.net/attilio/segnapunti:2.0.0 \
|
||||
-t santantonio.sytes.net/attilio/segnapunti:latest .
|
||||
|
||||
docker push santantonio.sytes.net/attilio/segnapunti:2.0.0
|
||||
docker push santantonio.sytes.net/attilio/segnapunti:latest
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Sviluppo
|
||||
|
||||
### Requisiti
|
||||
|
||||
| Strumento | Versione minima |
|
||||
|---|---|
|
||||
| Node.js | >= 18 |
|
||||
| npm | >= 9 |
|
||||
|
||||
### Avvio
|
||||
### Installazione
|
||||
|
||||
```bash
|
||||
git clone https://santantonio.sytes.net/attilio/segnapunti.git
|
||||
cd segnapunti
|
||||
npm install
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Comandi per Sviluppo
|
||||
|
||||
### Dev Server
|
||||
|
||||
Avvia il server di sviluppo Vite:
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
| URL | Interfaccia |
|
||||
|---|---|
|
||||
| `http://localhost:5173/display` | Display |
|
||||
| `http://localhost:5173/controller` | Controller |
|
||||
Accesso tipico in sviluppo:
|
||||
- `http://localhost:5173/` -> Display
|
||||
- `http://localhost:5173/controller.html` -> Controller
|
||||
|
||||
Lo stato viene salvato in `.segnapunti/state.json` anche in modalità dev.
|
||||
|
||||
### Comandi disponibili
|
||||
|
||||
| Comando | Descrizione |
|
||||
|---|---|
|
||||
| `npm run dev` | Dev server con hot reload |
|
||||
| `npm run build` | Build di produzione in `dist/` |
|
||||
| `npm run serve` | Build + avvio server produzione |
|
||||
### Modalita Sviluppo
|
||||
- Hot reload attivo
|
||||
- Build veloce lato Vite
|
||||
- Buona per sviluppo UI/UX
|
||||
|
||||
---
|
||||
|
||||
## Test
|
||||
## Comandi per Build
|
||||
|
||||
| Comando | Descrizione |
|
||||
|---|---|
|
||||
| `npm run test:unit` | Unit + integration (Vitest) |
|
||||
| `npm run test:component` | Componenti Vue (Happy-DOM) |
|
||||
| `npm run test:stress` | Load test WebSocket (50+ client) |
|
||||
| `npm run test:all` | Tutti i test tranne E2E |
|
||||
| `npm run test:e2e` | Playwright — Chromium, Firefox, Mobile Chrome |
|
||||
### Build Produzione
|
||||
|
||||
> I test E2E richiedono il server in esecuzione (`npm run serve`) e i browser Playwright installati:
|
||||
> ```bash
|
||||
> npx playwright install chromium firefox
|
||||
> ```
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
Output:
|
||||
- cartella `dist/`
|
||||
- asset ottimizzati
|
||||
- file PWA (manifest + service worker)
|
||||
|
||||
### Avvio Server Applicativo Locale (Display + Controller)
|
||||
|
||||
```bash
|
||||
npm run serve
|
||||
```
|
||||
|
||||
Espone:
|
||||
- `http://localhost:3000` -> Display
|
||||
- `http://localhost:3001` -> Controller
|
||||
|
||||
### Altri comandi utili
|
||||
|
||||
```bash
|
||||
npm run preview
|
||||
npm run start
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Changelog
|
||||
## Configurazione PWA
|
||||
|
||||
Vedere [CHANGELOG.md](CHANGELOG.md) per la storia delle versioni.
|
||||
L'app usa `vite-plugin-pwa` (vedi `vite.config.js`) con:
|
||||
- `registerType: 'autoUpdate'`
|
||||
- manifest installabile
|
||||
- orientamento landscape
|
||||
- modalita fullscreen
|
||||
|
||||
Caratteristiche principali:
|
||||
- installabile su dispositivi supportati
|
||||
- aggiornamento automatico del service worker
|
||||
- supporto utilizzo offline (in base alle risorse cache)
|
||||
|
||||
---
|
||||
|
||||
## Logica Regolamentare Pallavolo
|
||||
|
||||
### Vittoria Set
|
||||
|
||||
- Set normali: vittoria a 25 con almeno 2 punti di scarto
|
||||
- Set decisivo: vittoria a 15 con almeno 2 punti di scarto
|
||||
- Modalita partita supportate: `2/3` e `3/5`
|
||||
|
||||
### Rotazione Formazione
|
||||
|
||||
La rotazione avviene durante i cambi palla secondo la logica implementata in `src/gameState.js`.
|
||||
|
||||
### Formazione in Campo
|
||||
|
||||
Il sistema gestisce 6 posizioni per squadra e permette cambi validati da Controller.
|
||||
|
||||
---
|
||||
|
||||
## Test (stato attuale)
|
||||
|
||||
Suite presenti:
|
||||
- Unit
|
||||
- Integration
|
||||
- Component
|
||||
- Stress
|
||||
- E2E (Playwright)
|
||||
|
||||
Comandi principali:
|
||||
|
||||
```bash
|
||||
npm run test:all
|
||||
npm run test:e2e
|
||||
```
|
||||
|
||||
Guida completa test:
|
||||
- `tests/README.md`
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/png" href="/segnap-192x192.png" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Segnapunti - Controller</title>
|
||||
</head>
|
||||
|
||||
+5
-6
@@ -1,9 +1,8 @@
|
||||
services:
|
||||
segnapunti:
|
||||
image: santantonio.sytes.net/attilio/segnapunti:2.0.0
|
||||
container_name: segnapunti
|
||||
build: .
|
||||
ports:
|
||||
- "3000:3000"
|
||||
volumes:
|
||||
- ./.segnapunti:/app/.segnapunti
|
||||
restart: unless-stopped
|
||||
- 3000:3000
|
||||
- 3001:3001
|
||||
container_name: segnapunti-container
|
||||
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<link rel="icon" type="image/png" href="/segnap-192x192.png" />
|
||||
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Segnapunti - Anto</title>
|
||||
</head>
|
||||
|
||||
Generated
+2237
-1015
File diff suppressed because it is too large
Load Diff
+11
-8
@@ -1,11 +1,13 @@
|
||||
{
|
||||
"name": "segnapuntianto",
|
||||
"private": true,
|
||||
"version": "2.0.0",
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --host",
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
"preview": "node server.js",
|
||||
"start": "node server.js",
|
||||
"serve": "vite build && node server.js",
|
||||
"test": "vitest",
|
||||
"test:unit": "vitest run tests/unit tests/integration",
|
||||
@@ -18,24 +20,25 @@
|
||||
"test:e2e:codegen": "playwright codegen --config=playwright.config.cjs"
|
||||
},
|
||||
"dependencies": {
|
||||
"better-sqlite3": "^12.6.2",
|
||||
"express": "^5.2.1",
|
||||
"vue": "^3.2.47",
|
||||
"vue-router": "^4.6.4",
|
||||
"wave-ui": "^3.3.0",
|
||||
"ws": "^8.19.0"
|
||||
},
|
||||
"overrides": {
|
||||
"serialize-javascript": "^7.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@axe-core/playwright": "^4.11.1",
|
||||
"@playwright/test": "^1.58.2",
|
||||
"@types/node": "^25.2.3",
|
||||
"@vitejs/plugin-vue": "^6.0.5",
|
||||
"@vitejs/plugin-vue": "^4.1.0",
|
||||
"@vitest/ui": "^4.0.18",
|
||||
"@vue/test-utils": "^2.4.6",
|
||||
"concurrently": "^9.2.1",
|
||||
"happy-dom": "^20.6.1",
|
||||
"jsdom": "^28.0.0",
|
||||
"vite": "^7.3.1",
|
||||
"vite-plugin-pwa": "^1.2.0",
|
||||
"vite": "^4.3.9",
|
||||
"vite-plugin-pwa": "^0.16.0",
|
||||
"vitest": "^4.0.18"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
import { defineConfig, devices } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* Read environment variables from file.
|
||||
* https://github.com/motdotla/dotenv
|
||||
*/
|
||||
// import dotenv from 'dotenv';
|
||||
// import path from 'path';
|
||||
// dotenv.config({ path: path.resolve(__dirname, '.env') });
|
||||
|
||||
/**
|
||||
* See https://playwright.dev/docs/test-configuration.
|
||||
*/
|
||||
export default defineConfig({
|
||||
testDir: './tests/e2e',
|
||||
/* Run tests in files in parallel */
|
||||
fullyParallel: true,
|
||||
/* Fail the build on CI if you accidentally left test.only in the source code. */
|
||||
forbidOnly: !!process.env.CI,
|
||||
/* Retry on CI only */
|
||||
retries: process.env.CI ? 2 : 0,
|
||||
/* Opt out of parallel tests on CI. */
|
||||
workers: process.env.CI ? 1 : undefined,
|
||||
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
|
||||
reporter: 'html',
|
||||
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
|
||||
use: {
|
||||
/* Base URL to use in actions like `await page.goto('')`. */
|
||||
// baseURL: 'http://localhost:3000',
|
||||
|
||||
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
|
||||
trace: 'on-first-retry',
|
||||
},
|
||||
|
||||
/* Configure projects for major browsers */
|
||||
projects: [
|
||||
{
|
||||
name: 'chromium',
|
||||
use: { ...devices['Desktop Chrome'] },
|
||||
},
|
||||
|
||||
{
|
||||
name: 'firefox',
|
||||
use: { ...devices['Desktop Firefox'] },
|
||||
},
|
||||
|
||||
// {
|
||||
// name: 'webkit',
|
||||
// use: { ...devices['Desktop Safari'] },
|
||||
// },
|
||||
|
||||
/* Test against mobile viewports. */
|
||||
{
|
||||
name: 'Mobile Chrome',
|
||||
use: { ...devices['Pixel 5'] },
|
||||
},
|
||||
|
||||
/* Test against branded browsers. */
|
||||
// {
|
||||
// name: 'Microsoft Edge',
|
||||
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
|
||||
// },
|
||||
// {
|
||||
// name: 'Google Chrome',
|
||||
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
|
||||
// },
|
||||
],
|
||||
|
||||
/* Run your local dev server before starting the tests */
|
||||
webServer: {
|
||||
command: 'npm run serve',
|
||||
url: 'http://localhost:3000',
|
||||
reuseExistingServer: !process.env.CI,
|
||||
timeout: 120 * 1000,
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 1.5 KiB |
@@ -1,43 +1,55 @@
|
||||
import { createServer } from 'http'
|
||||
import express from 'express'
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { fileURLToPath, pathToFileURL } from 'url'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { dirname, join } from 'path'
|
||||
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
||||
import { printServerInfo } from './src/server-utils.js'
|
||||
import { loadState, saveState } from './src/persist.js'
|
||||
import { getPartite, getPartita } from './src/db.js'
|
||||
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = dirname(__filename)
|
||||
|
||||
const DIST_DIR = join(__dirname, 'dist')
|
||||
// --- Configurazione del server ---
|
||||
|
||||
// Crea l'app Express (asset statici + route display/controller) senza avviare il
|
||||
// listen né il WebSocket: così il routing è testabile in isolamento.
|
||||
export function createApp(distDir = DIST_DIR) {
|
||||
const app = express()
|
||||
const DISPLAY_PORT = process.env.PORT || 3000
|
||||
const CONTROLLER_PORT = process.env.CONTROLLER_PORT || 3001
|
||||
|
||||
app.use(express.static(distDir, { index: false }))
|
||||
// ========================================
|
||||
// Server Display (porta principale)
|
||||
// ========================================
|
||||
|
||||
app.get(['/', '/display', '/display/*splat'], (_req, res) => {
|
||||
res.sendFile(join(distDir, 'index.html'))
|
||||
const displayApp = express()
|
||||
|
||||
// Espone i file generati dalla build di Vite.
|
||||
displayApp.use(express.static(join(__dirname, 'dist')))
|
||||
|
||||
// API REST per le partite salvate.
|
||||
displayApp.get('/api/partite', (_req, res) => {
|
||||
try { res.json(getPartite()) }
|
||||
catch (err) { res.status(500).json({ error: err.message }) }
|
||||
})
|
||||
|
||||
app.get(['/controller', '/controller/*splat'], (_req, res) => {
|
||||
res.sendFile(join(distDir, 'controller.html'))
|
||||
displayApp.get('/api/partite/:id', (req, res) => {
|
||||
try {
|
||||
const p = getPartita(Number(req.params.id))
|
||||
if (!p) return res.status(404).json({ error: 'Not found' })
|
||||
res.json(p)
|
||||
} catch (err) { res.status(500).json({ error: err.message }) }
|
||||
})
|
||||
|
||||
return app
|
||||
}
|
||||
// Fallback per SPA: restituisce `index.html` per tutte le route.
|
||||
displayApp.get(/.*/, (_req, res) => {
|
||||
res.sendFile(join(__dirname, 'dist', 'index.html'))
|
||||
})
|
||||
|
||||
// Avvia HTTP + WebSocket. Lo stato viene caricato da disco e ripersistito ad ogni azione.
|
||||
export function startServer(port = process.env.PORT || 3000) {
|
||||
const app = createApp()
|
||||
const server = createServer(app)
|
||||
const displayServer = createServer(displayApp)
|
||||
|
||||
// Inizializza il server WebSocket condiviso.
|
||||
const wss = new WebSocketServer({ noServer: true })
|
||||
setupWebSocketHandler(wss, { initialState: loadState(), onStateChange: saveState })
|
||||
setupWebSocketHandler(wss)
|
||||
|
||||
server.on('upgrade', (request, socket, head) => {
|
||||
displayServer.on('upgrade', (request, socket, head) => {
|
||||
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
||||
if (pathname === '/ws') {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
@@ -48,14 +60,39 @@ export function startServer(port = process.env.PORT || 3000) {
|
||||
}
|
||||
})
|
||||
|
||||
server.listen(port, '0.0.0.0', () => {
|
||||
printServerInfo(port)
|
||||
displayServer.listen(DISPLAY_PORT, '0.0.0.0', () => {
|
||||
console.log(`[Display] Server running on port ${DISPLAY_PORT}`)
|
||||
})
|
||||
|
||||
return server
|
||||
}
|
||||
// ========================================
|
||||
// Server Controller (porta separata)
|
||||
// ========================================
|
||||
|
||||
// Avvia solo se eseguito direttamente (`node server.js`), non quando importato nei test.
|
||||
if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) {
|
||||
startServer()
|
||||
const controllerApp = express()
|
||||
|
||||
// Espone gli stessi file statici della build.
|
||||
// IMPORTANTE: { index: false } impedisce di servire index.html (il display) sulla root.
|
||||
controllerApp.use(express.static(join(__dirname, 'dist'), { index: false }))
|
||||
|
||||
// Fallback: restituisce `controller.html` per tutte le route.
|
||||
controllerApp.get(/.*/, (_req, res) => {
|
||||
res.sendFile(join(__dirname, 'dist', 'controller.html'))
|
||||
})
|
||||
|
||||
const controllerServer = createServer(controllerApp)
|
||||
|
||||
// Gestisce l'upgrade WebSocket anche sulla porta del controller.
|
||||
controllerServer.on('upgrade', (request, socket, head) => {
|
||||
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
||||
if (pathname === '/ws') {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request)
|
||||
})
|
||||
} else {
|
||||
socket.destroy()
|
||||
}
|
||||
})
|
||||
|
||||
controllerServer.listen(CONTROLLER_PORT, '0.0.0.0', () => {
|
||||
printServerInfo(DISPLAY_PORT, CONTROLLER_PORT)
|
||||
})
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>
|
||||
|
After Width: | Height: | Size: 496 B |
+450
-479
File diff suppressed because it is too large
Load Diff
+283
-48
@@ -7,22 +7,22 @@
|
||||
<span :style="{ 'float': 'left' }">
|
||||
{{ state.sp.nomi.home }}
|
||||
<span class="serv-slot">
|
||||
<img v-show="servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
<img v-show="state.sp.servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ punt.home }}</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ state.sp.punt.home }}</span>
|
||||
</span>
|
||||
<span class="mr3" :style="{ 'float': 'right' }">set {{ set.home }}</span>
|
||||
<span class="mr3" :style="{ 'float': 'right' }">set {{ state.sp.set.home }}</span>
|
||||
</div>
|
||||
|
||||
<div class="hea guest">
|
||||
<span :style="{ 'float': 'right' }">
|
||||
<span v-if="state.visuForm" class="score-inline">{{ punt.guest }}</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ state.sp.punt.guest }}</span>
|
||||
<span class="serv-slot">
|
||||
<img v-show="!servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
<img v-show="!state.sp.servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
</span>
|
||||
{{ state.sp.nomi.guest }}
|
||||
</span>
|
||||
<span class="ml3" :style="{ 'float': 'left' }">set {{ set.guest }}</span>
|
||||
<span class="ml3" :style="{ 'float': 'left' }">set {{ state.sp.set.guest }}</span>
|
||||
</div>
|
||||
|
||||
<span v-if="state.visuForm">
|
||||
@@ -39,8 +39,8 @@
|
||||
</span>
|
||||
<span v-else>
|
||||
<div class="punteggio-container">
|
||||
<div class="col punt home">{{ punt.home }}</div>
|
||||
<div class="col punt guest">{{ punt.guest }}</div>
|
||||
<div class="col punt home">{{ state.sp.punt.home }}</div>
|
||||
<div class="col punt guest">{{ state.sp.punt.guest }}</div>
|
||||
</div>
|
||||
</span>
|
||||
</span>
|
||||
@@ -51,22 +51,22 @@
|
||||
<span :style="{ 'float': 'left' }">
|
||||
{{ state.sp.nomi.guest }}
|
||||
<span class="serv-slot">
|
||||
<img v-show="!servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
<img v-show="!state.sp.servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ punt.guest }}</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ state.sp.punt.guest }}</span>
|
||||
</span>
|
||||
<span class="mr3" :style="{ 'float': 'right' }">set {{ set.guest }}</span>
|
||||
<span class="mr3" :style="{ 'float': 'right' }">set {{ state.sp.set.guest }}</span>
|
||||
</div>
|
||||
|
||||
<div class="hea home">
|
||||
<span :style="{ 'float': 'right' }">
|
||||
<span v-if="state.visuForm" class="score-inline">{{ punt.home }}</span>
|
||||
<span v-if="state.visuForm" class="score-inline">{{ state.sp.punt.home }}</span>
|
||||
<span class="serv-slot">
|
||||
<img v-show="servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
<img v-show="state.sp.servHome" src="/serv.png" width="25" alt="Servizio" />
|
||||
</span>
|
||||
{{ state.sp.nomi.home }}
|
||||
</span>
|
||||
<span class="ml3" :style="{ 'float': 'left' }">set {{ set.home }}</span>
|
||||
<span class="ml3" :style="{ 'float': 'left' }">set {{ state.sp.set.home }}</span>
|
||||
</div>
|
||||
|
||||
<span v-if="state.visuForm">
|
||||
@@ -83,8 +83,8 @@
|
||||
</span>
|
||||
<span v-else>
|
||||
<div class="punteggio-container">
|
||||
<div class="col punt guest">{{ punt.guest }}</div>
|
||||
<div class="col punt home">{{ punt.home }}</div>
|
||||
<div class="col punt guest">{{ state.sp.punt.guest }}</div>
|
||||
<div class="col punt home">{{ state.sp.punt.home }}</div>
|
||||
</div>
|
||||
</span>
|
||||
</span>
|
||||
@@ -92,16 +92,16 @@
|
||||
<div class="striscia" v-if="state.visuStriscia">
|
||||
<span class="striscia-nome text-bold">{{ state.sp.nomi.home }}</span>
|
||||
<div class="striscia-items" ref="homeItems">
|
||||
<div v-for="(h, i) in stricciaStrip.home" :key="'sh'+i"
|
||||
class="item" :class="{ 'item-vuoto': h === ' ' }">
|
||||
{{ h }}
|
||||
<div v-for="(h, i) in state.sp.striscia.home" :key="'sh'+i"
|
||||
class="item" :class="{ 'item-vuoto': String(h).trim() === '' }">
|
||||
{{ String(h) }}
|
||||
</div>
|
||||
</div>
|
||||
<span class="striscia-nome text-bold guest-striscia">{{ state.sp.nomi.guest }}</span>
|
||||
<div class="striscia-items guest-striscia" ref="guestItems">
|
||||
<div v-for="(h, i) in stricciaStrip.guest" :key="'sg'+i"
|
||||
class="item" :class="{ 'item-vuoto': h === ' ' }">
|
||||
{{ h }}
|
||||
<div v-for="(h, i) in state.sp.striscia.guest" :key="'sg'+i"
|
||||
class="item" :class="{ 'item-vuoto': String(h).trim() === '' }">
|
||||
{{ String(h) }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -112,64 +112,265 @@
|
||||
{{ wsConnected ? '' : 'Disconnesso' }}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Overlay fine partita -->
|
||||
<div class="partita-finita-overlay" v-if="state.sp.partitaFinita">
|
||||
<div class="partita-finita-box">
|
||||
<div class="partita-finita-label">PARTITA FINITA</div>
|
||||
<div class="partita-finita-vincitore">{{ state.sp.nomi[state.sp.partitaFinita.vincitore] }}</div>
|
||||
<div class="partita-finita-set">{{ state.sp.set.home }} – {{ state.sp.set.guest }}</div>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { createWsMixin } from '../wsMixin.js'
|
||||
|
||||
export default {
|
||||
name: "DisplayPage",
|
||||
mixins: [createWsMixin('display')],
|
||||
data() {
|
||||
return {
|
||||
ws: null,
|
||||
wsConnected: false,
|
||||
isConnecting: false,
|
||||
reconnectTimeout: null,
|
||||
reconnectAttempts: 0,
|
||||
maxReconnectDelay: 30000, // Ritardo massimo di riconnessione: 30 secondi
|
||||
state: {
|
||||
order: true,
|
||||
visuForm: false,
|
||||
visuStriscia: true,
|
||||
modalitaPartita: "3/5",
|
||||
sp: {
|
||||
strisce: [],
|
||||
setFinito: null,
|
||||
partitaFinita: null,
|
||||
striscia: { home: [0], guest: [0] },
|
||||
servHome: true,
|
||||
punt: { home: 0, guest: 0 },
|
||||
set: { home: 0, guest: 0 },
|
||||
nomi: { home: "Antoniana", guest: "Guest" },
|
||||
form: {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
},
|
||||
storicoServizio: [],
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
this.connectWebSocket()
|
||||
// Attiva la modalita fullscreen su dispositivi mobili.
|
||||
if (this.isMobile()) {
|
||||
try { document.documentElement.requestFullscreen() } catch (e) {}
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
stricciaStrip() {
|
||||
const currentSet = this.state.sp.striscia.at(-1)
|
||||
if (!currentSet) return { home: [], guest: [] }
|
||||
let h = 0, g = 0
|
||||
const home = [], guest = []
|
||||
for (const scorer of currentSet.ris) {
|
||||
if (scorer === 'h') { h++; home.push(h); guest.push(' ') }
|
||||
else { g++; guest.push(g); home.push(' ') }
|
||||
|
||||
// Gestisce l'HMR di Vite evitando riconnessioni durante la ricarica a caldo.
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.on('vite:beforeUpdate', () => {
|
||||
// Annulla eventuali tentativi di riconnessione pianificati.
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
})
|
||||
}
|
||||
return { home, guest }
|
||||
},
|
||||
beforeUnmount() {
|
||||
// Pulisce il timeout di riconnessione.
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
|
||||
// Chiude il WebSocket con il codice di chiusura appropriato.
|
||||
if (this.ws) {
|
||||
this.ws.onclose = null // Rimuove il listener per evitare nuove riconnessioni pianificate.
|
||||
this.ws.onerror = null
|
||||
this.ws.onmessage = null
|
||||
this.ws.onopen = null
|
||||
|
||||
// Usa il codice 1000 (chiusura normale) se la connessione e aperta.
|
||||
try {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.close(1000, 'Component unmounting')
|
||||
} else if (this.ws.readyState === WebSocket.CONNECTING) {
|
||||
// Se la connessione e ancora in fase di apertura, chiude direttamente.
|
||||
this.ws.close()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Display] Error closing WebSocket:', err)
|
||||
}
|
||||
this.ws = null
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
'state.sp.striscia': {
|
||||
'state.sp.striscia.home': {
|
||||
deep: true,
|
||||
handler() {
|
||||
this.$nextTick(() => {
|
||||
if (this.$refs.homeItems) this.$refs.homeItems.scrollLeft = this.$refs.homeItems.scrollWidth
|
||||
if (this.$refs.guestItems) this.$refs.guestItems.scrollLeft = this.$refs.guestItems.scrollWidth
|
||||
})
|
||||
}
|
||||
},
|
||||
'state.sp.striscia.guest': {
|
||||
deep: true,
|
||||
handler() {
|
||||
this.$nextTick(() => {
|
||||
if (this.$refs.guestItems) this.$refs.guestItems.scrollLeft = this.$refs.guestItems.scrollWidth
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
onWsMessage(msg) {
|
||||
if (msg.type === 'speak') this.speakOnDisplay(msg.text)
|
||||
else if (msg.type === 'error') console.error('[Display] Server error:', msg.message)
|
||||
},
|
||||
isMobile() {
|
||||
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent)
|
||||
},
|
||||
connectWebSocket() {
|
||||
// Evita connessioni simultanee multiple.
|
||||
if (this.isConnecting) {
|
||||
console.log('[Display] Already connecting, skipping...')
|
||||
return
|
||||
}
|
||||
|
||||
// Chiude la connessione precedente, se presente.
|
||||
if (this.ws) {
|
||||
this.ws.onclose = null
|
||||
this.ws.onerror = null
|
||||
this.ws.onmessage = null
|
||||
this.ws.onopen = null
|
||||
try {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.close(1000, 'Reconnecting')
|
||||
} else if (this.ws.readyState === WebSocket.CONNECTING) {
|
||||
this.ws.close()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('[Display] Error closing previous WebSocket:', err)
|
||||
}
|
||||
this.ws = null
|
||||
}
|
||||
|
||||
this.isConnecting = true
|
||||
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
|
||||
// Permette di specificare un host WebSocket alternativo via query parameter
|
||||
// Utile per scenari WSL2 o development remoto: ?wsHost=192.168.1.100:5173
|
||||
const params = new URLSearchParams(location.search)
|
||||
const defaultHost = (
|
||||
location.hostname === 'localhost' || location.hostname === '::1'
|
||||
)
|
||||
? `127.0.0.1${location.port ? `:${location.port}` : ''}`
|
||||
: location.host
|
||||
const wsHost = params.get('wsHost') || defaultHost
|
||||
const wsUrl = `${protocol}//${wsHost}/ws`
|
||||
|
||||
console.log('[Display] Connecting to WebSocket:', wsUrl)
|
||||
|
||||
try {
|
||||
this.ws = new WebSocket(wsUrl)
|
||||
} catch (err) {
|
||||
console.error('[Display] Failed to create WebSocket:', err)
|
||||
this.isConnecting = false
|
||||
this.scheduleReconnect()
|
||||
return
|
||||
}
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.isConnecting = false
|
||||
this.wsConnected = true
|
||||
this.reconnectAttempts = 0
|
||||
console.log('[Display] Connected to server')
|
||||
|
||||
// Registra il client come display solo con connessione effettivamente aperta.
|
||||
if (this.ws && this.ws.readyState === WebSocket.OPEN) {
|
||||
try {
|
||||
this.ws.send(JSON.stringify({ type: 'register', role: 'display' }))
|
||||
} catch (err) {
|
||||
console.error('[Display] Failed to register:', err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
try {
|
||||
const msg = JSON.parse(event.data)
|
||||
|
||||
if (msg.type === 'state') {
|
||||
this.state = msg.state
|
||||
} else if (msg.type === 'speak') {
|
||||
this.speakOnDisplay(msg.text)
|
||||
} else if (msg.type === 'error') {
|
||||
console.error('[Display] Server error:', msg.message)
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('[Display] Error parsing message:', e)
|
||||
}
|
||||
}
|
||||
|
||||
this.ws.onclose = (event) => {
|
||||
this.isConnecting = false
|
||||
this.wsConnected = false
|
||||
console.log('[Display] Disconnected from server', event.code, event.reason)
|
||||
|
||||
// Non riconnette durante HMR (codice 1001, "going away")
|
||||
// ne in caso di chiusura pulita (codice 1000).
|
||||
if (event.code === 1000 || event.code === 1001) {
|
||||
console.log('[Display] Clean close, not reconnecting')
|
||||
return
|
||||
}
|
||||
|
||||
this.scheduleReconnect()
|
||||
}
|
||||
|
||||
this.ws.onerror = (err) => {
|
||||
console.error('[Display] WebSocket error:', err)
|
||||
this.isConnecting = false
|
||||
this.wsConnected = false
|
||||
}
|
||||
},
|
||||
scheduleReconnect() {
|
||||
// Evita pianificazioni multiple della riconnessione.
|
||||
if (this.reconnectTimeout) {
|
||||
return
|
||||
}
|
||||
|
||||
// Applica backoff esponenziale: 1s, 2s, 4s, 8s, 16s, fino a 30s.
|
||||
const delay = Math.min(
|
||||
1000 * Math.pow(2, this.reconnectAttempts),
|
||||
this.maxReconnectDelay
|
||||
)
|
||||
this.reconnectAttempts++
|
||||
|
||||
console.log(`[Display] Reconnecting in ${delay}ms (attempt ${this.reconnectAttempts})`)
|
||||
|
||||
this.reconnectTimeout = setTimeout(() => {
|
||||
this.reconnectTimeout = null
|
||||
this.connectWebSocket()
|
||||
}, delay)
|
||||
},
|
||||
speakOnDisplay(text) {
|
||||
if (typeof text !== 'string' || !text.trim() || !('speechSynthesis' in window)) return
|
||||
if (typeof text !== 'string' || !text.trim()) {
|
||||
return
|
||||
}
|
||||
if (!('speechSynthesis' in window)) {
|
||||
console.warn('[Display] speechSynthesis not supported')
|
||||
return
|
||||
}
|
||||
|
||||
const utterance = new SpeechSynthesisUtterance(text.trim())
|
||||
const voices = window.speechSynthesis.getVoices()
|
||||
utterance.voice = voices.find(v => v.name === 'Google italiano')
|
||||
|| voices.find(v => v.lang?.toLowerCase().startsWith('it'))
|
||||
|| null
|
||||
const preferredVoice = voices.find((v) => v.name === 'Google italiano')
|
||||
|| voices.find((v) => v.lang && v.lang.toLowerCase().startsWith('it'))
|
||||
if (preferredVoice) {
|
||||
utterance.voice = preferredVoice
|
||||
}
|
||||
utterance.lang = 'it-IT'
|
||||
|
||||
window.speechSynthesis.cancel()
|
||||
window.speechSynthesis.speak(utterance)
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -247,4 +448,38 @@ export default {
|
||||
overflow: hidden;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
/* Fine partita */
|
||||
.partita-finita-overlay {
|
||||
position: fixed;
|
||||
inset: 0;
|
||||
background: rgba(0, 0, 0, 0.88);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 500;
|
||||
}
|
||||
.partita-finita-box {
|
||||
text-align: center;
|
||||
color: #fff;
|
||||
}
|
||||
.partita-finita-label {
|
||||
font-size: 5vw;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.1em;
|
||||
color: #aaa;
|
||||
margin-bottom: 0.3em;
|
||||
}
|
||||
.partita-finita-vincitore {
|
||||
font-size: 14vw;
|
||||
font-weight: 900;
|
||||
color: #fdd835;
|
||||
text-transform: uppercase;
|
||||
line-height: 1;
|
||||
margin-bottom: 0.2em;
|
||||
}
|
||||
.partita-finita-set {
|
||||
font-size: 8vw;
|
||||
font-weight: 700;
|
||||
}
|
||||
</style>
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { createApp } from 'vue'
|
||||
import './style.css'
|
||||
import WaveUI from 'wave-ui'
|
||||
import 'wave-ui/dist/wave-ui.css'
|
||||
import ControllerPage from './components/ControllerPage.vue'
|
||||
|
||||
const app = createApp(ControllerPage)
|
||||
app.use(WaveUI)
|
||||
app.mount('#app')
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
import Database from 'better-sqlite3'
|
||||
import { mkdirSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const DATA_DIR = join(__dirname, '..', 'data')
|
||||
const DB_PATH = process.env.DB_PATH || join(DATA_DIR, 'partite.db')
|
||||
|
||||
mkdirSync(DATA_DIR, { recursive: true })
|
||||
|
||||
const db = new Database(DB_PATH)
|
||||
db.pragma('journal_mode = WAL')
|
||||
|
||||
db.exec(`
|
||||
CREATE TABLE IF NOT EXISTS partite (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
data TEXT NOT NULL,
|
||||
modalita TEXT NOT NULL,
|
||||
nome_home TEXT NOT NULL,
|
||||
nome_guest TEXT NOT NULL,
|
||||
set_home INTEGER NOT NULL,
|
||||
set_guest INTEGER NOT NULL,
|
||||
vincitore TEXT,
|
||||
json TEXT NOT NULL
|
||||
)
|
||||
`)
|
||||
|
||||
const stmtInsert = db.prepare(`
|
||||
INSERT INTO partite (data, modalita, nome_home, nome_guest, set_home, set_guest, vincitore, json)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
`)
|
||||
|
||||
export function salvaPartita(state) {
|
||||
const payload = {
|
||||
data: new Date().toISOString(),
|
||||
modalita: state.modalitaPartita,
|
||||
nomi: state.sp.nomi,
|
||||
set: state.sp.set,
|
||||
vincitore: state.sp.partitaFinita?.vincitore ?? null,
|
||||
strisce: state.sp.strisce,
|
||||
}
|
||||
const { lastInsertRowid } = stmtInsert.run(
|
||||
payload.data,
|
||||
payload.modalita,
|
||||
payload.nomi.home,
|
||||
payload.nomi.guest,
|
||||
payload.set.home,
|
||||
payload.set.guest,
|
||||
payload.vincitore,
|
||||
JSON.stringify(payload)
|
||||
)
|
||||
return lastInsertRowid
|
||||
}
|
||||
|
||||
export function getPartite() {
|
||||
return db.prepare('SELECT * FROM partite ORDER BY id DESC').all()
|
||||
}
|
||||
|
||||
export function getPartita(id) {
|
||||
return db.prepare('SELECT * FROM partite WHERE id = ?').get(id)
|
||||
}
|
||||
+145
-84
@@ -1,20 +1,7 @@
|
||||
export function punteggio(striscia) {
|
||||
let home = 0, guest = 0
|
||||
for (const c of striscia.at(-1).ris) c === 'h' ? home++ : guest++
|
||||
return { home, guest }
|
||||
}
|
||||
|
||||
export function servizio(striscia) {
|
||||
const set = striscia.at(-1)
|
||||
return set.ris.length === 0 ? set.serv === 'h' : set.ris.at(-1) === 'h'
|
||||
}
|
||||
|
||||
export function setVinti(striscia) {
|
||||
return {
|
||||
home: striscia.filter(s => s.vinc === 'h').length,
|
||||
guest: striscia.filter(s => s.vinc === 'g').length,
|
||||
}
|
||||
}
|
||||
/**
|
||||
* Logica di gioco condivisa per il segnapunti.
|
||||
* Utilizzata sia dal server WebSocket sia dal client per l'anteprima locale.
|
||||
*/
|
||||
|
||||
export function createInitialState() {
|
||||
return {
|
||||
@@ -23,129 +10,202 @@ export function createInitialState() {
|
||||
visuStriscia: true,
|
||||
modalitaPartita: "3/5",
|
||||
sp: {
|
||||
striscia: [{ serv: 'h', ris: '', vinc: null }],
|
||||
strisce: [],
|
||||
setFinito: null,
|
||||
partitaFinita: null,
|
||||
formInizioSet: {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
},
|
||||
striscia: { home: [0], guest: [" "] },
|
||||
servHome: true,
|
||||
punt: { home: 0, guest: 0 },
|
||||
set: { home: 0, guest: 0 },
|
||||
nomi: { home: "Antoniana", guest: "Guest" },
|
||||
form: {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
},
|
||||
storicoServizio: [],
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function checkVittoriaPartita(state) {
|
||||
const setsToWin = state.modalitaPartita === "2/3" ? 2 : 3
|
||||
if (state.sp.set.home >= setsToWin) return "home"
|
||||
if (state.sp.set.guest >= setsToWin) return "guest"
|
||||
return null
|
||||
}
|
||||
|
||||
export function checkVittoria(state) {
|
||||
const { home: puntHome, guest: puntGuest } = punteggio(state.sp.striscia)
|
||||
const sv = setVinti(state.sp.striscia)
|
||||
const totSet = sv.home + sv.guest
|
||||
const isSetDecisivo = state.modalitaPartita === "2/3" ? totSet >= 2 : totSet >= 4
|
||||
const puntHome = state.sp.punt.home
|
||||
const puntGuest = state.sp.punt.guest
|
||||
const setHome = state.sp.set.home
|
||||
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 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
|
||||
}
|
||||
|
||||
export function checkVittoriaPartita(state) {
|
||||
if (state.modalitaPartita === 'amichevole') return false
|
||||
const setsToWin = state.modalitaPartita === "2/3" ? 2 : 3
|
||||
const sv = setVinti(state.sp.striscia)
|
||||
return sv.home >= setsToWin || sv.guest >= setsToWin
|
||||
}
|
||||
|
||||
export function applyAction(state, action) {
|
||||
const s = structuredClone(state)
|
||||
// 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))
|
||||
|
||||
switch (action.type) {
|
||||
case "incPunt": {
|
||||
const team = action.team
|
||||
if (checkVittoria(s)) break
|
||||
if (s.sp.setFinito !== null || s.sp.partitaFinita !== null) break
|
||||
|
||||
const servHome = servizio(s.sp.striscia)
|
||||
const cambioPalla = (team === "home") !== servHome
|
||||
const setCorrente = s.sp.striscia.at(-1)
|
||||
if (setCorrente.ris === '') {
|
||||
setCorrente.formInizio = {
|
||||
home: [...s.sp.form.home],
|
||||
guest: [...s.sp.form.guest],
|
||||
}
|
||||
}
|
||||
s.sp.striscia.at(-1).ris += team === 'home' ? 'h' : 'g'
|
||||
s.sp.storicoServizio.push({
|
||||
servHome: s.sp.servHome,
|
||||
cambioPalla: (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome),
|
||||
})
|
||||
|
||||
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(" ")
|
||||
}
|
||||
|
||||
const cambioPalla = (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome)
|
||||
if (cambioPalla) {
|
||||
s.sp.form[team].push(s.sp.form[team].shift())
|
||||
}
|
||||
|
||||
s.sp.servHome = team === "home"
|
||||
|
||||
if (checkVittoria(s)) {
|
||||
s.sp.setFinito = { vincitore: team }
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "decPunt": {
|
||||
const currentSet = s.sp.striscia.at(-1)
|
||||
if (currentSet.ris.length === 0) break
|
||||
if (s.sp.setFinito !== null) {
|
||||
s.sp.setFinito = null
|
||||
}
|
||||
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 lastScorerShort = currentSet.ris.at(-1)
|
||||
const prevServerShort = currentSet.ris.length >= 2
|
||||
? currentSet.ris.at(-2)
|
||||
: currentSet.serv
|
||||
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
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
const wasCambioPalla = lastScorerShort !== prevServerShort
|
||||
case "confermaSet": {
|
||||
if (!s.sp.setFinito) break
|
||||
const vincitore = s.sp.setFinito.vincitore
|
||||
|
||||
currentSet.ris = currentSet.ris.slice(0, -1)
|
||||
if (currentSet.ris === '') delete currentSet.formInizio
|
||||
s.sp.strisce.push({
|
||||
set: s.sp.strisce.length + 1,
|
||||
formInizio: {
|
||||
home: [...s.sp.formInizioSet.home],
|
||||
guest: [...s.sp.formInizioSet.guest],
|
||||
},
|
||||
home: [...s.sp.striscia.home],
|
||||
guest: [...s.sp.striscia.guest],
|
||||
vincitore,
|
||||
punt: { ...s.sp.punt },
|
||||
})
|
||||
|
||||
const lastScorer = lastScorerShort === 'h' ? 'home' : 'guest'
|
||||
s.sp.formInizioSet = {
|
||||
home: [...s.sp.form.home],
|
||||
guest: [...s.sp.form.guest],
|
||||
}
|
||||
|
||||
if (wasCambioPalla) {
|
||||
s.sp.form[lastScorer].unshift(s.sp.form[lastScorer].pop())
|
||||
s.sp.set[vincitore]++
|
||||
|
||||
s.sp.punt.home = 0
|
||||
s.sp.punt.guest = 0
|
||||
s.sp.storicoServizio = []
|
||||
s.sp.setFinito = null
|
||||
|
||||
const vincitorePartita = checkVittoriaPartita(s)
|
||||
if (vincitorePartita) {
|
||||
s.sp.partitaFinita = { vincitore: vincitorePartita }
|
||||
} else {
|
||||
s.sp.striscia = s.sp.servHome
|
||||
? { home: [0], guest: [" "] }
|
||||
: { home: [" "], guest: [0] }
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "incSet": {
|
||||
const team = action.team
|
||||
const sv = setVinti(s.sp.striscia)
|
||||
const count = sv[team]
|
||||
const teamShort = team === 'home' ? 'h' : 'g'
|
||||
if (count >= 2) {
|
||||
// cicla a 0: rimuove le voci fantasma per questo team
|
||||
s.sp.striscia = s.sp.striscia.filter(
|
||||
entry => !(entry._phantom && entry.vinc === teamShort)
|
||||
)
|
||||
if (s.sp.set[team] === 2) {
|
||||
s.sp.set[team] = 0
|
||||
} else {
|
||||
// inserisce una voce fantasma prima dell'ultimo set (quello in corso)
|
||||
s.sp.striscia.splice(-1, 0, { serv: teamShort, ris: '', vinc: teamShort, _phantom: true })
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "nuovoSet": {
|
||||
const team = action.team
|
||||
if (team !== 'home' && team !== 'guest') break
|
||||
if (checkVittoriaPartita(s)) break
|
||||
s.sp.striscia.at(-1).vinc = team === 'home' ? 'h' : 'g'
|
||||
if (checkVittoriaPartita(s)) break
|
||||
s.sp.striscia.push({ serv: team === 'home' ? 'h' : 'g', ris: '', vinc: null })
|
||||
s.sp.form = {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
s.sp.set[team]++
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "cambiaPalla": {
|
||||
const currentSet = s.sp.striscia.at(-1)
|
||||
if (currentSet.ris.length === 0) {
|
||||
currentSet.serv = currentSet.serv === 'h' ? 'g' : 'h'
|
||||
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] }
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "resetta": {
|
||||
s.visuForm = false
|
||||
const servIniziale = s.sp.striscia[0]?.serv ?? 'h'
|
||||
s.sp.striscia = [{ serv: servIniziale, ris: '', vinc: null }]
|
||||
s.sp.punt.home = 0
|
||||
s.sp.punt.guest = 0
|
||||
s.sp.set.home = 0
|
||||
s.sp.set.guest = 0
|
||||
s.sp.form = {
|
||||
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.storicoServizio = []
|
||||
s.sp.strisce = []
|
||||
s.sp.setFinito = null
|
||||
s.sp.partitaFinita = null
|
||||
s.sp.formInizioSet = {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@@ -178,6 +238,7 @@ export function applyAction(state, action) {
|
||||
case "setFormazione": {
|
||||
if (action.team && action.form) {
|
||||
s.sp.form[action.team] = [...action.form]
|
||||
s.sp.formInizioSet[action.team] = [...action.form]
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
import { createApp } from 'vue'
|
||||
import './style.css'
|
||||
import App from './App.vue'
|
||||
import WaveUI from 'wave-ui'
|
||||
import 'wave-ui/dist/wave-ui.css'
|
||||
import DisplayPage from './components/DisplayPage.vue'
|
||||
|
||||
// In modalità display-only, non serve il router.
|
||||
// Il display viene montato direttamente.
|
||||
const app = createApp(DisplayPage)
|
||||
app.use(WaveUI)
|
||||
app.mount('#app')
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { createInitialState } from './gameState.js'
|
||||
|
||||
const STATE_PATH = join(dirname(fileURLToPath(import.meta.url)), '..', '.segnapunti', 'state.json')
|
||||
|
||||
export function loadState() {
|
||||
try {
|
||||
if (existsSync(STATE_PATH)) {
|
||||
return JSON.parse(readFileSync(STATE_PATH, 'utf8'))
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[Persist] Stato non leggibile, si riparte da zero:', err.message)
|
||||
}
|
||||
return createInitialState()
|
||||
}
|
||||
|
||||
export function saveState(state) {
|
||||
try {
|
||||
mkdirSync(dirname(STATE_PATH), { recursive: true })
|
||||
writeFileSync(STATE_PATH, JSON.stringify(state), 'utf8')
|
||||
} catch (err) {
|
||||
console.error('[Persist] Salvataggio fallito:', err.message)
|
||||
}
|
||||
}
|
||||
-141
@@ -1,141 +0,0 @@
|
||||
function vincitoreSet(s) {
|
||||
if (s.vinc === 'h' || s.vinc === 'g') return s.vinc
|
||||
let h = 0, g = 0
|
||||
for (const c of s.ris) c === 'h' ? h++ : g++
|
||||
if (h > g) return 'h'
|
||||
if (g > h) return 'g'
|
||||
return null
|
||||
}
|
||||
|
||||
// Costruisce l'HTML del referto (funzione pura, testabile).
|
||||
// `now` è iniettabile per rendere deterministica la data nei test.
|
||||
export function buildRefertoHtml(state, now = new Date()) {
|
||||
const { sp, modalitaPartita } = state
|
||||
const { nomi, striscia } = sp
|
||||
|
||||
const setReali = striscia.filter(s => !s._phantom)
|
||||
|
||||
const setVinti = { home: 0, guest: 0 }
|
||||
for (const s of setReali) {
|
||||
const v = vincitoreSet(s)
|
||||
if (v === 'h') setVinti.home++
|
||||
else if (v === 'g') setVinti.guest++
|
||||
}
|
||||
|
||||
const dataOra = now.toLocaleString('it-IT', {
|
||||
day: '2-digit', month: '2-digit', year: 'numeric',
|
||||
hour: '2-digit', minute: '2-digit',
|
||||
})
|
||||
|
||||
const giocatoreHtml = (n) => `<div class="giocatore">${n}</div>`
|
||||
|
||||
const formazioneHtml = (formazioneSet) => {
|
||||
if (!formazioneSet) return '<em style="color:#999;font-size:11px">non disponibile</em>'
|
||||
return `
|
||||
<div class="form-row">
|
||||
<div class="form-team">
|
||||
<div class="form-team-name">${nomi.home}</div>
|
||||
<div class="giocatori">${formazioneSet.home.map(giocatoreHtml).join('')}</div>
|
||||
</div>
|
||||
<div class="form-team">
|
||||
<div class="form-team-name">${nomi.guest}</div>
|
||||
<div class="giocatori">${formazioneSet.guest.map(giocatoreHtml).join('')}</div>
|
||||
</div>
|
||||
</div>`
|
||||
}
|
||||
|
||||
const setsHtml = setReali.map((s, i) => {
|
||||
let h = 0, g = 0
|
||||
const punti = []
|
||||
for (const c of s.ris) {
|
||||
c === 'h' ? h++ : g++
|
||||
punti.push({ chi: c, h, g })
|
||||
}
|
||||
|
||||
const vinc = vincitoreSet(s)
|
||||
const nomeVinc = vinc === 'h' ? nomi.home : vinc === 'g' ? nomi.guest : ''
|
||||
const etichettaVinc = nomeVinc ? ` · vinto da <strong>${nomeVinc}</strong>` : ''
|
||||
|
||||
const puntiHtml = punti.map(p =>
|
||||
`<span class="punto punto-${p.chi}">${p.h}-${p.g}</span>`
|
||||
).join('')
|
||||
|
||||
return `
|
||||
<div class="set-section">
|
||||
<div class="set-header">
|
||||
Set ${i + 1} — ${nomi.home} <strong>${h}</strong> · <strong>${g}</strong> ${nomi.guest}${etichettaVinc}
|
||||
</div>
|
||||
<div class="form-inizio">
|
||||
<div class="form-inizio-label">Formazione di partenza</div>
|
||||
${formazioneHtml(s.formInizio)}
|
||||
</div>
|
||||
<div class="punti-grid">${puntiHtml || '<em style="color:#999;font-size:11px">Nessun punto registrato</em>'}</div>
|
||||
</div>`
|
||||
}).join('')
|
||||
|
||||
const html = `<!DOCTYPE html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Referto — ${nomi.home} vs ${nomi.guest}</title>
|
||||
<style>
|
||||
* { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
body { font-family: Arial, Helvetica, sans-serif; color: #111; background: #fff; padding: 28px; font-size: 14px; }
|
||||
@media print { body { padding: 12px; } }
|
||||
|
||||
.header { text-align: center; border-bottom: 2px solid #111; padding-bottom: 12px; margin-bottom: 20px; }
|
||||
.titolo { font-size: 18px; font-weight: bold; letter-spacing: 3px; text-transform: uppercase; }
|
||||
.meta { font-size: 12px; color: #666; margin-top: 5px; }
|
||||
|
||||
.squadre-row { display: flex; justify-content: space-around; align-items: center; margin: 18px 0 6px; }
|
||||
.nome-sq { font-size: 22px; font-weight: bold; text-align: center; max-width: 40%; }
|
||||
.vs { font-size: 16px; color: #999; }
|
||||
.risultato { text-align: center; font-size: 36px; font-weight: bold; letter-spacing: 6px; margin-bottom: 4px; }
|
||||
.modalita-label { text-align: center; font-size: 12px; color: #888; margin-bottom: 22px; }
|
||||
|
||||
.set-section { margin-bottom: 12px; border: 1px solid #ddd; border-radius: 4px; overflow: hidden; }
|
||||
.set-header { background: #f5f5f5; padding: 7px 12px; font-size: 13px; border-bottom: 1px solid #ddd; }
|
||||
.punti-grid { display: flex; flex-wrap: wrap; gap: 3px; padding: 8px 10px; }
|
||||
.punto { display: inline-block; padding: 2px 5px; border-radius: 3px; font-size: 11px; font-family: 'Courier New', monospace; white-space: nowrap; }
|
||||
.punto-h { background: #d0e8ff; color: #003a6e; }
|
||||
.punto-g { background: #ffddc0; color: #6e2700; }
|
||||
|
||||
.form-inizio { padding: 8px 10px; border-bottom: 1px solid #eee; background: #fafafa; }
|
||||
.form-inizio-label { font-size: 11px; font-weight: bold; letter-spacing: 0.5px; text-transform: uppercase; color: #888; margin-bottom: 7px; }
|
||||
.form-row { display: flex; gap: 40px; }
|
||||
.form-team { flex: 1; }
|
||||
.form-team-name { font-weight: bold; font-size: 13px; margin-bottom: 6px; }
|
||||
.giocatori { display: flex; gap: 5px; flex-wrap: wrap; }
|
||||
.giocatore { background: #f0f0f0; border-radius: 50%; width: 30px; height: 30px; display: flex; align-items: center; justify-content: center; font-weight: bold; font-size: 12px; border: 1px solid #ccc; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="header">
|
||||
<div class="titolo">Referto di Gara</div>
|
||||
<div class="meta">${dataOra} · Modalità: ${modalitaPartita}</div>
|
||||
</div>
|
||||
|
||||
<div class="squadre-row">
|
||||
<div class="nome-sq">${nomi.home}</div>
|
||||
<div class="vs">vs</div>
|
||||
<div class="nome-sq">${nomi.guest}</div>
|
||||
</div>
|
||||
<div class="risultato">${setVinti.home} – ${setVinti.guest}</div>
|
||||
<div class="modalita-label">set vinti</div>
|
||||
|
||||
${setsHtml}
|
||||
</body>
|
||||
</html>`
|
||||
|
||||
return html
|
||||
}
|
||||
|
||||
// Apre il referto in una nuova scheda e avvia la stampa (effetto collaterale,
|
||||
// solo browser). La generazione dell'HTML è delegata a buildRefertoHtml.
|
||||
export function generaReferto(state) {
|
||||
const html = buildRefertoHtml(state)
|
||||
const w = window.open('', '_blank')
|
||||
w.document.write(html)
|
||||
w.document.close()
|
||||
w.print()
|
||||
}
|
||||
+31
-50
@@ -1,68 +1,49 @@
|
||||
import { networkInterfaces } from 'os'
|
||||
import { readFileSync, existsSync } from 'fs'
|
||||
import { execSync } from 'child_process'
|
||||
|
||||
function isWSL() {
|
||||
try {
|
||||
return existsSync('/proc/sys/kernel/osrelease') &&
|
||||
readFileSync('/proc/sys/kernel/osrelease', 'utf8').toLowerCase().includes('microsoft')
|
||||
} catch { return false }
|
||||
}
|
||||
/**
|
||||
* Restituisce gli indirizzi IP di rete del sistema, escludendo loopback e bridge Docker.
|
||||
* @returns {string[]} Elenco degli indirizzi IP disponibili.
|
||||
*/
|
||||
export function getNetworkIPs() {
|
||||
const nets = networkInterfaces()
|
||||
const networkIPs = []
|
||||
|
||||
// Un IPv4 è "pubblicabile" in LAN se non è loopback, link-local o bridge Docker.
|
||||
function isLanIPv4(address) {
|
||||
return !!address
|
||||
&& !address.startsWith('127.')
|
||||
&& !address.startsWith('169.254.')
|
||||
&& !address.startsWith('172.')
|
||||
}
|
||||
|
||||
// Estrae gli IP LAN da un oggetto in stile os.networkInterfaces().
|
||||
// Esportata per poter essere testata in isolamento, senza dipendere dalla piattaforma.
|
||||
export function collectIPs(nets) {
|
||||
const out = []
|
||||
for (const name of Object.keys(nets || {})) {
|
||||
for (const name of Object.keys(nets)) {
|
||||
for (const net of nets[name]) {
|
||||
if (net.family === 'IPv4' && !net.internal && isLanIPv4(net.address)) {
|
||||
out.push(net.address)
|
||||
// Esclude loopback (127.0.0.1), indirizzi non IPv4 e bridge Docker (172.17.x.x, 172.18.x.x).
|
||||
if (net.family === 'IPv4' &&
|
||||
!net.internal &&
|
||||
!net.address.startsWith('172.17.') &&
|
||||
!net.address.startsWith('172.18.')) {
|
||||
networkIPs.push(net.address)
|
||||
}
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Restituisce gli IP di rete della macchina.
|
||||
// Passando `nets` (oggetto in stile os.networkInterfaces) si forza il percorso
|
||||
// deterministico, scavalcando il rilevamento WSL/PowerShell: utile nei test.
|
||||
export function getNetworkIPs(nets) {
|
||||
if (nets) return collectIPs(nets)
|
||||
|
||||
if (isWSL()) {
|
||||
try {
|
||||
const out = execSync(
|
||||
'powershell.exe -NoProfile -Command "Get-NetIPAddress -AddressFamily IPv4 | Select-Object -ExpandProperty IPAddress"',
|
||||
{ timeout: 3000 }
|
||||
)
|
||||
return out.toString().trim().split('\n')
|
||||
.map(s => s.trim())
|
||||
.filter(isLanIPv4)
|
||||
} catch { return [] }
|
||||
return networkIPs
|
||||
}
|
||||
|
||||
return collectIPs(networkInterfaces())
|
||||
}
|
||||
/**
|
||||
* Stampa il riepilogo di avvio del server con gli URL di accesso.
|
||||
* @param {number} displayPort - Porta del display.
|
||||
* @param {number} controllerPort - Porta del controller.
|
||||
*/
|
||||
export function printServerInfo(displayPort = 5173, controllerPort = 3001, storicoPort = 3002) {
|
||||
const networkIPs = getNetworkIPs()
|
||||
|
||||
// `networkIPs` è iniettabile per rendere la stampa testabile in modo deterministico.
|
||||
export function printServerInfo(port = 3000, networkIPs = getNetworkIPs()) {
|
||||
console.log(`\nSegnapunti Server`)
|
||||
console.log(` Display: http://127.0.0.1:${port}/display`)
|
||||
console.log(` Controller: http://127.0.0.1:${port}/controller`)
|
||||
console.log(` Display: http://127.0.0.1:${displayPort}/`)
|
||||
console.log(` Controller: http://127.0.0.1:${controllerPort}/`)
|
||||
console.log(` Storico: http://127.0.0.1:${storicoPort}/`)
|
||||
|
||||
if (networkIPs.length > 0) {
|
||||
console.log(`\n Da dispositivi remoti:`)
|
||||
console.log(`\n Controller da dispositivi remoti:`)
|
||||
networkIPs.forEach(ip => {
|
||||
console.log(` Display: http://${ip}:${port}/display`)
|
||||
console.log(` Controller: http://${ip}:${port}/controller`)
|
||||
console.log(` http://${ip}:${controllerPort}/`)
|
||||
})
|
||||
console.log(`\n Storico da dispositivi remoti:`)
|
||||
networkIPs.forEach(ip => {
|
||||
console.log(` http://${ip}:${storicoPort}/`)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -248,7 +248,9 @@ button:focus-visible {
|
||||
max-width: 64px;
|
||||
}
|
||||
|
||||
.cambi-input input {
|
||||
.cambi-input input,
|
||||
.cambi-input .w-input__input,
|
||||
.cambi-input .w-input__field {
|
||||
border: 2px solid rgba(255, 255, 255, 0.35);
|
||||
border-radius: 8px;
|
||||
padding: 6px 10px;
|
||||
@@ -257,15 +259,21 @@ button:focus-visible {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.cambi-in input {
|
||||
.cambi-in input,
|
||||
.cambi-in .w-input__input,
|
||||
.cambi-in .w-input__field {
|
||||
background: rgba(120, 200, 120, 0.4);
|
||||
}
|
||||
|
||||
.cambi-out input {
|
||||
.cambi-out input,
|
||||
.cambi-out .w-input__input,
|
||||
.cambi-out .w-input__field {
|
||||
background: rgba(200, 120, 120, 0.4);
|
||||
}
|
||||
|
||||
.cambi-input input:focus {
|
||||
.cambi-input input:focus,
|
||||
.cambi-input .w-input__input:focus,
|
||||
.cambi-input .w-input__field:focus {
|
||||
border-color: rgba(255, 255, 255, 0.7);
|
||||
outline: none;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
import { createInitialState, applyAction } from './gameState.js'
|
||||
import { salvaPartita } from './db.js'
|
||||
|
||||
/**
|
||||
* Crea e configura il server WebSocket per la gestione dello stato di gioco.
|
||||
* @param {WebSocketServer} wss - Istanza del server WebSocket.
|
||||
* @returns {Object} Oggetto con metodi di gestione dello stato.
|
||||
*/
|
||||
export function setupWebSocketHandler(wss, options = {}) {
|
||||
export function setupWebSocketHandler(wss) {
|
||||
// Stato globale della partita.
|
||||
let gameState = options.initialState ?? createInitialState()
|
||||
let gameState = createInitialState()
|
||||
|
||||
// Mappa dei ruoli associati ai client connessi.
|
||||
const clients = new Map() // ws -> { role: 'display' | 'controller' }
|
||||
@@ -98,9 +99,18 @@ export function setupWebSocketHandler(wss, options = {}) {
|
||||
return
|
||||
}
|
||||
|
||||
// Salva su DB quando la partita appena finisce.
|
||||
if (!previousState.sp.partitaFinita && gameState.sp.partitaFinita) {
|
||||
try {
|
||||
const id = salvaPartita(gameState)
|
||||
console.log(`[DB] Partita salvata (id: ${id})`)
|
||||
} catch (err) {
|
||||
console.error('[DB] Errore salvataggio partita:', err)
|
||||
}
|
||||
}
|
||||
|
||||
// Propaga il nuovo stato a tutti i client connessi.
|
||||
broadcastState()
|
||||
options.onStateChange?.(gameState)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,9 +167,9 @@ export function setupWebSocketHandler(wss, options = {}) {
|
||||
*/
|
||||
function handleClose(ws) {
|
||||
const client = clients.get(ws)
|
||||
const role = client?.role || 'unknown'
|
||||
console.log(`[WebSocket] Client disconnected (role: ${role})`)
|
||||
const role = client?.role || 'unregistered'
|
||||
clients.delete(ws)
|
||||
console.log(`[WebSocket] Client disconnected (role: ${role}, remaining: ${wss.clients.size})`)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,6 +195,8 @@ export function setupWebSocketHandler(wss, options = {}) {
|
||||
// Imposta il tipo binario per ridurre i problemi di codifica.
|
||||
ws.binaryType = 'arraybuffer'
|
||||
|
||||
console.log(`[WebSocket] New connection (total: ${wss.clients.size})`)
|
||||
|
||||
ws.on('message', (data) => handleMessage(ws, data))
|
||||
ws.on('close', () => handleClose(ws))
|
||||
ws.on('error', (err) => handleError(err, ws))
|
||||
|
||||
-146
@@ -1,146 +0,0 @@
|
||||
import { createInitialState, punteggio, servizio, setVinti } from './gameState.js'
|
||||
|
||||
export function createWsMixin(role) {
|
||||
return {
|
||||
data() {
|
||||
return {
|
||||
ws: null,
|
||||
wsConnected: false,
|
||||
isConnecting: false,
|
||||
reconnectTimeout: null,
|
||||
reconnectAttempts: 0,
|
||||
maxReconnectDelay: 30000,
|
||||
state: createInitialState(),
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
punt() { return punteggio(this.state.sp.striscia) },
|
||||
servHome() { return servizio(this.state.sp.striscia) },
|
||||
set() { return setVinti(this.state.sp.striscia) },
|
||||
},
|
||||
mounted() {
|
||||
this.connectWebSocket()
|
||||
if (import.meta.hot) {
|
||||
import.meta.hot.on('vite:beforeUpdate', () => {
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
beforeUnmount() {
|
||||
if (this.reconnectTimeout) {
|
||||
clearTimeout(this.reconnectTimeout)
|
||||
this.reconnectTimeout = null
|
||||
}
|
||||
if (this.ws) {
|
||||
this.ws.onclose = null
|
||||
this.ws.onerror = null
|
||||
this.ws.onmessage = null
|
||||
this.ws.onopen = null
|
||||
try {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.close(1000, 'Component unmounting')
|
||||
} else if (this.ws.readyState === WebSocket.CONNECTING) {
|
||||
this.ws.close()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[${role}] Error closing WebSocket:`, err)
|
||||
}
|
||||
this.ws = null
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
connectWebSocket() {
|
||||
if (this.isConnecting) return
|
||||
if (this.ws) {
|
||||
this.ws.onclose = null
|
||||
this.ws.onerror = null
|
||||
this.ws.onmessage = null
|
||||
this.ws.onopen = null
|
||||
try {
|
||||
if (this.ws.readyState === WebSocket.OPEN) {
|
||||
this.ws.close(1000, 'Reconnecting')
|
||||
} else if (this.ws.readyState === WebSocket.CONNECTING) {
|
||||
this.ws.close()
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(`[${role}] Error closing previous WebSocket:`, err)
|
||||
}
|
||||
this.ws = null
|
||||
}
|
||||
|
||||
this.isConnecting = true
|
||||
const protocol = location.protocol === 'https:' ? 'wss:' : 'ws:'
|
||||
const params = new URLSearchParams(location.search)
|
||||
const defaultHost = (location.hostname === 'localhost' || location.hostname === '::1')
|
||||
? `127.0.0.1${location.port ? `:${location.port}` : ''}`
|
||||
: location.host
|
||||
const wsUrl = `${protocol}//${params.get('wsHost') || defaultHost}/ws`
|
||||
|
||||
try {
|
||||
this.ws = new WebSocket(wsUrl)
|
||||
} catch (err) {
|
||||
console.error(`[${role}] Failed to create WebSocket:`, err)
|
||||
this.isConnecting = false
|
||||
this.scheduleReconnect()
|
||||
return
|
||||
}
|
||||
|
||||
this.ws.onopen = () => {
|
||||
this.isConnecting = false
|
||||
this.wsConnected = true
|
||||
this.reconnectAttempts = 0
|
||||
try {
|
||||
this.ws?.send(JSON.stringify({ type: 'register', role }))
|
||||
} catch (err) {
|
||||
console.error(`[${role}] Failed to register:`, err)
|
||||
}
|
||||
}
|
||||
|
||||
this.ws.onmessage = (event) => {
|
||||
try {
|
||||
const msg = JSON.parse(event.data)
|
||||
if (msg.type === 'state') this.state = msg.state
|
||||
else this.onWsMessage?.(msg)
|
||||
} catch (e) {
|
||||
console.error(`[${role}] Error parsing message:`, e)
|
||||
}
|
||||
}
|
||||
|
||||
this.ws.onclose = (event) => {
|
||||
this.isConnecting = false
|
||||
this.wsConnected = false
|
||||
if (event.code !== 1000 && event.code !== 1001) this.scheduleReconnect()
|
||||
}
|
||||
|
||||
this.ws.onerror = () => {
|
||||
this.isConnecting = false
|
||||
this.wsConnected = false
|
||||
}
|
||||
},
|
||||
|
||||
scheduleReconnect() {
|
||||
if (this.reconnectTimeout) return
|
||||
const delay = Math.min(1000 * Math.pow(2, this.reconnectAttempts), this.maxReconnectDelay)
|
||||
this.reconnectAttempts++
|
||||
this.reconnectTimeout = setTimeout(() => {
|
||||
this.reconnectTimeout = null
|
||||
this.connectWebSocket()
|
||||
}, delay)
|
||||
},
|
||||
|
||||
sendWs(msg) {
|
||||
if (!this.wsConnected || this.ws?.readyState !== WebSocket.OPEN) return false
|
||||
try {
|
||||
this.ws.send(JSON.stringify(msg))
|
||||
return true
|
||||
} catch (err) {
|
||||
console.error(`[${role}] Failed to send:`, err)
|
||||
return false
|
||||
}
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
+293
@@ -0,0 +1,293 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="it">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Storico Partite</title>
|
||||
<style>
|
||||
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
|
||||
|
||||
body {
|
||||
background: #111;
|
||||
color: #e0e0e0;
|
||||
font-family: 'Inter', system-ui, sans-serif;
|
||||
min-height: 100vh;
|
||||
padding: 24px 16px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
text-align: center;
|
||||
font-size: 22px;
|
||||
font-weight: 800;
|
||||
letter-spacing: 0.1em;
|
||||
color: #fdd835;
|
||||
text-transform: uppercase;
|
||||
margin-bottom: 24px;
|
||||
}
|
||||
|
||||
#lista {
|
||||
max-width: 700px;
|
||||
margin: 0 auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.card {
|
||||
background: #1e1e1e;
|
||||
border: 1px solid rgba(255,255,255,0.1);
|
||||
border-radius: 16px;
|
||||
overflow: hidden;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.card-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 16px 20px;
|
||||
gap: 12px;
|
||||
user-select: none;
|
||||
}
|
||||
|
||||
.card-header:hover {
|
||||
background: rgba(255,255,255,0.04);
|
||||
}
|
||||
|
||||
.card-data {
|
||||
font-size: 11px;
|
||||
color: #777;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.card-teams {
|
||||
flex: 1;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.card-nomi {
|
||||
font-size: 16px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.card-result {
|
||||
font-size: 22px;
|
||||
font-weight: 900;
|
||||
letter-spacing: 0.05em;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.card-result .winner { color: #fdd835; }
|
||||
|
||||
.card-modalita {
|
||||
font-size: 11px;
|
||||
color: #777;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.card-arrow {
|
||||
font-size: 18px;
|
||||
color: #555;
|
||||
transition: transform 0.2s;
|
||||
}
|
||||
.card.open .card-arrow { transform: rotate(180deg); }
|
||||
|
||||
.card-detail {
|
||||
display: none;
|
||||
border-top: 1px solid rgba(255,255,255,0.08);
|
||||
padding: 16px 20px;
|
||||
}
|
||||
.card.open .card-detail { display: block; }
|
||||
|
||||
.set-table {
|
||||
width: 100%;
|
||||
border-collapse: collapse;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.set-table th {
|
||||
text-align: left;
|
||||
padding: 6px 8px;
|
||||
font-size: 11px;
|
||||
font-weight: 700;
|
||||
color: #777;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.05em;
|
||||
border-bottom: 1px solid rgba(255,255,255,0.08);
|
||||
}
|
||||
|
||||
.set-table td {
|
||||
padding: 8px 8px;
|
||||
vertical-align: top;
|
||||
border-bottom: 1px solid rgba(255,255,255,0.05);
|
||||
}
|
||||
|
||||
.set-table tr:last-child td { border-bottom: none; }
|
||||
|
||||
.set-num {
|
||||
font-weight: 800;
|
||||
color: #aaa;
|
||||
width: 32px;
|
||||
}
|
||||
|
||||
.set-vince {
|
||||
font-weight: 700;
|
||||
color: #fdd835;
|
||||
}
|
||||
|
||||
.set-punt {
|
||||
font-weight: 700;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
.form-grid {
|
||||
display: inline-grid;
|
||||
grid-template-columns: repeat(3, 28px);
|
||||
grid-template-rows: repeat(2, 24px);
|
||||
gap: 2px;
|
||||
background: rgba(255,255,255,0.05);
|
||||
border-radius: 6px;
|
||||
padding: 4px;
|
||||
}
|
||||
|
||||
.form-cell {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
font-size: 11px;
|
||||
font-weight: 700;
|
||||
background: rgba(255,255,255,0.08);
|
||||
border-radius: 4px;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
font-size: 11px;
|
||||
color: #666;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.form-wrap {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
#vuoto {
|
||||
text-align: center;
|
||||
color: #555;
|
||||
padding: 48px 16px;
|
||||
font-size: 15px;
|
||||
}
|
||||
|
||||
#errore {
|
||||
text-align: center;
|
||||
color: #ef5350;
|
||||
padding: 24px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Storico Partite</h1>
|
||||
<div id="lista"></div>
|
||||
|
||||
<script>
|
||||
function formatData(iso) {
|
||||
const d = new Date(iso)
|
||||
return d.toLocaleDateString('it-IT', { day: '2-digit', month: '2-digit', year: 'numeric' })
|
||||
+ ' ' + d.toLocaleTimeString('it-IT', { hour: '2-digit', minute: '2-digit' })
|
||||
}
|
||||
|
||||
// Layout campo: righe [fila attacco 3-2-1, fila difesa 4-5-0] → indici nell'array form
|
||||
const LAYOUT = [3, 2, 1, 4, 5, 0]
|
||||
|
||||
function renderForm(form, label) {
|
||||
const cells = LAYOUT.map(i => `<div class="form-cell">${form[i] ?? '?'}</div>`).join('')
|
||||
return `<div class="form-wrap">
|
||||
<div class="form-label">${label}</div>
|
||||
<div class="form-grid">${cells}</div>
|
||||
</div>`
|
||||
}
|
||||
|
||||
function renderDettaglio(dati, nomiHome, nomiGuest) {
|
||||
if (!dati.strisce || dati.strisce.length === 0) return '<p style="color:#555">Nessun set registrato.</p>'
|
||||
|
||||
const righe = dati.strisce.map(s => {
|
||||
const vince = s.vincitore === 'home' ? nomiHome : nomiGuest
|
||||
const formHome = s.formInizio?.home ?? []
|
||||
const formGuest = s.formInizio?.guest ?? []
|
||||
return `<tr>
|
||||
<td class="set-num">${s.set}</td>
|
||||
<td class="set-vince">${vince}</td>
|
||||
<td class="set-punt">${s.punt.home} – ${s.punt.guest}</td>
|
||||
<td>${renderForm(formHome, nomiHome)}</td>
|
||||
<td>${renderForm(formGuest, nomiGuest)}</td>
|
||||
</tr>`
|
||||
}).join('')
|
||||
|
||||
return `<table class="set-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Set</th>
|
||||
<th>Vincitore</th>
|
||||
<th>Punteggio</th>
|
||||
<th>Form Home</th>
|
||||
<th>Form Guest</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>${righe}</tbody>
|
||||
</table>`
|
||||
}
|
||||
|
||||
function renderCard(p) {
|
||||
const dati = JSON.parse(p.json)
|
||||
const nomeHome = p.nome_home
|
||||
const nomeGuest = p.nome_guest
|
||||
const vincitoreNome = p.vincitore === 'home' ? nomeHome : nomeGuest
|
||||
|
||||
const homeWin = p.vincitore === 'home'
|
||||
const resultHome = homeWin ? `<span class="winner">${p.set_home}</span>` : p.set_home
|
||||
const resultGuest = homeWin ? p.set_guest : `<span class="winner">${p.set_guest}</span>`
|
||||
|
||||
const card = document.createElement('div')
|
||||
card.className = 'card'
|
||||
card.innerHTML = `
|
||||
<div class="card-header">
|
||||
<div class="card-data">${formatData(p.data)}</div>
|
||||
<div class="card-teams">
|
||||
<div class="card-nomi">${nomeHome} vs ${nomeGuest}</div>
|
||||
<div class="card-result">${resultHome} – ${resultGuest}</div>
|
||||
</div>
|
||||
<div class="card-modalita">${p.modalita}</div>
|
||||
<div class="card-arrow">▾</div>
|
||||
</div>
|
||||
<div class="card-detail">
|
||||
${renderDettaglio(dati, nomeHome, nomeGuest)}
|
||||
</div>
|
||||
`
|
||||
card.querySelector('.card-header').addEventListener('click', () => {
|
||||
card.classList.toggle('open')
|
||||
})
|
||||
return card
|
||||
}
|
||||
|
||||
async function caricaPartite() {
|
||||
const lista = document.getElementById('lista')
|
||||
try {
|
||||
const res = await fetch('/api/partite')
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
const partite = await res.json()
|
||||
|
||||
if (partite.length === 0) {
|
||||
lista.innerHTML = '<div id="vuoto">Nessuna partita registrata.</div>'
|
||||
return
|
||||
}
|
||||
|
||||
partite.forEach(p => lista.appendChild(renderCard(p)))
|
||||
} catch (err) {
|
||||
lista.innerHTML = `<div id="errore">Errore caricamento: ${err.message}</div>`
|
||||
}
|
||||
}
|
||||
|
||||
caricaPartite()
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,108 +0,0 @@
|
||||
# TODO — completamento suite di test
|
||||
|
||||
Checkpoint: branch `test-suite-repair`, commit `bf9a6f8`.
|
||||
|
||||
## Contesto
|
||||
|
||||
La suite era allineata a una **vecchia forma dello stato** (`sp.punt` / `sp.set` /
|
||||
`sp.servHome`) e a una **vecchia architettura e2e** (controller su `:3001`).
|
||||
Baseline iniziale: **77/170 test Vitest falliti**, **tutti gli e2e rotti**.
|
||||
|
||||
Nel commit `bf9a6f8`:
|
||||
- Vitest riportato a **212/212 verde** (verificato con `npx vitest run`).
|
||||
- e2e migrati **parzialmente** (porte + viewport + reset→config), **NON verificati verdi**.
|
||||
|
||||
---
|
||||
|
||||
## ✅ Fatto (verificato)
|
||||
|
||||
- `tests/unit/gameState.test.js` — riscritto con helper che derivano
|
||||
punteggio/set/servizio dalla striscia; aggiunto blocco `formInizio`.
|
||||
- `src/server-utils.js` — `getNetworkIPs(nets)` con interfacce iniettabili +
|
||||
`printServerInfo(port, ips)` con IP iniettabili (deterministico anche su WSL);
|
||||
filtro LAN unificato (esclude `127.` / `169.254.` / `172.`). Test riscritto.
|
||||
- `tests/integration/websocket.test.js` + `tests/stress/websocket-load.test.js`
|
||||
— punteggio letto via `punteggio(striscia)`.
|
||||
- `tests/component/ControllerPage.test.js` + `DisplayPage.test.js` — forzato
|
||||
layout mobile (viewport portrait), punteggi via striscia; aggiunto test REFERTO.
|
||||
- Nuovi unit/integration: `referto.test.js`, `persist.test.js` (mock `fs`),
|
||||
`wsMixin.test.js`, `integration/server.test.js` (routing).
|
||||
- `src/referto.js` — estratta `buildRefertoHtml(state, now)` pura; `generaReferto`
|
||||
resta wrapper con `window.open`/`print`.
|
||||
- `server.js` — estratti `createApp()` / `startServer()`; avvio solo se entrypoint.
|
||||
|
||||
---
|
||||
|
||||
## ⚠️ Da finire / verificare — e2e Playwright
|
||||
|
||||
Gli e2e NON sono stati eseguiti fino a verde (run lenti). Punto di ripartenza.
|
||||
|
||||
### Fix già applicati nei 6 spec
|
||||
1. `:3001` → `:3000/controller`.
|
||||
2. `setViewportSize({ width: 390, height: 844 })` prima di ogni `goto` controller
|
||||
(su desktop renderizza la dashboard landscape `.e-dash`, ma i test cercano il
|
||||
markup mobile `.team-score` / `.team-pts` / `.btn-set`).
|
||||
3. Il reset ora chiude il dialog di configurazione che `doReset()` apre in automatico.
|
||||
4. `game-simulation`: a 25 gestito il modal automatico "SET VINTO" con
|
||||
"VAI AL SET SUCCESSIVO".
|
||||
|
||||
### Da verificare / probabili fix residui
|
||||
|
||||
- [ ] **`full-match.spec.cjs`** — RISCHIO ALTO. I test arrivano a 25/15 → scatta
|
||||
il modal automatico (`squadraVincente`). Le asserzioni che cliccano
|
||||
`.btn-set` o `.team-score` DOPO i 25 punti vengono bloccate dall'overlay del
|
||||
modal. Da rivedere il flusso (usare i bottoni del modal).
|
||||
- [ ] **`game-simulation.spec.cjs`** — verificare il nuovo finale col modal.
|
||||
- [ ] **`game-operations.spec.cjs`** — verificare flusso cambi/toggle/config dopo
|
||||
`resetGame` (config ora chiuso).
|
||||
- [ ] **`basic-flow.spec.cjs`** — probabile OK, confermare.
|
||||
- [ ] **`accessibility.spec.cjs`** — rieseguire axe sul markup attuale.
|
||||
- [ ] **`visual-regression.spec.cjs`** — FALLIRÀ: snapshot baseline della vecchia
|
||||
UI. Rigenerare con `npm run test:e2e -- --update-snapshots` DOPO aver
|
||||
sistemato il resto.
|
||||
|
||||
### Note importanti
|
||||
- Stato e2e **condiviso e persistente** (`.segnapunti/state.json`): i test
|
||||
dipendono dall'ordine e dal reset. Partire da stato pulito.
|
||||
- I 3 progetti Playwright (chromium, firefox, Mobile Chrome) girano in serie
|
||||
(`workers: 1`).
|
||||
|
||||
---
|
||||
|
||||
## ⬜ Non ancora iniziato
|
||||
|
||||
- [ ] **`tests/e2e/referto.spec.cjs`** (nuovo) — portare una partita 2/3 a fine
|
||||
match, intercettare il popup con `page.waitForEvent('popup')` dopo il click
|
||||
su REFERTO, verificare nomi squadre + punteggi. Stubbare `window.print`
|
||||
(via `addInitScript`) perché può bloccare.
|
||||
- [ ] **Documentazione**:
|
||||
- `README.md` — sezione Controller: aggiungere il referto (PARTITA FINITA →
|
||||
referto stampabile/PDF, prototipo). Sezione Test: aggiungere `test:ui`.
|
||||
- `CLAUDE.md` — aggiungere `src/referto.js` (`buildRefertoHtml` + `generaReferto`),
|
||||
menzionare `wsMixin.js` / `persist.js` / `server-utils.js`; nota su `formInizio`.
|
||||
- `tests/README.md` — correggere i conteggi obsoleti ("159 passed (159)",
|
||||
"6 files"); aggiungere referto / persist / wsMixin / routing server / spec
|
||||
referto.
|
||||
|
||||
---
|
||||
|
||||
## Comandi utili
|
||||
|
||||
```bash
|
||||
# Vitest (deve restare verde: 212/212)
|
||||
npx vitest run
|
||||
|
||||
# e2e — partire da stato pulito + server attivo
|
||||
rm -f .segnapunti/state.json
|
||||
npm run serve # in un terminale a parte
|
||||
|
||||
# e2e mirati (chromium, dai più rischiosi)
|
||||
npx playwright test --config=playwright.config.cjs --project=chromium tests/e2e/full-match.spec.cjs
|
||||
npx playwright test --config=playwright.config.cjs --project=chromium tests/e2e/game-simulation.spec.cjs
|
||||
|
||||
# rigenerare gli snapshot visual DOPO aver sistemato la UI dei test
|
||||
npm run test:e2e -- --update-snapshots
|
||||
|
||||
# suite e2e completa (3 browser, lenta)
|
||||
npm run test:e2e
|
||||
```
|
||||
@@ -2,10 +2,6 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import ControllerPage from '../../src/components/ControllerPage.vue'
|
||||
import { generaReferto } from '../../src/referto.js'
|
||||
|
||||
// Il referto apre una finestra/print: lo mockiamo per testarne solo l'invocazione.
|
||||
vi.mock('../../src/referto.js', () => ({ generaReferto: vi.fn() }))
|
||||
|
||||
// Mock globale WebSocket per jsdom
|
||||
class MockWebSocket {
|
||||
@@ -29,21 +25,6 @@ class MockWebSocket {
|
||||
|
||||
vi.stubGlobal('WebSocket', MockWebSocket)
|
||||
|
||||
// Forza l'orientamento portrait → il controller usa il layout "mobile"
|
||||
// (con .team-pts, .btn-ctrl, ecc.) su cui questi test fanno asserzioni.
|
||||
Object.defineProperty(window, 'innerWidth', { value: 400, writable: true, configurable: true })
|
||||
Object.defineProperty(window, 'innerHeight', { value: 800, writable: true, configurable: true })
|
||||
|
||||
// Imposta il punteggio del set in corso costruendo una ris coerente.
|
||||
// `serv` ('h'|'g') controlla l'ultimo punto, quindi chi risulta al servizio.
|
||||
function setScore(wrapper, home, guest, serv = 'h') {
|
||||
const altro = serv === 'h' ? 'g' : 'h'
|
||||
const nAltro = serv === 'h' ? guest : home
|
||||
const nServ = serv === 'h' ? home : guest
|
||||
// mette per ultimo il carattere del battitore desiderato
|
||||
wrapper.vm.state.sp.striscia.at(-1).ris = altro.repeat(nAltro) + serv.repeat(nServ)
|
||||
}
|
||||
|
||||
// Helper per creare il componente con stato personalizzato
|
||||
function mountController(stateOverrides = {}) {
|
||||
const wrapper = mount(ControllerPage, {
|
||||
@@ -124,7 +105,7 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe essere disabilitato se il punteggio non è 0-0', async () => {
|
||||
const wrapper = mountController()
|
||||
setScore(wrapper, 5, 0)
|
||||
wrapper.vm.state.sp.punt.home = 5
|
||||
await wrapper.vm.$nextTick()
|
||||
const btn = wrapper.findAll('.btn-ctrl').find(b => b.text().includes('Cambio Palla'))
|
||||
expect(btn.attributes('disabled')).toBeDefined()
|
||||
@@ -213,7 +194,8 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe generare "N pari" a punteggio uguale', () => {
|
||||
const wrapper = mountController()
|
||||
setScore(wrapper, 5, 5)
|
||||
wrapper.vm.state.sp.punt.home = 5
|
||||
wrapper.vm.state.sp.punt.guest = 5
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -223,7 +205,9 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe annunciare prima il punteggio di chi batte (home serve)', () => {
|
||||
const wrapper = mountController()
|
||||
setScore(wrapper, 15, 10, 'h')
|
||||
wrapper.vm.state.sp.punt.home = 15
|
||||
wrapper.vm.state.sp.punt.guest = 10
|
||||
wrapper.vm.state.sp.servHome = true
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -233,7 +217,9 @@ describe('ControllerPage.vue', () => {
|
||||
|
||||
it('dovrebbe annunciare prima il punteggio di chi batte (guest serve)', () => {
|
||||
const wrapper = mountController()
|
||||
setScore(wrapper, 10, 15, 'g')
|
||||
wrapper.vm.state.sp.punt.home = 10
|
||||
wrapper.vm.state.sp.punt.guest = 15
|
||||
wrapper.vm.state.sp.servHome = false
|
||||
wrapper.vm.wsConnected = true
|
||||
wrapper.vm.ws = { readyState: 1, send: vi.fn() }
|
||||
wrapper.vm.speak()
|
||||
@@ -242,51 +228,6 @@ describe('ControllerPage.vue', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// REFERTO (modal PARTITA FINITA)
|
||||
// =============================================
|
||||
describe('Referto', () => {
|
||||
// Porta il componente allo stato "partita finita" per home in 2/3
|
||||
function setPartitaFinita(wrapper) {
|
||||
wrapper.vm.state.modalitaPartita = '2/3'
|
||||
wrapper.vm.state.sp.striscia = [
|
||||
{ serv: 'h', ris: '', vinc: 'h' },
|
||||
{ serv: 'h', ris: '', vinc: null },
|
||||
]
|
||||
wrapper.vm.setVintoTeam = 'home'
|
||||
wrapper.vm.showSetVinto = true
|
||||
}
|
||||
|
||||
it('mostra il bottone REFERTO quando la partita è finita', async () => {
|
||||
const wrapper = mountController()
|
||||
setPartitaFinita(wrapper)
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.isPartitaFinita).toBe(true)
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
expect(btn).toBeDefined()
|
||||
})
|
||||
|
||||
it('il click su REFERTO invoca generaReferto con lo stato', async () => {
|
||||
const wrapper = mountController()
|
||||
setPartitaFinita(wrapper)
|
||||
await wrapper.vm.$nextTick()
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
await btn.trigger('click')
|
||||
expect(generaReferto).toHaveBeenCalledWith(wrapper.vm.state)
|
||||
})
|
||||
|
||||
it('NON mostra il bottone REFERTO a set vinto (partita non finita)', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.modalitaPartita = '3/5'
|
||||
wrapper.vm.setVintoTeam = 'home'
|
||||
wrapper.vm.showSetVinto = true
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.isPartitaFinita).toBe(false)
|
||||
const btn = wrapper.findAll('.btn-secondary').find(b => b.text().includes('REFERTO'))
|
||||
expect(btn).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// BARRA CONNESSIONE
|
||||
// =============================================
|
||||
@@ -311,4 +252,37 @@ describe('ControllerPage.vue', () => {
|
||||
expect(wrapper.find('.conn-bar').text()).toContain('Connesso')
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// OVERLAY SET FINITO
|
||||
// =============================================
|
||||
describe('Overlay set finito', () => {
|
||||
it('non mostra l\'overlay se setFinito è null', () => {
|
||||
const wrapper = mountController()
|
||||
expect(wrapper.find('.overlay').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('mostra l\'overlay quando setFinito è impostato', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.setFinito = { vincitore: 'home' }
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.find('.overlay').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('l\'overlay mostra il nome del vincitore del set', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.setFinito = { vincitore: 'home' }
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.find('.overlay').text()).toContain('Antoniana')
|
||||
})
|
||||
|
||||
it('click su CONFERMA invia l\'azione confermaSet', async () => {
|
||||
const wrapper = mountController()
|
||||
wrapper.vm.state.sp.setFinito = { vincitore: 'guest' }
|
||||
await wrapper.vm.$nextTick()
|
||||
const spy = vi.spyOn(wrapper.vm, 'sendAction')
|
||||
await wrapper.find('.btn-confirm').trigger('click')
|
||||
expect(spy).toHaveBeenCalledWith({ type: 'confermaSet' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,8 +75,8 @@ describe('DisplayPage.vue', () => {
|
||||
|
||||
it('dovrebbe aggiornare il punteggio quando lo stato cambia', async () => {
|
||||
const wrapper = mountDisplay()
|
||||
// il punteggio si ricava dalla striscia: 15 punti home + 12 guest
|
||||
wrapper.vm.state.sp.striscia.at(-1).ris = 'h'.repeat(15) + 'g'.repeat(12)
|
||||
wrapper.vm.state.sp.punt.home = 15
|
||||
wrapper.vm.state.sp.punt.guest = 12
|
||||
await wrapper.vm.$nextTick()
|
||||
const punti = wrapper.findAll('.punt')
|
||||
expect(punti[0].text()).toBe('15')
|
||||
@@ -192,4 +192,39 @@ describe('DisplayPage.vue', () => {
|
||||
expect(guestStyle).toContain('display: none')
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// OVERLAY PARTITA FINITA
|
||||
// =============================================
|
||||
describe('Overlay partita finita', () => {
|
||||
it('non mostra l\'overlay se partitaFinita è null', () => {
|
||||
const wrapper = mountDisplay()
|
||||
expect(wrapper.find('.partita-finita-overlay').exists()).toBe(false)
|
||||
})
|
||||
|
||||
it('mostra l\'overlay quando partitaFinita è impostato', async () => {
|
||||
const wrapper = mountDisplay()
|
||||
wrapper.vm.state.sp.partitaFinita = { vincitore: 'home' }
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.find('.partita-finita-overlay').exists()).toBe(true)
|
||||
})
|
||||
|
||||
it('l\'overlay mostra il nome del vincitore della partita', async () => {
|
||||
const wrapper = mountDisplay()
|
||||
wrapper.vm.state.sp.nomi = { home: 'Antoniana', guest: 'Rivali' }
|
||||
wrapper.vm.state.sp.partitaFinita = { vincitore: 'guest' }
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.find('.partita-finita-overlay').text()).toContain('Rivali')
|
||||
})
|
||||
|
||||
it('l\'overlay mostra il punteggio dei set', async () => {
|
||||
const wrapper = mountDisplay()
|
||||
wrapper.vm.state.sp.set = { home: 3, guest: 1 }
|
||||
wrapper.vm.state.sp.partitaFinita = { vincitore: 'home' }
|
||||
await wrapper.vm.$nextTick()
|
||||
const text = wrapper.find('.partita-finita-overlay').text()
|
||||
expect(text).toContain('3')
|
||||
expect(text).toContain('1')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,174 +0,0 @@
|
||||
// @vitest-environment happy-dom
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
import { mount } from '@vue/test-utils'
|
||||
import { createWsMixin } from '../../src/wsMixin.js'
|
||||
import { createInitialState } from '../../src/gameState.js'
|
||||
|
||||
// WebSocket mock controllabile: i gestori (onopen/onmessage/onclose) vengono
|
||||
// assegnati dal mixin e li invochiamo manualmente nei test.
|
||||
class MockWebSocket {
|
||||
static CONNECTING = 0
|
||||
static OPEN = 1
|
||||
static CLOSING = 2
|
||||
static CLOSED = 3
|
||||
constructor(url) {
|
||||
this.url = url
|
||||
this.readyState = MockWebSocket.CONNECTING
|
||||
this.send = vi.fn()
|
||||
this.close = vi.fn()
|
||||
MockWebSocket.instances.push(this)
|
||||
}
|
||||
}
|
||||
MockWebSocket.instances = []
|
||||
|
||||
vi.stubGlobal('WebSocket', MockWebSocket)
|
||||
|
||||
// Monta un componente che usa il mixin. `extra` permette di aggiungere hook.
|
||||
function mountWith(role = 'controller', extra = {}) {
|
||||
const Comp = {
|
||||
mixins: [createWsMixin(role)],
|
||||
template: '<div></div>',
|
||||
...extra,
|
||||
}
|
||||
return mount(Comp)
|
||||
}
|
||||
|
||||
function ultimaWs() {
|
||||
return MockWebSocket.instances.at(-1)
|
||||
}
|
||||
|
||||
describe('createWsMixin (wsMixin.js)', () => {
|
||||
beforeEach(() => {
|
||||
MockWebSocket.instances = []
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('computed derivati', () => {
|
||||
it('punt/servHome/set delegano alle funzioni pure sulla striscia', async () => {
|
||||
const wrapper = mountWith()
|
||||
wrapper.vm.state.sp.striscia = [
|
||||
{ serv: 'h', ris: 'h', vinc: 'h' },
|
||||
{ serv: 'h', ris: 'h'.repeat(10) + 'g'.repeat(8), vinc: null },
|
||||
]
|
||||
await wrapper.vm.$nextTick()
|
||||
expect(wrapper.vm.punt).toEqual({ home: 10, guest: 8 })
|
||||
expect(wrapper.vm.set).toEqual({ home: 1, guest: 0 })
|
||||
// ultimo punto 'g' → serve guest
|
||||
expect(wrapper.vm.servHome).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('connessione', () => {
|
||||
it('apre una WebSocket verso /ws al mount', () => {
|
||||
mountWith()
|
||||
const ws = ultimaWs()
|
||||
expect(ws).toBeDefined()
|
||||
expect(ws.url).toMatch(/^ws:\/\/.+\/ws$/)
|
||||
})
|
||||
|
||||
it('invia il messaggio register all\'apertura', () => {
|
||||
const wrapper = mountWith('controller')
|
||||
const ws = ultimaWs()
|
||||
ws.readyState = MockWebSocket.OPEN
|
||||
ws.onopen()
|
||||
expect(ws.send).toHaveBeenCalledTimes(1)
|
||||
const msg = JSON.parse(ws.send.mock.calls[0][0])
|
||||
expect(msg).toEqual({ type: 'register', role: 'controller' })
|
||||
expect(wrapper.vm.wsConnected).toBe(true)
|
||||
})
|
||||
|
||||
it('un messaggio "state" aggiorna lo stato locale', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const nuovo = createInitialState()
|
||||
nuovo.sp.nomi.home = 'Nuova Squadra'
|
||||
ws.onmessage({ data: JSON.stringify({ type: 'state', state: nuovo }) })
|
||||
expect(wrapper.vm.state.sp.nomi.home).toBe('Nuova Squadra')
|
||||
})
|
||||
|
||||
it('un messaggio non-state invoca l\'hook onWsMessage', () => {
|
||||
const onWsMessage = vi.fn()
|
||||
const wrapper = mountWith('display', { methods: { onWsMessage } })
|
||||
const ws = ultimaWs()
|
||||
ws.onmessage({ data: JSON.stringify({ type: 'speak', text: 'ciao' }) })
|
||||
expect(onWsMessage).toHaveBeenCalledWith({ type: 'speak', text: 'ciao' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('riconnessione', () => {
|
||||
it('scheduleReconnect usa backoff esponenziale con cap a 30s', () => {
|
||||
const wrapper = mountWith()
|
||||
const delays = []
|
||||
const spy = vi.spyOn(globalThis, 'setTimeout').mockImplementation(() => 123)
|
||||
// intercetta i delay leggendoli dalle chiamate
|
||||
spy.mockImplementation((_fn, d) => { delays.push(d); return 123 })
|
||||
|
||||
wrapper.vm.reconnectAttempts = 0
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 1000
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 2000
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect() // 4000
|
||||
expect(delays).toEqual([1000, 2000, 4000])
|
||||
|
||||
// attempts alto → cap a 30000
|
||||
delays.length = 0
|
||||
wrapper.vm.reconnectAttempts = 20
|
||||
wrapper.vm.reconnectTimeout = null
|
||||
wrapper.vm.scheduleReconnect()
|
||||
expect(delays[0]).toBe(30000)
|
||||
|
||||
spy.mockRestore()
|
||||
})
|
||||
|
||||
it('non riconnette su chiusura pulita (1000/1001)', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const spy = vi.spyOn(wrapper.vm, 'scheduleReconnect')
|
||||
ws.onclose({ code: 1000 })
|
||||
ws.onclose({ code: 1001 })
|
||||
expect(spy).not.toHaveBeenCalled()
|
||||
expect(wrapper.vm.wsConnected).toBe(false)
|
||||
})
|
||||
|
||||
it('riconnette su chiusura anomala (es. 1006)', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
const spy = vi.spyOn(wrapper.vm, 'scheduleReconnect')
|
||||
ws.onclose({ code: 1006 })
|
||||
expect(spy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('sendWs', () => {
|
||||
it('ritorna false se non connesso', () => {
|
||||
const wrapper = mountWith()
|
||||
wrapper.vm.wsConnected = false
|
||||
expect(wrapper.vm.sendWs({ type: 'action' })).toBe(false)
|
||||
})
|
||||
|
||||
it('serializza e invia se connesso e aperto', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
ws.readyState = MockWebSocket.OPEN
|
||||
wrapper.vm.wsConnected = true
|
||||
const ok = wrapper.vm.sendWs({ type: 'action', action: { type: 'incPunt' } })
|
||||
expect(ok).toBe(true)
|
||||
const inviato = JSON.parse(ws.send.mock.calls.at(-1)[0])
|
||||
expect(inviato.type).toBe('action')
|
||||
})
|
||||
})
|
||||
|
||||
describe('cleanup', () => {
|
||||
it('beforeUnmount chiude la WebSocket', () => {
|
||||
const wrapper = mountWith()
|
||||
const ws = ultimaWs()
|
||||
wrapper.unmount()
|
||||
expect(ws.close).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -17,8 +17,7 @@ test.describe('Accessibility (a11y)', () => {
|
||||
});
|
||||
|
||||
test('Controller: non dovrebbe avere violazioni critiche a11y', async ({ page }) => {
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
await page.goto('http://localhost:3000/controller');
|
||||
await page.goto('http://localhost:3001');
|
||||
await page.waitForTimeout(500);
|
||||
|
||||
const results = await new AxeBuilder({ page })
|
||||
@@ -43,8 +42,7 @@ test.describe('Accessibility (a11y)', () => {
|
||||
});
|
||||
|
||||
test('Controller: i touch target dovrebbero avere dimensione minima', async ({ page }) => {
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
await page.goto('http://localhost:3000/controller');
|
||||
await page.goto('http://localhost:3001');
|
||||
await page.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Controlla che i bottoni principali abbiano dimensione minima 44x44px
|
||||
@@ -59,8 +57,7 @@ test.describe('Accessibility (a11y)', () => {
|
||||
});
|
||||
|
||||
test('Controller: i bottoni punteggio dovrebbero avere dimensione adeguata', async ({ page }) => {
|
||||
await page.setViewportSize({ width: 390, height: 844 });
|
||||
await page.goto('http://localhost:3000/controller');
|
||||
await page.goto('http://localhost:3001');
|
||||
await page.waitForSelector('.conn-bar.connected');
|
||||
|
||||
const scoreButtons = page.locator('.team-score');
|
||||
|
||||
@@ -7,8 +7,7 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
await expect(displayPage).toHaveTitle(/Segnapunti/);
|
||||
await expect(controllerPage).toHaveTitle(/Controller/);
|
||||
@@ -16,8 +15,7 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
|
||||
test('il punteggio iniziale dovrebbe essere 0-0', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
// Attende la connessione WebSocket
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
@@ -33,8 +31,7 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
// Attende la connessione WebSocket del controller
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
@@ -45,11 +42,6 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(200);
|
||||
|
||||
// Click +1 Home
|
||||
@@ -68,8 +60,7 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
@@ -79,11 +70,6 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(200);
|
||||
|
||||
// Click +1 Guest
|
||||
@@ -102,8 +88,7 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
@@ -113,11 +98,6 @@ test.describe('Basic Flow: Controller ↔ Display', () => {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(200);
|
||||
|
||||
// Home +1, Guest +1, Home +1
|
||||
|
||||
@@ -7,11 +7,6 @@ async function resetGame(controllerPage) {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(300);
|
||||
}
|
||||
|
||||
@@ -41,8 +36,7 @@ test.describe('Full Match Simulation', () => {
|
||||
|
||||
test('Partita 2/3: Home vince 2 set a 0', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
@@ -70,8 +64,7 @@ test.describe('Full Match Simulation', () => {
|
||||
|
||||
test('Set decisivo 2/3: vittoria a 15 punti', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
@@ -108,8 +101,7 @@ test.describe('Full Match Simulation', () => {
|
||||
|
||||
test('Set normale: punti oltre 25 fino ai vantaggi', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
|
||||
@@ -7,11 +7,6 @@ async function resetGame(controllerPage) {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(300);
|
||||
}
|
||||
|
||||
@@ -19,8 +14,7 @@ test.describe('Game Operations', () => {
|
||||
|
||||
test('Undo: dovrebbe annullare l\'ultimo punto', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
@@ -38,8 +32,7 @@ test.describe('Game Operations', () => {
|
||||
|
||||
test('Reset: dovrebbe azzerare tutto dopo conferma', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Imposta qualche punto
|
||||
@@ -61,8 +54,7 @@ test.describe('Game Operations', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Apri config
|
||||
@@ -92,8 +84,7 @@ test.describe('Game Operations', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Inizialmente mostra punteggio, non formazione
|
||||
@@ -112,8 +103,7 @@ test.describe('Game Operations', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Inizialmente la striscia è visibile
|
||||
@@ -135,8 +125,7 @@ test.describe('Game Operations', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
@@ -168,8 +157,7 @@ test.describe('Game Operations', () => {
|
||||
|
||||
test('Cambi: dovrebbe mostrare errore per giocatore non in formazione', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
|
||||
@@ -7,8 +7,7 @@ test.describe('Game Simulation', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
|
||||
// Selettori (basati su ID ipotetici o classi, adattali al tuo HTML reale)
|
||||
// Assumo che nel DOM ci siano elementi con ID o classi riconoscibili
|
||||
@@ -24,12 +23,6 @@ test.describe('Game Simulation', () => {
|
||||
await btnConfirmReset.click();
|
||||
}
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(200);
|
||||
|
||||
// 2. Loop per vincere il primo set (25 punti)
|
||||
// In ControllerPage.vue, il click su .team-score.home-bg incrementa i punti home
|
||||
@@ -62,15 +55,15 @@ test.describe('Game Simulation', () => {
|
||||
// 2. Cliccare "SET HOME".
|
||||
// 3. Verificare che Set Home = 1.
|
||||
|
||||
// A 25-0 compare automaticamente il dialog "SET VINTO"
|
||||
// Verifica che siamo a 25
|
||||
await expect(controllerPage.locator('.team-score.home-bg .team-pts')).toHaveText('25');
|
||||
await controllerPage.waitForSelector('.dialog-winner');
|
||||
|
||||
// Procedi al set successivo: registra il set vinto da Home
|
||||
await controllerPage.getByText('VAI AL SET SUCCESSIVO').click();
|
||||
await controllerPage.waitForTimeout(300);
|
||||
// Clicca bottone SET
|
||||
const btnSetHome = controllerPage.locator('.btn-set.home-bg');
|
||||
await btnSetHome.click();
|
||||
|
||||
// Verifica che il set Home sia incrementato a 1
|
||||
// Verifica che il set sia incrementato
|
||||
// Nota: display potrebbe chiamarsi diversamente, controlliamo Controller per coerenza
|
||||
await expect(controllerPage.locator('.team-score.home-bg .team-set')).toContainText('SET 1');
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,11 +7,6 @@ async function resetGame(controllerPage) {
|
||||
if (await btnConfirm.isVisible()) {
|
||||
await btnConfirm.click();
|
||||
}
|
||||
// doReset apre automaticamente il dialog di configurazione: chiudilo
|
||||
const cfgCancel = controllerPage.locator('.dialog-config .btn-cancel');
|
||||
if (await cfgCancel.isVisible()) {
|
||||
await cfgCancel.click();
|
||||
}
|
||||
await controllerPage.waitForTimeout(300);
|
||||
}
|
||||
|
||||
@@ -21,8 +16,7 @@ test.describe('Visual Regression', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
const displayPage = await context.newPage();
|
||||
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
@@ -41,8 +35,7 @@ test.describe('Visual Regression', () => {
|
||||
const controllerPage = await context.newPage();
|
||||
const displayPage = await context.newPage();
|
||||
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await displayPage.goto('http://localhost:3000');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
@@ -66,8 +59,7 @@ test.describe('Visual Regression', () => {
|
||||
|
||||
test('Controller: screenshot stato iniziale', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
await resetGame(controllerPage);
|
||||
@@ -79,8 +71,7 @@ test.describe('Visual Regression', () => {
|
||||
|
||||
test('Controller: screenshot con modal config aperta', async ({ context }) => {
|
||||
const controllerPage = await context.newPage();
|
||||
await controllerPage.setViewportSize({ width: 390, height: 844 });
|
||||
await controllerPage.goto('http://localhost:3000/controller');
|
||||
await controllerPage.goto('http://localhost:3001');
|
||||
await controllerPage.waitForSelector('.conn-bar.connected');
|
||||
|
||||
// Apri config
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
import { describe, it, expect, beforeAll, afterAll } from 'vitest'
|
||||
import { mkdtempSync, writeFileSync, rmSync } from 'fs'
|
||||
import { tmpdir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { createApp } from '../../server.js'
|
||||
|
||||
// Usiamo una dist temporanea con file marcatori, così il test non dipende da
|
||||
// una build reale ed è deterministico.
|
||||
let server
|
||||
let baseUrl
|
||||
let distDir
|
||||
|
||||
beforeAll(async () => {
|
||||
distDir = mkdtempSync(join(tmpdir(), 'segnapunti-dist-'))
|
||||
writeFileSync(join(distDir, 'index.html'), 'DISPLAY_MARKER')
|
||||
writeFileSync(join(distDir, 'controller.html'), 'CONTROLLER_MARKER')
|
||||
writeFileSync(join(distDir, 'app.js'), 'ASSET_MARKER')
|
||||
|
||||
const app = createApp(distDir)
|
||||
await new Promise((resolve) => {
|
||||
server = app.listen(0, () => {
|
||||
baseUrl = `http://127.0.0.1:${server.address().port}`
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
afterAll(async () => {
|
||||
await new Promise((resolve) => server.close(resolve))
|
||||
rmSync(distDir, { recursive: true, force: true })
|
||||
})
|
||||
|
||||
async function get(path) {
|
||||
const res = await fetch(baseUrl + path)
|
||||
return { status: res.status, body: await res.text() }
|
||||
}
|
||||
|
||||
describe('Routing server (server.js)', () => {
|
||||
it('GET / serve la pagina display', async () => {
|
||||
const { status, body } = await get('/')
|
||||
expect(status).toBe(200)
|
||||
expect(body).toBe('DISPLAY_MARKER')
|
||||
})
|
||||
|
||||
it('GET /display serve la pagina display', async () => {
|
||||
expect((await get('/display')).body).toBe('DISPLAY_MARKER')
|
||||
})
|
||||
|
||||
it('GET /display/qualsiasi serve comunque la pagina display (SPA)', async () => {
|
||||
expect((await get('/display/foo/bar')).body).toBe('DISPLAY_MARKER')
|
||||
})
|
||||
|
||||
it('GET /controller serve la pagina controller', async () => {
|
||||
expect((await get('/controller')).body).toBe('CONTROLLER_MARKER')
|
||||
})
|
||||
|
||||
it('GET /controller/qualsiasi serve comunque la pagina controller', async () => {
|
||||
expect((await get('/controller/foo')).body).toBe('CONTROLLER_MARKER')
|
||||
})
|
||||
|
||||
it('serve gli asset statici dalla dist', async () => {
|
||||
const { status, body } = await get('/app.js')
|
||||
expect(status).toBe(200)
|
||||
expect(body).toBe('ASSET_MARKER')
|
||||
})
|
||||
})
|
||||
@@ -1,10 +1,13 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
||||
import { setupWebSocketHandler } from '../../src/websocket-handler.js'
|
||||
import { punteggio } from '../../src/gameState.js'
|
||||
import { EventEmitter } from 'events'
|
||||
import { salvaPartita } from '../../src/db.js'
|
||||
|
||||
// Il punteggio non è memorizzato nello stato: si ricava dalla striscia.
|
||||
const puntHome = (state) => punteggio(state.sp.striscia).home
|
||||
// Mock di db.js: evita connessioni reali al DB SQLite durante i test.
|
||||
// vi.mock è automaticamente hoistato da Vitest all'inizio del file.
|
||||
vi.mock('../../src/db.js', () => ({
|
||||
salvaPartita: vi.fn().mockReturnValue(42n),
|
||||
}))
|
||||
|
||||
// Mock parziale di una WebSocket e del Server
|
||||
class MockWebSocket extends EventEmitter {
|
||||
@@ -107,7 +110,7 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
|
||||
expect(controller.send).toHaveBeenCalled()
|
||||
const sentMsg = lastSent(controller)
|
||||
expect(sentMsg.type).toBe('state')
|
||||
expect(puntHome(sentMsg.state)).toBe(1)
|
||||
expect(sentMsg.state.sp.punt.home).toBe(1)
|
||||
})
|
||||
|
||||
it('dovrebbe impedire al display di inviare azioni', () => {
|
||||
@@ -186,8 +189,8 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
|
||||
const msg1 = lastSent(display1)
|
||||
const msg2 = lastSent(display2)
|
||||
expect(msg1.type).toBe('state')
|
||||
expect(puntHome(msg1.state)).toBe(1)
|
||||
expect(puntHome(msg2.state)).toBe(1)
|
||||
expect(msg1.state.sp.punt.home).toBe(1)
|
||||
expect(msg2.state.sp.punt.home).toBe(1)
|
||||
})
|
||||
|
||||
it('non dovrebbe inviare a client con readyState != OPEN', () => {
|
||||
@@ -306,7 +309,7 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
|
||||
|
||||
const msg = lastSent(controller)
|
||||
expect(msg.type).toBe('state')
|
||||
expect(puntHome(msg.state)).toBe(1)
|
||||
expect(msg.state.sp.punt.home).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -378,15 +381,15 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
|
||||
describe('API pubblica', () => {
|
||||
it('getState dovrebbe restituire lo stato corrente', () => {
|
||||
const state = handler.getState()
|
||||
expect(puntHome(state)).toBe(0)
|
||||
expect(punteggio(state.sp.striscia).guest).toBe(0)
|
||||
expect(state.sp.punt.home).toBe(0)
|
||||
expect(state.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('setState dovrebbe sovrascrivere lo stato', () => {
|
||||
const newState = handler.getState()
|
||||
newState.sp.striscia.at(-1).ris = 'hh'
|
||||
newState.sp.punt.home = 99
|
||||
handler.setState(newState)
|
||||
expect(puntHome(handler.getState())).toBe(2)
|
||||
expect(handler.getState().sp.punt.home).toBe(99)
|
||||
})
|
||||
|
||||
it('broadcastState dovrebbe inviare a tutti i client', () => {
|
||||
@@ -404,4 +407,72 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
|
||||
expect(handler.getClients().size).toBe(1)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// SALVATAGGIO DB
|
||||
// =============================================
|
||||
describe('Salvataggio DB', () => {
|
||||
// Helper: inietta uno stato con setFinito già impostato e N set vinti da home
|
||||
function injectPreFinaleState(setHomeVinti, modalita = '3/5') {
|
||||
const base = handler.getState()
|
||||
handler.setState({
|
||||
...base,
|
||||
modalitaPartita: modalita,
|
||||
sp: {
|
||||
...base.sp,
|
||||
set: { home: setHomeVinti, guest: 0 },
|
||||
punt: { home: 25, guest: 0 },
|
||||
setFinito: { vincitore: 'home' },
|
||||
partitaFinita: null,
|
||||
storicoServizio: [],
|
||||
strisce: [],
|
||||
striscia: { home: [0], guest: [" "] },
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
vi.mocked(salvaPartita).mockClear()
|
||||
})
|
||||
|
||||
it('salvaPartita viene chiamata quando confermaSet porta partitaFinita a non-null', () => {
|
||||
// 3/5: servono 3 set → inietta 2 già vinti + setFinito, poi confermaSet
|
||||
injectPreFinaleState(2, '3/5')
|
||||
const controller = connectAndRegister(wss, 'controller')
|
||||
controller.emit('message', JSON.stringify({ type: 'action', action: { type: 'confermaSet' } }))
|
||||
expect(salvaPartita).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('salvaPartita NON viene chiamata per azioni normali (incPunt)', () => {
|
||||
const controller = connectAndRegister(wss, 'controller')
|
||||
controller.emit('message', JSON.stringify({ type: 'action', action: { type: 'incPunt', team: 'home' } }))
|
||||
expect(salvaPartita).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('salvaPartita NON viene chiamata se partitaFinita era già impostata', () => {
|
||||
// Inietta stato con partitaFinita già presente
|
||||
const base = handler.getState()
|
||||
handler.setState({
|
||||
...base,
|
||||
sp: { ...base.sp, partitaFinita: { vincitore: 'home' } },
|
||||
})
|
||||
const controller = connectAndRegister(wss, 'controller')
|
||||
// Qualsiasi azione non dovrebbe triggerare un secondo salvataggio
|
||||
controller.emit('message', JSON.stringify({ type: 'action', action: { type: 'incPunt', team: 'home' } }))
|
||||
expect(salvaPartita).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('se salvaPartita lancia un errore il broadcast avviene comunque', () => {
|
||||
vi.mocked(salvaPartita).mockImplementationOnce(() => { throw new Error('DB error') })
|
||||
injectPreFinaleState(2, '3/5')
|
||||
const display = connectAndRegister(wss, 'display')
|
||||
const controller = connectAndRegister(wss, 'controller')
|
||||
display.send.mockClear()
|
||||
controller.emit('message', JSON.stringify({ type: 'action', action: { type: 'confermaSet' } }))
|
||||
// Il broadcast deve avvenire anche se il DB ha fallito
|
||||
expect(display.send).toHaveBeenCalled()
|
||||
const msg = lastSent(display)
|
||||
expect(msg.type).toBe('state')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
|
||||
import { setupWebSocketHandler } from '../../src/websocket-handler.js'
|
||||
import { punteggio } from '../../src/gameState.js'
|
||||
import { EventEmitter } from 'events'
|
||||
|
||||
// Il punteggio si ricava dalla striscia, non è memorizzato nello stato.
|
||||
const punt = (state) => punteggio(state.sp.striscia)
|
||||
|
||||
class MockWebSocket extends EventEmitter {
|
||||
constructor() {
|
||||
super()
|
||||
@@ -61,7 +57,7 @@ describe('Stress Test WebSocket', () => {
|
||||
expect(display.send).toHaveBeenCalled()
|
||||
const msg = JSON.parse(display.send.mock.calls[display.send.mock.calls.length - 1][0])
|
||||
expect(msg.type).toBe('state')
|
||||
expect(punt(msg.state).home).toBe(1)
|
||||
expect(msg.state.sp.punt.home).toBe(1)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -85,11 +81,11 @@ describe('Stress Test WebSocket', () => {
|
||||
// Lo stato finale dipende da checkVittoria che blocca a 25+2
|
||||
// Home arriva a 25-0 → vittoria → blocca. Quindi punti home = 25
|
||||
const state = handler.getState()
|
||||
expect(punt(state).home).toBe(25)
|
||||
expect(state.sp.punt.home).toBe(25)
|
||||
// Guest: non può segnare dopo vittoria? No, checkVittoria blocca solo il team che ha vinto?
|
||||
// Controlliamo: checkVittoria controlla ENTRAMBI i team.
|
||||
// A 25-0 → vittoria=true → incPunt per guest è anche bloccato
|
||||
expect(punt(state).guest).toBe(0)
|
||||
expect(state.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe garantire che tutti i display ricevano ogni update sotto carico', () => {
|
||||
@@ -116,7 +112,7 @@ describe('Stress Test WebSocket', () => {
|
||||
// Verifica stato finale su tutti i display
|
||||
for (const display of displays) {
|
||||
const lastMsg = JSON.parse(display.send.mock.calls[4][0])
|
||||
expect(punt(lastMsg.state).home).toBe(5)
|
||||
expect(lastMsg.state.sp.punt.home).toBe(5)
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest'
|
||||
|
||||
// Il modulo db.js apre il DB a livello di modulo (top-level).
|
||||
// Per isolarlo usiamo un DB in memoria: vi.stubEnv + vi.resetModules + import dinamico.
|
||||
let salvaPartita, getPartite, getPartita
|
||||
|
||||
beforeAll(async () => {
|
||||
vi.stubEnv('DB_PATH', ':memory:')
|
||||
vi.resetModules()
|
||||
const mod = await import('../../src/db.js')
|
||||
salvaPartita = mod.salvaPartita
|
||||
getPartite = mod.getPartite
|
||||
getPartita = mod.getPartita
|
||||
})
|
||||
|
||||
afterAll(() => {
|
||||
vi.unstubAllEnvs()
|
||||
})
|
||||
|
||||
// Stato minimo valido da passare a salvaPartita
|
||||
function makeState({ vincitore = 'home', setHome = 3, setGuest = 1, strisce = [] } = {}) {
|
||||
return {
|
||||
modalitaPartita: '3/5',
|
||||
sp: {
|
||||
nomi: { home: 'Antoniana', guest: 'Ospiti' },
|
||||
set: { home: setHome, guest: setGuest },
|
||||
partitaFinita: vincitore ? { vincitore } : null,
|
||||
strisce,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
describe('db.js', () => {
|
||||
|
||||
// =============================================
|
||||
// salvaPartita
|
||||
// =============================================
|
||||
describe('salvaPartita', () => {
|
||||
it('ritorna un ID numerico intero positivo', () => {
|
||||
const id = salvaPartita(makeState())
|
||||
expect(typeof id).toBe('number')
|
||||
expect(id).toBeGreaterThan(0)
|
||||
expect(Number.isInteger(id)).toBe(true)
|
||||
})
|
||||
|
||||
it('IDs sono incrementali su inserimenti multipli', () => {
|
||||
const id1 = salvaPartita(makeState())
|
||||
const id2 = salvaPartita(makeState())
|
||||
expect(id2).toBeGreaterThan(id1)
|
||||
})
|
||||
|
||||
it('il record ha i campi corretti: modalita, nomi, set, vincitore', () => {
|
||||
const state = makeState({ vincitore: 'guest', setHome: 1, setGuest: 3 })
|
||||
const id = salvaPartita(state)
|
||||
const row = getPartita(Number(id))
|
||||
|
||||
expect(row.modalita).toBe('3/5')
|
||||
expect(row.nome_home).toBe('Antoniana')
|
||||
expect(row.nome_guest).toBe('Ospiti')
|
||||
expect(row.set_home).toBe(1)
|
||||
expect(row.set_guest).toBe(3)
|
||||
expect(row.vincitore).toBe('guest')
|
||||
})
|
||||
|
||||
it('il campo json è una stringa JSON parsabile', () => {
|
||||
const id = salvaPartita(makeState())
|
||||
const row = getPartita(Number(id))
|
||||
expect(() => JSON.parse(row.json)).not.toThrow()
|
||||
})
|
||||
|
||||
it('il JSON contiene nomi, set, strisce, vincitore e data', () => {
|
||||
const strisce = [{ set: 1, vincitore: 'home', punt: { home: 25, guest: 20 } }]
|
||||
const state = makeState({ strisce })
|
||||
const id = salvaPartita(state)
|
||||
const row = getPartita(Number(id))
|
||||
const json = JSON.parse(row.json)
|
||||
|
||||
expect(json.nomi).toEqual({ home: 'Antoniana', guest: 'Ospiti' })
|
||||
expect(json.set).toEqual({ home: 3, guest: 1 })
|
||||
expect(json.vincitore).toBe('home')
|
||||
expect(json.strisce).toEqual(strisce)
|
||||
expect(typeof json.data).toBe('string')
|
||||
expect(() => new Date(json.data)).not.toThrow()
|
||||
})
|
||||
|
||||
it('vincitore nel DB è null se partitaFinita è null', () => {
|
||||
const state = makeState({ vincitore: null })
|
||||
const id = salvaPartita(state)
|
||||
const row = getPartita(Number(id))
|
||||
expect(row.vincitore).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// getPartite
|
||||
// =============================================
|
||||
describe('getPartite', () => {
|
||||
it('ritorna tutte le partite inserite', () => {
|
||||
const prima = getPartite().length
|
||||
salvaPartita(makeState())
|
||||
salvaPartita(makeState())
|
||||
const dopo = getPartite()
|
||||
expect(dopo.length).toBe(prima + 2)
|
||||
})
|
||||
|
||||
it('ordina per ID discendente (più recente prima)', () => {
|
||||
const id1 = Number(salvaPartita(makeState()))
|
||||
const id2 = Number(salvaPartita(makeState()))
|
||||
const partite = getPartite()
|
||||
const ids = partite.map(p => p.id)
|
||||
const idx1 = ids.indexOf(id1)
|
||||
const idx2 = ids.indexOf(id2)
|
||||
// id2 (inserito dopo) deve apparire prima (indice minore)
|
||||
expect(idx2).toBeLessThan(idx1)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// getPartita
|
||||
// =============================================
|
||||
describe('getPartita', () => {
|
||||
it('ritorna undefined per ID inesistente', () => {
|
||||
const result = getPartita(999999)
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
it('ritorna il record corretto per ID valido', () => {
|
||||
const id = Number(salvaPartita(makeState({ vincitore: 'home', setHome: 3, setGuest: 0 })))
|
||||
const row = getPartita(id)
|
||||
expect(row).toBeDefined()
|
||||
expect(row.id).toBe(id)
|
||||
expect(row.set_home).toBe(3)
|
||||
expect(row.set_guest).toBe(0)
|
||||
})
|
||||
|
||||
it('il record ha tutti i campi attesi', () => {
|
||||
const id = Number(salvaPartita(makeState()))
|
||||
const row = getPartita(id)
|
||||
const campi = ['id', 'data', 'modalita', 'nome_home', 'nome_guest', 'set_home', 'set_guest', 'vincitore', 'json']
|
||||
for (const campo of campi) {
|
||||
expect(row).toHaveProperty(campo)
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
+422
-301
@@ -1,39 +1,5 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest'
|
||||
import {
|
||||
createInitialState, applyAction, checkVittoria, checkVittoriaPartita,
|
||||
punteggio, setVinti, servizio,
|
||||
} from '../../src/gameState.js'
|
||||
|
||||
// =============================================
|
||||
// HELPER
|
||||
// Lo stato non memorizza più punteggio/set/servizio direttamente:
|
||||
// si ricavano dalla striscia tramite le funzioni pure esportate.
|
||||
// =============================================
|
||||
|
||||
// Punteggio del set in corso
|
||||
const puntDi = (s) => punteggio(s.sp.striscia)
|
||||
// Set vinti nel match
|
||||
const setDi = (s) => setVinti(s.sp.striscia)
|
||||
// true se serve Home
|
||||
const servDi = (s) => servizio(s.sp.striscia)
|
||||
|
||||
// Imposta chi serve a inizio set (set in corso a 0-0)
|
||||
function setServizioIniziale(state, team) {
|
||||
state.sp.striscia.at(-1).serv = team === 'home' ? 'h' : 'g'
|
||||
}
|
||||
|
||||
// Imposta il punteggio del set in corso costruendo una ris coerente
|
||||
function setPunteggio(state, home, guest) {
|
||||
state.sp.striscia.at(-1).ris = 'h'.repeat(home) + 'g'.repeat(guest)
|
||||
}
|
||||
|
||||
// Aggiunge set già conclusi (vinti) PRIMA del set in corso
|
||||
function setSetVinti(state, home, guest) {
|
||||
const conclusi = []
|
||||
for (let i = 0; i < home; i++) conclusi.push({ serv: 'h', ris: '', vinc: 'h' })
|
||||
for (let i = 0; i < guest; i++) conclusi.push({ serv: 'g', ris: '', vinc: 'g' })
|
||||
state.sp.striscia = [...conclusi, state.sp.striscia.at(-1)]
|
||||
}
|
||||
import { createInitialState, applyAction, checkVittoria, checkVittoriaPartita } from '../../src/gameState.js'
|
||||
|
||||
describe('Game Logic (gameState.js)', () => {
|
||||
let state
|
||||
@@ -47,17 +13,17 @@ describe('Game Logic (gameState.js)', () => {
|
||||
// =============================================
|
||||
describe('Stato iniziale', () => {
|
||||
it('dovrebbe iniziare con 0-0', () => {
|
||||
expect(puntDi(state).home).toBe(0)
|
||||
expect(puntDi(state).guest).toBe(0)
|
||||
expect(state.sp.punt.home).toBe(0)
|
||||
expect(state.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe avere i set a 0', () => {
|
||||
expect(setDi(state).home).toBe(0)
|
||||
expect(setDi(state).guest).toBe(0)
|
||||
expect(state.sp.set.home).toBe(0)
|
||||
expect(state.sp.set.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe avere servizio Home', () => {
|
||||
expect(servDi(state)).toBe(true)
|
||||
expect(state.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('dovrebbe avere formazione di default [1-6]', () => {
|
||||
@@ -65,10 +31,14 @@ describe('Game Logic (gameState.js)', () => {
|
||||
expect(state.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('dovrebbe avere la striscia iniziale con un set vuoto', () => {
|
||||
expect(state.sp.striscia).toHaveLength(1)
|
||||
expect(state.sp.striscia[0].serv).toBe('h')
|
||||
expect(state.sp.striscia[0].ris).toBe('')
|
||||
it('dovrebbe avere la striscia iniziale a [0] per home e [" "] per guest', () => {
|
||||
// home serve per primo → home parte con [0], guest con [" "]
|
||||
expect(state.sp.striscia.home).toEqual([0])
|
||||
expect(state.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
|
||||
it('dovrebbe avere storico servizio vuoto', () => {
|
||||
expect(state.sp.storicoServizio).toEqual([])
|
||||
})
|
||||
|
||||
it('dovrebbe avere modalità 3/5 di default', () => {
|
||||
@@ -103,43 +73,43 @@ describe('Game Logic (gameState.js)', () => {
|
||||
describe('incPunt', () => {
|
||||
it('dovrebbe incrementare i punti Home', () => {
|
||||
const newState = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(puntDi(newState).home).toBe(1)
|
||||
expect(puntDi(newState).guest).toBe(0)
|
||||
expect(newState.sp.punt.home).toBe(1)
|
||||
expect(newState.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe incrementare i punti Guest', () => {
|
||||
const newState = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(puntDi(newState).guest).toBe(1)
|
||||
expect(puntDi(newState).home).toBe(0)
|
||||
expect(newState.sp.punt.guest).toBe(1)
|
||||
expect(newState.sp.punt.home).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe gestire il cambio palla (Guest segna, batteva Home)', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(servDi(s1)).toBe(false)
|
||||
expect(s1.sp.servHome).toBe(false)
|
||||
})
|
||||
|
||||
it('dovrebbe gestire il cambio palla (Home segna, batteva Guest)', () => {
|
||||
setServizioIniziale(state, 'guest')
|
||||
state.sp.servHome = false
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(servDi(s1)).toBe(true)
|
||||
expect(s1.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('non dovrebbe cambiare palla se segna chi batte', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(servDi(s1)).toBe(true)
|
||||
expect(s1.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('dovrebbe ruotare la formazione al cambio palla', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
state.sp.form.guest = ["1", "2", "3", "4", "5", "6"]
|
||||
const newState = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(newState.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
|
||||
})
|
||||
|
||||
it('non dovrebbe ruotare la formazione se non c\'è cambio palla', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
state.sp.form.home = ["1", "2", "3", "4", "5", "6"]
|
||||
const newState = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(newState.sp.form.home).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
@@ -147,25 +117,32 @@ describe('Game Logic (gameState.js)', () => {
|
||||
|
||||
it('dovrebbe aggiornare la striscia per punto Home', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.striscia.at(-1).ris).toBe('h')
|
||||
// Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
|
||||
expect(s.sp.striscia.home).toEqual([0, 1])
|
||||
expect(s.sp.striscia.guest).toEqual([" ", " "])
|
||||
})
|
||||
|
||||
it('dovrebbe aggiornare la striscia per punto Guest', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(s.sp.striscia.at(-1).ris).toBe('g')
|
||||
// Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
|
||||
expect(s.sp.striscia.guest).toEqual([" ", 1])
|
||||
expect(s.sp.striscia.home).toEqual([0, " "])
|
||||
})
|
||||
|
||||
it('dovrebbe registrare scorer nella striscia', () => {
|
||||
let s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'incPunt', team: 'guest' })
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.striscia.at(-1).ris).toBe('hgh')
|
||||
})
|
||||
|
||||
it('non dovrebbe incrementare i punti dopo vittoria', () => {
|
||||
setPunteggio(state, 25, 23)
|
||||
it('dovrebbe registrare lo storico servizio', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(puntDi(s).home).toBe(25)
|
||||
expect(s.sp.storicoServizio).toHaveLength(1)
|
||||
expect(s.sp.storicoServizio[0]).toHaveProperty('servHome')
|
||||
expect(s.sp.storicoServizio[0]).toHaveProperty('cambioPalla')
|
||||
})
|
||||
|
||||
it('non dovrebbe incrementare i punti dopo vittoria (setFinito impostato)', () => {
|
||||
// Il guard controlla setFinito: va impostato come farebbe il ciclo di gioco reale
|
||||
state.sp.punt.home = 25
|
||||
state.sp.punt.guest = 23
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.punt.home).toBe(25)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -176,33 +153,33 @@ describe('Game Logic (gameState.js)', () => {
|
||||
it('dovrebbe annullare l\'ultimo punto Home', () => {
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
const s2 = applyAction(s1, { type: 'decPunt' })
|
||||
expect(puntDi(s2).home).toBe(0)
|
||||
expect(puntDi(s2).guest).toBe(0)
|
||||
expect(s2.sp.punt.home).toBe(0)
|
||||
expect(s2.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe annullare l\'ultimo punto Guest', () => {
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
const s2 = applyAction(s1, { type: 'decPunt' })
|
||||
expect(puntDi(s2).home).toBe(0)
|
||||
expect(puntDi(s2).guest).toBe(0)
|
||||
expect(s2.sp.punt.home).toBe(0)
|
||||
expect(s2.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('non dovrebbe fare nulla sullo stato iniziale', () => {
|
||||
const s = applyAction(state, { type: 'decPunt' })
|
||||
expect(puntDi(s).home).toBe(0)
|
||||
expect(puntDi(s).guest).toBe(0)
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe ripristinare il servizio dopo undo con cambio palla', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(servDi(s1)).toBe(false)
|
||||
expect(s1.sp.servHome).toBe(false)
|
||||
const s2 = applyAction(s1, { type: 'decPunt' })
|
||||
expect(servDi(s2)).toBe(true)
|
||||
expect(s2.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('dovrebbe invertire la rotazione dopo undo con cambio palla', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
state.sp.servHome = true
|
||||
state.sp.form.guest = ["1", "2", "3", "4", "5", "6"]
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(s1.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
|
||||
@@ -213,7 +190,7 @@ describe('Game Logic (gameState.js)', () => {
|
||||
it('dovrebbe ripristinare la striscia', () => {
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
const s2 = applyAction(s1, { type: 'decPunt' })
|
||||
expect(s2.sp.striscia.at(-1).ris).toBe('')
|
||||
expect(s2.sp.striscia.home).toEqual([0])
|
||||
})
|
||||
|
||||
it('dovrebbe gestire undo multipli in sequenza', () => {
|
||||
@@ -221,66 +198,14 @@ describe('Game Logic (gameState.js)', () => {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'incPunt', team: 'guest' })
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
expect(puntDi(s).home).toBe(2)
|
||||
expect(puntDi(s).guest).toBe(1)
|
||||
expect(s.sp.punt.home).toBe(2)
|
||||
expect(s.sp.punt.guest).toBe(1)
|
||||
s = applyAction(s, { type: 'decPunt' })
|
||||
expect(puntDi(s).home).toBe(1)
|
||||
expect(s.sp.punt.home).toBe(1)
|
||||
s = applyAction(s, { type: 'decPunt' })
|
||||
expect(puntDi(s).guest).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
s = applyAction(s, { type: 'decPunt' })
|
||||
expect(puntDi(s).home).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// FORMAZIONE DI PARTENZA (formInizio)
|
||||
// =============================================
|
||||
describe('formInizio', () => {
|
||||
it('dovrebbe salvare la formazione corrente al primo punto del set', () => {
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.striscia.at(-1).formInizio).toEqual({
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
})
|
||||
})
|
||||
|
||||
it('formInizio è uno snapshot: la rotazione successiva non lo modifica', () => {
|
||||
setServizioIniziale(state, 'home')
|
||||
// 1° punto Home: nessun cambio palla, salva formInizio
|
||||
let s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
// 2° punto Guest: cambio palla → ruota la formazione guest
|
||||
s = applyAction(s, { type: 'incPunt', team: 'guest' })
|
||||
expect(s.sp.form.guest).toEqual(["2", "3", "4", "5", "6", "1"])
|
||||
// lo snapshot resta quello iniziale
|
||||
expect(s.sp.striscia.at(-1).formInizio.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('decPunt che riporta il set a 0-0 cancella formInizio', () => {
|
||||
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s1.sp.striscia.at(-1).formInizio).toBeDefined()
|
||||
const s2 = applyAction(s1, { type: 'decPunt' })
|
||||
expect(s2.sp.striscia.at(-1).formInizio).toBeUndefined()
|
||||
})
|
||||
|
||||
it('decPunt con punti ancora presenti NON cancella formInizio', () => {
|
||||
let s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'decPunt' })
|
||||
expect(s.sp.striscia.at(-1).ris).toBe('h')
|
||||
expect(s.sp.striscia.at(-1).formInizio).toBeDefined()
|
||||
})
|
||||
|
||||
it('ogni set mantiene la propria formInizio', () => {
|
||||
const custom = ['7', '8', '9', '10', '11', '12']
|
||||
let s = applyAction(state, { type: 'setFormazione', team: 'home', form: custom })
|
||||
// primo punto del set 1: salva formInizio custom
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
// chiude il set 1 e ne apre uno nuovo (formazioni resettate a default)
|
||||
s = applyAction(s, { type: 'nuovoSet', team: 'home' })
|
||||
// primo punto del set 2: salva formInizio default
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.striscia[0].formInizio.home).toEqual(custom)
|
||||
expect(s.sp.striscia.at(-1).formInizio.home).toEqual(['1', '2', '3', '4', '5', '6'])
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -290,134 +215,24 @@ describe('Game Logic (gameState.js)', () => {
|
||||
describe('incSet', () => {
|
||||
it('dovrebbe incrementare il set Home', () => {
|
||||
const s = applyAction(state, { type: 'incSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(1)
|
||||
expect(s.sp.set.home).toBe(1)
|
||||
})
|
||||
|
||||
it('dovrebbe incrementare il set Guest', () => {
|
||||
const s = applyAction(state, { type: 'incSet', team: 'guest' })
|
||||
expect(setDi(s).guest).toBe(1)
|
||||
expect(s.sp.set.guest).toBe(1)
|
||||
})
|
||||
|
||||
it('dovrebbe fare wrap da 2 a 0', () => {
|
||||
let s = applyAction(state, { type: 'incSet', team: 'home' })
|
||||
s = applyAction(s, { type: 'incSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(2)
|
||||
s = applyAction(s, { type: 'incSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(0)
|
||||
state.sp.set.home = 2
|
||||
const s = applyAction(state, { type: 'incSet', team: 'home' })
|
||||
expect(s.sp.set.home).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe incrementare da 1 a 2', () => {
|
||||
let s = applyAction(state, { type: 'incSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(1)
|
||||
s = applyAction(s, { type: 'incSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// NUOVO SET (nuovoSet)
|
||||
// =============================================
|
||||
describe('nuovoSet', () => {
|
||||
it('dovrebbe incrementare il set della squadra vincente', () => {
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(1)
|
||||
expect(setDi(s).guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe azzerare i punti nel nuovo set', () => {
|
||||
setPunteggio(state, 25, 10)
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(puntDi(s).home).toBe(0)
|
||||
expect(puntDi(s).guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe aggiungere un nuovo set vuoto alla striscia', () => {
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.striscia).toHaveLength(2)
|
||||
expect(s.sp.striscia.at(-1).ris).toBe('')
|
||||
expect(s.sp.striscia.at(-1).serv).toBe('h')
|
||||
})
|
||||
|
||||
it('dovrebbe conservare il set precedente nella striscia', () => {
|
||||
state.sp.striscia[0].ris = 'hgh'
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.striscia[0].ris).toBe('hgh')
|
||||
})
|
||||
|
||||
it('dovrebbe resettare le formazioni', () => {
|
||||
state.sp.form.home = ['7', '8', '9', '10', '11', '12']
|
||||
state.sp.form.guest = ['7', '8', '9', '10', '11', '12']
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.form.home).toEqual(['1', '2', '3', '4', '5', '6'])
|
||||
expect(s.sp.form.guest).toEqual(['1', '2', '3', '4', '5', '6'])
|
||||
})
|
||||
|
||||
it('dovrebbe ignorare team non valido', () => {
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'invalid' })
|
||||
expect(setDi(s).home).toBe(0)
|
||||
expect(setDi(s).guest).toBe(0)
|
||||
})
|
||||
|
||||
it('in 2/3 alla palla match registra il set vincente senza aprirne uno nuovo', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
setSetVinti(state, 1, 0)
|
||||
setPunteggio(state, 25, 18)
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(2)
|
||||
expect(puntDi(s).home).toBe(25)
|
||||
expect(puntDi(s).guest).toBe(18)
|
||||
})
|
||||
|
||||
it('in 3/5 alla palla match registra il set vincente senza aprirne uno nuovo', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
setSetVinti(state, 2, 0)
|
||||
setPunteggio(state, 25, 20)
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(3)
|
||||
expect(puntDi(s).home).toBe(25)
|
||||
expect(puntDi(s).guest).toBe(20)
|
||||
})
|
||||
|
||||
it('dovrebbe ignorare nuovoSet se la partita è già finita', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
setSetVinti(state, 2, 0)
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(setDi(s).home).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// VITTORIA PARTITA (checkVittoriaPartita)
|
||||
// =============================================
|
||||
describe('checkVittoriaPartita', () => {
|
||||
it('in 2/3 restituisce false se nessuno ha 2 set', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
setSetVinti(state, 1, 0)
|
||||
expect(checkVittoriaPartita(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('in 2/3 restituisce true se home ha 2 set', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
setSetVinti(state, 2, 0)
|
||||
expect(checkVittoriaPartita(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('in 3/5 restituisce false se nessuno ha 3 set', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
setSetVinti(state, 2, 2)
|
||||
expect(checkVittoriaPartita(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('in 3/5 restituisce true se guest ha 3 set', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
setSetVinti(state, 0, 3)
|
||||
expect(checkVittoriaPartita(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('in amichevole restituisce sempre false', () => {
|
||||
state.modalitaPartita = 'amichevole'
|
||||
setSetVinti(state, 5, 0)
|
||||
expect(checkVittoriaPartita(state)).toBe(false)
|
||||
state.sp.set.home = 1
|
||||
const s = applyAction(state, { type: 'incSet', team: 'home' })
|
||||
expect(s.sp.set.home).toBe(2)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -426,29 +241,27 @@ describe('Game Logic (gameState.js)', () => {
|
||||
// =============================================
|
||||
describe('cambiaPalla', () => {
|
||||
it('dovrebbe invertire il servizio a 0-0', () => {
|
||||
expect(servDi(state)).toBe(true)
|
||||
expect(state.sp.servHome).toBe(true)
|
||||
const s = applyAction(state, { type: 'cambiaPalla' })
|
||||
expect(servDi(s)).toBe(false)
|
||||
expect(s.sp.servHome).toBe(false)
|
||||
})
|
||||
|
||||
it('dovrebbe tornare a Home con doppio toggle', () => {
|
||||
let s = applyAction(state, { type: 'cambiaPalla' })
|
||||
s = applyAction(s, { type: 'cambiaPalla' })
|
||||
expect(servDi(s)).toBe(true)
|
||||
expect(s.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('non dovrebbe cambiare palla se il punteggio non è 0-0', () => {
|
||||
setPunteggio(state, 1, 0)
|
||||
const prima = servDi(state)
|
||||
state.sp.punt.home = 1
|
||||
const s = applyAction(state, { type: 'cambiaPalla' })
|
||||
expect(servDi(s)).toBe(prima)
|
||||
expect(s.sp.servHome).toBe(true)
|
||||
})
|
||||
|
||||
it('non dovrebbe cambiare palla se Guest ha punti', () => {
|
||||
setPunteggio(state, 0, 3)
|
||||
const prima = servDi(state)
|
||||
state.sp.punt.guest = 3
|
||||
const s = applyAction(state, { type: 'cambiaPalla' })
|
||||
expect(servDi(s)).toBe(prima)
|
||||
expect(s.sp.servHome).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
@@ -659,37 +472,44 @@ describe('Game Logic (gameState.js)', () => {
|
||||
// =============================================
|
||||
describe('checkVittoria', () => {
|
||||
it('non dovrebbe dare vittoria a 24-24', () => {
|
||||
setPunteggio(state, 24, 24)
|
||||
state.sp.punt.home = 24
|
||||
state.sp.punt.guest = 24
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('dovrebbe dare vittoria a 25-23', () => {
|
||||
setPunteggio(state, 25, 23)
|
||||
state.sp.punt.home = 25
|
||||
state.sp.punt.guest = 23
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('non dovrebbe dare vittoria a 25-24 (serve 2 punti di scarto)', () => {
|
||||
setPunteggio(state, 25, 24)
|
||||
state.sp.punt.home = 25
|
||||
state.sp.punt.guest = 24
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('dovrebbe dare vittoria a 26-24', () => {
|
||||
setPunteggio(state, 26, 24)
|
||||
state.sp.punt.home = 26
|
||||
state.sp.punt.guest = 24
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('dovrebbe dare vittoria Guest a 25-20', () => {
|
||||
setPunteggio(state, 20, 25)
|
||||
state.sp.punt.home = 20
|
||||
state.sp.punt.guest = 25
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('dovrebbe dare vittoria ai vantaggi (30-28)', () => {
|
||||
setPunteggio(state, 30, 28)
|
||||
state.sp.punt.home = 30
|
||||
state.sp.punt.guest = 28
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('non dovrebbe dare vittoria ai vantaggi senza scarto (28-27)', () => {
|
||||
setPunteggio(state, 28, 27)
|
||||
state.sp.punt.home = 28
|
||||
state.sp.punt.guest = 27
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -700,64 +520,82 @@ describe('Game Logic (gameState.js)', () => {
|
||||
describe('Set decisivo', () => {
|
||||
it('modalità 3/5: set decisivo dopo 4 set totali → vittoria a 15', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 2)
|
||||
setPunteggio(state, 15, 10)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('modalità 3/5: non vittoria a 14-10 nel set decisivo', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 2)
|
||||
setPunteggio(state, 14, 10)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
state.sp.punt.home = 14
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('modalità 3/5: vittoria a 15-13 nel set decisivo', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 2)
|
||||
setPunteggio(state, 15, 13)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 13
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('modalità 3/5: non vittoria a 15-14 nel set decisivo (serve scarto)', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 2)
|
||||
setPunteggio(state, 15, 14)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 14
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('modalità 3/5: vittoria a 16-14 nel set decisivo', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 2)
|
||||
setPunteggio(state, 16, 14)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
state.sp.punt.home = 16
|
||||
state.sp.punt.guest = 14
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('modalità 2/3: set decisivo dopo 2 set totali → vittoria a 15', () => {
|
||||
state.modalitaPartita = "2/3"
|
||||
setSetVinti(state, 1, 1)
|
||||
setPunteggio(state, 15, 10)
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 1
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(true)
|
||||
})
|
||||
|
||||
it('modalità 2/3: non vittoria a 14-10 nel set decisivo (soglia 15)', () => {
|
||||
it('modalità 2/3: non vittoria a 24-20 nel set decisivo (soglia 15)', () => {
|
||||
state.modalitaPartita = "2/3"
|
||||
setSetVinti(state, 1, 1)
|
||||
setPunteggio(state, 14, 10)
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 1
|
||||
state.sp.punt.home = 14
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('modalità 2/3: set non decisivo (1-0) → soglia 25', () => {
|
||||
state.modalitaPartita = "2/3"
|
||||
setSetVinti(state, 1, 0)
|
||||
setPunteggio(state, 15, 10)
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 0
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
|
||||
it('modalità 3/5: set non decisivo (2-1) → soglia 25', () => {
|
||||
state.modalitaPartita = "3/5"
|
||||
setSetVinti(state, 2, 1)
|
||||
setPunteggio(state, 15, 10)
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 1
|
||||
state.sp.punt.home = 15
|
||||
state.sp.punt.guest = 10
|
||||
expect(checkVittoria(state)).toBe(false)
|
||||
})
|
||||
})
|
||||
@@ -767,13 +605,15 @@ describe('Game Logic (gameState.js)', () => {
|
||||
// =============================================
|
||||
describe('Reset', () => {
|
||||
it('dovrebbe resettare punti e set a zero', () => {
|
||||
setSetVinti(state, 1, 1)
|
||||
setPunteggio(state, 10, 8)
|
||||
state.sp.punt.home = 10
|
||||
state.sp.punt.guest = 8
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 1
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(puntDi(s).home).toBe(0)
|
||||
expect(puntDi(s).guest).toBe(0)
|
||||
expect(setDi(s).home).toBe(0)
|
||||
expect(setDi(s).guest).toBe(0)
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
expect(s.sp.set.home).toBe(0)
|
||||
expect(s.sp.set.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe resettare formazioni a default', () => {
|
||||
@@ -783,11 +623,18 @@ describe('Game Logic (gameState.js)', () => {
|
||||
expect(s.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('dovrebbe resettare la striscia a un set vuoto', () => {
|
||||
state.sp.striscia = [{ serv: 'h', ris: 'hgh', vinc: 'h' }, { serv: 'h', ris: 'g', vinc: null }]
|
||||
it('dovrebbe resettare la striscia', () => {
|
||||
state.sp.striscia = { home: [0, 1, 2, 3], guest: [" ", " ", " ", 1] }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.striscia).toHaveLength(1)
|
||||
expect(s.sp.striscia[0].ris).toBe('')
|
||||
// servHome è true di default → home parte con [0], guest con [" "]
|
||||
expect(s.sp.striscia.home).toEqual([0])
|
||||
expect(s.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
|
||||
it('dovrebbe resettare lo storico servizio', () => {
|
||||
state.sp.storicoServizio = [{ servHome: true, cambioPalla: false }]
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.storicoServizio).toEqual([])
|
||||
})
|
||||
|
||||
it('dovrebbe impostare visuForm a false', () => {
|
||||
@@ -811,8 +658,282 @@ describe('Game Logic (gameState.js)', () => {
|
||||
describe('Azione sconosciuta', () => {
|
||||
it('dovrebbe restituire lo stato invariato con azione non riconosciuta', () => {
|
||||
const s = applyAction(state, { type: 'azioneInesistente' })
|
||||
expect(puntDi(s).home).toBe(0)
|
||||
expect(puntDi(s).guest).toBe(0)
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// FORMAZIONEINIZIOSET
|
||||
// =============================================
|
||||
describe('formInizioSet', () => {
|
||||
it('dovrebbe esistere nello stato iniziale con valori di default', () => {
|
||||
expect(state.sp.formInizioSet.home).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
expect(state.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
|
||||
it('setFormazione aggiorna sia form che formInizioSet per il team indicato', () => {
|
||||
const nuova = ["10", "11", "12", "13", "14", "15"]
|
||||
const s = applyAction(state, { type: 'setFormazione', team: 'home', form: nuova })
|
||||
expect(s.sp.form.home).toEqual(nuova)
|
||||
expect(s.sp.formInizioSet.home).toEqual(nuova)
|
||||
})
|
||||
|
||||
it('setFormazione non tocca formInizioSet dell\'altro team', () => {
|
||||
const nuova = ["10", "11", "12", "13", "14", "15"]
|
||||
const s = applyAction(state, { type: 'setFormazione', team: 'home', form: nuova })
|
||||
expect(s.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// CONFERMASET
|
||||
// =============================================
|
||||
describe('confermaSet', () => {
|
||||
// Helper: porta lo stato a fine set (home vince 25-0)
|
||||
function stateConSetFinito(modalita = '3/5') {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = modalita
|
||||
// Aggiungiamo 25 punti a home: a 24-0 il prossimo punto vince il set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
// Ora setFinito dovrebbe essere impostato
|
||||
return s
|
||||
}
|
||||
|
||||
it('non fa nulla se setFinito è null', () => {
|
||||
expect(state.sp.setFinito).toBeNull()
|
||||
const s = applyAction(state, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce).toEqual([])
|
||||
expect(s.sp.set.home).toBe(0)
|
||||
})
|
||||
|
||||
it('aggiunge una entry in strisce con i campi corretti', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.strisce).toHaveLength(1)
|
||||
const striscia = s.sp.strisce[0]
|
||||
expect(striscia).toHaveProperty('set')
|
||||
expect(striscia).toHaveProperty('formInizio')
|
||||
expect(striscia).toHaveProperty('home')
|
||||
expect(striscia).toHaveProperty('guest')
|
||||
expect(striscia).toHaveProperty('vincitore')
|
||||
expect(striscia).toHaveProperty('punt')
|
||||
})
|
||||
|
||||
it('il numero set è 1 per il primo set, 2 per il secondo', () => {
|
||||
let s = stateConSetFinito()
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[0].set).toBe(1)
|
||||
|
||||
// Secondo set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[1].set).toBe(2)
|
||||
})
|
||||
|
||||
it('formInizio nella striscia corrisponde a formInizioSet prima del conferma', () => {
|
||||
const formazioneInizio = ["7", "8", "9", "10", "11", "12"]
|
||||
let s = createInitialState()
|
||||
s = applyAction(s, { type: 'setFormazione', team: 'home', form: formazioneInizio })
|
||||
// porta a fine set
|
||||
for (let i = 0; i < 25; i++) {
|
||||
s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
}
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
expect(s.sp.strisce[0].formInizio.home).toEqual(formazioneInizio)
|
||||
})
|
||||
|
||||
it('incrementa set[vincitore]', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.set.home).toBe(1)
|
||||
expect(s.sp.set.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('resetta punt a 0', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('svuota storicoServizio', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.storicoServizio).toEqual([])
|
||||
})
|
||||
|
||||
it('azzera setFinito', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
|
||||
it('aggiorna formInizioSet con la form corrente post-conferma', () => {
|
||||
const preConferma = stateConSetFinito()
|
||||
// La form potrebbe essere ruotata durante il set
|
||||
const formDopoSet = [...preConferma.sp.form.home]
|
||||
const s = applyAction(preConferma, { type: 'confermaSet' })
|
||||
expect(s.sp.formInizioSet.home).toEqual(formDopoSet)
|
||||
})
|
||||
|
||||
it('NON imposta partitaFinita se la partita non è ancora vinta (3/5)', () => {
|
||||
const s = applyAction(stateConSetFinito('3/5'), { type: 'confermaSet' })
|
||||
// 1 set vinto su 3 necessari → partita non finita
|
||||
expect(s.sp.partitaFinita).toBeNull()
|
||||
})
|
||||
|
||||
it('imposta partitaFinita quando home vince 3 set (modalità 3/5)', () => {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = '3/5'
|
||||
// Vinci 3 set
|
||||
for (let set = 0; set < 3; set++) {
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
}
|
||||
expect(s.sp.partitaFinita).not.toBeNull()
|
||||
expect(s.sp.partitaFinita.vincitore).toBe('home')
|
||||
})
|
||||
|
||||
it('imposta partitaFinita quando home vince 2 set (modalità 2/3)', () => {
|
||||
let s = createInitialState()
|
||||
s.modalitaPartita = '2/3'
|
||||
for (let set = 0; set < 2; set++) {
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'home' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
}
|
||||
expect(s.sp.partitaFinita).not.toBeNull()
|
||||
expect(s.sp.partitaFinita.vincitore).toBe('home')
|
||||
})
|
||||
|
||||
it('resetta striscia per il set successivo con 0 per chi serve', () => {
|
||||
const s = applyAction(stateConSetFinito(), { type: 'confermaSet' })
|
||||
// servHome era true (home segna per primo → resta home a servire)
|
||||
expect(s.sp.striscia.home).toEqual([0])
|
||||
expect(s.sp.striscia.guest).toEqual([" "])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// GUARDIE setFinito / partitaFinita
|
||||
// =============================================
|
||||
describe('Guardie setFinito e partitaFinita', () => {
|
||||
it('incPunt non incrementa se setFinito è impostato', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'home' })
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
})
|
||||
|
||||
it('incPunt non incrementa se partitaFinita è impostata', () => {
|
||||
state.sp.partitaFinita = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'incPunt', team: 'guest' })
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('decPunt azzera setFinito se era impostato', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'decPunt' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// RESETTA — nuovi campi
|
||||
// =============================================
|
||||
describe('resetta (nuovi campi)', () => {
|
||||
it('azzera strisce', () => {
|
||||
state.sp.strisce = [{ set: 1, vincitore: 'home', punt: { home: 25, guest: 20 } }]
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.strisce).toEqual([])
|
||||
})
|
||||
|
||||
it('azzera setFinito', () => {
|
||||
state.sp.setFinito = { vincitore: 'home' }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.setFinito).toBeNull()
|
||||
})
|
||||
|
||||
it('azzera partitaFinita', () => {
|
||||
state.sp.partitaFinita = { vincitore: 'guest' }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.partitaFinita).toBeNull()
|
||||
})
|
||||
|
||||
it('reimposta formInizioSet ai valori di default', () => {
|
||||
state.sp.formInizioSet = { home: ["7", "8", "9", "10", "11", "12"], guest: ["7", "8", "9", "10", "11", "12"] }
|
||||
const s = applyAction(state, { type: 'resetta' })
|
||||
expect(s.sp.formInizioSet.home).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
expect(s.sp.formInizioSet.guest).toEqual(["1", "2", "3", "4", "5", "6"])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// checkVittoriaPartita
|
||||
// =============================================
|
||||
describe('checkVittoriaPartita', () => {
|
||||
it('ritorna null se nessuno ha vinto (3/5, 1-1)', () => {
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 1
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
|
||||
it('ritorna "home" con 3 set in modalità 3/5', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 3
|
||||
state.sp.set.guest = 1
|
||||
expect(checkVittoriaPartita(state)).toBe('home')
|
||||
})
|
||||
|
||||
it('ritorna "guest" con 3 set in modalità 3/5', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 0
|
||||
state.sp.set.guest = 3
|
||||
expect(checkVittoriaPartita(state)).toBe('guest')
|
||||
})
|
||||
|
||||
it('ritorna null con 2 set in modalità 3/5 (non ancora vinto)', () => {
|
||||
state.modalitaPartita = '3/5'
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 2
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
|
||||
it('ritorna "home" con 2 set in modalità 2/3', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 2
|
||||
state.sp.set.guest = 0
|
||||
expect(checkVittoriaPartita(state)).toBe('home')
|
||||
})
|
||||
|
||||
it('ritorna "guest" con 2 set in modalità 2/3', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 2
|
||||
expect(checkVittoriaPartita(state)).toBe('guest')
|
||||
})
|
||||
|
||||
it('ritorna null con 1 set in modalità 2/3 (non ancora vinto)', () => {
|
||||
state.modalitaPartita = '2/3'
|
||||
state.sp.set.home = 1
|
||||
state.sp.set.guest = 0
|
||||
expect(checkVittoriaPartita(state)).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// confermaSet con servHome=false
|
||||
// =============================================
|
||||
describe('confermaSet — striscia con servHome=false', () => {
|
||||
it('resetta striscia con guest a [0] se è guest a servire', () => {
|
||||
let s = createInitialState()
|
||||
// guest serve
|
||||
s = applyAction(s, { type: 'cambiaPalla' })
|
||||
expect(s.sp.servHome).toBe(false)
|
||||
// porta a fine set (guest segna 25 volte)
|
||||
for (let i = 0; i < 25; i++) s = applyAction(s, { type: 'incPunt', team: 'guest' })
|
||||
s = applyAction(s, { type: 'confermaSet' })
|
||||
// dopo il set, continua a servire guest
|
||||
expect(s.sp.striscia.guest).toEqual([0])
|
||||
expect(s.sp.striscia.home).toEqual([" "])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,71 +0,0 @@
|
||||
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
|
||||
|
||||
// Mock dell'I/O su disco: i test non toccano il filesystem reale né dipendono
|
||||
// dal path relativo a src/.
|
||||
vi.mock('fs', () => ({
|
||||
readFileSync: vi.fn(),
|
||||
writeFileSync: vi.fn(),
|
||||
mkdirSync: vi.fn(),
|
||||
existsSync: vi.fn(),
|
||||
}))
|
||||
|
||||
import * as fs from 'fs'
|
||||
import { loadState, saveState } from '../../src/persist.js'
|
||||
import { createInitialState } from '../../src/gameState.js'
|
||||
|
||||
describe('Persistenza stato (persist.js)', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks()
|
||||
})
|
||||
|
||||
describe('saveState', () => {
|
||||
it('crea la directory e scrive lo stato serializzato', () => {
|
||||
const state = createInitialState()
|
||||
saveState(state)
|
||||
|
||||
expect(fs.mkdirSync).toHaveBeenCalledWith(expect.any(String), { recursive: true })
|
||||
expect(fs.writeFileSync).toHaveBeenCalled()
|
||||
|
||||
const [, contenuto, encoding] = fs.writeFileSync.mock.calls[0]
|
||||
expect(JSON.parse(contenuto)).toEqual(state)
|
||||
expect(encoding).toBe('utf8')
|
||||
})
|
||||
|
||||
it('non lancia eccezioni se la scrittura fallisce', () => {
|
||||
fs.mkdirSync.mockImplementation(() => { throw new Error('EACCES') })
|
||||
const errSpy = vi.spyOn(console, 'error').mockImplementation(() => {})
|
||||
|
||||
expect(() => saveState(createInitialState())).not.toThrow()
|
||||
expect(errSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe('loadState', () => {
|
||||
it('legge e fa il parse di un file valido', () => {
|
||||
const salvato = createInitialState()
|
||||
salvato.sp.nomi.home = 'Squadra X'
|
||||
fs.existsSync.mockReturnValue(true)
|
||||
fs.readFileSync.mockReturnValue(JSON.stringify(salvato))
|
||||
|
||||
expect(loadState()).toEqual(salvato)
|
||||
})
|
||||
|
||||
it('ritorna lo stato iniziale se il file non esiste', () => {
|
||||
fs.existsSync.mockReturnValue(false)
|
||||
expect(loadState()).toEqual(createInitialState())
|
||||
})
|
||||
|
||||
it('ritorna lo stato iniziale se il JSON è corrotto', () => {
|
||||
fs.existsSync.mockReturnValue(true)
|
||||
fs.readFileSync.mockReturnValue('{ questo non è json')
|
||||
const warnSpy = vi.spyOn(console, 'warn').mockImplementation(() => {})
|
||||
|
||||
expect(loadState()).toEqual(createInitialState())
|
||||
expect(warnSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,110 +0,0 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { buildRefertoHtml } from '../../src/referto.js'
|
||||
import { createInitialState } from '../../src/gameState.js'
|
||||
|
||||
// Data fissa per asserzioni deterministiche
|
||||
const NOW = new Date('2026-03-14T20:30:00')
|
||||
|
||||
// Costruisce uno stato con una striscia di set arbitraria
|
||||
function statoConSet(striscia, extra = {}) {
|
||||
const state = createInitialState()
|
||||
state.sp.striscia = striscia
|
||||
state.sp.nomi = { home: 'Antoniana', guest: 'Rivali' }
|
||||
return { ...state, ...extra }
|
||||
}
|
||||
|
||||
describe('buildRefertoHtml (referto.js)', () => {
|
||||
it('esclude i set _phantom dal referto', () => {
|
||||
const striscia = [
|
||||
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(20), vinc: 'h' },
|
||||
{ serv: 'g', ris: '', vinc: 'g', _phantom: true },
|
||||
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(18), vinc: 'h' },
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
// due set reali → "Set 1" e "Set 2", mai "Set 3"
|
||||
expect(html).toContain('Set 1')
|
||||
expect(html).toContain('Set 2')
|
||||
expect(html).not.toContain('Set 3')
|
||||
})
|
||||
|
||||
it('calcola il punteggio finale di ogni set dalla ris', () => {
|
||||
const striscia = [
|
||||
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(20), vinc: 'h' },
|
||||
{ serv: 'h', ris: '', vinc: null },
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
// header del set: "Antoniana 25 · 20 Rivali"
|
||||
expect(html).toContain('<strong>25</strong>')
|
||||
expect(html).toContain('<strong>20</strong>')
|
||||
})
|
||||
|
||||
it('conta i set vinti usando vinc', () => {
|
||||
const striscia = [
|
||||
{ serv: 'h', ris: '', vinc: 'h' },
|
||||
{ serv: 'g', ris: '', vinc: 'g' },
|
||||
{ serv: 'h', ris: '', vinc: 'h' },
|
||||
{ serv: 'h', ris: '', vinc: null },
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
// risultato 2 – 1
|
||||
expect(html).toContain('2 – 1')
|
||||
})
|
||||
|
||||
it('ricava il vincitore dal conteggio punti se vinc è nullo', () => {
|
||||
const striscia = [
|
||||
{ serv: 'h', ris: 'h'.repeat(25) + 'g'.repeat(23), vinc: null },
|
||||
{ serv: 'h', ris: '', vinc: null },
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
// il primo set, pur con vinc null, conta come vinto da home → 1 – 0
|
||||
expect(html).toContain('1 – 0')
|
||||
})
|
||||
|
||||
it('include la progressione punto-punto con classi per squadra', () => {
|
||||
const striscia = [
|
||||
{ serv: 'h', ris: 'hhg', vinc: null },
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
expect(html).toContain('punto-h')
|
||||
expect(html).toContain('punto-g')
|
||||
expect(html).toContain('1-0')
|
||||
expect(html).toContain('2-0')
|
||||
expect(html).toContain('2-1')
|
||||
})
|
||||
|
||||
it('rende la formazione di partenza quando presente', () => {
|
||||
const striscia = [
|
||||
{
|
||||
serv: 'h', ris: 'h', vinc: null,
|
||||
formInizio: { home: ['4', '8', '15'], guest: ['16', '23', '42'] },
|
||||
},
|
||||
]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
expect(html).toContain('Formazione di partenza')
|
||||
expect(html).toContain('>4<')
|
||||
expect(html).toContain('>42<')
|
||||
})
|
||||
|
||||
it('mostra "non disponibile" se manca formInizio', () => {
|
||||
const striscia = [{ serv: 'h', ris: 'h', vinc: null }]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
expect(html).toContain('non disponibile')
|
||||
})
|
||||
|
||||
it('mostra "Nessun punto registrato" per un set senza punti', () => {
|
||||
const striscia = [{ serv: 'h', ris: '', vinc: null }]
|
||||
const html = buildRefertoHtml(statoConSet(striscia), NOW)
|
||||
expect(html).toContain('Nessun punto registrato')
|
||||
})
|
||||
|
||||
it('header contiene nomi squadre, modalità e data iniettata', () => {
|
||||
const striscia = [{ serv: 'h', ris: '', vinc: null }]
|
||||
const state = statoConSet(striscia)
|
||||
state.modalitaPartita = '2/3'
|
||||
const html = buildRefertoHtml(state, NOW)
|
||||
expect(html).toContain('Antoniana')
|
||||
expect(html).toContain('Rivali')
|
||||
expect(html).toContain('Modalità: 2/3')
|
||||
expect(html).toContain('14/03/2026')
|
||||
})
|
||||
})
|
||||
@@ -1,9 +1,14 @@
|
||||
import { describe, it, expect, vi, afterEach } from 'vitest'
|
||||
import { getNetworkIPs, collectIPs, printServerInfo } from '../../src/server-utils.js'
|
||||
import * as os from 'os'
|
||||
|
||||
// Nota: gli IP vengono iniettati nei test (oggetto stile os.networkInterfaces o
|
||||
// array di IP), così i risultati sono deterministici su qualsiasi piattaforma —
|
||||
// incluso WSL, dove getNetworkIPs() userebbe altrimenti PowerShell.
|
||||
vi.mock('os', async (importOriginal) => {
|
||||
return {
|
||||
...await importOriginal(),
|
||||
networkInterfaces: vi.fn(() => ({}))
|
||||
}
|
||||
})
|
||||
|
||||
import { getNetworkIPs, printServerInfo } from '../../src/server-utils.js'
|
||||
|
||||
describe('Server Utils', () => {
|
||||
|
||||
@@ -12,109 +17,140 @@ describe('Server Utils', () => {
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// getNetworkIPs / collectIPs
|
||||
// getNetworkIPs
|
||||
// =============================================
|
||||
describe('getNetworkIPs', () => {
|
||||
it('dovrebbe restituire indirizzi IPv4 non-loopback', () => {
|
||||
expect(getNetworkIPs({
|
||||
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
|
||||
})).toEqual(['192.168.1.100'])
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
eth0: [
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
|
||||
]
|
||||
})
|
||||
expect(getNetworkIPs()).toEqual(['192.168.1.100'])
|
||||
})
|
||||
|
||||
it('dovrebbe escludere indirizzi loopback (internal)', () => {
|
||||
const ips = getNetworkIPs({
|
||||
lo: [{ family: 'IPv4', internal: true, address: '127.0.0.1' }],
|
||||
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }]
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
lo: [
|
||||
{ family: 'IPv4', internal: true, address: '127.0.0.1' }
|
||||
],
|
||||
eth0: [
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
|
||||
]
|
||||
})
|
||||
const ips = getNetworkIPs()
|
||||
expect(ips).not.toContain('127.0.0.1')
|
||||
expect(ips).toContain('192.168.1.100')
|
||||
})
|
||||
|
||||
it('dovrebbe escludere indirizzi IPv6', () => {
|
||||
const ips = getNetworkIPs({
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
eth0: [
|
||||
{ family: 'IPv6', internal: false, address: 'fe80::1' },
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
|
||||
]
|
||||
})
|
||||
const ips = getNetworkIPs()
|
||||
expect(ips).toEqual(['192.168.1.100'])
|
||||
})
|
||||
|
||||
it('dovrebbe escludere bridge Docker 172.17.x.x', () => {
|
||||
const ips = getNetworkIPs({
|
||||
docker0: [{ family: 'IPv4', internal: false, address: '172.17.0.1' }],
|
||||
eth0: [{ family: 'IPv4', internal: false, address: '10.0.0.5' }]
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
docker0: [
|
||||
{ family: 'IPv4', internal: false, address: '172.17.0.1' }
|
||||
],
|
||||
eth0: [
|
||||
{ family: 'IPv4', internal: false, address: '10.0.0.5' }
|
||||
]
|
||||
})
|
||||
const ips = getNetworkIPs()
|
||||
expect(ips).not.toContain('172.17.0.1')
|
||||
expect(ips).toContain('10.0.0.5')
|
||||
})
|
||||
|
||||
it('dovrebbe escludere bridge Docker 172.18.x.x', () => {
|
||||
expect(getNetworkIPs({
|
||||
br0: [{ family: 'IPv4', internal: false, address: '172.18.0.1' }]
|
||||
})).toEqual([])
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
br0: [
|
||||
{ family: 'IPv4', internal: false, address: '172.18.0.1' }
|
||||
]
|
||||
})
|
||||
|
||||
it('dovrebbe escludere indirizzi link-local 169.254.x.x', () => {
|
||||
expect(getNetworkIPs({
|
||||
eth0: [{ family: 'IPv4', internal: false, address: '169.254.1.1' }]
|
||||
})).toEqual([])
|
||||
expect(getNetworkIPs()).toEqual([])
|
||||
})
|
||||
|
||||
it('dovrebbe restituire array vuoto se nessuna interfaccia disponibile', () => {
|
||||
expect(getNetworkIPs({})).toEqual([])
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
expect(getNetworkIPs()).toEqual([])
|
||||
})
|
||||
|
||||
it('dovrebbe restituire più indirizzi da interfacce diverse', () => {
|
||||
const ips = getNetworkIPs({
|
||||
eth0: [{ family: 'IPv4', internal: false, address: '192.168.1.100' }],
|
||||
wlan0: [{ family: 'IPv4', internal: false, address: '192.168.1.101' }]
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
eth0: [
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.100' }
|
||||
],
|
||||
wlan0: [
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.101' }
|
||||
]
|
||||
})
|
||||
const ips = getNetworkIPs()
|
||||
expect(ips).toHaveLength(2)
|
||||
expect(ips).toContain('192.168.1.100')
|
||||
expect(ips).toContain('192.168.1.101')
|
||||
})
|
||||
|
||||
it('collectIPs gestisce input vuoto/undefined senza errori', () => {
|
||||
expect(collectIPs()).toEqual([])
|
||||
expect(collectIPs({})).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// printServerInfo
|
||||
// =============================================
|
||||
describe('printServerInfo', () => {
|
||||
it('dovrebbe stampare la porta di default (3000)', () => {
|
||||
it('dovrebbe stampare le porte corrette (default)', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, [])
|
||||
printServerInfo()
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('5173')
|
||||
expect(allLogs).toContain('3001')
|
||||
expect(allLogs).toContain('3002')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('dovrebbe stampare le porte personalizzate', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, 4000)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('3000')
|
||||
expect(allLogs).toContain('/display')
|
||||
expect(allLogs).toContain('/controller')
|
||||
expect(allLogs).toContain('4000')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('dovrebbe stampare la porta personalizzata', () => {
|
||||
it('dovrebbe stampare storicoPort personalizzato', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(8080, [])
|
||||
printServerInfo(3000, 3001, 5000)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('8080')
|
||||
expect(allLogs).toContain('5000')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('dovrebbe mostrare gli URL remoti se ci sono IP di rete', () => {
|
||||
it('dovrebbe mostrare gli URL remoti per controller e storico', () => {
|
||||
os.networkInterfaces.mockReturnValue({
|
||||
eth0: [
|
||||
{ family: 'IPv4', internal: false, address: '192.168.1.50' }
|
||||
]
|
||||
})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, ['192.168.1.50'])
|
||||
printServerInfo(3000, 3001, 3002)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('192.168.1.50')
|
||||
expect(allLogs).toContain('remoti')
|
||||
expect(allLogs).toContain('3001')
|
||||
expect(allLogs).toContain('3002')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('non dovrebbe mostrare sezione remoti se nessun IP di rete', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, [])
|
||||
printServerInfo(3000, 3001)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).not.toContain('remoti')
|
||||
consoleSpy.mockRestore()
|
||||
|
||||
+173
-10
@@ -1,24 +1,36 @@
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { createServer as createHttpServer, request as httpRequest } from 'http'
|
||||
import { readFile } from 'fs'
|
||||
import { join, dirname } from 'path'
|
||||
import { fileURLToPath } from 'url'
|
||||
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
||||
import { printServerInfo } from './src/server-utils.js'
|
||||
import { loadState, saveState } from './src/persist.js'
|
||||
import { getPartite, getPartita } from './src/db.js'
|
||||
|
||||
const __dirname = dirname(fileURLToPath(import.meta.url))
|
||||
const CONTROLLER_PORT = 3001
|
||||
const STORICO_PORT = process.env.STORICO_PORT || 3002
|
||||
const DEV_PROXY_HOST = process.env.DEV_PROXY_HOST || '127.0.0.1'
|
||||
|
||||
/**
|
||||
* Plugin Vite che integra un server WebSocket per la gestione dello stato di gioco
|
||||
* e un server separato sulla porta 3001 per il controller.
|
||||
* @returns {import('vite').Plugin}
|
||||
*/
|
||||
export default function websocketPlugin() {
|
||||
return {
|
||||
name: 'vite-plugin-websocket',
|
||||
configureServer(server) {
|
||||
// Inizializza un server WebSocket collegato al server HTTP di Vite.
|
||||
const wss = new WebSocketServer({ noServer: true })
|
||||
setupWebSocketHandler(wss, { initialState: loadState(), onStateChange: saveState })
|
||||
|
||||
// Rewrite /display → / (index.html) e /controller → /controller.html
|
||||
server.middlewares.use((req, _res, next) => {
|
||||
if (req.url === '/display' || req.url === '/display/') req.url = '/'
|
||||
else if (req.url === '/controller' || req.url === '/controller/') req.url = '/controller.html'
|
||||
next()
|
||||
})
|
||||
// Registra i gestori WebSocket con la logica di gioco.
|
||||
setupWebSocketHandler(wss)
|
||||
|
||||
// Intercetta le richieste di upgrade WebSocket solo sul path /ws.
|
||||
server.httpServer.on('upgrade', (request, socket, head) => {
|
||||
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
||||
|
||||
if (pathname === '/ws') {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request)
|
||||
@@ -26,10 +38,161 @@ export default function websocketPlugin() {
|
||||
}
|
||||
})
|
||||
|
||||
// Avvia un server separato per il controller sulla porta 3001.
|
||||
server.httpServer.once('listening', () => {
|
||||
const { port } = server.httpServer.address()
|
||||
setTimeout(() => printServerInfo(port), 100)
|
||||
const viteAddr = server.httpServer.address()
|
||||
const vitePort = viteAddr.port
|
||||
|
||||
startControllerDevServer(vitePort, wss)
|
||||
startStoricoDevServer()
|
||||
|
||||
setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT, STORICO_PORT), 100)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Avvia il server di sviluppo per il controller.
|
||||
* Fa da proxy verso il dev server di Vite per moduli ES, HMR, e asset.
|
||||
*/
|
||||
function startControllerDevServer(vitePort, wss) {
|
||||
const controllerServer = createHttpServer((req, res) => {
|
||||
// Se richiesta alla root, riscrive verso controller.html
|
||||
let targetPath = req.url
|
||||
if (targetPath === '/' || targetPath === '') {
|
||||
targetPath = '/controller.html'
|
||||
}
|
||||
|
||||
// Proxy verso il dev server di Vite
|
||||
const proxyReq = httpRequest(
|
||||
{
|
||||
hostname: DEV_PROXY_HOST,
|
||||
port: vitePort,
|
||||
path: targetPath,
|
||||
method: req.method,
|
||||
headers: {
|
||||
...req.headers,
|
||||
host: `${DEV_PROXY_HOST}:${vitePort}`,
|
||||
},
|
||||
},
|
||||
(proxyRes) => {
|
||||
res.writeHead(proxyRes.statusCode, proxyRes.headers)
|
||||
proxyRes.pipe(res, { end: true })
|
||||
}
|
||||
)
|
||||
|
||||
proxyReq.on('error', (err) => {
|
||||
console.error('[Controller Proxy] Error:', err.message)
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(502)
|
||||
res.end('Proxy error')
|
||||
}
|
||||
})
|
||||
|
||||
req.pipe(proxyReq, { end: true })
|
||||
})
|
||||
|
||||
// Gestisce l'upgrade WebSocket anche sulla porta del controller
|
||||
controllerServer.on('upgrade', (request, socket, head) => {
|
||||
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
|
||||
|
||||
if (pathname === '/ws') {
|
||||
wss.handleUpgrade(request, socket, head, (ws) => {
|
||||
wss.emit('connection', ws, request)
|
||||
})
|
||||
} else {
|
||||
// Per l'HMR di Vite, proxare l'upgrade WebSocket verso Vite
|
||||
const proxyReq = httpRequest({
|
||||
hostname: DEV_PROXY_HOST,
|
||||
port: vitePort,
|
||||
path: request.url,
|
||||
method: 'GET',
|
||||
headers: request.headers,
|
||||
})
|
||||
|
||||
proxyReq.on('upgrade', (proxyRes, proxySocket, proxyHead) => {
|
||||
socket.write(
|
||||
`HTTP/1.1 101 Switching Protocols\r\n` +
|
||||
Object.entries(proxyRes.headers)
|
||||
.map(([k, v]) => `${k}: ${v}`)
|
||||
.join('\r\n') +
|
||||
'\r\n\r\n'
|
||||
)
|
||||
proxySocket.pipe(socket)
|
||||
socket.pipe(proxySocket)
|
||||
})
|
||||
|
||||
proxyReq.on('error', (err) => {
|
||||
console.error('[Controller Proxy] WS upgrade error:', err.message)
|
||||
socket.destroy()
|
||||
})
|
||||
|
||||
proxyReq.end()
|
||||
}
|
||||
})
|
||||
|
||||
controllerServer.listen(CONTROLLER_PORT, '0.0.0.0', () => {
|
||||
console.log(`[Controller] Dev server running on port ${CONTROLLER_PORT}`)
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Avvia il server di sviluppo per lo storico sulla porta 3002.
|
||||
* Serve storico.html e gli endpoint /api/partite.
|
||||
*/
|
||||
function startStoricoDevServer() {
|
||||
const storicoServer = createHttpServer((req, res) => {
|
||||
const url = new URL(req.url, `http://${req.headers.host}`)
|
||||
const pathname = url.pathname
|
||||
|
||||
if (pathname === '/api/partite') {
|
||||
try {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify(getPartite()))
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ error: err.message }))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
const matchId = pathname.match(/^\/api\/partite\/(\d+)$/)
|
||||
if (matchId) {
|
||||
try {
|
||||
const p = getPartita(Number(matchId[1]))
|
||||
if (!p) {
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ error: 'Not found' }))
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify(p))
|
||||
}
|
||||
} catch (err) {
|
||||
res.writeHead(500, { 'Content-Type': 'application/json' })
|
||||
res.end(JSON.stringify({ error: err.message }))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if (pathname === '/' || pathname === '') {
|
||||
readFile(join(__dirname, 'storico.html'), (err, data) => {
|
||||
if (err) {
|
||||
res.writeHead(500)
|
||||
res.end('Error loading storico.html')
|
||||
} else {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html' })
|
||||
res.end(data)
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
res.writeHead(404)
|
||||
res.end('Not found')
|
||||
})
|
||||
|
||||
storicoServer.listen(STORICO_PORT, '0.0.0.0', () => {
|
||||
console.log(`[Storico] http://localhost:${STORICO_PORT}`)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user