15 Commits

Author SHA1 Message Date
davide b1a400cf81 refactor: rimuove terminal controller CLI 2026-05-12 14:54:40 +02:00
davide 4bfc12fb00 docs(readme): riscrive documentazione con guida utente e architettura 2.0.0 2026-05-12 14:49:11 +02:00
davide 496266039b chore: versione 2.0.0 2026-05-12 14:43:48 +02:00
davide 0e49d361fe docs(changelog): aggiunge versione 2.0.0 2026-05-12 14:42:55 +02:00
davide 9bbf303be9 chore(docker): usa immagine da registro Gitea v1.0.0 2026-05-12 14:38:42 +02:00
davide f38c0eaf72 chore(docker): ristruttura Dockerfile e docker-compose per produzione
- Multi-stage build: builder (npm ci + vite build) + runtime minimale
- Immagine runtime senza devDependencies e senza sorgenti frontend
- docker-compose: porta singola 3000, volume .segnapunti per persistenza stato
- Aggiunge .dockerignore per escludere node_modules, test, dist dal contesto
2026-05-12 14:21:36 +02:00
davide 1a43864919 chore: rimuove script e dipendenze inutilizzati in dev
- Rimuove script preview e start (duplicati di serve)
- Rimuove dipendenza concurrently (mai usata)
- Aggiunge persistenza stato al plugin dev WebSocket
2026-05-12 14:17:19 +02:00
davide 15dac9f965 feat(persist): salva stato su .segnapunti/state.json ad ogni azione
All'avvio il server carica lo stato dal file se esiste; ad ogni azione
lo riscrive. Il riavvio del server riprende dall'ultimo punto salvato.
2026-05-12 14:08:10 +02:00
davide 0ba49ead5d chore: rimuove dipendenze e file inutilizzati
- Rimuove wave-ui e vue-router (mai usati nell'app)
- Elimina playwright.config.ts (duplicato di .cjs)
- Elimina asset template Vite (vite.svg, vue.svg, serve.png)
- Sostituisce favicon con segnap-192x192.png
- Aggiunge dev-dist/ a .gitignore
- Rimuove selettori CSS .w-input__* (morti senza wave-ui)
2026-05-12 14:00:16 +02:00
davide c900153eed refactor(striscia): nuova struttura array-di-set, elimina storicoServizio
La striscia diventa un array di set: ogni elemento è { serv, r[] }
dove r è la sequenza di scorer ('home'|'guest') del set.

- Un rally = un elemento in r: minimo non-derivabile
- Tutti i set (passati e corrente) sono conservati nell'array
- Dal set corrente si derivano: punteggio, servizio, cambio palla, rotazione
- Dal set completo si derivano: vincitore (r.at(-1)), score finale (count)
- storicoServizio eliminato: l'undo legge l'entry precedente di r

DisplayPage calcola le strip visive (home/guest) tramite computed
da striscia.at(-1).r senza dati ridondanti nel modello.
2026-05-12 13:49:51 +02:00
davide 5f9e37062c fix(controller): aggiunge tasto indietro nel dialog set vinto
Permette di annullare l'ultimo punto (decPunt) nel caso in cui
l'ultimo punto del set sia stato assegnato per errore.
2026-05-12 13:26:19 +02:00
davide 3188994299 feat(controller): dialog set vinto con transizione automatica al set successivo
Quando una squadra raggiunge il punteggio di vittoria (25 con +2 di
scarto, 15 nel set decisivo), il controller mostra un dialog "SET VINTO"
con il nome della squadra vincente.

Alla conferma: invia l'azione nuovoSet (incrementa il set, azzera punti,
striscia, storico servizio e formazioni) e apre automaticamente il dialog
di configurazione per inserire le formazioni del set successivo.
2026-05-12 13:19:02 +02:00
davide eec4ef0526 chore: aggiunge CLAUDE.md con architettura e comandi del progetto 2026-05-12 12:22:42 +02:00
davide 16a3fb912a fix(gameState): striscia simmetrica + structuredClone + cambioPalla dedup
- Inizializza striscia con { home: [0], guest: [0] } invece di usare
  [" "] per il team non servente; corregge anche reset e cambiaPalla.
- Sostituisce JSON.parse/stringify con structuredClone (nativo, più veloce).
- Calcola cambioPalla una sola volta in incPunt invece di due volte.
2026-05-12 12:22:28 +02:00
davide 2fe1808fc9 refactor(server): porta singola, /display e /controller come percorsi
Unifica i due server Express (display :3000, controller :3001) in un
unico processo su PORT (default 3000). Le route /display e /controller
servono rispettivamente index.html e controller.html.

