4 Commits

Author SHA1 Message Date
5621830803 feat(controller): aggiunge modal formazione a inizio set
Dopo la conferma di un set finito appare automaticamente un secondo
modal "FORMAZIONE SET N" con gli input (resettati a 1-6) per
registrare la formazione del set successivo. Al click INIZIA vengono
inviati setFormazione per home e guest, e la formazione viene
archiviata in formInizioSet (e quindi nel DB a fine partita via
strisce[].formInizio).

Se la conferma del set conclude la partita, il modal formazione viene
chiuso automaticamente non appena lo stato con partitaFinita arriva
dal server
2026-02-21 19:16:27 +01:00
b3faf06477 test: aggiunge copertura completa per le nuove funzionalità
- Nuovo db.test.js: 11 test per salvaPartita, getPartite, getPartita
  (isolamento con DB in memoria via vi.stubEnv + vi.resetModules)
- gameState.test.js: test per confermaSet, formInizioSet, partitaFinita,
  checkVittoriaPartita e guardie setFinito/partitaFinita; fix di 6 test
  pre-esistenti non allineati con la logica striscia aggiornata
- websocket.test.js: mock di db.js e 4 test per il salvataggio automatico
  su DB al termine della partita
- server-utils.test.js: 2 test aggiuntivi per storicoPort
- ControllerPage.test.js: 4 test per l'overlay di fine set (setFinito)
- DisplayPage.test.js: 4 test per l'overlay di fine partita (partitaFinita)
2026-02-21 18:59:50 +01:00
1df239ed3d feat: salva partite su SQLite e aggiunge storico in dev
- Aggiunge src/db.js con better-sqlite3: tabella partite con nomi,
  modalità, set, formazione di partenza per set, punteggi e vincitore
- Salvataggio automatico al termine della partita (websocket-handler.js)
- Aggiunge formInizioSet in gameState per tracciare la formazione
  iniziale di ogni set
- Aggiunge storico.html: pagina vanilla dark-theme con lista partite
  espandibili (set, punteggio, formazioni)
- Aggiunge server storico su porta 3002 in dev (vite-plugin-websocket.js)
- Aggiunge endpoint /api/partite su displayApp (server.js)
- Migliora banner di avvio con URL storico locale e da rete
- Migliora log WebSocket: connessione aperta, ruolo unregistered, client rimanenti
- Aggiorna .gitignore: ignora tutta la cartella data/
2026-02-21 18:36:58 +01:00
d9e1ac751f feat(partita): storico strisce multi-set, conferma fine set e avviso fine partita
- sp.strisce[] archivia la striscia di ogni set completato
- fine set rilevata automaticamente: controller mostra modale di conferma
- confermaSet salva il set, incrementa il contatore e prepara il successivo
- fine partita rilevata da checkVittoriaPartita(): display mostra overlay con
  vincitore e punteggio set; incPunt bloccato
