feat: salva partite su SQLite e aggiunge storico in dev

- Aggiunge src/db.js con better-sqlite3: tabella partite con nomi,
  modalità, set, formazione di partenza per set, punteggi e vincitore
- Salvataggio automatico al termine della partita (websocket-handler.js)
- Aggiunge formInizioSet in gameState per tracciare la formazione
  iniziale di ogni set
- Aggiunge storico.html: pagina vanilla dark-theme con lista partite
  espandibili (set, punteggio, formazioni)
- Aggiunge server storico su porta 3002 in dev (vite-plugin-websocket.js)
- Aggiunge endpoint /api/partite su displayApp (server.js)
- Migliora banner di avvio con URL storico locale e da rete
- Migliora log WebSocket: connessione aperta, ruolo unregistered, client rimanenti
- Aggiorna .gitignore: ignora tutta la cartella data/
This commit is contained in:
2026-02-21 18:36:58 +01:00
parent d9e1ac751f
commit 1df239ed3d
10 changed files with 856 additions and 7 deletions

View File

@@ -1,9 +1,15 @@
import { WebSocketServer } from 'ws'
import { createServer as createHttpServer, request as httpRequest } from 'http'
import { readFile } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
import { setupWebSocketHandler } from './src/websocket-handler.js'
import { printServerInfo } from './src/server-utils.js'
import { getPartite, getPartita } from './src/db.js'
const __dirname = dirname(fileURLToPath(import.meta.url))
const CONTROLLER_PORT = 3001
const STORICO_PORT = process.env.STORICO_PORT || 3002
const DEV_PROXY_HOST = process.env.DEV_PROXY_HOST || '127.0.0.1'
/**
@@ -38,8 +44,9 @@ export default function websocketPlugin() {
const vitePort = viteAddr.port
startControllerDevServer(vitePort, wss)
startStoricoDevServer()
setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT), 100)
setTimeout(() => printServerInfo(vitePort, CONTROLLER_PORT, STORICO_PORT), 100)
})
}
}
@@ -129,3 +136,63 @@ function startControllerDevServer(vitePort, wss) {
console.log(`[Controller] Dev server running on port ${CONTROLLER_PORT}`)
})
}
/**
* Avvia il server di sviluppo per lo storico sulla porta 3002.
* Serve storico.html e gli endpoint /api/partite.
*/
function startStoricoDevServer() {
const storicoServer = createHttpServer((req, res) => {
const url = new URL(req.url, `http://${req.headers.host}`)
const pathname = url.pathname
if (pathname === '/api/partite') {
try {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(getPartite()))
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: err.message }))
}
return
}
const matchId = pathname.match(/^\/api\/partite\/(\d+)$/)
if (matchId) {
try {
const p = getPartita(Number(matchId[1]))
if (!p) {
res.writeHead(404, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: 'Not found' }))
} else {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end(JSON.stringify(p))
}
} catch (err) {
res.writeHead(500, { 'Content-Type': 'application/json' })
res.end(JSON.stringify({ error: err.message }))
}
return
}
if (pathname === '/' || pathname === '') {
readFile(join(__dirname, 'storico.html'), (err, data) => {
if (err) {
res.writeHead(500)
res.end('Error loading storico.html')
} else {
res.writeHead(200, { 'Content-Type': 'text/html' })
res.end(data)
}
})
return
}
res.writeHead(404)
res.end('Not found')
})
storicoServer.listen(STORICO_PORT, '0.0.0.0', () => {
console.log(`[Storico] http://localhost:${STORICO_PORT}`)
})
}