In sviluppo elimina il server proxy su :3001; il plugin Vite riscrive
/display → / e /controller → /controller.html internamente.
printServerInfo aggiornata alla firma a porta singola.
2026-05-12 12:22:50 +02:00
30 changed files with 545 additions and 1470 deletions
+11
View File
@@ -0,0 +1,11 @@
node_modules
dist
dev-dist
.segnapunti
tests
playwright-report
test-results
*.md
.git
.gitignore
.vscode
+2
View File
@@ -13,6 +13,8 @@ currentCommit.txt
node_modules
dist
dist-ssr
dev-dist
.segnapunti
*.local
# Editor directories and files
+29
View File
@@ -7,6 +7,35 @@ e questo progetto aderisce al [Versionamento Semantico](https://semver.org/lang/
---
## [2.0.0] - 2026-05-12
### Aggiunto
- Architettura client-server con WebSocket: server Express (`server.js`) + handler (`src/websocket-handler.js`) come unica fonte di verità; display e controller sono client separati sincronizzati in tempo reale
- Interfaccia display (`/display`) e controller (`/controller`) su porta singola `:3000`
- Robustezza connessione WebSocket: reconnect automatico con backoff esponenziale, indicatore stato connessione sul display
- Supporto query parameter `?wsHost=` per scenari WSL2 / development remoto
- Validazione cambi giocatori già in formazione lato client
- Sintesi vocale inoltrata dal controller al display via WebSocket
- Dialog set vinto sul controller al raggiungimento dei 25 punti: mostra il vincitore, permette di annullare l'ultimo punto (INDIETRO) o avanzare al set successivo con reset automatico formazioni
- Struttura striscia ottimizzata: array di set `[{ serv, r[] }]` che registra la sequenza dei punti e preserva la storia di tutti i set; elimina `storicoServizio`
- Persistenza stato su `.segnapunti/state.json`: salvato ad ogni azione, ricaricato all'avvio del server
- Suite di test completa: unit (Vitest), integration, component (Vue Test Utils + Happy-DOM), stress (50+ client), E2E (Playwright su Chromium, Firefox, Mobile Chrome)
- Dockerfile multi-stage (builder + runtime minimale) e docker-compose con volume per persistenza stato; immagine pubblicata su registro Gitea
### Modificato
- `applyAction` usa `structuredClone` al posto di `JSON.parse/stringify`
- Calcolo cambio palla deduplicato in `applyAction`
- Undo punto (`decPunt`) ricostruisce il servizio precedente dalla storia `r[]`
- `nuovoSet` come azione dedicata per la progressione regolare tra set
### Rimosso
- Terminal controller CLI (`cli.js`)
- Dipendenze inutilizzate: `wave-ui`, `vue-router`, `concurrently`
- Script npm ridondanti: `preview`, `start`, `cli`, `cli:dev`
- Asset template Vite: `vite.svg`, `vue.svg`, `serve.png`
---
## [1.0.0] - 2026-02-10
Rilascio iniziale di **Segnapunti Anto**, un'applicazione web Progressive Web App (PWA) professionale per il tracciamento in tempo reale dei punteggi durante partite di pallavolo.
+62
View File
@@ -0,0 +1,62 @@
# 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.
## Commands
```bash
npm run dev # Vite dev server — display: :5173/display, controller: :5173/controller
npm run serve # Build + run production — display: :3000/display, controller: :3000/controller
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
│ │
│ └── persist.js ── .segnapunti/state.json
```
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 `structuredClone`)
- `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`, broadcasts the new state to all clients, then calls `onStateChange` to persist state to disk.
`server.js` (Express) serves both interfaces on port 3000: `/display``dist/index.html`, `/controller``dist/controller.html`. Single WebSocket endpoint at `/ws`.
In development, `vite-plugin-websocket.js` embeds the WebSocket server inside the Vite dev server with URL rewrite middleware for `/display` and `/controller`.
## 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.
- **Persistent state** — `src/persist.js` saves state to `.segnapunti/state.json` after every action and loads it on server startup.
## 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`.
+16 -12
View File
@@ -1,18 +1,22 @@
# Stage 1: build frontend
FROM node:20-alpine AS builder
WORKDIR /usr/src/app
# Copia tutto
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Aggiunge GIT ma serve solo se si vuole evidenziare un hash del commit
RUN apk add git
# Stage 2: runtime
FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production PORT=3000
# aggiunge l'ultima versione di node
RUN npm install -g npm@latest
# Installa tutte le dipendenze del progetto
RUN npm install
COPY package*.json ./
RUN npm ci --omit=dev
# Qui fa partire il comando...
# Per adesso è dev perchè non ho capito bene il tutto... (Attilio)
CMD ["npm", "run", "serve"]
COPY server.js ./
COPY src/gameState.js src/websocket-handler.js src/server-utils.js src/persist.js ./src/
COPY --from=builder /app/dist ./dist
EXPOSE 3000
CMD ["node", "server.js"]
+156 -154
View File
@@ -1,213 +1,215 @@
# Segnapunti Anto
# Segnapunti
Applicazione web **fullstack real-time** per il tracciamento dei punteggi di partite di pallavolo, installabile come PWA.
![Version](https://img.shields.io/badge/versione-2.0.0-blue)
![Node](https://img.shields.io/badge/node-%3E%3D18-green)
![Docker](https://img.shields.io/badge/docker-ready-2496ED?logo=docker&logoColor=white)
![License](https://img.shields.io/badge/licenza-privata-lightgrey)
Segnapunti digitale in tempo reale per partite di pallavolo. Un server centrale gestisce lo stato della partita; un **display** mostra il tabellone pubblico e un **controller** (smartphone o tablet) permette all'operatore di gestire punti, formazioni e cambi.
---
## Panoramica
## Indice
**Segnapunti Anto** è un'applicazione fullstack per il tracciamento del punteggio durante partite di pallavolo, ottimizzata per tablet e smartphone in contesto sportivo.
- [Architettura](#architettura)
- [Guida utente](#guida-utente)
- [Funzionalità](#funzionalità)
- [Shortcuts tastiera](#shortcuts-tastiera)
- [Deploy con Docker](#deploy-con-docker)
- [Sviluppo](#sviluppo)
- [Test](#test)
### Architettura
---
Il sistema è composto da un **backend Node.js/Express** e due interfacce web separate:
## Architettura
| Interfaccia | Porta | Ruolo |
|-------------|-------|-------|
| **Display** | 3000 | Tabellone pubblico — mostra punteggi, formazioni e storico |
| **Controller** | 3001 | Pannello operatore — invia azioni e gestisce la partita |
```
Controller (smartphone) ──WebSocket──┐
├── Server Node.js ── gameState.js
Display (schermo) ──WebSocket──┘ │
└── .segnapunti/state.json
```
Le due interfacce comunicano tramite **WebSocket** (`/ws`): ogni azione del Controller viene elaborata dal server e trasmessa in broadcast a tutti i client connessi.
Il server è l'unica fonte di verità. Ogni azione del controller viene elaborata e trasmessa in broadcast a tutti i client connessi. Lo stato viene salvato su disco ad ogni azione e ricaricato all'avvio, sopravvivendo ai riavvii del server.
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.
| Percorso | Ruolo |
|---|---|
| `http://<host>:3000/display` | Tabellone pubblico — sola lettura |
| `http://<host>:3000/controller` | Pannello operatore — gestione partita |
| `ws://<host>:3000/ws` | WebSocket endpoint |
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.
---
## Guida utente
### Scenario tipico: schermo fisso + smartphone operatore
#### 1. Avvia il server
```bash
docker compose up -d
```
All'avvio il terminale mostra gli URL locali e di rete.
#### 2. Apri il display
Collega il PC/server allo schermo via HDMI e apri il browser a schermo intero:
```
http://localhost:3000/display
```
Se il display è su un dispositivo separato nella stessa rete:
```
http://<IP-del-server>:3000/display
```
> **Trovare l'IP:** il server lo stampa all'avvio. In alternativa usa `ip a` su Linux.
#### 3. Apri il controller sullo smartphone
Connetti il telefono alla stessa rete Wi-Fi e apri:
```
http://<IP-del-server>:3000/controller
```
> **Installazione come app:** nel browser tocca *"Aggiungi a schermata Home"* per avere il controller come icona dedicata.
---
## 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
### Display
- Nomi squadre con indicatore di servizio
- Punteggio del set corrente (grande, leggibile da lontano)
- Contatore set vinti
- Striscia storica punti del set in corso, scorrevole verso destra
- Modalità formazioni: posizioni dei 6 giocatori in campo
- Indicatore connessione WebSocket (scompare quando connesso, rosso lampeggiante se disconnesso)
### Controller
- **Punti** — `+1` per casa e ospite, con annullamento dell'ultimo punto
- **Dialog set vinto** — appare automaticamente al raggiungimento dei 25 punti (o 15 nel tie-break); permette di confermare il set o annullare l'ultimo punto
- **Formazioni** — configura i numeri di maglia; la rotazione avviene automaticamente al cambio palla
- **Cambi** — dialog `IN → OUT` con validazione
- **Servizio** — cambio manuale (disponibile solo a 0-0)
- **Visualizzazione** — alterna tra punteggio grande e formazioni in campo
- **Striscia** — mostra/nasconde lo storico punti sul display
- **Reset** — azzera la partita (richiede conferma)
### Regole pallavolo integrate
- Set 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)
| Set | Condizione di vittoria |
|---|---|
| Set 14 (modalità 3/5) o 12 (modalità 2/3) | Primo a **25** con almeno 2 punti di scarto |
| Set decisivo (tie-break) | Primo a **15** con almeno 2 punti di scarto |
---
## Requisiti
## Shortcuts tastiera
### Ambiente di sviluppo
> Valide sul controller da browser desktop.
| Requisito | Versione minima | Consigliata |
|-----------|-----------------|-------------|
| **Node.js** | `>= 18.19.0` | `24 LTS` |
| **npm** | `>= 9` | — |
| **RAM** | 2 GB | 4 GB |
| **OS** | Linux, macOS, Windows | — |
| Tasto | Azione |
|---|---|
| `Ctrl + ↑` | Punto casa |
| `Ctrl + ↓` | Annulla ultimo punto |
| `Shift + ↑` | Punto ospite |
| `Ctrl + ←` | Cambia servizio (solo a 0-0) |
| `Ctrl + M` | Apri configurazione |
| `Ctrl + C` | Cambi squadra casa |
| `Shift + C` | Cambi squadra ospite |
| `Ctrl + Z` | Toggle punteggio / formazioni |
| `Ctrl + S` | Annuncio vocale punteggio |
| `Ctrl + B` | Mostra/nascondi barra pulsanti |
| `Ctrl + F` | Fullscreen |
### Test E2E
---
I test end-to-end richiedono i browser Playwright. Su Linux potrebbero essere necessarie dipendenze di sistema aggiuntive.
## Deploy con Docker
### Prima installazione
```bash
npx playwright install chromium firefox
# Linux (con dipendenze di sistema):
# npx playwright install --with-deps chromium firefox
docker compose up -d
```
### Requisiti browser (utente finale)
Lo stato viene salvato nel volume Docker `segnapunti-state` e sopravvive ai riavvii del container.
| API | Utilizzo | Necessità |
|-----|----------|-----------|
| JavaScript ES6+ | Moduli, async/await | Obbligatorio |
| WebSocket | Sincronizzazione stato live | Obbligatorio |
| Service Worker | Supporto PWA offline | Consigliato |
| Web Speech API | Annunci vocali punteggio | Opzionale |
**Browser testati:** Chrome/Chromium, Firefox, Mobile Chrome (Playwright Pixel 5).
---
## Installazione
### Aggiornamento a nuova versione
```bash
git clone https://santantonio.sytes.net/attilio/segnapunti.git
cd segnapunti
npm install
docker compose pull && docker compose up -d
```
### Build e pubblicazione immagine
```bash
docker build \
-t santantonio.sytes.net/attilio/segnapunti:2.0.0 \
-t santantonio.sytes.net/attilio/segnapunti:latest .
docker push santantonio.sytes.net/attilio/segnapunti:2.0.0
docker push santantonio.sytes.net/attilio/segnapunti:latest
```
---
## Sviluppo
### Dev server
### Requisiti
```bash
npm run dev
```
Avvia il server Vite con hot reload:
- `http://localhost:5173/` — Display
- `http://localhost:5173/controller.html` — Controller
### Build di produzione
```bash
npm run build
```
Genera la cartella `dist/` con asset ottimizzati, manifest e service worker PWA.
### Avvio in produzione (locale)
```bash
npm run serve
```
Espone i due server:
- `http://localhost:3000` — Display
- `http://localhost:3001` — Controller
---
## Terminal Controller (CLI)
Il CLI è un controller da terminale che si connette al server via WebSocket e permette di gestire la partita senza browser.
| Strumento | Versione minima |
|---|---|
| Node.js | >= 18 |
| npm | >= 9 |
### Avvio
```bash
# 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>
npm install
npm run dev
```
Il CLI richiede che il server sia già in esecuzione in un altro terminale.
| URL | Interfaccia |
|---|---|
| `http://localhost:5173/display` | Display |
| `http://localhost:5173/controller` | Controller |
Lo stato viene salvato in `.segnapunti/state.json` anche in modalità dev.
### Comandi disponibili
#### Punteggio
| 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
| Comando | Descrizione |
|---|---|
| `npm run dev` | Dev server con hot reload |
| `npm run build` | Build di produzione in `dist/` |
| `npm run serve` | Build + avvio server produzione |
---
## Test
La suite di test copre tutti i livelli dell'applicazione:
| Comando | Descrizione |
|---|---|
| `npm run test:unit` | Unit + integration (Vitest) |
| `npm run test:component` | Componenti Vue (Happy-DOM) |
| `npm run test:stress` | Load test WebSocket (50+ client) |
| `npm run test:all` | Tutti i test tranne E2E |
| `npm run test:e2e` | Playwright — Chromium, Firefox, Mobile Chrome |
| 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) |
Per la guida completa ai test, consultare [`tests/README.md`](tests/README.md).
> I test E2E richiedono il server in esecuzione (`npm run serve`) e i browser Playwright installati:
> ```bash
> npx playwright install chromium firefox
> ```
---
## Docker
## Changelog
```bash
docker-compose up --build
```
Espone le porte `3000` (Display) e `3001` (Controller).
Vedere [CHANGELOG.md](CHANGELOG.md) per la storia delle versioni.
-302
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();
});
+1 -1
View File
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/png" href="/segnap-192x192.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Segnapunti - Controller</title>
</head>
+8 -4
View File
@@ -1,8 +1,12 @@
services:
segnapunti:
build: .
image: santantonio.sytes.net/attilio/segnapunti:1.0.0
container_name: segnapunti
ports:
- 3000:3000
- 3001:3001
container_name: segnapunti-container
- "3000:3000"
volumes:
- segnapunti-state:/app/.segnapunti
restart: unless-stopped
volumes:
segnapunti-state:
+1 -1
View File
@@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<link rel="icon" type="image/png" href="/segnap-192x192.png" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Segnapunti - Anto</title>
</head>
-296
View File
@@ -10,8 +10,6 @@
"dependencies": {
"express": "^5.2.1",
"vue": "^3.2.47",
"vue-router": "^4.6.4",
"wave-ui": "^3.3.0",
"ws": "^8.19.0"
},
"devDependencies": {
@@ -21,7 +19,6 @@
"@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": "^7.3.1",
@@ -3318,11 +3315,6 @@
"@vue/shared": "3.5.28"
}
},
"node_modules/@vue/devtools-api": {
"version": "6.6.4",
"resolved": "https://registry.npmjs.org/@vue/devtools-api/-/devtools-api-6.6.4.tgz",
"integrity": "sha512-sGhTPMuXqZ1rVOk32RylztWkfXTRhuS7vgAKv0zjqk8gbsHkJ7xfFf+jbySxt7tWObEJwyKaHMikV/WGDiQm8g=="
},
"node_modules/@vue/reactivity": {
"version": "3.5.28",
"resolved": "https://registry.npmjs.org/@vue/reactivity/-/reactivity-3.5.28.tgz",
@@ -3722,20 +3714,6 @@
"node": ">=18"
}
},
"node_modules/cliui": {
"version": "8.0.1",
"resolved": "https://registry.npmjs.org/cliui/-/cliui-8.0.1.tgz",
"integrity": "sha512-BSeNnyus75C4//NQ9gQt1/csTXyo/8Sb+afLAkzAptFuMsod9HFokGNudZpi/oQV73hnVK+sR+5PVRMd+Dr7YQ==",
"dev": true,
"dependencies": {
"string-width": "^4.2.0",
"strip-ansi": "^6.0.1",
"wrap-ansi": "^7.0.0"
},
"engines": {
"node": ">=12"
}
},
"node_modules/commander": {
"version": "2.20.3",
"resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz",
@@ -3757,115 +3735,6 @@
"integrity": "sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==",
"dev": true
},
"node_modules/concurrently": {
"version": "9.2.1",
"resolved": "https://registry.npmjs.org/concurrently/-/concurrently-9.2.1.tgz",
"integrity": "sha512-fsfrO0MxV64Znoy8/l1vVIjjHa29SZyyqPgQBwhiDcaW8wJc2W3XWVOGx4M3oJBnv/zdUZIIp1gDeS98GzP8Ng==",
"dev": true,
"dependencies": {
"chalk": "4.1.2",
"rxjs": "7.8.2",
"shell-quote": "1.8.3",
"supports-color": "8.1.1",
"tree-kill": "1.2.2",
"yargs": "17.7.2"
},
"bin": {
"conc": "dist/bin/concurrently.js",
"concurrently": "dist/bin/concurrently.js"
},
"engines": {
"node": ">=18"
},
"funding": {
"url": "https://github.com/open-cli-tools/concurrently?sponsor=1"
}
},
"node_modules/concurrently/node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/concurrently/node_modules/chalk": {
"version": "4.1.2",
"resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz",
"integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==",
"dev": true,
"dependencies": {
"ansi-styles": "^4.1.0",
"supports-color": "^7.1.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/chalk?sponsor=1"
}
},
"node_modules/concurrently/node_modules/chalk/node_modules/supports-color": {
"version": "7.2.0",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz",
"integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==",
"dev": true,
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=8"
}
},
"node_modules/concurrently/node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/concurrently/node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/concurrently/node_modules/has-flag": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz",
"integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==",
"dev": true,
"engines": {
"node": ">=8"
}
},
"node_modules/concurrently/node_modules/supports-color": {
"version": "8.1.1",
"resolved": "https://registry.npmjs.org/supports-color/-/supports-color-8.1.1.tgz",
"integrity": "sha512-MpUEN2OodtUzxvKQl72cUF7RQ5EiHsGvSsVG0ia9c5RbWGL2CI4C7EpPS8UTBIplnlzZiNuV56w+FuNxy3ty2Q==",
"dev": true,
"dependencies": {
"has-flag": "^4.0.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/supports-color?sponsor=1"
}
},
"node_modules/config-chain": {
"version": "1.1.13",
"resolved": "https://registry.npmjs.org/config-chain/-/config-chain-1.1.13.tgz",
@@ -4669,15 +4538,6 @@
"node": ">=6.9.0"
}
},
"node_modules/get-caller-file": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/get-caller-file/-/get-caller-file-2.0.5.tgz",
"integrity": "sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg==",
"dev": true,
"engines": {
"node": "6.* || 8.* || >= 10.*"
}
},
"node_modules/get-intrinsic": {
"version": "1.3.0",
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
@@ -6285,15 +6145,6 @@
"jsesc": "bin/jsesc"
}
},
"node_modules/require-directory": {
"version": "2.1.1",
"resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz",
"integrity": "sha512-fGxEI7+wsG9xrvdjsrlmL22OMTTiHRwAMroiEeMgq8gzoLC/PQr7RsRDSTLUg/bZAZtF+TVIkHc6/4RIKrui+Q==",
"dev": true,
"engines": {
"node": ">=0.10.0"
}
},
"node_modules/require-from-string": {
"version": "2.0.2",
"resolved": "https://registry.npmjs.org/require-from-string/-/require-from-string-2.0.2.tgz",
@@ -6351,15 +6202,6 @@
"node": ">= 18"
}
},
"node_modules/rxjs": {
"version": "7.8.2",
"resolved": "https://registry.npmjs.org/rxjs/-/rxjs-7.8.2.tgz",
"integrity": "sha512-dhKf903U/PQZY6boNNtAGdWbG85WAbjT/1xYoZIC7FAY0yWapOBQVsVrDl58W86//e1VpMNBtRV4MaXfdMySFA==",
"dev": true,
"dependencies": {
"tslib": "^2.1.0"
}
},
"node_modules/safe-regex-test": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/safe-regex-test/-/safe-regex-test-1.0.0.tgz",
@@ -6480,18 +6322,6 @@
"node": ">=8"
}
},
"node_modules/shell-quote": {
"version": "1.8.3",
"resolved": "https://registry.npmjs.org/shell-quote/-/shell-quote-1.8.3.tgz",
"integrity": "sha512-ObmnIF4hXNg1BqhnHmgbDETF8dLPCggZWBjkQfhZpbszZnYur5DUljTcCHii5LC3J5E0yeO/1LIMyH+UvHQgyw==",
"dev": true,
"engines": {
"node": ">= 0.4"
},
"funding": {
"url": "https://github.com/sponsors/ljharb"
}
},
"node_modules/side-channel": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/side-channel/-/side-channel-1.1.0.tgz",
@@ -6998,21 +6828,6 @@
"punycode": "^2.1.0"
}
},
"node_modules/tree-kill": {
"version": "1.2.2",
"resolved": "https://registry.npmjs.org/tree-kill/-/tree-kill-1.2.2.tgz",
"integrity": "sha512-L0Orpi8qGpRG//Nd+H90vFB+3iHnue1zSSGmNOOCh1GLJ7rUKVwV2HvijphGQS2UmhUZewS9VgvxYIdgr+fG1A==",
"dev": true,
"bin": {
"tree-kill": "cli.js"
}
},
"node_modules/tslib": {
"version": "2.8.1",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.8.1.tgz",
"integrity": "sha512-oJFu94HQb+KVduSUQL7wnpmqnfmLsOA/nAh6b6EH0wCEoK0/mPeXU6c3wKDV83MkOuHPRHtSXKKU99IBazS/2w==",
"dev": true
},
"node_modules/type-fest": {
"version": "0.16.0",
"resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.16.0.tgz",
@@ -7547,20 +7362,6 @@
"integrity": "sha512-YbGqHZ5/eW4SnkPNR44mKVc6ZKQoRs/Rux1sxC6rdwXb4qpbOSYfDr9DsTHolOTGmIKgM9j141mZbBeg05R1pw==",
"dev": true
},
"node_modules/vue-router": {
"version": "4.6.4",
"resolved": "https://registry.npmjs.org/vue-router/-/vue-router-4.6.4.tgz",
"integrity": "sha512-Hz9q5sa33Yhduglwz6g9skT8OBPii+4bFn88w6J+J4MfEo4KRRpmiNG/hHHkdbRFlLBOqxN8y8gf2Fb0MTUgVg==",
"dependencies": {
"@vue/devtools-api": "^6.6.4"
},
"funding": {
"url": "https://github.com/sponsors/posva"
},
"peerDependencies": {
"vue": "^3.5.0"
}
},
"node_modules/w3c-xmlserializer": {
"version": "5.0.0",
"resolved": "https://registry.npmjs.org/w3c-xmlserializer/-/w3c-xmlserializer-5.0.0.tgz",
@@ -7573,17 +7374,6 @@
"node": ">=18"
}
},
"node_modules/wave-ui": {
"version": "3.3.0",
"resolved": "https://registry.npmjs.org/wave-ui/-/wave-ui-3.3.0.tgz",
"integrity": "sha512-z4hBt/tOFMwG3S+pNE1+is+6diSSgll7zeYOwr84v4+mdE1o+u1M4zTRwqYx1NvLE9DqeXD3iplCjhrxEjsziA==",
"funding": {
"url": "https://github.com/sponsors/antoniandre"
},
"peerDependencies": {
"vue": "^2.6.14 || ^3.2.0"
}
},
"node_modules/webidl-conversions": {
"version": "4.0.2",
"resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-4.0.2.tgz",
@@ -7896,23 +7686,6 @@
"workbox-core": "7.4.0"
}
},
"node_modules/wrap-ansi": {
"version": "7.0.0",
"resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz",
"integrity": "sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==",
"dev": true,
"dependencies": {
"ansi-styles": "^4.0.0",
"string-width": "^4.1.0",
"strip-ansi": "^6.0.0"
},
"engines": {
"node": ">=10"
},
"funding": {
"url": "https://github.com/chalk/wrap-ansi?sponsor=1"
}
},
"node_modules/wrap-ansi-cjs": {
"name": "wrap-ansi",
"version": "7.0.0",
@@ -7964,39 +7737,6 @@
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/wrap-ansi/node_modules/ansi-styles": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz",
"integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==",
"dev": true,
"dependencies": {
"color-convert": "^2.0.1"
},
"engines": {
"node": ">=8"
},
"funding": {
"url": "https://github.com/chalk/ansi-styles?sponsor=1"
}
},
"node_modules/wrap-ansi/node_modules/color-convert": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
"integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==",
"dev": true,
"dependencies": {
"color-name": "~1.1.4"
},
"engines": {
"node": ">=7.0.0"
}
},
"node_modules/wrap-ansi/node_modules/color-name": {
"version": "1.1.4",
"resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz",
"integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==",
"dev": true
},
"node_modules/wrappy": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz",
@@ -8037,47 +7777,11 @@
"integrity": "sha512-JZnDKK8B0RCDw84FNdDAIpZK+JuJw+s7Lz8nksI7SIuU3UXJJslUthsi+uWBUYOwPFwW7W7PRLRfUKpxjtjFCw==",
"dev": true
},
"node_modules/y18n": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/y18n/-/y18n-5.0.8.tgz",
"integrity": "sha512-0pfFzegeDWJHJIAmTLRP2DwHjdF5s7jo9tuztdQxAhINCdvS+3nGINqPd00AphqJR/0LhANUS6/+7SCb98YOfA==",
"dev": true,
"engines": {
"node": ">=10"
}
},
"node_modules/yallist": {
"version": "3.1.1",
"resolved": "https://registry.npmjs.org/yallist/-/yallist-3.1.1.tgz",
"integrity": "sha512-a4UGQaWPH59mOXUYnAG2ewncQS4i4F43Tv3JoAM+s2VDAmS9NsK8GpDMLrCHPksFT7h3K6TOoUNn2pb7RoXx4g==",
"dev": true
},
"node_modules/yargs": {
"version": "17.7.2",
"resolved": "https://registry.npmjs.org/yargs/-/yargs-17.7.2.tgz",
"integrity": "sha512-7dSzzRQ++CKnNI/krKnYRV7JKKPUXMEh61soaHKg9mrWEhzFWhFnxPxGl+69cD1Ou63C13NUPCnmIcrvqCuM6w==",
"dev": true,
"dependencies": {
"cliui": "^8.0.1",
"escalade": "^3.1.1",
"get-caller-file": "^2.0.5",
"require-directory": "^2.1.1",
"string-width": "^4.2.3",
"y18n": "^5.0.5",
"yargs-parser": "^21.1.1"
},
"engines": {
"node": ">=12"
}
},
"node_modules/yargs-parser": {
"version": "21.1.1",
"resolved": "https://registry.npmjs.org/yargs-parser/-/yargs-parser-21.1.1.tgz",
"integrity": "sha512-tVpsJW7DdjecAiFpbIB1e3qxIQsE6NoPc5/eTdrbbIC4h0LVsWhnoa3g+m2HclBIujHzsxZ4VJVA+GUuc2/LBw==",
"dev": true,
"engines": {
"node": ">=12"
}
}
}
}
+1 -8
View File
@@ -1,16 +1,12 @@
{
"name": "segnapuntianto",
"private": true,
"version": "0.0.0",
"version": "2.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"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",
@@ -24,8 +20,6 @@
"dependencies": {
"express": "^5.2.1",
"vue": "^3.2.47",
"vue-router": "^4.6.4",
"wave-ui": "^3.3.0",
"ws": "^8.19.0"
},
"overrides": {
@@ -38,7 +32,6 @@
"@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": "^7.3.1",
-76
View File
@@ -1,76 +0,0 @@
import { defineConfig, devices } from '@playwright/test';
/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// import dotenv from 'dotenv';
// import path from 'path';
// dotenv.config({ path: path.resolve(__dirname, '.env') });
/**
* See https://playwright.dev/docs/test-configuration.
*/
export default defineConfig({
testDir: './tests/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('')`. */
// baseURL: 'http://localhost:3000',
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},
/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},
{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},
// {
// name: 'webkit',
// use: { ...devices['Desktop Safari'] },
// },
/* Test against mobile viewports. */
{
name: 'Mobile Chrome',
use: { ...devices['Pixel 5'] },
},
/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
// },
],
/* Run your local dev server before starting the tests */
webServer: {
command: 'npm run serve',
url: 'http://localhost:3000',
reuseExistingServer: !process.env.CI,
timeout: 120 * 1000,
},
});
-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

