Files
Cs2-Mapveto/server/src/servers/demoAdapter.js
T

77 lines
2.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// DEMO-ADAPTER: Es wird KEIN echter CS2-Server gestartet.
// Er simuliert nur Startdauer und Konfiguration, damit der Ablauf ohne Hosting getestet werden kann.
// Das Flag simulated=true sorgt dafür, dass das Frontend den Server ausdrücklich als Simulation kennzeichnet.
import { PassThrough } from 'node:stream';
import { backupPrefix, parseBackupFiles } from '../rounds.js';
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
export class DemoServerAdapter {
constructor({ startSeconds = 20, failFirstConfigure = false } = {}) {
this.name = 'demo';
this.simulated = true;
this.startMs = startSeconds * 1000;
this.failFirstConfigure = failFirstConfigure;
this.configureCalls = new Map();
}
async start({ matchId }) {
return {
handle: { demo: true, matchId, startedAt: Date.now() },
host: 'demo.invalid',
port: 27015,
tvPort: 27020,
};
}
async status(handle) {
if (handle.stopped) return { state: 'stopped', message: 'Simulation beendet' };
const elapsed = Date.now() - handle.startedAt;
if (elapsed < this.startMs) {
return { state: 'starting', message: `Simulierter Start (${Math.ceil((this.startMs - elapsed) / 1000)} s)` };
}
return { state: 'running', message: 'Simulation – kein echter Server', playerCount: null };
}
async configure(handle, plan) {
await sleep(1500);
const calls = (this.configureCalls.get(handle.matchId) ?? 0) + 1;
this.configureCalls.set(handle.matchId, calls);
if (this.failFirstConfigure && calls === 1) {
throw new Error('Simulierter Konfigurationsfehler (DEMO_FAIL_FIRST_CONFIGURE) – bitte erneut versuchen');
}
handle.map = plan.map;
}
// Simulierte Sicherungen: eine Datei je Runde der aktuellen Map, damit die Oberfläche testbar ist
async listRoundBackups(handle, matchId, map = 'de_demo') {
const files = [1, 2, 3].map((r) => `${backupPrefix(matchId)}_01_01_${map}_round${r}_score_${r}_0.txt`);
return parseBackupFiles(files.join('\n'), matchId);
}
async loadRoundBackup() {}
/** Simulierte Konsolenausgabe, damit die Konsolen-Ansicht ohne echten Server testbar ist. */
streamLogs(handle, { intervalMs = 1000 } = {}) {
const stdout = new PassThrough();
const stderr = new PassThrough();
let n = 0;
stdout.write('Demo-Adapter: kein echter Container, die Ausgabe ist simuliert\n');
const timer = setInterval(() => {
n += 1;
stdout.write(`[Demo] Server läuft (${handle.map ?? 'de_demo'}), Zeile ${n}\n`);
}, intervalMs);
const proc = { stdout, stderr, kill() { clearInterval(timer); stdout.end(); stderr.end(); proc.onExit?.(0); }, on(evt, fn) { if (evt === 'close') proc.onExit = fn; return proc; } };
return proc;
}
async sendCommands(handle, commands) {
return commands.map(() => '(Demo – nicht ausgeführt)');
}
async stop(handle) {
handle.stopped = true;
}
}