Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5f9e37062c | |||
| 3188994299 | |||
| eec4ef0526 | |||
| 16a3fb912a | |||
| 2fe1808fc9 | |||
| b3d114c108 | |||
| b9aed683c6 | |||
| 606b2c1ee6 | |||
| 27e29a78e7 |
@@ -0,0 +1,66 @@
|
||||
# CLAUDE.md
|
||||
|
||||
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||
|
||||
## Project Purpose
|
||||
|
||||
**Segnapunti Anto** is a real-time volleyball scoreboard PWA. An Express/WebSocket server hosts the game state; two separate web interfaces — a **display** (public scoreboard) and a **controller** (operator panel) — stay in sync via WebSocket. A terminal CLI (`cli.js`) provides an alternative controller interface.
|
||||
|
||||
## Commands
|
||||
|
||||
```bash
|
||||
npm run dev # Vite dev server — display: :5173, controller: :5173/controller.html (proxied on :3001)
|
||||
npm run serve # Build + run production — display: :3000, controller: :3001
|
||||
npm run cli # Terminal controller (connects to production :3000)
|
||||
npm run cli:dev # Terminal controller (connects to dev :5173)
|
||||
|
||||
npm run test # Vitest watch mode
|
||||
npm run test:all # All Vitest suites once (unit, integration, component, stress)
|
||||
npm run test:unit # Unit + integration only
|
||||
npm run test:component # Vue component tests
|
||||
npm run test:stress # Load tests (50+ concurrent clients)
|
||||
npm run test:e2e # Playwright E2E (requires servers to be running via npm run serve)
|
||||
npm run test:e2e:ui # Playwright with interactive UI
|
||||
```
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Controller (Vue) ──WebSocket──┐
|
||||
Display (Vue) ──WebSocket──┤── websocket-handler.js ── gameState.js
|
||||
CLI (Node) ──WebSocket──┘
|
||||
```
|
||||
|
||||
All game logic lives in `src/gameState.js` as three pure functions:
|
||||
- `createInitialState()` — returns the initial state
|
||||
- `applyAction(state, action)` — immutable reducer (deep-clones via `JSON.parse/stringify`)
|
||||
- `checkVittoria(state)` — volleyball win conditions (25-point sets with 2-point margin, 15-point final set)
|
||||
|
||||
`src/websocket-handler.js` receives actions, validates that the sender is a registered controller (not just a display), calls `applyAction`, then broadcasts the new state to all clients.
|
||||
|
||||
Two production HTTP servers are started by `server.js` (Express): port 3000 serves `dist/index.html` (display), port 3001 serves `dist/controller.html` (controller). Both share a single WebSocket endpoint at `/ws`.
|
||||
|
||||
In development, `vite-plugin-websocket.js` is a custom Vite plugin that embeds the WebSocket server inside the Vite dev server and proxies port 3001 traffic back to Vite.
|
||||
|
||||
## Key Design Constraints
|
||||
|
||||
- **All game rules on the server** — clients are pure UI; the server is the source of truth.
|
||||
- **Role-based WebSocket** — clients register as `display` or `controller`; only controllers may send actions.
|
||||
- **Immutable state** — `applyAction` never mutates; always returns a new state object.
|
||||
- **Single-controller intent** — the design targets one active controller and one display; no conflict resolution exists for simultaneous controllers.
|
||||
|
||||
## Test Layout
|
||||
|
||||
| Suite | Path | 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) |
|
||||
|
||||
E2E tests run serially (`workers: 1`) to avoid WebSocket state races. Run `npm run serve` before `npm run test:e2e`.
|
||||
|
||||
## Simplification Goals (Current Work)
|
||||
|
||||
The intended architecture is: **one host acts as both WebSocket server and display; one connected device acts as controller**. Complexity reduction should be evaluated against this constraint — anything that supports multi-controller scenarios, complex client topologies, or unneeded abstractions is a candidate for removal.
|
||||
@@ -1,98 +1,90 @@
|
||||
# Segnapunti Anto
|
||||
|
||||
Applicazione web **Progressive Web App (PWA)** per tracciare i punteggi di partite di pallavolo in tempo reale.
|
||||
Applicazione web **fullstack real-time** per il tracciamento dei punteggi di partite di pallavolo, installabile come PWA.
|
||||
|
||||
---
|
||||
|
||||
## Panoramica
|
||||
|
||||
**Segnapunti Anto** e un'applicazione digitale per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone.
|
||||
**Segnapunti Anto** è un'applicazione fullstack per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone in contesto sportivo.
|
||||
|
||||
L'app e composta da due interfacce:
|
||||
- **Display** (tabellone pubblico)
|
||||
- **Controller** (pannello operatore)
|
||||
### Architettura
|
||||
|
||||
Le due interfacce condividono lo stato in tempo reale tramite WebSocket.
|
||||
Il sistema è composto da un **backend Node.js/Express** e due interfacce web separate:
|
||||
|
||||
### Funzionalita Principali
|
||||
| Interfaccia | Porta | Ruolo |
|
||||
|-------------|-------|-------|
|
||||
| **Display** | 3000 | Tabellone pubblico — mostra punteggi, formazioni e storico |
|
||||
| **Controller** | 3001 | Pannello operatore — invia azioni e gestisce la partita |
|
||||
|
||||
- **Gestione partita in tempo reale**
|
||||
- Tracciamento punti home/guest
|
||||
- Gestione set
|
||||
- Indicatore servizio
|
||||
- Storico punti (striscia)
|
||||
- Blocchi logici quando il set e gia vinto
|
||||
Le due interfacce comunicano tramite **WebSocket** (`/ws`): ogni azione del Controller viene elaborata dal server e trasmessa in broadcast a tutti i client connessi.
|
||||
|
||||
- **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`
|
||||
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.
|
||||
|
||||
- **Formazioni e cambi**
|
||||
- Gestione formazione a 6 giocatori
|
||||
- Rotazione automatica al cambio palla
|
||||
- Dialog cambi con validazioni (`IN -> OUT`)
|
||||
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.
|
||||
|
||||
- **Controlli e personalizzazione**
|
||||
- Configurazione nomi squadre
|
||||
- Toggle ordine squadre (inverti)
|
||||
- Toggle visualizzazione punteggio/formazioni
|
||||
- Toggle striscia storico
|
||||
- Sintesi vocale punteggio (Web Speech API)
|
||||
---
|
||||
|
||||
## Funzionalità
|
||||
|
||||
### Gestione partita in tempo reale
|
||||
- Tracciamento punti home/guest con indicatore di servizio
|
||||
- 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 di Sistema
|
||||
### Ambiente di sviluppo
|
||||
|
||||
#### Per Sviluppo
|
||||
- **Sistema Operativo**: Linux, macOS, Windows
|
||||
- **Node.js**: `>= 18.19.0` (consigliato `20 LTS`)
|
||||
- **npm**: `>= 9`
|
||||
- **RAM**: minimo 2GB (consigliato 4GB)
|
||||
| Requisito | Versione minima | Consigliata |
|
||||
|-----------|-----------------|-------------|
|
||||
| **Node.js** | `>= 18.19.0` | `24 LTS` |
|
||||
| **npm** | `>= 9` | — |
|
||||
| **RAM** | 2 GB | 4 GB |
|
||||
| **OS** | Linux, macOS, Windows | — |
|
||||
|
||||
#### Per Esecuzione Test E2E
|
||||
- Browser Playwright installati (`chromium`, `firefox`)
|
||||
- Su Linux, eventuali dipendenze sistema per browser headless
|
||||
### Test E2E
|
||||
|
||||
Comandi utili:
|
||||
I test end-to-end richiedono i browser Playwright. Su Linux potrebbero essere necessarie dipendenze di sistema aggiuntive.
|
||||
|
||||
```bash
|
||||
node -v
|
||||
npm -v
|
||||
npx playwright install chromium firefox
|
||||
# Linux, se necessario:
|
||||
# Linux (con dipendenze di sistema):
|
||||
# npx playwright install --with-deps chromium firefox
|
||||
```
|
||||
|
||||
### Requisiti Browser (Utente Finale)
|
||||
### Requisiti browser (utente finale)
|
||||
|
||||
| Requisito | Dettaglio | Necessita |
|
||||
|-----------|-----------|-----------|
|
||||
| API | Utilizzo | Necessità |
|
||||
|-----|----------|-----------|
|
||||
| JavaScript ES6+ | Moduli, async/await | Obbligatorio |
|
||||
| WebSocket | Sincronizzazione stato live | Obbligatorio |
|
||||
| Service Worker API | Supporto PWA offline | Consigliato |
|
||||
| Web Speech API | Annunci vocali | Opzionale |
|
||||
| Service Worker | Supporto PWA offline | Consigliato |
|
||||
| Web Speech API | Annunci vocali punteggio | Opzionale |
|
||||
|
||||
### Browser Testati e Supportati
|
||||
|
||||
| Browser | Supporto | Note |
|
||||
|---------|----------|------|
|
||||
| Chrome/Chromium | ✅ | Completo |
|
||||
| Firefox | ✅ | Completo |
|
||||
| Mobile Chrome (Playwright Pixel 5) | ✅ | Copertura E2E mobile |
|
||||
**Browser testati:** Chrome/Chromium, Firefox, Mobile Chrome (Playwright Pixel 5).
|
||||
|
||||
---
|
||||
|
||||
## Installazione e Setup
|
||||
|
||||
### Prerequisiti
|
||||
|
||||
- Node.js `>= 18.19.0`
|
||||
- npm `>= 9`
|
||||
|
||||
### Installazione
|
||||
## Installazione
|
||||
|
||||
```bash
|
||||
git clone https://santantonio.sytes.net/attilio/segnapunti.git
|
||||
@@ -102,107 +94,120 @@ npm install
|
||||
|
||||
---
|
||||
|
||||
## Comandi per Sviluppo
|
||||
## Sviluppo
|
||||
|
||||
### Dev Server
|
||||
|
||||
Avvia il server di sviluppo Vite:
|
||||
### Dev server
|
||||
|
||||
```bash
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Accesso tipico in sviluppo:
|
||||
- `http://localhost:5173/` -> Display
|
||||
- `http://localhost:5173/controller.html` -> Controller
|
||||
Avvia il server Vite con hot reload:
|
||||
- `http://localhost:5173/` — Display
|
||||
- `http://localhost:5173/controller.html` — Controller
|
||||
|
||||
### Modalita Sviluppo
|
||||
- Hot reload attivo
|
||||
- Build veloce lato Vite
|
||||
- Buona per sviluppo UI/UX
|
||||
|
||||
---
|
||||
|
||||
## Comandi per Build
|
||||
|
||||
### Build Produzione
|
||||
### Build di produzione
|
||||
|
||||
```bash
|
||||
npm run build
|
||||
```
|
||||
|
||||
Output:
|
||||
- cartella `dist/`
|
||||
- asset ottimizzati
|
||||
- file PWA (manifest + service worker)
|
||||
Genera la cartella `dist/` con asset ottimizzati, manifest e service worker PWA.
|
||||
|
||||
### Avvio Server Applicativo Locale (Display + Controller)
|
||||
### Avvio in produzione (locale)
|
||||
|
||||
```bash
|
||||
npm run serve
|
||||
```
|
||||
|
||||
Espone:
|
||||
- `http://localhost:3000` -> Display
|
||||
- `http://localhost:3001` -> Controller
|
||||
Espone i due server:
|
||||
- `http://localhost:3000` — Display
|
||||
- `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
|
||||
npm run preview
|
||||
npm run start
|
||||
# Modalità produzione (server su porta 3000)
|
||||
npm run cli
|
||||
|
||||
# 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.
|
||||
|
||||
## Configurazione PWA
|
||||
### Comandi disponibili
|
||||
|
||||
L'app usa `vite-plugin-pwa` (vedi `vite.config.js`) con:
|
||||
- `registerType: 'autoUpdate'`
|
||||
- manifest installabile
|
||||
- orientamento landscape
|
||||
- modalita fullscreen
|
||||
#### Punteggio
|
||||
|
||||
Caratteristiche principali:
|
||||
- installabile su dispositivi supportati
|
||||
- aggiornamento automatico del service worker
|
||||
- supporto utilizzo offline (in base alle risorse cache)
|
||||
| Comando | Alias | Effetto |
|
||||
|---------|-------|---------|
|
||||
| `punto casa` | `+`, `pc` | Assegna un punto alla squadra di casa |
|
||||
| `punto ospite` | `-`, `po` | Assegna un punto alla squadra ospite |
|
||||
| `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
|
||||
|
||||
---
|
||||
|
||||
## Logica Regolamentare Pallavolo
|
||||
## Test
|
||||
|
||||
### Vittoria Set
|
||||
La suite di test copre tutti i livelli dell'applicazione:
|
||||
|
||||
- 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`
|
||||
| Suite | Comando | Descrizione |
|
||||
|-------|---------|-------------|
|
||||
| Tutti | `npm run test:all` | Unit + integration + component + stress |
|
||||
| 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) |
|
||||
|
||||
### 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.
|
||||
Per la guida completa ai test, consultare [`tests/README.md`](tests/README.md).
|
||||
|
||||
---
|
||||
|
||||
## Test (stato attuale)
|
||||
|
||||
Suite presenti:
|
||||
- Unit
|
||||
- Integration
|
||||
- Component
|
||||
- Stress
|
||||
- E2E (Playwright)
|
||||
|
||||
Comandi principali:
|
||||
## Docker
|
||||
|
||||
```bash
|
||||
npm run test:all
|
||||
npm run test:e2e
|
||||
docker-compose up --build
|
||||
```
|
||||
|
||||
Guida completa test:
|
||||
- `tests/README.md`
|
||||
Espone le porte `3000` (Display) e `3001` (Controller).
|
||||
|
||||
@@ -0,0 +1,302 @@
|
||||
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();
|
||||
});
|
||||
Generated
+1017
-1572
File diff suppressed because it is too large
Load Diff
+8
-3
@@ -9,6 +9,8 @@
|
||||
"preview": "node server.js",
|
||||
"start": "node server.js",
|
||||
"serve": "vite build && node server.js",
|
||||
"cli": "node cli.js",
|
||||
"cli:dev": "node cli.js 5173",
|
||||
"test": "vitest",
|
||||
"test:unit": "vitest run tests/unit tests/integration",
|
||||
"test:component": "vitest run tests/component",
|
||||
@@ -26,18 +28,21 @@
|
||||
"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": "^4.1.0",
|
||||
"@vitejs/plugin-vue": "^6.0.5",
|
||||
"@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": "^4.3.9",
|
||||
"vite-plugin-pwa": "^0.16.0",
|
||||
"vite": "^7.3.1",
|
||||
"vite-plugin-pwa": "^1.2.0",
|
||||
"vitest": "^4.0.18"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,32 +9,26 @@ import { printServerInfo } from './src/server-utils.js'
|
||||
const __filename = fileURLToPath(import.meta.url)
|
||||
const __dirname = dirname(__filename)
|
||||
|
||||
// --- Configurazione del server ---
|
||||
const PORT = process.env.PORT || 3000
|
||||
const distDir = join(__dirname, 'dist')
|
||||
|
||||
const DISPLAY_PORT = process.env.PORT || 3000
|
||||
const CONTROLLER_PORT = process.env.CONTROLLER_PORT || 3001
|
||||
const app = express()
|
||||
|
||||
// ========================================
|
||||
// Server Display (porta principale)
|
||||
// ========================================
|
||||
app.use(express.static(distDir, { index: false }))
|
||||
|
||||
const displayApp = express()
|
||||
|
||||
// Espone i file generati dalla build di Vite.
|
||||
displayApp.use(express.static(join(__dirname, 'dist')))
|
||||
|
||||
// Fallback per SPA: restituisce `index.html` per tutte le route.
|
||||
displayApp.get(/.*/, (_req, res) => {
|
||||
res.sendFile(join(__dirname, 'dist', 'index.html'))
|
||||
app.get(['/', '/display', '/display/*splat'], (_req, res) => {
|
||||
res.sendFile(join(distDir, 'index.html'))
|
||||
})
|
||||
|
||||
const displayServer = createServer(displayApp)
|
||||
app.get(['/controller', '/controller/*splat'], (_req, res) => {
|
||||
res.sendFile(join(distDir, 'controller.html'))
|
||||
})
|
||||
|
||||
// Inizializza il server WebSocket condiviso.
|
||||
const server = createServer(app)
|
||||
const wss = new WebSocketServer({ noServer: true })
|
||||
setupWebSocketHandler(wss)
|
||||
|
||||
displayServer.on('upgrade', (request, socket, head) => {
|
||||
server.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) => {
|
||||
@@ -45,39 +39,6 @@ displayServer.on('upgrade', (request, socket, head) => {
|
||||
}
|
||||
})
|
||||
|
||||
displayServer.listen(DISPLAY_PORT, '0.0.0.0', () => {
|
||||
console.log(`[Display] Server running on port ${DISPLAY_PORT}`)
|
||||
})
|
||||
|
||||
// ========================================
|
||||
// Server Controller (porta separata)
|
||||
// ========================================
|
||||
|
||||
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)
|
||||
server.listen(PORT, '0.0.0.0', () => {
|
||||
printServerInfo(PORT)
|
||||
})
|
||||
|
||||
@@ -78,6 +78,19 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Finestra set vinto -->
|
||||
<div class="overlay" v-if="showSetVinto">
|
||||
<div class="dialog">
|
||||
<div class="dialog-title">SET VINTO</div>
|
||||
<div class="dialog-winner">{{ state.sp.nomi[setVintoTeam] }}</div>
|
||||
<div class="dialog-subtitle">Configura le formazioni per il prossimo set</div>
|
||||
<div class="dialog-buttons">
|
||||
<button class="btn btn-cancel" @click="undoUltimoPoint()">INDIETRO</button>
|
||||
<button class="btn btn-confirm" @click="doNuovoSet()">VAI AL SET SUCCESSIVO</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Finestra configurazione -->
|
||||
<div class="overlay" v-if="showConfig" @click.self="showConfig = false">
|
||||
<div class="dialog dialog-config">
|
||||
@@ -187,6 +200,8 @@ export default {
|
||||
reconnectAttempts: 0,
|
||||
maxReconnectDelay: 30000,
|
||||
confirmReset: false,
|
||||
showSetVinto: false,
|
||||
setVintoTeam: null,
|
||||
showConfig: false,
|
||||
showCambiTeam: false,
|
||||
showCambi: false,
|
||||
@@ -227,6 +242,15 @@ export default {
|
||||
isPunteggioZeroZero() {
|
||||
return this.state.sp.punt.home === 0 && this.state.sp.punt.guest === 0
|
||||
},
|
||||
squadraVincente() {
|
||||
const { home, guest } = this.state.sp.punt
|
||||
const totSet = this.state.sp.set.home + this.state.sp.set.guest
|
||||
const isSetDecisivo = this.state.modalitaPartita === '2/3' ? totSet >= 2 : totSet >= 4
|
||||
const soglia = isSetDecisivo ? 15 : 25
|
||||
if (home >= soglia && home - guest >= 2) return 'home'
|
||||
if (guest >= soglia && guest - home >= 2) return 'guest'
|
||||
return null
|
||||
},
|
||||
cambiValid() {
|
||||
let hasComplete = false
|
||||
let allValid = true
|
||||
@@ -240,6 +264,14 @@ export default {
|
||||
return allValid && hasComplete
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
squadraVincente(val) {
|
||||
if (val && !this.showSetVinto) {
|
||||
this.setVintoTeam = val
|
||||
this.showSetVinto = true
|
||||
}
|
||||
},
|
||||
},
|
||||
mounted() {
|
||||
this.connectWebSocket()
|
||||
|
||||
@@ -440,6 +472,22 @@ export default {
|
||||
this.confirmReset = false
|
||||
},
|
||||
|
||||
undoUltimoPoint() {
|
||||
this.sendAction({ type: 'decPunt' })
|
||||
this.showSetVinto = false
|
||||
},
|
||||
|
||||
doNuovoSet() {
|
||||
this.sendAction({ type: 'nuovoSet', team: this.setVintoTeam })
|
||||
this.showSetVinto = false
|
||||
this.configData.nomeHome = this.state.sp.nomi.home
|
||||
this.configData.nomeGuest = this.state.sp.nomi.guest
|
||||
this.configData.modalita = this.state.modalitaPartita
|
||||
this.configData.formHome = ["1", "2", "3", "4", "5", "6"]
|
||||
this.configData.formGuest = ["1", "2", "3", "4", "5", "6"]
|
||||
this.showConfig = true
|
||||
},
|
||||
|
||||
openConfig() {
|
||||
this.configData.nomeHome = this.state.sp.nomi.home
|
||||
this.configData.nomeGuest = this.state.sp.nomi.guest
|
||||
@@ -747,6 +795,20 @@ export default {
|
||||
border: 1px solid rgba(255,255,255,0.12);
|
||||
}
|
||||
|
||||
.dialog-winner {
|
||||
font-size: 28px;
|
||||
font-weight: 900;
|
||||
text-align: center;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.dialog-subtitle {
|
||||
font-size: 13px;
|
||||
color: #aaa;
|
||||
text-align: center;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.dialog-config {
|
||||
max-height: 85vh;
|
||||
overflow-y: auto;
|
||||
|
||||
+21
-15
@@ -10,7 +10,7 @@ export function createInitialState() {
|
||||
visuStriscia: true,
|
||||
modalitaPartita: "3/5",
|
||||
sp: {
|
||||
striscia: { home: [0], guest: [" "] },
|
||||
striscia: { home: [0], guest: [0] },
|
||||
servHome: true,
|
||||
punt: { home: 0, guest: 0 },
|
||||
set: { home: 0, guest: 0 },
|
||||
@@ -51,19 +51,15 @@ export function checkVittoria(state) {
|
||||
}
|
||||
|
||||
export function applyAction(state, action) {
|
||||
// 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))
|
||||
const s = structuredClone(state)
|
||||
|
||||
switch (action.type) {
|
||||
case "incPunt": {
|
||||
const team = action.team
|
||||
if (checkVittoria(s)) break
|
||||
|
||||
s.sp.storicoServizio.push({
|
||||
servHome: s.sp.servHome,
|
||||
cambioPalla: (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome),
|
||||
})
|
||||
const cambioPalla = (team === "home") !== s.sp.servHome
|
||||
s.sp.storicoServizio.push({ servHome: s.sp.servHome, cambioPalla })
|
||||
|
||||
s.sp.punt[team]++
|
||||
if (team === "home") {
|
||||
@@ -74,7 +70,6 @@ export function applyAction(state, action) {
|
||||
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())
|
||||
}
|
||||
@@ -115,12 +110,25 @@ export function applyAction(state, action) {
|
||||
break
|
||||
}
|
||||
|
||||
case "nuovoSet": {
|
||||
const team = action.team
|
||||
if (team !== 'home' && team !== 'guest') break
|
||||
s.sp.set[team]++
|
||||
s.sp.punt.home = 0
|
||||
s.sp.punt.guest = 0
|
||||
s.sp.striscia = { home: [0], guest: [0] }
|
||||
s.sp.storicoServizio = []
|
||||
s.sp.form = {
|
||||
home: ["1", "2", "3", "4", "5", "6"],
|
||||
guest: ["1", "2", "3", "4", "5", "6"],
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
case "cambiaPalla": {
|
||||
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] }
|
||||
s.sp.striscia = { home: [0], guest: [0] }
|
||||
}
|
||||
break
|
||||
}
|
||||
@@ -135,9 +143,7 @@ export function applyAction(state, action) {
|
||||
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.striscia = { home: [0], guest: [0] }
|
||||
s.sp.storicoServizio = []
|
||||
break
|
||||
}
|
||||
|
||||
+4
-14
@@ -1,16 +1,11 @@
|
||||
import { networkInterfaces } from 'os'
|
||||
|
||||
/**
|
||||
* 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 = []
|
||||
|
||||
for (const name of Object.keys(nets)) {
|
||||
for (const net of nets[name]) {
|
||||
// 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.') &&
|
||||
@@ -23,22 +18,17 @@ export function getNetworkIPs() {
|
||||
return networkIPs
|
||||
}
|
||||
|
||||
/**
|
||||
* 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) {
|
||||
export function printServerInfo(port = 3000) {
|
||||
const networkIPs = getNetworkIPs()
|
||||
|
||||
console.log(`\nSegnapunti Server`)
|
||||
console.log(` Display: http://127.0.0.1:${displayPort}/`)
|
||||
console.log(` Controller: http://127.0.0.1:${controllerPort}/`)
|
||||
console.log(` Display: http://127.0.0.1:${port}/display`)
|
||||
console.log(` Controller: http://127.0.0.1:${port}/controller`)
|
||||
|
||||
if (networkIPs.length > 0) {
|
||||
console.log(`\n Controller da dispositivi remoti:`)
|
||||
networkIPs.forEach(ip => {
|
||||
console.log(` http://${ip}:${controllerPort}/`)
|
||||
console.log(` http://${ip}:${port}/controller`)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,303 @@
|
||||
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()
|
||||
})
|
||||
})
|
||||
@@ -231,6 +231,52 @@ describe('Game Logic (gameState.js)', () => {
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// NUOVO SET (nuovoSet)
|
||||
// =============================================
|
||||
describe('nuovoSet', () => {
|
||||
it('dovrebbe incrementare il set della squadra vincente', () => {
|
||||
state.sp.punt.home = 25
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.set.home).toBe(1)
|
||||
expect(s.sp.set.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe azzerare i punti', () => {
|
||||
state.sp.punt.home = 25
|
||||
state.sp.punt.guest = 10
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.punt.home).toBe(0)
|
||||
expect(s.sp.punt.guest).toBe(0)
|
||||
})
|
||||
|
||||
it('dovrebbe resettare la striscia', () => {
|
||||
state.sp.punt.home = 25
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.striscia).toEqual({ home: [0], guest: [0] })
|
||||
})
|
||||
|
||||
it('dovrebbe resettare lo storico servizio', () => {
|
||||
state.sp.storicoServizio = [{ servHome: true, cambioPalla: false }]
|
||||
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
|
||||
expect(s.sp.storicoServizio).toEqual([])
|
||||
})
|
||||
|
||||
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(s.sp.set.home).toBe(0)
|
||||
expect(s.sp.set.guest).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
// =============================================
|
||||
// CAMBIO PALLA (cambiaPalla)
|
||||
// =============================================
|
||||
|
||||
@@ -102,23 +102,23 @@ describe('Server Utils', () => {
|
||||
// printServerInfo
|
||||
// =============================================
|
||||
describe('printServerInfo', () => {
|
||||
it('dovrebbe stampare le porte corrette (default)', () => {
|
||||
it('dovrebbe stampare la porta di default (3000)', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo()
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('5173')
|
||||
expect(allLogs).toContain('3001')
|
||||
expect(allLogs).toContain('3000')
|
||||
expect(allLogs).toContain('/display')
|
||||
expect(allLogs).toContain('/controller')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
it('dovrebbe stampare le porte personalizzate', () => {
|
||||
it('dovrebbe stampare la porta personalizzata', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, 4000)
|
||||
printServerInfo(8080)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('3000')
|
||||
expect(allLogs).toContain('4000')
|
||||
expect(allLogs).toContain('8080')
|
||||
consoleSpy.mockRestore()
|
||||
})
|
||||
|
||||
@@ -129,7 +129,7 @@ describe('Server Utils', () => {
|
||||
]
|
||||
})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, 3001)
|
||||
printServerInfo(3000)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).toContain('192.168.1.50')
|
||||
expect(allLogs).toContain('remoti')
|
||||
@@ -139,7 +139,7 @@ describe('Server Utils', () => {
|
||||
it('non dovrebbe mostrare sezione remoti se nessun IP di rete', () => {
|
||||
os.networkInterfaces.mockReturnValue({})
|
||||
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
|
||||
printServerInfo(3000, 3001)
|
||||
printServerInfo(3000)
|
||||
const allLogs = consoleSpy.mock.calls.map(c => c[0]).join('\n')
|
||||
expect(allLogs).not.toContain('remoti')
|
||||
consoleSpy.mockRestore()
|
||||
|
||||
+9
-106
@@ -1,30 +1,23 @@
|
||||
import { WebSocketServer } from 'ws'
|
||||
import { createServer as createHttpServer, request as httpRequest } from 'http'
|
||||
import { setupWebSocketHandler } from './src/websocket-handler.js'
|
||||
import { printServerInfo } from './src/server-utils.js'
|
||||
|
||||
const CONTROLLER_PORT = 3001
|
||||
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 })
|
||||
|
||||
// Registra i gestori WebSocket con la logica di gioco.
|
||||
setupWebSocketHandler(wss)
|
||||
|
||||
// Intercetta le richieste di upgrade WebSocket solo sul path /ws.
|
||||
// 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()
|
||||
})
|
||||
|
||||
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)
|
||||
@@ -32,100 +25,10 @@ export default function websocketPlugin() {
|
||||
}
|
||||
})
|
||||
|
||||
// Avvia un server separato per il controller sulla porta 3001.
|
||||
server.httpServer.once('listening', () => {
|
||||
const viteAddr = server.httpServer.address()
|
||||
const vitePort = viteAddr.port
|
||||
|
||||
startControllerDevServer(vitePort, wss)
|
||||
|
||||
setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT), 100)
|
||||
const { port } = server.httpServer.address()
|
||||
setTimeout(() => printServerInfo(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}`)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user