Before

Width:  |  Height:  |  Size: 1.5 KiB

+15 -53
View File
@@ -5,36 +5,31 @@ import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import { setupWebSocketHandler } from './src/websocket-handler.js'
import { printServerInfo } from './src/server-utils.js'
import { loadState, saveState } from './src/persist.js'
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)
setupWebSocketHandler(wss, { initialState: loadState(), onStateChange: saveState })
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 +40,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)
})
Binary file not shown.

Before

Width:  |  Height:  |  Size: 12 KiB

-1
View File
@@ -1 +0,0 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="37.07" height="36" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 198"><path fill="#41B883" d="M204.8 0H256L128 220.8L0 0h97.92L128 51.2L157.44 0h47.36Z"></path><path fill="#41B883" d="m0 0l128 220.8L256 0h-51.2L128 132.48L50.56 0H0Z"></path><path fill="#35495E" d="M50.56 0L128 133.12L204.8 0h-47.36L128 51.2L97.92 0H50.56Z"></path></svg>

Before

Width:  |  Height:  |  Size: 496 B

+63 -2
View File
@@ -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,
@@ -209,7 +224,7 @@ export default {
visuStriscia: true,
modalitaPartita: "3/5",
sp: {
striscia: { home: [0], guest: [0] },
striscia: [{ serv: 'home', r: [] }],
servHome: true,
punt: { home: 0, guest: 0 },
set: { home: 0, guest: 0 },
@@ -218,7 +233,6 @@ export default {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
},
storicoServizio: [],
},
},
}
@@ -227,6 +241,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 +263,14 @@ export default {
return allValid && hasComplete
}
},
watch: {
squadraVincente(val) {
if (val && !this.showSetVinto) {
this.setVintoTeam = val
this.showSetVinto = true
}
},
},
mounted() {
this.connectWebSocket()
@@ -440,6 +471,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 +794,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;
+22 -17
View File
@@ -92,16 +92,16 @@
<div class="striscia" v-if="state.visuStriscia">
<span class="striscia-nome text-bold">{{ state.sp.nomi.home }}</span>
<div class="striscia-items" ref="homeItems">
<div v-for="(h, i) in state.sp.striscia.home" :key="'sh'+i"
class="item" :class="{ 'item-vuoto': String(h).trim() === '' }">
{{ String(h) }}
<div v-for="(h, i) in stricciaStrip.home" :key="'sh'+i"
class="item" :class="{ 'item-vuoto': h === ' ' }">
{{ h }}
</div>
</div>
<span class="striscia-nome text-bold guest-striscia">{{ state.sp.nomi.guest }}</span>
<div class="striscia-items guest-striscia" ref="guestItems">
<div v-for="(h, i) in state.sp.striscia.guest" :key="'sg'+i"
class="item" :class="{ 'item-vuoto': String(h).trim() === '' }">
{{ String(h) }}
<div v-for="(h, i) in stricciaStrip.guest" :key="'sg'+i"
class="item" :class="{ 'item-vuoto': h === ' ' }">
{{ h }}
</div>
</div>
</div>
@@ -132,7 +132,7 @@ export default {
visuStriscia: true,
modalitaPartita: "3/5",
sp: {
striscia: { home: [0], guest: [0] },
striscia: [{ serv: 'home', r: [] }],
servHome: true,
punt: { home: 0, guest: 0 },
set: { home: 0, guest: 0 },
@@ -141,7 +141,6 @@ export default {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
},
storicoServizio: [],
},
},
}
@@ -192,23 +191,29 @@ export default {
this.ws = null
}
},
watch: {
'state.sp.striscia.home': {
deep: true,
handler() {
this.$nextTick(() => {
if (this.$refs.homeItems) this.$refs.homeItems.scrollLeft = this.$refs.homeItems.scrollWidth
})
computed: {
stricciaStrip() {
const currentSet = this.state.sp.striscia.at(-1)
if (!currentSet) return { home: [], guest: [] }
let h = 0, g = 0
const home = [], guest = []
for (const scorer of currentSet.r) {
if (scorer === 'home') { h++; home.push(h); guest.push(' ') }
else { g++; guest.push(g); home.push(' ') }
}
return { home, guest }
},
'state.sp.striscia.guest': {
},
watch: {
'state.sp.striscia': {
deep: true,
handler() {
this.$nextTick(() => {
if (this.$refs.homeItems) this.$refs.homeItems.scrollLeft = this.$refs.homeItems.scrollWidth
if (this.$refs.guestItems) this.$refs.guestItems.scrollLeft = this.$refs.guestItems.scrollWidth
})
}
}
},
},
methods: {
isMobile() {
-3
View File
@@ -1,9 +1,6 @@
import { createApp } from 'vue'
import './style.css'
import WaveUI from 'wave-ui'
import 'wave-ui/dist/wave-ui.css'
import ControllerPage from './components/ControllerPage.vue'
const app = createApp(ControllerPage)
app.use(WaveUI)
app.mount('#app')
+39 -60
View File
@@ -1,8 +1,3 @@
/**
* Logica di gioco condivisa per il segnapunti.
* Utilizzata sia dal server WebSocket sia dal client per l'anteprima locale.
*/
export function createInitialState() {
return {
order: true,
@@ -10,7 +5,7 @@ export function createInitialState() {
visuStriscia: true,
modalitaPartita: "3/5",
sp: {
striscia: { home: [0], guest: [" "] },
striscia: [{ serv: 'home', r: [] }],
servHome: true,
punt: { home: 0, guest: 0 },
set: { home: 0, guest: 0 },
@@ -19,7 +14,6 @@ export function createInitialState() {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
},
storicoServizio: [],
},
}
}
@@ -31,50 +25,26 @@ export function checkVittoria(state) {
const setGuest = state.sp.set.guest
const totSet = setHome + setGuest
let isSetDecisivo = false
if (state.modalitaPartita === "2/3") {
isSetDecisivo = totSet >= 2
} else {
isSetDecisivo = totSet >= 4
}
const isSetDecisivo = state.modalitaPartita === "2/3" ? totSet >= 2 : totSet >= 4
const punteggioVittoria = isSetDecisivo ? 15 : 25
if (puntHome >= punteggioVittoria && puntHome - puntGuest >= 2) {
return true
}
if (puntGuest >= punteggioVittoria && puntGuest - puntHome >= 2) {
return true
}
if (puntHome >= punteggioVittoria && puntHome - puntGuest >= 2) return true
if (puntGuest >= punteggioVittoria && puntGuest - puntHome >= 2) return true
return false
}
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.punt[team]++
if (team === "home") {
s.sp.striscia.home.push(s.sp.punt.home)
s.sp.striscia.guest.push(" ")
} else {
s.sp.striscia.guest.push(s.sp.punt.guest)
s.sp.striscia.home.push(" ")
}
s.sp.striscia.at(-1).r.push(team)
const cambioPalla = (team === "home" && !s.sp.servHome) || (team === "guest" && s.sp.servHome)
if (cambioPalla) {
s.sp.form[team].push(s.sp.form[team].shift())
}
@@ -84,23 +54,22 @@ export function applyAction(state, action) {
}
case "decPunt": {
if (s.sp.storicoServizio.length > 0) {
const tmpHome = s.sp.striscia.home.pop()
s.sp.striscia.guest.pop()
const statoServizio = s.sp.storicoServizio.pop()
const currentSet = s.sp.striscia.at(-1)
if (currentSet.r.length === 0) break
if (tmpHome === " ") {
s.sp.punt.guest--
if (statoServizio.cambioPalla) {
s.sp.form.guest.unshift(s.sp.form.guest.pop())
}
} else {
s.sp.punt.home--
if (statoServizio.cambioPalla) {
s.sp.form.home.unshift(s.sp.form.home.pop())
}
}
s.sp.servHome = statoServizio.servHome
const lastScorer = currentSet.r[currentSet.r.length - 1]
const prevServer = currentSet.r.length >= 2
? currentSet.r[currentSet.r.length - 2]
: currentSet.serv
const wasCambioPalla = lastScorer !== prevServer
currentSet.r.pop()
s.sp.punt[lastScorer]--
s.sp.servHome = prevServer === 'home'
if (wasCambioPalla) {
s.sp.form[lastScorer].unshift(s.sp.form[lastScorer].pop())
}
break
}
@@ -115,12 +84,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.servHome = team === 'home'
s.sp.striscia.push({ serv: team, r: [] })
s.sp.form = {
home: ["1", "2", "3", "4", "5", "6"],
guest: ["1", "2", "3", "4", "5", "6"],
}
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.at(-1).serv = s.sp.servHome ? 'home' : 'guest'
}
break
}
@@ -135,10 +117,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.storicoServizio = []
s.sp.striscia = [{ serv: s.sp.servHome ? 'home' : 'guest', r: [] }]
break
}
-6
View File
@@ -1,12 +1,6 @@
import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import WaveUI from 'wave-ui'
import 'wave-ui/dist/wave-ui.css'
import DisplayPage from './components/DisplayPage.vue'
// In modalità display-only, non serve il router.
// Il display viene montato direttamente.
const app = createApp(DisplayPage)
app.use(WaveUI)
app.mount('#app')
+26
View File
@@ -0,0 +1,26 @@
import { readFileSync, writeFileSync, mkdirSync, existsSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { createInitialState } from './gameState.js'
const STATE_PATH = join(dirname(fileURLToPath(import.meta.url)), '..', '.segnapunti', 'state.json')
export function loadState() {
try {
if (existsSync(STATE_PATH)) {
return JSON.parse(readFileSync(STATE_PATH, 'utf8'))
}
} catch (err) {
console.warn('[Persist] Stato non leggibile, si riparte da zero:', err.message)
}
return createInitialState()
}
export function saveState(state) {
try {
mkdirSync(dirname(STATE_PATH), { recursive: true })
writeFileSync(STATE_PATH, JSON.stringify(state), 'utf8')
} catch (err) {
console.error('[Persist] Salvataggio fallito:', err.message)
}
}
+4 -14
View File
@@ -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`)
})
}
+4 -12
View File
@@ -248,9 +248,7 @@ button:focus-visible {
max-width: 64px;
}
.cambi-input input,
.cambi-input .w-input__input,
.cambi-input .w-input__field {
.cambi-input input {
border: 2px solid rgba(255, 255, 255, 0.35);
border-radius: 8px;
padding: 6px 10px;
@@ -259,21 +257,15 @@ button:focus-visible {
box-sizing: border-box;
}
.cambi-in input,
.cambi-in .w-input__input,
.cambi-in .w-input__field {
.cambi-in input {
background: rgba(120, 200, 120, 0.4);
}
.cambi-out input,
.cambi-out .w-input__input,
.cambi-out .w-input__field {
.cambi-out input {
background: rgba(200, 120, 120, 0.4);
}
.cambi-input input:focus,
.cambi-input .w-input__input:focus,
.cambi-input .w-input__field:focus {
.cambi-input input:focus {
border-color: rgba(255, 255, 255, 0.7);
outline: none;
}
+3 -2
View File
@@ -5,9 +5,9 @@ import { createInitialState, applyAction } from './gameState.js'
* @param {WebSocketServer} wss - Istanza del server WebSocket.
* @returns {Object} Oggetto con metodi di gestione dello stato.
*/
export function setupWebSocketHandler(wss) {
export function setupWebSocketHandler(wss, options = {}) {
// Stato globale della partita.
let gameState = createInitialState()
let gameState = options.initialState ?? createInitialState()
// Mappa dei ruoli associati ai client connessi.
const clients = new Map() // ws -> { role: 'display' | 'controller' }
@@ -100,6 +100,7 @@ export function setupWebSocketHandler(wss) {
// Propaga il nuovo stato a tutti i client connessi.
broadcastState()
options.onStateChange?.(gameState)
}
/**
-303
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()
})
})
+63 -27
View File
@@ -31,13 +31,10 @@ describe('Game Logic (gameState.js)', () => {
expect(state.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
})
it('dovrebbe avere la striscia iniziale a [0]', () => {
expect(state.sp.striscia.home).toEqual([0])
expect(state.sp.striscia.guest).toEqual([0])
})
it('dovrebbe avere storico servizio vuoto', () => {
expect(state.sp.storicoServizio).toEqual([])
it('dovrebbe avere la striscia iniziale con un set vuoto', () => {
expect(state.sp.striscia).toHaveLength(1)
expect(state.sp.striscia[0].serv).toBe('home')
expect(state.sp.striscia[0].r).toEqual([])
})
it('dovrebbe avere modalità 3/5 di default', () => {
@@ -116,21 +113,19 @@ describe('Game Logic (gameState.js)', () => {
it('dovrebbe aggiornare la striscia per punto Home', () => {
const s = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s.sp.striscia.home).toEqual([0, 1])
expect(s.sp.striscia.guest).toEqual([0, " "])
expect(s.sp.striscia.at(-1).r).toEqual(['home'])
})
it('dovrebbe aggiornare la striscia per punto Guest', () => {
const s = applyAction(state, { type: 'incPunt', team: 'guest' })
expect(s.sp.striscia.guest).toEqual([0, 1])
expect(s.sp.striscia.home).toEqual([0, " "])
expect(s.sp.striscia.at(-1).r).toEqual(['guest'])
})
it('dovrebbe registrare lo storico servizio', () => {
const s = applyAction(state, { type: 'incPunt', team: 'home' })
expect(s.sp.storicoServizio).toHaveLength(1)
expect(s.sp.storicoServizio[0]).toHaveProperty('servHome')
expect(s.sp.storicoServizio[0]).toHaveProperty('cambioPalla')
it('dovrebbe registrare scorer nella striscia', () => {
let s = applyAction(state, { type: 'incPunt', team: 'home' })
s = applyAction(s, { type: 'incPunt', team: 'guest' })
s = applyAction(s, { type: 'incPunt', team: 'home' })
expect(s.sp.striscia.at(-1).r).toEqual(['home', 'guest', 'home'])
})
it('non dovrebbe incrementare i punti dopo vittoria', () => {
@@ -185,7 +180,7 @@ describe('Game Logic (gameState.js)', () => {
it('dovrebbe ripristinare la striscia', () => {
const s1 = applyAction(state, { type: 'incPunt', team: 'home' })
const s2 = applyAction(s1, { type: 'decPunt' })
expect(s2.sp.striscia.home).toEqual([0])
expect(s2.sp.striscia.at(-1).r).toEqual([])
})
it('dovrebbe gestire undo multipli in sequenza', () => {
@@ -231,6 +226,53 @@ 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 aggiungere un nuovo set vuoto alla striscia', () => {
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.striscia).toHaveLength(2)
expect(s.sp.striscia.at(-1).r).toEqual([])
expect(s.sp.striscia.at(-1).serv).toBe('home')
})
it('dovrebbe conservare il set precedente nella striscia', () => {
state.sp.striscia[0].r = ['home', 'guest', 'home']
const s = applyAction(state, { type: 'nuovoSet', team: 'home' })
expect(s.sp.striscia[0].r).toEqual(['home', 'guest', 'home'])
})
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)
// =============================================
@@ -618,17 +660,11 @@ describe('Game Logic (gameState.js)', () => {
expect(s.sp.form.guest).toEqual(["1", "2", "3", "4", "5", "6"])
})
it('dovrebbe resettare la striscia', () => {
state.sp.striscia = { home: [0, 1, 2, 3], guest: [0, " ", " ", 1] }
it('dovrebbe resettare la striscia a un set vuoto', () => {
state.sp.striscia = [{ serv: 'home', r: ['home', 'guest', 'home'] }, { serv: 'home', r: ['guest'] }]
const s = applyAction(state, { type: 'resetta' })
expect(s.sp.striscia.home).toEqual([0])
expect(s.sp.striscia.guest).toEqual([0])
})
it('dovrebbe resettare lo storico servizio', () => {
state.sp.storicoServizio = [{ servHome: true, cambioPalla: false }]
const s = applyAction(state, { type: 'resetta' })
expect(s.sp.storicoServizio).toEqual([])
expect(s.sp.striscia).toHaveLength(1)
expect(s.sp.striscia[0].r).toEqual([])
})
it('dovrebbe impostare visuForm a false', () => {
+9 -9
View File
@@ -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()
+10 -106
View File
@@ -1,30 +1,24 @@
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'
import { loadState, saveState } from './src/persist.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 })
setupWebSocketHandler(wss, { initialState: loadState(), onStateChange: saveState })
// Registra i gestori WebSocket con la logica di gioco.
setupWebSocketHandler(wss)
// 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()
})
// Intercetta le richieste di upgrade WebSocket solo sul path /ws.
server.httpServer.on('upgrade', (request, socket, head) => {
const pathname = new URL(request.url, `http://${request.headers.host}`).pathname
if (pathname === '/ws') {
wss.handleUpgrade(request, socket, head, (ws) => {
wss.emit('connection', ws, request)
@@ -32,100 +26,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}`)
})
}