- decPunt pulisce setFinito (undo dell'ultimo punto di vittoria)
- resetta azzera strisce[], setFinito e partitaFinita
2026-02-21 17:49:19 +01:00
21 changed files with 3342 additions and 1776 deletions

3
.gitignore vendored
View File

@@ -35,3 +35,6 @@ dist-ssr
# Vitest # Vitest
coverage/ coverage/
# Database SQLite
data/

255
README.md
View File

@@ -1,90 +1,98 @@
# Segnapunti Anto # Segnapunti Anto
Applicazione web **fullstack real-time** per il tracciamento dei punteggi di partite di pallavolo, installabile come PWA. Applicazione web **Progressive Web App (PWA)** per tracciare i punteggi di partite di pallavolo in tempo reale.
--- ---
## Panoramica ## Panoramica
**Segnapunti Anto** è un'applicazione fullstack per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone in contesto sportivo. **Segnapunti Anto** e un'applicazione digitale per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone.
### Architettura L'app e composta da due interfacce:
- **Display** (tabellone pubblico)
- **Controller** (pannello operatore)
Il sistema è composto da un **backend Node.js/Express** e due interfacce web separate: Le due interfacce condividono lo stato in tempo reale tramite WebSocket.
| Interfaccia | Porta | Ruolo | ### Funzionalita Principali
|-------------|-------|-------|
| **Display** | 3000 | Tabellone pubblico — mostra punteggi, formazioni e storico |
| **Controller** | 3001 | Pannello operatore — invia azioni e gestisce la partita |
Le due interfacce comunicano tramite **WebSocket** (`/ws`): ogni azione del Controller viene elaborata dal server e trasmessa in broadcast a tutti i client connessi. - **Gestione partita in tempo reale**
- Tracciamento punti home/guest
- Gestione set
- Indicatore servizio
- Storico punti (striscia)
- Blocchi logici quando il set e gia vinto
La logica di gioco risiede interamente **lato server** (`gameState.js`), con aggiornamenti di stato immutabili. Il frontend Vue 3 è puramente reattivo: riceve lo stato e lo visualizza senza gestirne la consistenza. - **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`
In produzione, entrambi i server sono gestiti da un unico processo Node.js (`server.js`) e l'intera applicazione è containerizzabile via Docker. Il frontend è installabile come **PWA** (service worker, manifest, modalità fullscreen landscape) per utilizzo kiosk su dispositivi sportivi. - **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
## Funzionalità - Toggle ordine squadre (inverti)
- Toggle visualizzazione punteggio/formazioni
### Gestione partita in tempo reale - Toggle striscia storico
- Tracciamento punti home/guest con indicatore di servizio - Sintesi vocale punteggio (Web Speech API)
- Gestione set e storico punti (striscia)
- Blocco azioni quando il set è già vinto
### Regole pallavolo integrate
- Set normali: vittoria a 25 con almeno 2 punti di scarto
- Set decisivo (5° set): vittoria a 15 con almeno 2 punti di scarto
- Modalità partita `2/3` o `3/5`
### Formazioni e cambi
- Gestione formazione a 6 giocatori per squadra
- Rotazione automatica al cambio palla
- Dialog cambi con validazione `IN → OUT`
### Controlli e personalizzazione
- Configurazione nomi squadre
- Inversione ordine di visualizzazione squadre
- Toggle punteggio/formazioni e visibilità striscia storico
- Sintesi vocale del punteggio (Web Speech API)
--- ---
## Requisiti ## Requisiti
### Ambiente di sviluppo ### Requisiti di Sistema
| Requisito | Versione minima | Consigliata | #### Per Sviluppo
|-----------|-----------------|-------------| - **Sistema Operativo**: Linux, macOS, Windows
| **Node.js** | `>= 18.19.0` | `24 LTS` | - **Node.js**: `>= 18.19.0` (consigliato `20 LTS`)
| **npm** | `>= 9` | — | - **npm**: `>= 9`
| **RAM** | 2 GB | 4 GB | - **RAM**: minimo 2GB (consigliato 4GB)
| **OS** | Linux, macOS, Windows | — |
### Test E2E #### Per Esecuzione Test E2E
- Browser Playwright installati (`chromium`, `firefox`)
- Su Linux, eventuali dipendenze sistema per browser headless
I test end-to-end richiedono i browser Playwright. Su Linux potrebbero essere necessarie dipendenze di sistema aggiuntive. Comandi utili:
```bash ```bash
node -v
npm -v
npx playwright install chromium firefox npx playwright install chromium firefox
# Linux (con dipendenze di sistema): # Linux, se necessario:
# npx playwright install --with-deps chromium firefox # npx playwright install --with-deps chromium firefox
``` ```
### Requisiti browser (utente finale) ### Requisiti Browser (Utente Finale)
| API | Utilizzo | Necessità | | Requisito | Dettaglio | Necessita |
|-----|----------|-----------| |-----------|-----------|-----------|
| JavaScript ES6+ | Moduli, async/await | Obbligatorio | | JavaScript ES6+ | Moduli, async/await | Obbligatorio |
| WebSocket | Sincronizzazione stato live | Obbligatorio | | WebSocket | Sincronizzazione stato live | Obbligatorio |
| Service Worker | Supporto PWA offline | Consigliato | | Service Worker API | Supporto PWA offline | Consigliato |
| Web Speech API | Annunci vocali punteggio | Opzionale | | Web Speech API | Annunci vocali | Opzionale |
**Browser testati:** Chrome/Chromium, Firefox, Mobile Chrome (Playwright Pixel 5). ### Browser Testati e Supportati
| Browser | Supporto | Note |
|---------|----------|------|
| Chrome/Chromium | ✅ | Completo |
| Firefox | ✅ | Completo |
| Mobile Chrome (Playwright Pixel 5) | ✅ | Copertura E2E mobile |
--- ---
## Installazione ## Installazione e Setup
### Prerequisiti
- Node.js `>= 18.19.0`
- npm `>= 9`
### Installazione
```bash ```bash
git clone https://santantonio.sytes.net/attilio/segnapunti.git git clone https://santantonio.sytes.net/attilio/segnapunti.git
@@ -94,120 +102,107 @@ npm install
--- ---
## Sviluppo ## Comandi per Sviluppo
### Dev server ### Dev Server
Avvia il server di sviluppo Vite:
```bash ```bash
npm run dev npm run dev
``` ```
Avvia il server Vite con hot reload: Accesso tipico in sviluppo:
- `http://localhost:5173/` Display - `http://localhost:5173/` -> Display
- `http://localhost:5173/controller.html` Controller - `http://localhost:5173/controller.html` -> Controller
### Build di produzione ### Modalita Sviluppo
- Hot reload attivo
- Build veloce lato Vite
- Buona per sviluppo UI/UX
---
## Comandi per Build
### Build Produzione
```bash ```bash
npm run build npm run build
``` ```
Genera la cartella `dist/` con asset ottimizzati, manifest e service worker PWA. Output:
- cartella `dist/`
- asset ottimizzati
- file PWA (manifest + service worker)
### Avvio in produzione (locale) ### Avvio Server Applicativo Locale (Display + Controller)
```bash ```bash
npm run serve npm run serve
``` ```
Espone i due server: Espone:
- `http://localhost:3000` Display - `http://localhost:3000` -> Display
- `http://localhost:3001` Controller - `http://localhost:3001` -> Controller
--- ### Altri comandi utili
## Terminal Controller (CLI)
Il CLI è un controller da terminale che si connette al server via WebSocket e permette di gestire la partita senza browser.
### Avvio
```bash ```bash
# Modalità produzione (server su porta 3000) npm run preview
npm run cli npm run start
# Modalità sviluppo (server Vite su porta 5173)
npm run cli:dev
# Porta custom
node cli.js <porta>
``` ```
Il CLI richiede che il server sia già in esecuzione in un altro terminale. ---
### Comandi disponibili ## Configurazione PWA
#### Punteggio L'app usa `vite-plugin-pwa` (vedi `vite.config.js`) con:
- `registerType: 'autoUpdate'`
- manifest installabile
- orientamento landscape
- modalita fullscreen
| Comando | Alias | Effetto | Caratteristiche principali:
|---------|-------|---------| - installabile su dispositivi supportati
| `punto casa` | `+`, `pc` | Assegna un punto alla squadra di casa | - aggiornamento automatico del service worker
| `punto ospite` | `-`, `po` | Assegna un punto alla squadra ospite | - supporto utilizzo offline (in base alle risorse cache)
| `undo` | `u` | Annulla l'ultimo punto assegnato |
#### Set
| Comando | Effetto |
|---------|---------|
| `set casa` | Incrementa il contatore set della squadra di casa |
| `set ospite` | Incrementa il contatore set della squadra ospite |
#### Partita
| Comando | Effetto |
|---------|---------|
| `serv` | Cambia il servizio (disponibile solo se il punteggio è 0-0) |
| `reset` | Resetta la partita — chiede conferma prima di procedere |
| `nomi <casa> <ospite>` | Imposta i nomi delle squadre (es. `nomi Antoniana Teate`) |
| `modalita 2/3` | Imposta la modalità best-of-3 |
| `modalita 3/5` | Imposta la modalità best-of-5 |
#### Informazioni
| Comando | Alias | Effetto |
|---------|-------|---------|
| `stato` | — | Mostra il punteggio corrente nel terminale |
| `help` | — | Mostra la lista dei comandi |
| `exit` | `q` | Chiude il CLI |
### Note
- **Tab**: completamento automatico dei comandi
- **Freccia su/giù**: navigazione nella history dei comandi (ultime 100 voci)
- Il Display nel browser si aggiorna in tempo reale ad ogni comando inviato
--- ---
## Test ## Logica Regolamentare Pallavolo
La suite di test copre tutti i livelli dell'applicazione: ### Vittoria Set
| Suite | Comando | Descrizione | - Set normali: vittoria a 25 con almeno 2 punti di scarto
|-------|---------|-------------| - Set decisivo: vittoria a 15 con almeno 2 punti di scarto
| Tutti | `npm run test:all` | Unit + integration + component + stress | - Modalita partita supportate: `2/3` e `3/5`
| Unit + integration | `npm run test:unit` | Logica di gioco e WebSocket |
| Component | `npm run test:component` | Componenti Vue |
| Stress | `npm run test:stress` | Load test WebSocket |
| E2E | `npm run test:e2e` | Playwright (chromium, firefox, mobile) |
Per la guida completa ai test, consultare [`tests/README.md`](tests/README.md). ### 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.
--- ---
## Docker ## Test (stato attuale)
Suite presenti:
- Unit
- Integration
- Component
- Stress
- E2E (Playwright)
Comandi principali:
```bash ```bash
docker-compose up --build npm run test:all
npm run test:e2e
``` ```
Espone le porte `3000` (Display) e `3001` (Controller). Guida completa test:
- `tests/README.md`

302
cli.js
View File

@@ -1,302 +0,0 @@
import { WebSocket } from 'ws';
import readline from 'readline';
// ---------------------------------------------------------------------------
// ANSI helpers
// ---------------------------------------------------------------------------
const c = {
reset: '\x1b[0m',
bold: '\x1b[1m',
dim: '\x1b[2m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
cyan: '\x1b[36m',
brightWhite: '\x1b[97m',
};
// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------
const port = process.argv[2] || process.env.PORT || 3000;
const url = `ws://localhost:${port}/ws`;
// ---------------------------------------------------------------------------
// State
// ---------------------------------------------------------------------------
let currentState = null;
let connected = false;
// ---------------------------------------------------------------------------
// Startup banner
// ---------------------------------------------------------------------------
console.log(`\n${c.bold}${c.cyan} Segnapunti Anto — Terminal Controller${c.reset}`);
console.log(`${c.dim} Connessione a ${url}...${c.reset}\n`);
// ---------------------------------------------------------------------------
// WebSocket
// ---------------------------------------------------------------------------
const ws = new WebSocket(url);
ws.on('error', (err) => {
console.error(`${c.red}Errore di connessione: ${err.message}${c.reset}`);
console.error(`${c.dim}Assicurati che il server sia avviato su ${url}${c.reset}`);
process.exit(1);
});
ws.on('open', () => {
connected = true;
console.log(` ${c.green}Connesso.${c.reset} Digita ${c.bold}help${c.reset} per i comandi disponibili.\n`);
ws.send(JSON.stringify({ type: 'register', role: 'controller' }));
});
ws.on('message', (data) => {
try {
const msg = JSON.parse(data.toString());
if (msg.type === 'state') {
currentState = msg.state;
printState(currentState);
} else if (msg.type === 'error') {
clearLine();
console.error(` ${c.red}Errore server: ${msg.message}${c.reset}`);
}
} catch {
// ignora messaggi malformati
}
rl.prompt(true);
});
ws.on('close', () => {
console.log(`\n${c.dim} Connessione chiusa.${c.reset}\n`);
process.exit(0);
});
// ---------------------------------------------------------------------------
// Output helpers
// ---------------------------------------------------------------------------
/** Cancella la riga corrente del terminale (evita di sovrascrivere il prompt). */
function clearLine() {
process.stdout.write('\r\x1b[K');
}
function printState(s) {
if (!s) return;
const { nomi, punt, set, servHome } = s.sp;
const homeServ = servHome ? `${c.yellow}${c.reset}` : ' ';
const guestServ = !servHome ? `${c.yellow}${c.reset}` : ' ';
const homeName = nomi.home.padEnd(15);
const guestName = nomi.guest.padEnd(15);
const homeScore = String(punt.home).padStart(3);
const guestScore = String(punt.guest).padStart(3);
clearLine();
console.log(
` ${homeServ} ${c.bold}${homeName}${c.reset}` +
`${c.brightWhite}${homeScore}${c.reset} ${c.dim}(set ${set.home})${c.reset}` +
` ${c.dim}${c.reset} ` +
`${c.brightWhite}${guestScore}${c.reset} ${c.dim}(set ${set.guest})${c.reset}` +
` ${c.bold}${guestName}${c.reset} ${guestServ}` +
` ${c.dim}[${s.modalitaPartita}]${c.reset}`
);
}
function printHelp() {
console.log(`
${c.bold} Punteggio${c.reset}
${c.cyan}+${c.reset} / ${c.cyan}pc${c.reset} Punto casa (shortcut)
${c.cyan}-${c.reset} / ${c.cyan}po${c.reset} Punto ospite (shortcut)
${c.cyan}punto casa${c.reset} Punto casa
${c.cyan}punto ospite${c.reset} Punto ospite
${c.cyan}undo${c.reset} / ${c.cyan}u${c.reset} Annulla ultimo punto
${c.bold} Set${c.reset}
${c.cyan}set casa${c.reset} Incrementa set casa
${c.cyan}set ospite${c.reset} Incrementa set ospite
${c.bold} Partita${c.reset}
${c.cyan}serv${c.reset} Cambia servizio (solo se 0-0)
${c.cyan}reset${c.reset} Resetta la partita (chiede conferma)
${c.cyan}nomi <casa> <ospite>${c.reset} Imposta nomi squadre
${c.cyan}modalita 2/3${c.reset} Imposta modalità best-of-3
${c.cyan}modalita 3/5${c.reset} Imposta modalità best-of-5
${c.bold} Informazioni${c.reset}
${c.cyan}stato${c.reset} Mostra punteggio attuale
${c.cyan}help${c.reset} Mostra questo aiuto
${c.cyan}exit${c.reset} / ${c.cyan}q${c.reset} Esci
${c.dim}Suggerimento: usa Tab per il completamento automatico dei comandi.${c.reset}
`);
}
// ---------------------------------------------------------------------------
// Command dispatch
// ---------------------------------------------------------------------------
function sendAction(action) {
if (!connected || ws.readyState !== WebSocket.OPEN) {
console.error(` ${c.red}Non connesso al server.${c.reset}`);
rl.prompt();
return;
}
ws.send(JSON.stringify({ type: 'action', action }));
}
function parseCommand(line) {
const parts = line.trim().split(/\s+/);
const cmd = parts[0].toLowerCase();
switch (cmd) {
case '+':
case 'pc':
sendAction({ type: 'incPunt', team: 'home' });
break;
case '-':
case 'po':
sendAction({ type: 'incPunt', team: 'guest' });
break;
case 'punto': {
const team = parts[1]?.toLowerCase();
if (team === 'casa' || team === 'home') {
sendAction({ type: 'incPunt', team: 'home' });
} else if (team === 'ospite' || team === 'guest') {
sendAction({ type: 'incPunt', team: 'guest' });
} else {
console.error(` ${c.red}Uso: punto casa | punto ospite${c.reset}`);
rl.prompt();
}
break;
}
case 'undo':
case 'u':
sendAction({ type: 'decPunt' });
break;
case 'set': {
const team = parts[1]?.toLowerCase();
if (team === 'casa' || team === 'home') {
sendAction({ type: 'incSet', team: 'home' });
} else if (team === 'ospite' || team === 'guest') {
sendAction({ type: 'incSet', team: 'guest' });
} else {
console.error(` ${c.red}Uso: set casa | set ospite${c.reset}`);
rl.prompt();
}
break;
}
case 'serv':
sendAction({ type: 'cambiaPalla' });
break;
case 'reset':
rl.question(` ${c.yellow}Confermi reset partita? (s/N) ${c.reset}`, (answer) => {
if (answer.trim().toLowerCase() === 's') {
sendAction({ type: 'resetta' });
} else {
console.log(` ${c.dim}Reset annullato.${c.reset}`);
rl.prompt();
}
});
return;
case 'nomi': {
const home = parts[1];
const guest = parts[2];
if (!home) {
console.error(` ${c.red}Uso: nomi <casa> <ospite>${c.reset}`);
rl.prompt();
break;
}
const payload = { type: 'setNomi', home };
if (guest) payload.guest = guest;
sendAction(payload);
break;
}
case 'modalita': {
const m = parts[1];
if (m !== '2/3' && m !== '3/5') {
console.error(` ${c.red}Uso: modalita 2/3 | modalita 3/5${c.reset}`);
rl.prompt();
break;
}
sendAction({ type: 'setModalita', modalita: m });
break;
}
case 'stato':
printState(currentState);
rl.prompt();
break;
case 'help':
printHelp();
rl.prompt();
break;
case 'exit':
case 'q':
ws.close();
break;
default:
console.error(
` ${c.red}Comando non riconosciuto: "${cmd}"${c.reset}` +
` — digita ${c.bold}help${c.reset} per la lista`
);
rl.prompt();
}
}
// ---------------------------------------------------------------------------
// REPL
// ---------------------------------------------------------------------------
const TAB_COMPLETIONS = [
'+', '-', 'pc', 'po',
'punto casa', 'punto ospite',
'undo', 'u',
'set casa', 'set ospite',
'serv',
'reset',
'nomi',
'modalita 2/3', 'modalita 3/5',
'stato', 'help',
'exit', 'q',
];
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: '> ',
historySize: 100,
completer(line) {
const hits = TAB_COMPLETIONS.filter((entry) => entry.startsWith(line));
return [hits.length ? hits : TAB_COMPLETIONS, line];
},
});
rl.on('line', (line) => {
if (!line.trim()) {
rl.prompt();
return;
}
parseCommand(line.trim());
});
rl.on('close', () => {
ws.close();
});

