mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-07-05 10:32:04 +00:00
af5249655c
## Description: - Inline the game worker into the main bundle (`?worker&inline`) instead of loading it from the CDN via a same-origin Blob trampoline. - Some users were failing to load the worker — most likely a cross-origin / CDN edge case the trampoline didn't paper over. Inlining serves the worker as a same-origin Blob from the main bundle, bypassing the cross-origin `new Worker(url)` restriction entirely. - Removes `createGameWorker()`, the trampoline script, the `trampoline_error` message type, and the `onTrampolineError` listener in `initialize()`. The 60s init timeout backstop stays. - `cdnBase` is still forwarded in the `init` message — that's for in-worker asset fetches (maps, etc.), unrelated to loading the worker module itself. Tradeoff: the worker bundles all of `src/core`, so the main JS download grows by that amount on every page load (including lobby/settings pages that never start a game). Accepted because load reliability > bundle size here. ## Please complete the following: - [x] I have added screenshots for all UI updates - [x] I process any text displayed to the user through translateText() and I've added it to the en.json file - [x] I have added relevant tests to the test directory - [x] I confirm I have thoroughly tested these changes and take full responsibility for any bugs introduced ## Please put your Discord username so you can be contacted if a bug or regression is found: evan
164 lines
4.1 KiB
TypeScript
164 lines
4.1 KiB
TypeScript
import {
|
|
BuildableUnit,
|
|
PlayerActions,
|
|
PlayerBorderTiles,
|
|
PlayerBuildableUnitType,
|
|
PlayerID,
|
|
PlayerProfile,
|
|
} from "../game/Game";
|
|
import { TileRef } from "../game/GameMap";
|
|
import { ErrorUpdate, GameUpdateViewData } from "../game/GameUpdates";
|
|
import { ClientID, GameStartInfo, Turn } from "../Schemas";
|
|
|
|
export type WorkerMessageType =
|
|
| "init"
|
|
| "initialized"
|
|
| "turn"
|
|
| "game_update"
|
|
| "game_update_batch"
|
|
| "game_error"
|
|
| "player_actions"
|
|
| "player_actions_result"
|
|
| "player_buildables"
|
|
| "player_buildables_result"
|
|
| "player_profile"
|
|
| "player_profile_result"
|
|
| "player_border_tiles"
|
|
| "player_border_tiles_result"
|
|
| "attack_clustered_positions"
|
|
| "attack_clustered_positions_result"
|
|
| "transport_ship_spawn"
|
|
| "transport_ship_spawn_result";
|
|
|
|
// Base interface for all messages
|
|
interface BaseWorkerMessage {
|
|
type: WorkerMessageType;
|
|
id?: string;
|
|
}
|
|
|
|
// Messages from main thread to worker
|
|
export interface InitMessage extends BaseWorkerMessage {
|
|
type: "init";
|
|
gameStartInfo: GameStartInfo;
|
|
clientID: ClientID | undefined;
|
|
cdnBase: string;
|
|
}
|
|
|
|
export interface TurnMessage extends BaseWorkerMessage {
|
|
type: "turn";
|
|
turn: Turn;
|
|
}
|
|
|
|
// Messages from worker to main thread
|
|
export interface InitializedMessage extends BaseWorkerMessage {
|
|
type: "initialized";
|
|
}
|
|
|
|
export interface GameUpdateMessage extends BaseWorkerMessage {
|
|
type: "game_update";
|
|
gameUpdate: GameUpdateViewData;
|
|
}
|
|
|
|
export interface GameUpdateBatchMessage extends BaseWorkerMessage {
|
|
type: "game_update_batch";
|
|
gameUpdates: GameUpdateViewData[];
|
|
}
|
|
|
|
export interface GameErrorMessage extends BaseWorkerMessage {
|
|
type: "game_error";
|
|
error: ErrorUpdate;
|
|
}
|
|
|
|
export interface PlayerActionsMessage extends BaseWorkerMessage {
|
|
type: "player_actions";
|
|
playerID: PlayerID;
|
|
x?: number;
|
|
y?: number;
|
|
units?: readonly PlayerBuildableUnitType[] | null;
|
|
}
|
|
|
|
export interface PlayerActionsResultMessage extends BaseWorkerMessage {
|
|
type: "player_actions_result";
|
|
result: PlayerActions;
|
|
}
|
|
|
|
export interface PlayerBuildablesMessage extends BaseWorkerMessage {
|
|
type: "player_buildables";
|
|
playerID: PlayerID;
|
|
x?: number;
|
|
y?: number;
|
|
units?: readonly PlayerBuildableUnitType[];
|
|
}
|
|
|
|
export interface PlayerBuildablesResultMessage extends BaseWorkerMessage {
|
|
type: "player_buildables_result";
|
|
result: BuildableUnit[];
|
|
}
|
|
|
|
export interface PlayerProfileMessage extends BaseWorkerMessage {
|
|
type: "player_profile";
|
|
playerID: number;
|
|
}
|
|
|
|
export interface PlayerProfileResultMessage extends BaseWorkerMessage {
|
|
type: "player_profile_result";
|
|
result: PlayerProfile;
|
|
}
|
|
|
|
export interface PlayerBorderTilesMessage extends BaseWorkerMessage {
|
|
type: "player_border_tiles";
|
|
playerID: PlayerID;
|
|
}
|
|
|
|
export interface PlayerBorderTilesResultMessage extends BaseWorkerMessage {
|
|
type: "player_border_tiles_result";
|
|
result: PlayerBorderTiles;
|
|
}
|
|
|
|
export interface AttackClusteredPositionsMessage extends BaseWorkerMessage {
|
|
type: "attack_clustered_positions";
|
|
playerID: number;
|
|
attackID?: string;
|
|
}
|
|
|
|
export interface AttackClusteredPositionsResultMessage
|
|
extends BaseWorkerMessage {
|
|
type: "attack_clustered_positions_result";
|
|
attacks: { id: string; positions: { x: number; y: number }[] }[];
|
|
}
|
|
|
|
export interface TransportShipSpawnMessage extends BaseWorkerMessage {
|
|
type: "transport_ship_spawn";
|
|
playerID: PlayerID;
|
|
targetTile: TileRef;
|
|
}
|
|
|
|
export interface TransportShipSpawnResultMessage extends BaseWorkerMessage {
|
|
type: "transport_ship_spawn_result";
|
|
result: TileRef | false;
|
|
}
|
|
|
|
// Union types for type safety
|
|
export type MainThreadMessage =
|
|
| InitMessage
|
|
| TurnMessage
|
|
| PlayerActionsMessage
|
|
| PlayerBuildablesMessage
|
|
| PlayerProfileMessage
|
|
| PlayerBorderTilesMessage
|
|
| AttackClusteredPositionsMessage
|
|
| TransportShipSpawnMessage;
|
|
|
|
// Message send from worker
|
|
export type WorkerMessage =
|
|
| InitializedMessage
|
|
| GameUpdateMessage
|
|
| GameUpdateBatchMessage
|
|
| GameErrorMessage
|
|
| PlayerActionsResultMessage
|
|
| PlayerBuildablesResultMessage
|
|
| PlayerProfileResultMessage
|
|
| PlayerBorderTilesResultMessage
|
|
| AttackClusteredPositionsResultMessage
|
|
| TransportShipSpawnResultMessage;
|