merge branch v0.17.3

This commit is contained in:
Evan
2025-03-06 15:46:57 -08:00
28 changed files with 1398 additions and 456 deletions
+1 -1
View File
@@ -32,7 +32,7 @@ export async function createGameRunner(
clientID: ClientID,
callBack: (gu: GameUpdateViewData) => void,
): Promise<GameRunner> {
const config = getConfig(gameConfig, null);
const config = await getConfig(gameConfig, null);
const gameMap = await loadGameMap(gameConfig.gameMap);
const game = createGame(
gameMap.gameMap,
+42 -16
View File
@@ -22,21 +22,24 @@ import { GameMap, TileRef } from "../game/GameMap";
import { PlayerView } from "../game/GameView";
import { UserSettings } from "../game/UserSettings";
let cachedSC: ServerConfig = null;
export enum GameEnv {
Dev,
Preprod,
Prod,
}
export function getConfig(
export async function getConfig(
gameConfig: GameConfig,
userSettings: UserSettings | null = null,
): Config {
const sc = getServerConfig();
switch (process.env.GAME_ENV) {
case "dev":
): Promise<Config> {
const sc = await getServerConfigFromClient();
switch (sc.env()) {
case GameEnv.Dev:
return new DevConfig(sc, gameConfig, userSettings);
case "preprod":
case "prod":
case GameEnv.Preprod:
case GameEnv.Prod:
consolex.log("using prod config");
return new DefaultConfig(sc, gameConfig, userSettings);
default:
@@ -44,20 +47,43 @@ export function getConfig(
}
}
export function getServerConfig(): ServerConfig {
switch (process.env.GAME_ENV) {
export async function getServerConfigFromClient(): Promise<ServerConfig> {
if (cachedSC) {
return cachedSC;
}
const response = await fetch("/api/env");
if (!response.ok) {
throw new Error(
`Failed to fetch server config: ${response.status} ${response.statusText}`,
);
}
const config = await response.json();
// Log the retrieved configuration
console.log("Server config loaded:", config);
cachedSC = getServerConfig(config.game_env);
return cachedSC;
}
export function getServerConfigFromServer(): ServerConfig {
const gameEnv = process.env.GAME_ENV;
return getServerConfig(gameEnv);
}
function getServerConfig(gameEnv: string) {
switch (gameEnv) {
case "dev":
consolex.log("using dev config");
consolex.log("using dev server config");
return new DevServerConfig();
case "preprod":
consolex.log("using preprod config");
case "staging":
consolex.log("using preprod server config");
return preprodConfig;
case "prod":
default:
consolex.log("using prod config");
consolex.log("using prod server config");
return prodConfig;
// default:
// throw Error(`unsupported server configuration: ${process.env.GAME_ENV}`)
default:
throw Error(`unsupported server configuration: ${gameEnv}`);
}
}
+3 -5
View File
@@ -28,9 +28,7 @@ export abstract class DefaultServerConfig implements ServerConfig {
adminToken(): string {
return process.env.ADMIN_TOKEN;
}
numWorkers(): number {
return 2;
}
abstract numWorkers(): number;
abstract env(): GameEnv;
abstract discordRedirectURI(): string;
turnIntervalMs(): number {
@@ -38,9 +36,9 @@ export abstract class DefaultServerConfig implements ServerConfig {
}
gameCreationRate(highTraffic: boolean): number {
if (highTraffic) {
return 30 * 1000;
return 20 * 1000;
} else {
return 60 * 1000;
return 50 * 1000;
}
}
lobbyLifetime(highTraffic: boolean): number {
+3
View File
@@ -20,6 +20,9 @@ export class DevServerConfig extends DefaultServerConfig {
discordRedirectURI(): string {
return "http://localhost:3000/auth/callback";
}
numWorkers(): number {
return 2;
}
}
export class DevConfig extends DefaultConfig {
+3
View File
@@ -8,4 +8,7 @@ export const preprodConfig = new (class extends DefaultServerConfig {
discordRedirectURI(): string {
return "https://openfront.dev/auth/callback";
}
numWorkers(): number {
return 3;
}
})();
+3
View File
@@ -2,6 +2,9 @@ import { GameEnv } from "./Config";
import { DefaultConfig, DefaultServerConfig } from "./DefaultConfig";
export const prodConfig = new (class extends DefaultServerConfig {
numWorkers(): number {
return 6;
}
env(): GameEnv {
return GameEnv.Prod;
}