2956
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -9,8 +9,6 @@
"preview": "node server.js", "preview": "node server.js",
"start": "node server.js", "start": "node server.js",
"serve": "vite build && node server.js", "serve": "vite build && node server.js",
"cli": "node cli.js",
"cli:dev": "node cli.js 5173",
"test": "vitest", "test": "vitest",
"test:unit": "vitest run tests/unit tests/integration", "test:unit": "vitest run tests/unit tests/integration",
"test:component": "vitest run tests/component", "test:component": "vitest run tests/component",
@@ -22,27 +20,25 @@
"test:e2e:codegen": "playwright codegen --config=playwright.config.cjs" "test:e2e:codegen": "playwright codegen --config=playwright.config.cjs"
}, },
"dependencies": { "dependencies": {
"better-sqlite3": "^12.6.2",
"express": "^5.2.1", "express": "^5.2.1",
"vue": "^3.2.47", "vue": "^3.2.47",
"vue-router": "^4.6.4", "vue-router": "^4.6.4",
"wave-ui": "^3.3.0", "wave-ui": "^3.3.0",
"ws": "^8.19.0" "ws": "^8.19.0"
}, },
"overrides": {
"serialize-javascript": "^7.0.5"
},
"devDependencies": { "devDependencies": {
"@axe-core/playwright": "^4.11.1", "@axe-core/playwright": "^4.11.1",
"@playwright/test": "^1.58.2", "@playwright/test": "^1.58.2",
"@types/node": "^25.2.3", "@types/node": "^25.2.3",
"@vitejs/plugin-vue": "^6.0.5", "@vitejs/plugin-vue": "^4.1.0",
"@vitest/ui": "^4.0.18", "@vitest/ui": "^4.0.18",
"@vue/test-utils": "^2.4.6", "@vue/test-utils": "^2.4.6",
"concurrently": "^9.2.1", "concurrently": "^9.2.1",
"happy-dom": "^20.6.1", "happy-dom": "^20.6.1",
"jsdom": "^28.0.0", "jsdom": "^28.0.0",
"vite": "^7.3.1", "vite": "^4.3.9",
"vite-plugin-pwa": "^1.2.0", "vite-plugin-pwa": "^0.16.0",
"vitest": "^4.0.18" "vitest": "^4.0.18"
} }
} }

