Files
tiefenkontakt/apps/server/src/match-store.ts
T
db9979 7b0a071e8e Add bridge lookouts, radar room, ops tooling, graphic mails and Pi deploy
- Escorts: bridge lookouts (periscope feather, snorkel, surfaced boats,
  torpedo tracks), spoken sighting reports, 3D bridge station with
  binoculars and own foredeck, lookout hit inset
- 3D sea split into models/shaders: multi-wave ocean with whitecaps,
  clouds, sun and moon, detailed ships with smoke and turning radar
- Radar room rebuilt as phosphor PPI with range scales, ESM rose and
  radar status; NATO symbols on plot, mission charts and replay
- Sortable and filterable contact list, leaderboard and alert log;
  smoothed list bearings; clear convoy progress bar
- Ops: backups, metrics, structured logs, CI, Elo ratings, private
  matches, corvette and coastal sub, network load test
- Mail: configurable SMTP (Gmail/iCloud), signal-style HTML mails with
  inline sonar header, mail test command
- Fixes: spoofable client IP behind proxy, phone layout and lost taps,
  stacked debriefs, station 5 rejected by server
- Raspberry Pi deploy script and .env.example
2026-11-22 09:15:08 +01:00

58 lines
1.7 KiB
TypeScript

import { mkdirSync, readFileSync, readdirSync, renameSync, rmSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
import type { Lobby } from './lobby.js';
import { log } from './log.js';
import { Match, type SavedMatch } from './match.js';
/** Running matches are written to disk so a server restart doesn't end them. */
const SAFE_ID = /^[a-f0-9-]{8}$/;
export class MatchStore {
constructor(private dir = process.env.MATCH_DIR ?? '.matches') {
mkdirSync(this.dir, { recursive: true });
}
private file(id: string): string | undefined {
return SAFE_ID.test(id) ? join(this.dir, `${id}.json`) : undefined;
}
saveAll(lobby: Lobby): void {
for (const m of lobby.allMatches()) {
const f = this.file(m.id);
if (!f) continue;
const data = m.save();
if (!data) {
if (m.state === 'ended') this.remove(m.id);
continue;
}
writeFileSync(`${f}.tmp`, JSON.stringify(data));
renameSync(`${f}.tmp`, f);
}
}
remove(id: string): void {
const f = this.file(id);
if (f) rmSync(f, { force: true });
}
loadAll(lobby: Lobby): number {
let n = 0;
for (const name of readdirSync(this.dir)) {
if (!name.endsWith('.json')) continue;
const f = this.file(name.slice(0, -5));
if (!f) continue;
try {
const data = JSON.parse(readFileSync(f, 'utf8')) as SavedMatch;
if (data.v !== 1) continue;
lobby.adopt(Match.restore(data));
n++;
} catch (e) {
log('warn', 'Spielstand nicht lesbar, wird verworfen', { file: name, error: (e as Error).message });
rmSync(f, { force: true });
}
}
return n;
}
}