From 33ea33d4dfd2bdb3cc9336a57e31f26fc0ff01c0 Mon Sep 17 00:00:00 2001 From: scamiv <6170744+scamiv@users.noreply.github.com> Date: Sat, 29 Nov 2025 12:25:07 +0100 Subject: [PATCH] test replayspeed --- src/client/LocalServer.ts | 61 +++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 12 deletions(-) diff --git a/src/client/LocalServer.ts b/src/client/LocalServer.ts index c21114911..169e2271c 100644 --- a/src/client/LocalServer.ts +++ b/src/client/LocalServer.ts @@ -18,7 +18,7 @@ import { replacer, } from "../core/Util"; import { LobbyConfig } from "./ClientGameRunner"; -import { ReplaySpeedChangeEvent } from "./InputHandler"; +import { BacklogStatusEvent, ReplaySpeedChangeEvent } from "./InputHandler"; import { getPersistentID } from "./Main"; import { defaultReplaySpeedMultiplier } from "./utilities/ReplaySpeedMultiplier"; @@ -34,6 +34,10 @@ export class LocalServer { private paused = false; private replaySpeedMultiplier = defaultReplaySpeedMultiplier; + private clientBacklog = 0; + private readonly TARGET_BACKLOG = 10; + private readonly MAX_TURNS_PER_BATCH = 50; // Prevent sending too many at once + private winner: ClientSendWinnerMessage | null = null; private allPlayersStats: AllPlayersStats = {}; @@ -51,18 +55,16 @@ export class LocalServer { ) {} start() { - this.turnCheckInterval = setInterval(() => { - const turnIntervalMs = - this.lobbyConfig.serverConfig.turnIntervalMs() * - this.replaySpeedMultiplier; + this.eventBus.on(BacklogStatusEvent, (event) => { + this.clientBacklog = event.backlogTurns; + }); - if ( - this.turnsExecuted === this.turns.length && - Date.now() > this.turnStartTime + turnIntervalMs - ) { - this.turnStartTime = Date.now(); - // End turn on the server means the client will start processing the turn. - this.endTurn(); + this.turnCheckInterval = setInterval(() => { + if (this.replaySpeedMultiplier === 0) { + // Only for fastest speed + this.handleFastestReplay(); + } else { + this.handleTimedReplay(); } }, 5); @@ -88,6 +90,41 @@ export class LocalServer { } satisfies ServerStartGameMessage); } + private handleFastestReplay() { + const turnsNeeded = Math.max( + 0, + this.TARGET_BACKLOG * 2 - this.clientBacklog, + ); + if (turnsNeeded > 0) { + const turnsToSend = Math.min(turnsNeeded, this.MAX_TURNS_PER_BATCH); + this.sendTurnBatch(turnsToSend); + } + } + + private handleTimedReplay() { + const turnIntervalMs = + this.lobbyConfig.serverConfig.turnIntervalMs() * + this.replaySpeedMultiplier; + + if ( + this.turnsExecuted === this.turns.length && + Date.now() > this.turnStartTime + turnIntervalMs + ) { + this.turnStartTime = Date.now(); + this.endTurn(); + } + } + + private sendTurnBatch(count: number) { + for (let i = 0; i < count; i++) { + if (this.turnsExecuted === this.turns.length) { + this.endTurn(); + } else { + break; // No more turns available + } + } + } + pause() { this.paused = true; }