Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/client/LocalServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ import {
import { getPersistentID } from "./Auth";
import { LobbyConfig } from "./ClientGameRunner";
import { ReplaySpeedChangeEvent } from "./InputHandler";
import { defaultReplaySpeedMultiplier } from "./utilities/ReplaySpeedMultiplier";
import {
defaultReplaySpeedMultiplier,
ReplaySpeedMultiplier,
} from "./utilities/ReplaySpeedMultiplier";

// 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.
Expand Down Expand Up @@ -64,9 +70,16 @@ export class LocalServer {
const turnIntervalMs =
this.lobbyConfig.serverConfig.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 (
this.turnsExecuted === this.turns.length &&
canQueueNextTurn &&
Date.now() > this.turnStartTime + turnIntervalMs
) {
this.turnStartTime = Date.now();
Expand Down
14 changes: 10 additions & 4 deletions src/core/GameRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,12 @@ export class GameRunner {
this.turns.push(turn);
}

public executeNextTick() {
public executeNextTick(): boolean {
if (this.isExecuting) {
return;
return false;
}
if (this.currTurn >= this.turns.length) {
return;
return false;
}
this.isExecuting = true;

Expand All @@ -144,7 +144,8 @@ export class GameRunner {
} else {
console.error("Game tick error:", error);
}
return;
this.isExecuting = false;
return false;
}

if (this.game.inSpawnPhase() && this.game.ticks() % 2 === 0) {
Expand Down Expand Up @@ -177,6 +178,11 @@ export class GameRunner {
tickExecutionDuration: tickExecutionDuration,
});
this.isExecuting = false;
return true;
}

public pendingTurns(): number {
return Math.max(0, this.turns.length - this.currTurn);
}

public playerActions(
Expand Down
15 changes: 13 additions & 2 deletions src/core/worker/Worker.worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
const ctx: Worker = self as any;
let gameRunner: Promise<GameRunner> | null = null;
const mapLoader = new FetchGameMapLoader(`/maps`, version);
const MAX_TICKS_PER_HEARTBEAT = 4;

function gameUpdate(gu: GameUpdateViewData | ErrorUpdate) {
// skip if ErrorUpdate
Expand All @@ -36,9 +37,19 @@ ctx.addEventListener("message", async (e: MessageEvent<MainThreadMessage>) => {
const message = e.data;

switch (message.type) {
case "heartbeat":
(await gameRunner)?.executeNextTick();
case "heartbeat": {
const gr = await gameRunner;
if (!gr) {
break;
}
const ticksToRun = Math.min(gr.pendingTurns(), MAX_TICKS_PER_HEARTBEAT);
for (let i = 0; i < ticksToRun; i++) {
if (!gr.executeNextTick()) {
break;
}
}
break;
}
case "init":
try {
gameRunner = createGameRunner(
Expand Down
Loading