Files
OpenFrontIO/src/client/LocalServer.ts
T
Evan 275fd0dccc refactor: collapse per-env Configs into ClientEnv + ServerEnv (#3906)
## Description:

This is a refactor to simplify config handling.

Replaces the per-environment DevConfig/PreprodConfig/ProdConfig class
hierarchy with two static classes: ClientEnv (browser main thread, reads
from window.BOOTSTRAP_CONFIG) and ServerEnv (Node server, reads from
process.env). The four config classes are deleted, the abstract
DefaultServerConfig is gone, and DefaultConfig is renamed to Config.

The values that flow server → client (gameEnv, numWorkers,
turnstileSiteKey, jwtAudience, instanceId) used to be baked into the
hardcoded per-env classes. They're now real env vars on the server,
embedded into a single window.BOOTSTRAP_CONFIG object in index.html at
request time (alongside the existing gitCommit/assetManifest/cdnBase
globals, which moved into the same object), and read back by ClientEnv
on the client. The dev defaults previously hidden inside DevServerConfig
are now explicit in start:server-dev (NUM_WORKERS=2,
TURNSTILE_SITE_KEY=1x..., JWT_AUDIENCE=localhost, etc.) and in
vite.config.ts's html plugin inject.data. Production deploys plumb
NUM_WORKERS and TURNSTILE_SITE_KEY through deploy.yml (GitHub vars) into
the remote env file; JWT_AUDIENCE is derived from DOMAIN in deploy.sh.
The dynamic /api/instance endpoint is gone — INSTANCE_ID rides along in
BOOTSTRAP_CONFIG now.

ServerEnv is the only thing server code touches; ClientEnv is
browser-only. The two classes have intentional overlap (env, numWorkers,
jwtIssuer, gameCreationRate, workerIndex, etc.) since they derive
identical logic from different sources — there's a TODO in each to
consolidate via a shared helper later. The game-logic Config no longer
stores a ServerConfig/ClientEnv reference and its serverConfig() getter
is gone; the one caller (MultiTabModal) now reads ClientEnv.env()
directly. Worker init no longer carries server-config values since
nothing in the worker actually reads them.

## Please complete the following:

- [x] I have added screenshots for all UI updates
- [x] I process any text displayed to the user through translateText()
and I've added it to the en.json file
- [x] I have added relevant tests to the test directory
- [x] I confirm I have thoroughly tested these changes and take full
responsibility for any bugs introduced

## Please put your Discord username so you can be contacted if a bug or
regression is found:

evan
2026-05-11 19:24:01 -07:00

355 lines
11 KiB
TypeScript

import { ClientEnv } from "src/client/ClientEnv";
import { z } from "zod";
import { EventBus } from "../core/EventBus";
import {
AllPlayersStats,
ClientID,
ClientMessage,
ClientSendWinnerMessage,
PartialGameRecordSchema,
PlayerRecord,
ServerMessage,
ServerStartGameMessage,
StampedIntent,
Turn,
} from "../core/Schemas";
import {
createPartialGameRecord,
decompressGameRecord,
replacer,
} from "../core/Util";
import { getPersistentID } from "./Auth";
import { LobbyConfig } from "./ClientGameRunner";
import {
GameSpeedDownIntentEvent,
GameSpeedUpIntentEvent,
ReplaySpeedChangeEvent,
} from "./InputHandler";
import {
defaultReplaySpeedMultiplier,
ReplaySpeedMultiplier,
} from "./utilities/ReplaySpeedMultiplier";
// Order: 0.5, 1, 2, max (same as ReplayPanel)
const SPEED_ORDER: ReplaySpeedMultiplier[] = [
ReplaySpeedMultiplier.slow,
ReplaySpeedMultiplier.normal,
ReplaySpeedMultiplier.fast,
ReplaySpeedMultiplier.fastest,
];
// build a small backlog so MAX can catch up.
const MAX_REPLAY_BACKLOG_TURNS = 60;
export class LocalServer {
// All turns from the game record on replay.
private replayTurns: Turn[] = [];
private turns: Turn[] = [];
private intents: StampedIntent[] = [];
private startedAt: number;
private paused = false;
private replaySpeedMultiplier = defaultReplaySpeedMultiplier;
private clientID: ClientID | undefined;
private winner: ClientSendWinnerMessage | null = null;
private allPlayersStats: AllPlayersStats = {};
private turnsExecuted = 0;
private turnStartTime = 0;
private turnCheckInterval: NodeJS.Timeout;
private clientConnect: () => void;
private clientMessage: (message: ServerMessage) => void;
constructor(
private lobbyConfig: LobbyConfig,
private isReplay: boolean,
private eventBus: EventBus,
) {}
public updateCallback(
clientConnect: () => void,
clientMessage: (message: ServerMessage) => void,
) {
this.clientConnect = clientConnect;
this.clientMessage = clientMessage;
}
start() {
console.log("local server starting");
this.turnCheckInterval = setInterval(() => {
const turnIntervalMs =
ClientEnv.turnIntervalMs() * this.replaySpeedMultiplier;
const backlog = Math.max(0, this.turns.length - this.turnsExecuted);
const allowReplayBacklog =
this.replaySpeedMultiplier === ReplaySpeedMultiplier.fastest &&
this.lobbyConfig.gameRecord !== undefined;
const maxBacklog = allowReplayBacklog ? MAX_REPLAY_BACKLOG_TURNS : 0;
const canQueueNextTurn =
backlog === 0 || (maxBacklog > 0 && backlog < maxBacklog);
if (
canQueueNextTurn &&
Date.now() > this.turnStartTime + turnIntervalMs
) {
this.turnStartTime = Date.now();
// End turn on the server means the client will start processing the turn.
this.endTurn();
}
}, 5);
this.eventBus.on(ReplaySpeedChangeEvent, (event) => {
this.replaySpeedMultiplier = event.replaySpeedMultiplier;
});
if (!this.isReplay) {
this.eventBus.on(GameSpeedUpIntentEvent, () => {
const idx = SPEED_ORDER.indexOf(this.replaySpeedMultiplier);
if (idx < 0 || idx >= SPEED_ORDER.length - 1) return;
this.replaySpeedMultiplier = SPEED_ORDER[idx + 1];
this.eventBus.emit(
new ReplaySpeedChangeEvent(this.replaySpeedMultiplier),
);
});
this.eventBus.on(GameSpeedDownIntentEvent, () => {
const idx = SPEED_ORDER.indexOf(this.replaySpeedMultiplier);
if (idx <= 0) return;
this.replaySpeedMultiplier = SPEED_ORDER[idx - 1];
this.eventBus.emit(
new ReplaySpeedChangeEvent(this.replaySpeedMultiplier),
);
});
}
this.startedAt = Date.now();
this.clientConnect();
if (this.lobbyConfig.gameRecord) {
this.replayTurns = decompressGameRecord(
this.lobbyConfig.gameRecord,
).turns;
}
if (this.lobbyConfig.gameStartInfo === undefined) {
throw new Error("missing gameStartInfo");
}
this.clientID = this.lobbyConfig.gameStartInfo.players[0]?.clientID;
if (!this.clientID) {
throw new Error("missing clientID");
}
this.clientMessage({
type: "start",
gameStartInfo: this.lobbyConfig.gameStartInfo,
turns: [],
lobbyCreatedAt: this.lobbyConfig.gameStartInfo.lobbyCreatedAt,
// Don't send myClientID for replays — viewer has no player identity.
myClientID: this.lobbyConfig.gameRecord ? undefined : this.clientID,
} satisfies ServerStartGameMessage);
}
onMessage(clientMsg: ClientMessage) {
if (clientMsg.type === "rejoin") {
if (!this.clientID) {
throw new Error("missing clientID");
}
this.clientMessage({
type: "start",
gameStartInfo: this.lobbyConfig.gameStartInfo!,
turns: this.turns,
lobbyCreatedAt: this.lobbyConfig.gameStartInfo!.lobbyCreatedAt,
myClientID: this.lobbyConfig.gameRecord ? undefined : this.clientID,
} satisfies ServerStartGameMessage);
}
if (clientMsg.type === "intent") {
// Server stamps clientID - client doesn't send it
const stampedIntent = {
...clientMsg.intent,
clientID: this.clientID!,
};
if (stampedIntent.type === "toggle_pause") {
if (stampedIntent.paused) {
// Pausing: add intent and end turn before pause takes effect
this.intents.push(stampedIntent);
this.endTurn();
this.paused = true;
} else {
// Unpausing: clear pause flag before adding intent so next turn can execute
this.paused = false;
this.intents.push(stampedIntent);
this.endTurn();
}
return;
}
// Don't process non-pause intents during replays or while paused
if (this.lobbyConfig.gameRecord || this.paused) {
return;
}
this.intents.push(stampedIntent);
}
if (clientMsg.type === "hash") {
if (!this.lobbyConfig.gameRecord) {
if (clientMsg.turnNumber % 100 === 0) {
// In singleplayer, only store hash every 100 turns to reduce size of game record.
const turn = this.turns[clientMsg.turnNumber];
if (turn) {
turn.hash = clientMsg.hash;
}
}
return;
}
// If we are replaying a game then verify hash.
const archivedHash = this.replayTurns[clientMsg.turnNumber].hash;
if (!archivedHash) {
console.warn(
`no archived hash found for turn ${clientMsg.turnNumber}, client hash: ${clientMsg.hash}`,
);
return;
}
if (archivedHash !== clientMsg.hash) {
console.error(
`desync detected on turn ${clientMsg.turnNumber}, client hash: ${clientMsg.hash}, server hash: ${archivedHash}`,
);
this.clientMessage({
type: "desync",
turn: clientMsg.turnNumber,
correctHash: archivedHash,
clientsWithCorrectHash: 0,
totalActiveClients: 1,
yourHash: clientMsg.hash,
});
} else {
console.log(
`hash verified on turn ${clientMsg.turnNumber}, client hash: ${clientMsg.hash}, server hash: ${archivedHash}`,
);
}
}
if (clientMsg.type === "winner") {
this.winner = clientMsg;
this.allPlayersStats = clientMsg.allPlayersStats;
}
}
// This is so the client can tell us when it finished processing the turn.
public turnComplete() {
this.turnsExecuted++;
}
// endTurn in this context means the server has collected all the intents
// and will send the turn to the client.
private endTurn() {
if (this.paused) {
return;
}
if (this.replayTurns.length > 0) {
if (this.turns.length >= this.replayTurns.length) {
this.endGame();
return;
}
this.intents = this.replayTurns[this.turns.length].intents;
}
const pastTurn: Turn = {
turnNumber: this.turns.length,
intents: this.intents,
};
this.turns.push(pastTurn);
this.intents = [];
this.clientMessage({
type: "turn",
turn: pastTurn,
});
}
public endGame() {
console.log("local server ending game");
clearInterval(this.turnCheckInterval);
if (this.isReplay) {
return;
}
const players: PlayerRecord[] = [
{
persistentID: getPersistentID(),
username: this.lobbyConfig.playerName,
clanTag: this.lobbyConfig.playerClanTag ?? null,
clientID: this.clientID!,
stats: this.allPlayersStats[this.clientID!],
cosmetics: this.lobbyConfig.gameStartInfo?.players[0].cosmetics,
},
];
if (this.lobbyConfig.gameStartInfo === undefined) {
throw new Error("missing gameStartInfo");
}
const record = createPartialGameRecord(
this.lobbyConfig.gameStartInfo.gameID,
this.lobbyConfig.gameStartInfo.config,
players,
this.turns,
this.startedAt,
Date.now(),
this.winner?.winner,
);
const result = PartialGameRecordSchema.safeParse(record);
if (!result.success) {
const error = z.prettifyError(result.error);
console.error("Error parsing game record", error);
return;
}
const workerPath = ClientEnv.workerPath(
this.lobbyConfig.gameStartInfo.gameID,
);
const jsonString = JSON.stringify(result.data, replacer);
compress(jsonString)
.then((compressedData) => {
return fetch(`/${workerPath}/api/archive_singleplayer_game`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"Content-Encoding": "gzip",
},
body: compressedData,
keepalive: true, // Ensures request completes even if page unloads
});
})
.catch((error) => {
console.error("Failed to archive singleplayer game:", error);
});
}
}
async function compress(data: string): Promise<ArrayBuffer> {
const stream = new CompressionStream("gzip");
const writer = stream.writable.getWriter();
const reader = stream.readable.getReader();
// Write the data to the compression stream
writer.write(new TextEncoder().encode(data));
writer.close();
// Read the compressed data
const chunks: Uint8Array[] = [];
let done = false;
while (!done) {
const { value, done: readerDone } = await reader.read();
done = readerDone;
if (value) {
chunks.push(value);
}
}
// Combine all chunks into a single Uint8Array
const totalLength = chunks.reduce((acc, chunk) => acc + chunk.length, 0);
const compressedData = new Uint8Array(totalLength);
let offset = 0;
for (const chunk of chunks) {
compressedData.set(chunk, offset);
offset += chunk.length;
}
return compressedData.buffer;
}