View File

@@ -5,6 +5,7 @@ import { fileURLToPath } from 'url'
import { dirname, join } from 'path' import { dirname, join } from 'path'
import { setupWebSocketHandler } from './src/websocket-handler.js' import { setupWebSocketHandler } from './src/websocket-handler.js'
import { printServerInfo } from './src/server-utils.js' import { printServerInfo } from './src/server-utils.js'
import { getPartite, getPartita } from './src/db.js'
const __filename = fileURLToPath(import.meta.url) const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename) const __dirname = dirname(__filename)
@@ -23,6 +24,20 @@ const displayApp = express()
// Espone i file generati dalla build di Vite. // Espone i file generati dalla build di Vite.
displayApp.use(express.static(join(__dirname, 'dist'))) 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 }) }
})
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 }) }
})
// Fallback per SPA: restituisce `index.html` per tutte le route. // Fallback per SPA: restituisce `index.html` per tutte le route.
displayApp.get(/.*/, (_req, res) => { displayApp.get(/.*/, (_req, res) => {
res.sendFile(join(__dirname, 'dist', 'index.html')) res.sendFile(join(__dirname, 'dist', 'index.html'))

View File

@@ -78,6 +78,64 @@
</div> </div>
</div> </div>
<!-- Finestra fine set -->
<div class="overlay" v-if="setFinito">
<div class="dialog">
<div class="dialog-title">SET FINITO</div>
<div class="set-finito-info">
<div class="set-vincitore">{{ state.sp.nomi[setFinito.vincitore] }}</div>
<div class="set-score">{{ state.sp.punt.home }} {{ state.sp.punt.guest }}</div>
</div>
<div class="dialog-buttons">
<button class="btn btn-confirm" @click="confermaSetEApriFormazione()">CONFERMA</button>
</div>
</div>
</div>
<!-- Finestra formazione inizio set -->
<div class="overlay" v-if="showFormazioneModal">
<div class="dialog dialog-config">
<div class="dialog-title">
FORMAZIONE SET {{ state.sp.set.home + state.sp.set.guest + 1 }}
</div>
<div class="form-group">
<label>{{ state.sp.nomi.home }}</label>
<div class="form-grid">
<div class="form-row">
<input type="text" v-model="formazioneSetData.home[3]" class="input-num" />
<input type="text" v-model="formazioneSetData.home[2]" class="input-num" />
<input type="text" v-model="formazioneSetData.home[1]" class="input-num" />
</div>
<div class="form-line"></div>
<div class="form-row">
<input type="text" v-model="formazioneSetData.home[4]" class="input-num" />
<input type="text" v-model="formazioneSetData.home[5]" class="input-num" />
<input type="text" v-model="formazioneSetData.home[0]" class="input-num" />
</div>
</div>
</div>
<div class="form-group">
<label>{{ state.sp.nomi.guest }}</label>
<div class="form-grid">
<div class="form-row">
<input type="text" v-model="formazioneSetData.guest[3]" class="input-num" />
<input type="text" v-model="formazioneSetData.guest[2]" class="input-num" />
<input type="text" v-model="formazioneSetData.guest[1]" class="input-num" />
</div>
<div class="form-line"></div>
<div class="form-row">
<input type="text" v-model="formazioneSetData.guest[4]" class="input-num" />
<input type="text" v-model="formazioneSetData.guest[5]" class="input-num" />
<input type="text" v-model="formazioneSetData.guest[0]" class="input-num" />
</div>
</div>
</div>
<div class="dialog-buttons">
<button class="btn btn-confirm" @click="confermaFormazioneSet()">INIZIA</button>
</div>
</div>
</div>
<!-- Finestra configurazione --> <!-- Finestra configurazione -->
<div class="overlay" v-if="showConfig" @click.self="showConfig = false"> <div class="overlay" v-if="showConfig" @click.self="showConfig = false">
<div class="dialog dialog-config"> <div class="dialog dialog-config">
@@ -187,6 +245,11 @@ export default {
reconnectAttempts: 0, reconnectAttempts: 0,
maxReconnectDelay: 30000, maxReconnectDelay: 30000,
confirmReset: false, confirmReset: false,
showFormazioneModal: false,
formazioneSetData: {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
},
showConfig: false, showConfig: false,
showCambiTeam: false, showCambiTeam: false,
showCambi: false, showCambi: false,
@@ -209,6 +272,8 @@ export default {
visuStriscia: true, visuStriscia: true,
modalitaPartita: "3/5", modalitaPartita: "3/5",
sp: { sp: {
strisce: [],
setFinito: null,
striscia: { home: [0], guest: [0] }, striscia: { home: [0], guest: [0] },
servHome: true, servHome: true,
punt: { home: 0, guest: 0 }, punt: { home: 0, guest: 0 },
@@ -227,6 +292,9 @@ export default {
isPunteggioZeroZero() { isPunteggioZeroZero() {
return this.state.sp.punt.home === 0 && this.state.sp.punt.guest === 0 return this.state.sp.punt.home === 0 && this.state.sp.punt.guest === 0
}, },
setFinito() {
return this.state.sp.setFinito
},
cambiValid() { cambiValid() {
let hasComplete = false let hasComplete = false
let allValid = true let allValid = true
@@ -355,6 +423,9 @@ export default {
if (msg.type === 'state') { if (msg.type === 'state') {
this.state = msg.state this.state = msg.state
if (this.state.sp.partitaFinita && this.showFormazioneModal) {
this.showFormazioneModal = false
}
} else if (msg.type === 'error') { } else if (msg.type === 'error') {
console.error('[Controller] Server error:', msg.message) console.error('[Controller] Server error:', msg.message)
// Fornisce feedback di errore all'utente. // Fornisce feedback di errore all'utente.
@@ -435,6 +506,21 @@ export default {
console.error('[Controller] Error:', message) console.error('[Controller] Error:', message)
}, },
confermaSetEApriFormazione() {
this.sendAction({ type: 'confermaSet' })
this.formazioneSetData = {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
}
this.showFormazioneModal = true
},
confermaFormazioneSet() {
this.sendAction({ type: 'setFormazione', team: 'home', form: this.formazioneSetData.home })
this.sendAction({ type: 'setFormazione', team: 'guest', form: this.formazioneSetData.guest })
this.showFormazioneModal = false
},
doReset() { doReset() {
this.sendAction({ type: 'resetta' }) this.sendAction({ type: 'resetta' })
this.confirmReset = false this.confirmReset = false
@@ -905,4 +991,21 @@ export default {
padding: 8px 0; padding: 8px 0;
font-weight: 600; font-weight: 600;
} }
/* Fine set */
.set-finito-info {
text-align: center;
padding: 16px 0;
}
.set-vincitore {
font-size: 22px;
font-weight: 800;
color: #fdd835;
text-transform: uppercase;
margin-bottom: 8px;
}
.set-score {
font-size: 36px;
font-weight: 900;
}
</style> </style>

View File

@@ -112,6 +112,15 @@
{{ wsConnected ? '' : 'Disconnesso' }} {{ wsConnected ? '' : 'Disconnesso' }}
</div> </div>
</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> </section>
</template> </template>
@@ -132,6 +141,9 @@ export default {
visuStriscia: true, visuStriscia: true,
modalitaPartita: "3/5", modalitaPartita: "3/5",
sp: { sp: {
strisce: [],
setFinito: null,
partitaFinita: null,
striscia: { home: [0], guest: [0] }, striscia: { home: [0], guest: [0] },
servHome: true, servHome: true,
punt: { home: 0, guest: 0 }, punt: { home: 0, guest: 0 },
@@ -436,4 +448,38 @@ export default {
overflow: hidden; overflow: hidden;
box-sizing: border-box; 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> </style>

62
src/db.js Normal file
View File

@@ -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)
}

View File

@@ -10,6 +10,13 @@ export function createInitialState() {
visuStriscia: true, visuStriscia: true,
modalitaPartita: "3/5", modalitaPartita: "3/5",
sp: { sp: {
strisce: [],
setFinito: null,
partitaFinita: null,
formInizioSet: {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
},
striscia: { home: [0], guest: [" "] }, striscia: { home: [0], guest: [" "] },
servHome: true, servHome: true,
punt: { home: 0, guest: 0 }, punt: { home: 0, guest: 0 },
@@ -24,6 +31,13 @@ export function createInitialState() {
} }
} }
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) { export function checkVittoria(state) {
const puntHome = state.sp.punt.home const puntHome = state.sp.punt.home
const puntGuest = state.sp.punt.guest const puntGuest = state.sp.punt.guest
@@ -58,7 +72,7 @@ export function applyAction(state, action) {
switch (action.type) { switch (action.type) {
case "incPunt": { case "incPunt": {
const team = action.team const team = action.team
if (checkVittoria(s)) break if (s.sp.setFinito !== null || s.sp.partitaFinita !== null) break
s.sp.storicoServizio.push({ s.sp.storicoServizio.push({
servHome: s.sp.servHome, servHome: s.sp.servHome,
@@ -80,10 +94,17 @@ export function applyAction(state, action) {
} }
s.sp.servHome = team === "home" s.sp.servHome = team === "home"
if (checkVittoria(s)) {
s.sp.setFinito = { vincitore: team }
}
break break
} }
case "decPunt": { case "decPunt": {
if (s.sp.setFinito !== null) {
s.sp.setFinito = null
}
if (s.sp.storicoServizio.length > 0) { if (s.sp.storicoServizio.length > 0) {
const tmpHome = s.sp.striscia.home.pop() const tmpHome = s.sp.striscia.home.pop()
s.sp.striscia.guest.pop() s.sp.striscia.guest.pop()
@@ -105,6 +126,45 @@ export function applyAction(state, action) {
break break
} }
case "confermaSet": {
if (!s.sp.setFinito) break
const vincitore = s.sp.setFinito.vincitore
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 },
})
s.sp.formInizioSet = {
home: [...s.sp.form.home],
guest: [...s.sp.form.guest],
}
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": { case "incSet": {
const team = action.team const team = action.team
if (s.sp.set[team] === 2) { if (s.sp.set[team] === 2) {
@@ -139,6 +199,13 @@ export function applyAction(state, action) {
? { home: [0], guest: [" "] } ? { home: [0], guest: [" "] }
: { home: [" "], guest: [0] } : { home: [" "], guest: [0] }
s.sp.storicoServizio = [] 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 break
} }
@@ -171,6 +238,7 @@ export function applyAction(state, action) {
case "setFormazione": { case "setFormazione": {
if (action.team && action.form) { if (action.team && action.form) {
s.sp.form[action.team] = [...action.form] s.sp.form[action.team] = [...action.form]
s.sp.formInizioSet[action.team] = [...action.form]
} }
break break
} }

View File

@@ -28,18 +28,23 @@ export function getNetworkIPs() {
* @param {number} displayPort - Porta del display. * @param {number} displayPort - Porta del display.
* @param {number} controllerPort - Porta del controller. * @param {number} controllerPort - Porta del controller.
*/ */
export function printServerInfo(displayPort = 5173, controllerPort = 3001) { export function printServerInfo(displayPort = 5173, controllerPort = 3001, storicoPort = 3002) {
const networkIPs = getNetworkIPs() const networkIPs = getNetworkIPs()
console.log(`\nSegnapunti Server`) console.log(`\nSegnapunti Server`)
console.log(` Display: http://127.0.0.1:${displayPort}/`) console.log(` Display: http://127.0.0.1:${displayPort}/`)
console.log(` Controller: http://127.0.0.1:${controllerPort}/`) console.log(` Controller: http://127.0.0.1:${controllerPort}/`)
console.log(` Storico: http://127.0.0.1:${storicoPort}/`)
if (networkIPs.length > 0) { if (networkIPs.length > 0) {
console.log(`\n Controller da dispositivi remoti:`) console.log(`\n Controller da dispositivi remoti:`)
networkIPs.forEach(ip => { networkIPs.forEach(ip => {
console.log(` http://${ip}:${controllerPort}/`) console.log(` http://${ip}:${controllerPort}/`)
}) })
console.log(`\n Storico da dispositivi remoti:`)
networkIPs.forEach(ip => {
console.log(` http://${ip}:${storicoPort}/`)
})
} }
console.log() console.log()

View File

@@ -1,4 +1,5 @@
import { createInitialState, applyAction } from './gameState.js' import { createInitialState, applyAction } from './gameState.js'
import { salvaPartita } from './db.js'
/** /**
* Crea e configura il server WebSocket per la gestione dello stato di gioco. * Crea e configura il server WebSocket per la gestione dello stato di gioco.
@@ -98,6 +99,16 @@ export function setupWebSocketHandler(wss) {
return 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. // Propaga il nuovo stato a tutti i client connessi.
broadcastState() broadcastState()
} }
@@ -156,9 +167,9 @@ export function setupWebSocketHandler(wss) {
*/ */
function handleClose(ws) { function handleClose(ws) {
const client = clients.get(ws) const client = clients.get(ws)
const role = client?.role || 'unknown' const role = client?.role || 'unregistered'
console.log(`[WebSocket] Client disconnected (role: ${role})`)
clients.delete(ws) clients.delete(ws)
console.log(`[WebSocket] Client disconnected (role: ${role}, remaining: ${wss.clients.size})`)
} }
/** /**
@@ -184,6 +195,8 @@ export function setupWebSocketHandler(wss) {
// Imposta il tipo binario per ridurre i problemi di codifica. // Imposta il tipo binario per ridurre i problemi di codifica.
ws.binaryType = 'arraybuffer' ws.binaryType = 'arraybuffer'
console.log(`[WebSocket] New connection (total: ${wss.clients.size})`)
ws.on('message', (data) => handleMessage(ws, data)) ws.on('message', (data) => handleMessage(ws, data))
ws.on('close', () => handleClose(ws)) ws.on('close', () => handleClose(ws))
ws.on('error', (err) => handleError(err, ws)) ws.on('error', (err) => handleError(err, ws))

293
storico.html Normal file
View File

@@ -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>

View File

@@ -252,4 +252,37 @@ describe('ControllerPage.vue', () => {
expect(wrapper.find('.conn-bar').text()).toContain('Connesso') 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' })
})
})
}) })

View File

@@ -192,4 +192,39 @@ describe('DisplayPage.vue', () => {
expect(guestStyle).toContain('display: none') 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')
})
})
}) })

View File

@@ -1,6 +1,13 @@
import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest' import { describe, it, expect, beforeEach, vi, afterEach } from 'vitest'
import { setupWebSocketHandler } from '../../src/websocket-handler.js' import { setupWebSocketHandler } from '../../src/websocket-handler.js'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { salvaPartita } from '../../src/db.js'
// 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 // Mock parziale di una WebSocket e del Server
class MockWebSocket extends EventEmitter { class MockWebSocket extends EventEmitter {
@@ -400,4 +407,72 @@ describe('WebSocket Integration (websocket-handler.js)', () => {
expect(handler.getClients().size).toBe(1) 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')
})
})
}) })

View File

@@ -1,303 +0,0 @@
import { vi, describe, it, expect, beforeAll, beforeEach } from 'vitest'
// vi.hoisted garantisce che refs sia disponibile nelle factory dei mock,
// che vengono hoistate prima degli import statici.
const refs = vi.hoisted(() => ({ ws: null, rl: null }))
// ---------------------------------------------------------------------------
// Mock: ws
// ---------------------------------------------------------------------------
vi.mock('ws', async () => {
const { EventEmitter } = await import('events')
class WebSocket extends EventEmitter {
static OPEN = 1
constructor() {
super()
this.readyState = 1
this.send = vi.fn()
this.close = vi.fn()
refs.ws = this
}
}
return { WebSocket }
})
// ---------------------------------------------------------------------------
// Mock: readline
// ---------------------------------------------------------------------------
vi.mock('readline', async () => {
const { EventEmitter } = await import('events')
return {
default: {
createInterface: vi.fn(() => {
const rl = new EventEmitter()
rl.prompt = vi.fn()
rl.question = vi.fn()
rl.close = vi.fn()
refs.rl = rl
return rl
}),
},
}
})
// ---------------------------------------------------------------------------
// Silenzia output e blocca process.exit durante i test
// ---------------------------------------------------------------------------
vi.spyOn(process, 'exit').mockImplementation(() => {})
vi.spyOn(process.stdout, 'write').mockReturnValue(true)
vi.spyOn(console, 'log').mockImplementation(() => {})
vi.spyOn(console, 'error').mockImplementation(() => {})
// ---------------------------------------------------------------------------
// Importa cli.js — esegue gli effetti collaterali con le dipendenze mockate
// ---------------------------------------------------------------------------
beforeAll(async () => {
await import('../../cli.js')
refs.ws.emit('open')
})
// ---------------------------------------------------------------------------
// Helper
// ---------------------------------------------------------------------------
function sendLine(line) {
refs.rl.emit('line', line)
}
function lastSent() {
const calls = refs.ws.send.mock.calls
return calls.length ? JSON.parse(calls[calls.length - 1][0]) : null
}
// ---------------------------------------------------------------------------
// Test
// ---------------------------------------------------------------------------
describe('CLI — Registrazione', () => {
it('invia register con ruolo controller all\'apertura del WebSocket', () => {
const call = refs.ws.send.mock.calls.find((c) => {
try { return JSON.parse(c[0]).type === 'register' } catch { return false }
})
expect(call).toBeDefined()
expect(JSON.parse(call[0])).toEqual({ type: 'register', role: 'controller' })
})
})
describe('CLI — Ricezione messaggi', () => {
beforeEach(() => {
refs.rl.prompt.mockClear()
vi.mocked(console.error).mockClear()
})
it('ripristina il prompt dopo un messaggio "state"', () => {
const state = {
sp: { nomi: { home: 'A', guest: 'B' }, punt: { home: 0, guest: 0 }, set: { home: 0, guest: 0 }, servHome: true },
modalitaPartita: '3/5',
}
refs.ws.emit('message', JSON.stringify({ type: 'state', state }))
expect(refs.rl.prompt).toHaveBeenCalled()
})
it('mostra un errore alla ricezione di un messaggio "error"', () => {
refs.ws.emit('message', JSON.stringify({ type: 'error', message: 'azione non valida' }))
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comandi punteggio', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"+" → incPunt home', () => {
sendLine('+')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"pc" → incPunt home (shortcut)', () => {
sendLine('pc')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"-" → incPunt guest', () => {
sendLine('-')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"po" → incPunt guest (shortcut)', () => {
sendLine('po')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"punto casa" → incPunt home', () => {
sendLine('punto casa')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'home' } })
})
it('"punto ospite" → incPunt guest', () => {
sendLine('punto ospite')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incPunt', team: 'guest' } })
})
it('"punto" senza squadra → errore, nessun invio', () => {
sendLine('punto')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('"undo" → decPunt', () => {
sendLine('undo')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'decPunt' } })
})
it('"u" → decPunt (shortcut)', () => {
sendLine('u')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'decPunt' } })
})
})
describe('CLI — Comandi set', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"set casa" → incSet home', () => {
sendLine('set casa')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incSet', team: 'home' } })
})
it('"set ospite" → incSet guest', () => {
sendLine('set ospite')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'incSet', team: 'guest' } })
})
it('"set" senza squadra → errore, nessun invio', () => {
sendLine('set')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comandi partita', () => {
beforeEach(() => refs.ws.send.mockClear())
it('"serv" → cambiaPalla', () => {
sendLine('serv')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'cambiaPalla' } })
})
it('"nomi <casa> <ospite>" → setNomi con entrambi i nomi', () => {
sendLine('nomi Antoniana Riviera')
expect(lastSent()).toMatchObject({
type: 'action',
action: { type: 'setNomi', home: 'Antoniana', guest: 'Riviera' },
})
})
it('"nomi <casa>" → setNomi con solo nome casa', () => {
sendLine('nomi Antoniana')
const sent = lastSent()
expect(sent).toMatchObject({ type: 'action', action: { type: 'setNomi', home: 'Antoniana' } })
expect(sent.action.guest).toBeUndefined()
})
it('"nomi" senza argomenti → errore, nessun invio', () => {
sendLine('nomi')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('"modalita 2/3" → setModalita 2/3', () => {
sendLine('modalita 2/3')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'setModalita', modalita: '2/3' } })
})
it('"modalita 3/5" → setModalita 3/5', () => {
sendLine('modalita 3/5')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'setModalita', modalita: '3/5' } })
})
it('"modalita" con valore non valido → errore, nessun invio', () => {
sendLine('modalita 4/7')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
})
describe('CLI — Comando reset (con conferma)', () => {
beforeEach(() => {
refs.ws.send.mockClear()
refs.rl.question.mockClear()
})
it('conferma "s" → invia resetta', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb('s'))
sendLine('reset')
expect(lastSent()).toMatchObject({ type: 'action', action: { type: 'resetta' } })
})
it('risposta "n" → non invia nulla', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb('n'))
sendLine('reset')
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('risposta vuota → non invia nulla', () => {
refs.rl.question.mockImplementationOnce((_msg, cb) => cb(''))
sendLine('reset')
expect(refs.ws.send).not.toHaveBeenCalled()
})
})
describe('CLI — Comandi informativi', () => {
beforeEach(() => {
refs.ws.send.mockClear()
vi.mocked(console.log).mockClear()
})
it('"help" → stampa aiuto, nessun invio', () => {
sendLine('help')
expect(console.log).toHaveBeenCalled()
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('"stato" → nessun invio', () => {
sendLine('stato')
expect(refs.ws.send).not.toHaveBeenCalled()
})
it('comando sconosciuto → messaggio di errore, nessun invio', () => {
sendLine('xyzzy')
expect(refs.ws.send).not.toHaveBeenCalled()
expect(console.error).toHaveBeenCalled()
})
it('riga vuota → nessun invio', () => {
refs.rl.emit('line', ' ')
expect(refs.ws.send).not.toHaveBeenCalled()
})
})
describe('CLI — Uscita', () => {
it('"exit" → chiude il WebSocket', () => {
refs.ws.close.mockClear()
sendLine('exit')
expect(refs.ws.close).toHaveBeenCalled()
})
it('"q" → chiude il WebSocket (shortcut)', () => {
refs.ws.close.mockClear()
sendLine('q')
expect(refs.ws.close).toHaveBeenCalled()
})
it('chiusura readline → chiude il WebSocket', () => {
refs.ws.close.mockClear()
refs.rl.emit('close')
expect(refs.ws.close).toHaveBeenCalled()
})
})

145
tests/unit/db.test.js Normal file
View File

@@ -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)
}
})
})
})

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach } from 'vitest' import { describe, it, expect, beforeEach } from 'vitest'
import { createInitialState, applyAction, checkVittoria } from '../../src/gameState.js' import { createInitialState, applyAction, checkVittoria, checkVittoriaPartita } from '../../src/gameState.js'
describe('Game Logic (gameState.js)', () => { describe('Game Logic (gameState.js)', () => {
let state let state
@@ -31,9 +31,10 @@ describe('Game Logic (gameState.js)', () => {
expect(state.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"]) expect(state.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
}) })
it('dovrebbe avere la striscia iniziale a [0]', () => { 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.home).toEqual([0])
expect(state.sp.striscia.guest).toEqual([0]) expect(state.sp.striscia.guest).toEqual([" "])
}) })
it('dovrebbe avere storico servizio vuoto', () => { it('dovrebbe avere storico servizio vuoto', () => {
@@ -116,13 +117,15 @@ describe('Game Logic (gameState.js)', () => {
it('dovrebbe aggiornare la striscia per punto Home', () => { it('dovrebbe aggiornare la striscia per punto Home', () => {
const s = applyAction(state, { type: 'incPunt', team: 'home' }) const s = applyAction(state, { type: 'incPunt', team: 'home' })
// Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
expect(s.sp.striscia.home).toEqual([0, 1]) expect(s.sp.striscia.home).toEqual([0, 1])
expect(s.sp.striscia.guest).toEqual([0, " "]) expect(s.sp.striscia.guest).toEqual([" ", " "])
}) })
it('dovrebbe aggiornare la striscia per punto Guest', () => { it('dovrebbe aggiornare la striscia per punto Guest', () => {
const s = applyAction(state, { type: 'incPunt', team: 'guest' }) const s = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s.sp.striscia.guest).toEqual([0, 1]) // Lo stato iniziale ha striscia = { home: [0], guest: [" "] }
expect(s.sp.striscia.guest).toEqual([" ", 1])
expect(s.sp.striscia.home).toEqual([0, " "]) expect(s.sp.striscia.home).toEqual([0, " "])
}) })
@@ -133,9 +136,11 @@ describe('Game Logic (gameState.js)', () => {
expect(s.sp.storicoServizio[0]).toHaveProperty('cambioPalla') expect(s.sp.storicoServizio[0]).toHaveProperty('cambioPalla')
}) })
it('non dovrebbe incrementare i punti dopo vittoria', () => { 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.home = 25
state.sp.punt.guest = 23 state.sp.punt.guest = 23
state.sp.setFinito = { vincitore: 'home' }
const s = applyAction(state, { type: 'incPunt', team: 'home' }) const s = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s.sp.punt.home).toBe(25) expect(s.sp.punt.home).toBe(25)
}) })
@@ -619,10 +624,11 @@ describe('Game Logic (gameState.js)', () => {
}) })
it('dovrebbe resettare la striscia', () => { it('dovrebbe resettare la striscia', () => {
state.sp.striscia = { home: [0, 1, 2, 3], guest: [0, " ", " ", 1] } state.sp.striscia = { home: [0, 1, 2, 3], guest: [" ", " ", " ", 1] }
const s = applyAction(state, { type: 'resetta' }) const s = applyAction(state, { type: 'resetta' })
// servHome è true di default → home parte con [0], guest con [" "]
expect(s.sp.striscia.home).toEqual([0]) expect(s.sp.striscia.home).toEqual([0])
expect(s.sp.striscia.guest).toEqual([0]) expect(s.sp.striscia.guest).toEqual([" "])
}) })
it('dovrebbe resettare lo storico servizio', () => { it('dovrebbe resettare lo storico servizio', () => {
@@ -656,4 +662,278 @@ describe('Game Logic (gameState.js)', () => {
expect(s.sp.punt.guest).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([" "])
})
})
}) })

View File

@@ -109,6 +109,7 @@ describe('Server Utils', () => {
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n') const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('5173') expect(allLogs).toContain('5173')
expect(allLogs).toContain('3001') expect(allLogs).toContain('3001')
expect(allLogs).toContain('3002')
consoleSpy.mockRestore() consoleSpy.mockRestore()
}) })
@@ -122,17 +123,27 @@ describe('Server Utils', () => {
consoleSpy.mockRestore() consoleSpy.mockRestore()
}) })
it('dovrebbe mostrare gli URL remoti se ci sono IP di rete', () => { it('dovrebbe stampare storicoPort personalizzato', () => {
os.networkInterfaces.mockReturnValue({})
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo(3000, 3001, 5000)
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('5000')
consoleSpy.mockRestore()
})
it('dovrebbe mostrare gli URL remoti per controller e storico', () => {
os.networkInterfaces.mockReturnValue({ os.networkInterfaces.mockReturnValue({
eth0: [ eth0: [
{ family: 'IPv4', internal: false, address: '192.168.1.50' } { family: 'IPv4', internal: false, address: '192.168.1.50' }
] ]
}) })
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
printServerInfo(3000, 3001) printServerInfo(3000, 3001, 3002)
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n') const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
expect(allLogs).toContain('192.168.1.50') expect(allLogs).toContain('192.168.1.50')
expect(allLogs).toContain('remoti') expect(allLogs).toContain('3001')
expect(allLogs).toContain('3002')
consoleSpy.mockRestore() consoleSpy.mockRestore()
}) })

View File

@@ -1,9 +1,15 @@
import { WebSocketServer } from 'ws' import { WebSocketServer } from 'ws'
import { createServer as createHttpServer, request as httpRequest } from 'http' 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 { setupWebSocketHandler } from './src/websocket-handler.js'
import { printServerInfo } from './src/server-utils.js' import { printServerInfo } from './src/server-utils.js'
import { getPartite, getPartita } from './src/db.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const CONTROLLER_PORT = 3001 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' const DEV_PROXY_HOST = process.env.DEV_PROXY_HOST || '127.0.0.1'
/** /**
@@ -38,8 +44,9 @@ export default function websocketPlugin() {
const vitePort = viteAddr.port const vitePort = viteAddr.port
startControllerDevServer(vitePort, wss) startControllerDevServer(vitePort, wss)
startStoricoDevServer()
setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT), 100) setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT, STORICO_PORT), 100)
}) })
} }
} }
@@ -129,3 +136,63 @@ function startControllerDevServer(vitePort, wss) {
console.log(`[Controller] Dev server running on port ${CONTROLLER_PORT}`) 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}`)
})
}