2025-05-28 02:29:36 +02:00
|
|
|
// server/server.js
|
|
|
|
|
|
2025-05-28 02:02:42 +02:00
|
|
|
const express = require('express');
|
|
|
|
|
const http = require('http');
|
2025-05-28 02:29:36 +02:00
|
|
|
const ws = require('ws');
|
2025-05-28 02:18:40 +02:00
|
|
|
const path = require('path');
|
2025-05-28 02:02:42 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
|
|
2025-05-28 02:29:36 +02:00
|
|
|
// --- Server Setup ---
|
2025-05-28 02:02:42 +02:00
|
|
|
const app = express();
|
|
|
|
|
const server = http.createServer(app);
|
2025-05-28 02:29:36 +02:00
|
|
|
const wss = new ws.Server({ server });
|
2025-05-28 02:02:42 +02:00
|
|
|
|
2025-05-28 02:29:36 +02:00
|
|
|
// Statisches Hosting von /public
|
2025-05-28 02:18:40 +02:00
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
2025-05-28 02:02:42 +02:00
|
|
|
|
2025-05-28 02:29:36 +02:00
|
|
|
// --- Spirits laden ---
|
2025-05-28 02:30:37 +02:00
|
|
|
const SPIRITS_PATH = path.join(__dirname, '.', 'spirits', 'spirit_list.json');
|
2025-05-28 02:29:36 +02:00
|
|
|
let spirits = [];
|
|
|
|
|
try {
|
|
|
|
|
spirits = JSON.parse(fs.readFileSync(SPIRITS_PATH, 'utf8'));
|
|
|
|
|
if (!Array.isArray(spirits) || spirits.length === 0) throw 'Spirit-Liste leer oder ungültig!';
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error('Fehler beim Laden der Spirits:', e);
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --- WebSocket Logik ---
|
|
|
|
|
wss.on('connection', (socket) => {
|
|
|
|
|
console.log('Neuer Client verbunden');
|
|
|
|
|
|
|
|
|
|
// Sende sofort den aktuellen Spirit
|
2025-05-28 03:17:43 +02:00
|
|
|
/*
|
2025-05-28 02:29:36 +02:00
|
|
|
if (typeof currentSpiritIndex === 'number') {
|
|
|
|
|
socket.send(JSON.stringify({
|
|
|
|
|
type: 'spirit',
|
|
|
|
|
data: spirits[currentSpiritIndex]
|
|
|
|
|
}));
|
2025-05-28 03:17:43 +02:00
|
|
|
}*/
|
2025-05-28 02:02:42 +02:00
|
|
|
});
|
|
|
|
|
|
2025-05-28 02:29:36 +02:00
|
|
|
// --- Spirit-Zeitsteuerung ---
|
|
|
|
|
let currentSpiritIndex = Math.floor(Math.random() * spirits.length);
|
|
|
|
|
|
|
|
|
|
function pushSpiritToAllClients() {
|
|
|
|
|
const spirit = spirits[currentSpiritIndex];
|
|
|
|
|
const payload = JSON.stringify({ type: 'spirit', data: spirit });
|
|
|
|
|
wss.clients.forEach(client => {
|
|
|
|
|
if (client.readyState === ws.OPEN) {
|
|
|
|
|
client.send(payload);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
console.log(`Spirit "${spirit.name}" wurde an alle Clients gepusht.`);
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-28 03:12:32 +02:00
|
|
|
// --- Timer: Alle 20 Sekunden neues Spirit ---
|
2025-05-28 02:29:36 +02:00
|
|
|
setInterval(() => {
|
|
|
|
|
currentSpiritIndex = Math.floor(Math.random() * spirits.length);
|
|
|
|
|
pushSpiritToAllClients();
|
2025-05-28 03:18:28 +02:00
|
|
|
}, 5000);
|
2025-05-28 02:29:36 +02:00
|
|
|
|
|
|
|
|
// Beim Start: Ersten Spirit pushen
|
|
|
|
|
setTimeout(pushSpiritToAllClients, 1500);
|
|
|
|
|
|
|
|
|
|
// --- Server Start ---
|
2025-05-28 02:18:40 +02:00
|
|
|
const PORT = process.env.PORT || 3000;
|
2025-05-28 02:02:42 +02:00
|
|
|
server.listen(PORT, () => {
|
2025-05-28 02:29:36 +02:00
|
|
|
console.log(`Server läuft auf http://localhost:${PORT}`);
|
2025-05-28 02:02:42 +02:00
|
|
|
});
|