mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-16 15:42:53 +00:00
test replayspeed
This commit is contained in:
+49
-12
@@ -18,7 +18,7 @@ import {
|
|||||||
replacer,
|
replacer,
|
||||||
} from "../core/Util";
|
} from "../core/Util";
|
||||||
import { LobbyConfig } from "./ClientGameRunner";
|
import { LobbyConfig } from "./ClientGameRunner";
|
||||||
import { ReplaySpeedChangeEvent } from "./InputHandler";
|
import { BacklogStatusEvent, ReplaySpeedChangeEvent } from "./InputHandler";
|
||||||
import { getPersistentID } from "./Main";
|
import { getPersistentID } from "./Main";
|
||||||
import { defaultReplaySpeedMultiplier } from "./utilities/ReplaySpeedMultiplier";
|
import { defaultReplaySpeedMultiplier } from "./utilities/ReplaySpeedMultiplier";
|
||||||
|
|
||||||
@@ -34,6 +34,10 @@ export class LocalServer {
|
|||||||
private paused = false;
|
private paused = false;
|
||||||
private replaySpeedMultiplier = defaultReplaySpeedMultiplier;
|
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 winner: ClientSendWinnerMessage | null = null;
|
||||||
private allPlayersStats: AllPlayersStats = {};
|
private allPlayersStats: AllPlayersStats = {};
|
||||||
|
|
||||||
@@ -51,18 +55,16 @@ export class LocalServer {
|
|||||||
) {}
|
) {}
|
||||||
|
|
||||||
start() {
|
start() {
|
||||||
this.turnCheckInterval = setInterval(() => {
|
this.eventBus.on(BacklogStatusEvent, (event) => {
|
||||||
const turnIntervalMs =
|
this.clientBacklog = event.backlogTurns;
|
||||||
this.lobbyConfig.serverConfig.turnIntervalMs() *
|
});
|
||||||
this.replaySpeedMultiplier;
|
|
||||||
|
|
||||||
if (
|
this.turnCheckInterval = setInterval(() => {
|
||||||
this.turnsExecuted === this.turns.length &&
|
if (this.replaySpeedMultiplier === 0) {
|
||||||
Date.now() > this.turnStartTime + turnIntervalMs
|
// Only for fastest speed
|
||||||
) {
|
this.handleFastestReplay();
|
||||||
this.turnStartTime = Date.now();
|
} else {
|
||||||
// End turn on the server means the client will start processing the turn.
|
this.handleTimedReplay();
|
||||||
this.endTurn();
|
|
||||||
}
|
}
|
||||||
}, 5);
|
}, 5);
|
||||||
|
|
||||||
@@ -88,6 +90,41 @@ export class LocalServer {
|
|||||||
} satisfies ServerStartGameMessage);
|
} 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() {
|
pause() {
|
||||||
this.paused = true;
|
this.paused = true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user