Refactor: Split config into Config and ServerConfig

This commit is contained in:
evanpelle
2024-12-25 14:04:19 -08:00
parent 111775a3f4
commit 57cbf5c55e
16 changed files with 101 additions and 79 deletions
+5 -5
View File
@@ -2,7 +2,6 @@ import { Executor } from "../core/execution/ExecutionManager";
import { Cell, MutableGame, PlayerEvent, PlayerID, MutablePlayer, TileEvent, Player, Game, UnitEvent, Tile, PlayerType, GameMap, Difficulty, GameType } from "../core/game/Game";
import { createGame } from "../core/game/GameImpl";
import { EventBus } from "../core/EventBus";
import { Config, getConfig } from "../core/configuration/Config";
import { createRenderer, GameRenderer } from "./graphics/GameRenderer";
import { InputHandler, MouseUpEvent, ZoomEvent, DragEvent, MouseDownEvent } from "./InputHandler"
import { ClientID, ClientIntentMessageSchema, ClientJoinMessageSchema, ClientMessageSchema, GameConfig, GameID, Intent, ServerMessage, ServerMessageSchema, ServerSyncMessage, Turn } from "../core/Schemas";
@@ -14,6 +13,7 @@ import { createCanvas } from "./Utils";
import { DisplayMessageEvent, MessageType } from "./graphics/layers/EventsDisplay";
import { WorkerClient } from "../core/worker/WorkerClient";
import { consolex, initRemoteSender } from "../core/Consolex";
import { getConfig, getServerConfig } from "../core/configuration/Config";
export interface LobbyConfig {
playerName: () => string
@@ -32,7 +32,7 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
consolex.log(`joinging lobby: gameID: ${lobbyConfig.gameID}, clientID: ${lobbyConfig.clientID}, persistentID: ${lobbyConfig.persistentID}`)
const config = getConfig()
const serverConfig = getServerConfig()
let gameConfig: GameConfig = null
if (lobbyConfig.gameType == GameType.Singleplayer) {
@@ -47,7 +47,7 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
lobbyConfig,
gameConfig,
eventBus,
config,
serverConfig,
)
const onconnect = () => {
@@ -70,11 +70,11 @@ export function joinLobby(lobbyConfig: LobbyConfig, onjoin: () => void): () => v
export async function createClientGame(lobbyConfig: LobbyConfig, gameConfig: GameConfig, eventBus: EventBus, transport: Transport): Promise<GameRunner> {
const config = getConfig()
const config = getConfig(gameConfig)
const terrainMap = await loadTerrainMap(gameConfig.gameMap);
let game = createGame(terrainMap.map, terrainMap.miniMap, eventBus, config, gameConfig)
let game = createGame(terrainMap.map, terrainMap.miniMap, eventBus, config)
const worker = new WorkerClient(game, gameConfig.gameMap)
consolex.log('going to init path finder')
+3 -3
View File
@@ -1,4 +1,4 @@
import { Config } from "../core/configuration/Config";
import { Config, ServerConfig } from "../core/configuration/Config";
import { consolex } from "../core/Consolex";
import { ClientID, ClientMessage, ClientMessageSchema, GameConfig, GameID, GameRecordSchema, Intent, PlayerRecord, ServerMessage, ServerStartGameMessageSchema, ServerTurnMessageSchema, Turn } from "../core/Schemas";
import { CreateGameRecord, generateID } from "../core/Util";
@@ -16,7 +16,7 @@ export class LocalServer {
constructor(
private config: Config,
private serverConfig: ServerConfig,
private gameConfig: GameConfig,
private lobbyConfig: LobbyConfig,
private clientConnect: () => void,
@@ -26,7 +26,7 @@ export class LocalServer {
start() {
this.startedAt = Date.now()
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.config.turnIntervalMs());
this.endTurnIntervalID = setInterval(() => this.endTurn(), this.serverConfig.turnIntervalMs());
this.clientConnect()
this.clientMessage(ServerStartGameMessageSchema.parse({
type: "start",
View File
+3 -3
View File
@@ -1,4 +1,4 @@
import { Config } from "../core/configuration/Config"
import { Config, ServerConfig } from "../core/configuration/Config"
import { SendLogEvent } from "../core/Consolex"
import { EventBus, GameEvent } from "../core/EventBus"
import { AllianceRequest, AllPlayers, Cell, GameType, Player, PlayerID, PlayerType, Tile, UnitType } from "../core/game/Game"
@@ -104,7 +104,7 @@ export class Transport {
// gameConfig only set on private games
private gameConfig: GameConfig | null,
private eventBus: EventBus,
private config: Config,
private serverConfig: ServerConfig,
) {
this.isLocal = lobbyConfig.gameType == GameType.Singleplayer
@@ -154,7 +154,7 @@ export class Transport {
}
private connectLocal(onconnect: () => void, onmessage: (message: ServerMessage) => void) {
this.localServer = new LocalServer(this.config, this.gameConfig, this.lobbyConfig, onconnect, onmessage)
this.localServer = new LocalServer(this.serverConfig, this.gameConfig, this.lobbyConfig, onconnect, onmessage)
this.localServer.start()
}
+1 -1
View File
@@ -35,7 +35,7 @@ export class UILayer implements Layer {
const barHeight = 15;
const barBackgroundWidth = this.transformHandler.width();
const ratio = this.game.ticks() / this.game.config().numSpawnPhaseTurns(this.game.gameConfig().gameType)
const ratio = this.game.ticks() / this.game.config().numSpawnPhaseTurns(this.game.config().gameConfig().gameType)
// Draw bar background
context.fillStyle = 'rgba(0, 0, 0, 0.5)';