use error update to show error modal

This commit is contained in:
evanpelle
2025-01-23 19:40:52 -08:00
committed by Evan
parent 4bbb63fd48
commit d009ef925c
4 changed files with 35 additions and 13 deletions
+8 -6
View File
@@ -8,7 +8,7 @@ import { loadTerrainFromFile, loadTerrainMap } from "../core/game/TerrainMapLoad
import { SendAttackIntentEvent, SendSpawnIntentEvent, Transport } from "./Transport";
import { createCanvas } from "./Utils";
import { MessageType } from '../core/game/Game';
import { DisplayMessageUpdate } from "../core/game/GameUpdates";
import { DisplayMessageUpdate, ErrorUpdate } from "../core/game/GameUpdates";
import { WorkerClient } from "../core/worker/WorkerClient";
import { consolex, initRemoteSender } from "../core/Consolex";
import { getConfig, getServerConfig } from "../core/configuration/Config";
@@ -121,9 +121,11 @@ export class ClientGameRunner {
this.renderer.initialize()
this.input.initialize()
this.worker.start((gu: GameUpdateViewData) => {
const size = gu.packedTileUpdates.length * 4 / 1000
// console.log(`game update size: ${size}kb`)
this.worker.start((gu: GameUpdateViewData | ErrorUpdate) => {
if ('errMsg' in gu) {
showErrorModal(gu.errMsg, gu.stack, this.clientID)
return
}
this.gameView.update(gu)
this.renderer.tick()
})
@@ -209,8 +211,8 @@ export class ClientGameRunner {
}
}
function showErrorModal(error: Error, clientID: ClientID) {
const errorText = `Error: ${error.message}\nStack: ${error.stack}`;
function showErrorModal(errMsg: string, stack: string, clientID: ClientID) {
const errorText = `Error: ${errMsg}\nStack: ${stack}`;
consolex.error(errorText);
const modal = document.createElement('div');
+19 -4
View File
@@ -4,8 +4,8 @@ import { getConfig } from "./configuration/Config";
import { EventBus } from "./EventBus";
import { Executor } from "./execution/ExecutionManager";
import { WinCheckExecution } from "./execution/WinCheckExecution";
import { AllPlayers, Cell, Game, MessageType, Player, PlayerActions, PlayerID, PlayerProfile, PlayerType, UnitType } from "./game/Game";
import { DisplayMessageUpdate } from "./game/GameUpdates";
import { AllPlayers, Cell, Game, GameUpdates, MessageType, Player, PlayerActions, PlayerID, PlayerProfile, PlayerType, UnitType } from "./game/Game";
import { DisplayMessageUpdate, ErrorUpdate } from "./game/GameUpdates";
import { NameViewData } from './game/Game';
import { GameUpdateType } from "./game/GameUpdates";
import { createGame } from "./game/GameImpl";
@@ -35,7 +35,7 @@ export class GameRunner {
constructor(
public game: Game,
private execManager: Executor,
private callBack: (gu: GameUpdateViewData) => void
private callBack: (gu: GameUpdateViewData | ErrorUpdate) => void
) {
}
@@ -64,7 +64,22 @@ export class GameRunner {
this.game.addExecution(...this.execManager.createExecs(this.turns[this.currTurn]))
this.currTurn++
const updates = this.game.executeNextTick()
let updates: GameUpdates;
try {
updates = this.game.executeNextTick();
} catch (error: unknown) {
if (error instanceof Error) {
console.error('Game tick error:', error.message);
this.callBack({
errMsg: error.message,
stack: error.stack
} as ErrorUpdate)
clearInterval(this.tickInterval)
return
}
}
if (this.game.inSpawnPhase() && this.game.ticks() % 2 == 0) {
this.game.players()
+5
View File
@@ -10,6 +10,11 @@ export interface GameUpdateViewData {
playerNameViewData: Record<number, NameViewData>;
}
export interface ErrorUpdate {
errMsg: string
stack?: string
}
export enum GameUpdateType {
Tile,
Unit,
+3 -3
View File
@@ -1,5 +1,5 @@
import { PlayerActions, PlayerID, PlayerInfo, PlayerProfile } from "../game/Game";
import { GameUpdateViewData } from '../game/GameUpdates';
import { ErrorUpdate, GameUpdateViewData } from '../game/GameUpdates';
import { GameConfig, GameID, Turn } from "../Schemas";
import { generateID } from "../Util";
import { WorkerMessage } from "./WorkerMessages";
@@ -8,7 +8,7 @@ export class WorkerClient {
private worker: Worker;
private isInitialized = false;
private messageHandlers: Map<string, (message: WorkerMessage) => void>;
private gameUpdateCallback?: (update: GameUpdateViewData) => void;
private gameUpdateCallback?: (update: GameUpdateViewData | ErrorUpdate) => void;
constructor(private gameID: GameID, private gameConfig: GameConfig) {
this.worker = new Worker(new URL('./Worker.worker.ts', import.meta.url));
@@ -67,7 +67,7 @@ export class WorkerClient {
});
}
start(gameUpdate: (gu: GameUpdateViewData) => void) {
start(gameUpdate: (gu: GameUpdateViewData | ErrorUpdate) => void) {
if (!this.isInitialized) {
throw new Error('Failed to initialize pathfinder');
}