mirror of
https://github.com/openfrontio/OpenFrontIO.git
synced 2026-06-24 07:44:41 +00:00
thread-split events display
This commit is contained in:
@@ -4,7 +4,7 @@ import { getConfig } from "./configuration/Config";
|
||||
import { EventBus } from "./EventBus";
|
||||
import { Executor } from "./execution/ExecutionManager";
|
||||
import { WinCheckExecution } from "./execution/WinCheckExecution";
|
||||
import { Cell, DisplayMessageUpdate, Game, GameUpdateType, MessageType, MutableGame, MutableTile, NameViewData, Player, PlayerActions, PlayerID, Tile, TileUpdate, UnitType, UnitUpdate } from "./game/Game";
|
||||
import { Cell, DisplayMessageUpdate, Game, GameUpdateType, MessageType, MutableGame, MutableTile, NameViewData, Player, PlayerActions, PlayerID, PlayerProfile, Tile, TileUpdate, UnitType, UnitUpdate } from "./game/Game";
|
||||
import { createGame } from "./game/GameImpl";
|
||||
import { loadTerrainMap } from "./game/TerrainMapLoader";
|
||||
import { GameConfig, Turn } from "./Schemas";
|
||||
@@ -105,6 +105,12 @@ export class GameRunner {
|
||||
return actions
|
||||
}
|
||||
|
||||
public playerProfile(playerID: number): PlayerProfile {
|
||||
return {
|
||||
relations: this.game.players().filter(p => p.smallID() == playerID)[0]?.allRelationsSorted()
|
||||
}
|
||||
}
|
||||
|
||||
private canBoat(myPlayer: Player, tile: Tile): boolean {
|
||||
const other = tile.owner()
|
||||
if (myPlayer.units(UnitType.TransportShip).length >= this.game.config().boatMaxNumber()) {
|
||||
|
||||
@@ -271,6 +271,10 @@ export class GameView {
|
||||
}
|
||||
}
|
||||
|
||||
public updatesSinceLastTick(): GameUpdates {
|
||||
return this.lastUpdate.updates
|
||||
}
|
||||
|
||||
public update(gu: GameUpdateViewData) {
|
||||
this.lastUpdate = gu
|
||||
|
||||
|
||||
@@ -412,6 +412,11 @@ export interface PlayerActions {
|
||||
interaction?: PlayerInteraction
|
||||
}
|
||||
|
||||
export interface PlayerProfile {
|
||||
relations: Record<number, Relation>
|
||||
// TODO: add alliances etc
|
||||
}
|
||||
|
||||
export interface PlayerInteraction {
|
||||
sharedBorder: boolean
|
||||
canSendEmoji: boolean
|
||||
@@ -495,8 +500,8 @@ export interface BrokeAllianceUpdate {
|
||||
|
||||
export interface AllianceExpiredUpdate {
|
||||
type: GameUpdateType.AllianceExpired
|
||||
player1: number
|
||||
player2: number
|
||||
player1ID: number
|
||||
player2ID: number
|
||||
}
|
||||
|
||||
export interface TargetPlayerUpdate {
|
||||
|
||||
@@ -473,8 +473,8 @@ export class GameImpl implements MutableGame {
|
||||
this.alliances_ = this.alliances_.filter(a => a != alliances[0])
|
||||
this.addUpdate({
|
||||
type: GameUpdateType.AllianceExpired,
|
||||
player1: alliance.requestor().smallID(),
|
||||
player2: alliance.recipient().smallID()
|
||||
player1ID: alliance.requestor().smallID(),
|
||||
player2ID: alliance.recipient().smallID()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -77,6 +77,24 @@ ctx.addEventListener('message', async (e: MessageEvent<MainThreadMessage>) => {
|
||||
throw error;
|
||||
}
|
||||
break;
|
||||
case 'player_profile':
|
||||
if (!gameRunner) {
|
||||
throw new Error('Game runner not initialized');
|
||||
}
|
||||
|
||||
try {
|
||||
const actions = (await gameRunner).playerActions(message.playerID, message.x, message.y)
|
||||
sendMessage({
|
||||
type: 'player_actions_result',
|
||||
id: message.id,
|
||||
result: actions
|
||||
} as PlayerActionsResultMessage);
|
||||
} catch (error) {
|
||||
console.error('Failed to check borders:', error);
|
||||
throw error;
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
console.warn('Unknown message :', message);
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { PlayerActions, PlayerID, Tile } from "../game/Game";
|
||||
import { PlayerActions, PlayerID, PlayerInfo, PlayerProfile, Tile } from "../game/Game";
|
||||
import { GameUpdateViewData } from "../GameView";
|
||||
import { GameConfig, GameID, Turn } from "../Schemas";
|
||||
import { generateID } from "../Util";
|
||||
@@ -91,6 +91,30 @@ export class WorkerClient {
|
||||
});
|
||||
}
|
||||
|
||||
playerInfo(playerID: number): Promise<PlayerProfile> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.isInitialized) {
|
||||
reject(new Error('Worker not initialized'));
|
||||
return;
|
||||
}
|
||||
|
||||
const messageId = generateID()
|
||||
|
||||
this.messageHandlers.set(messageId, (message) => {
|
||||
if (message.type === 'player_profile_result' && message.result !== undefined) {
|
||||
resolve(message.result);
|
||||
}
|
||||
});
|
||||
|
||||
this.worker.postMessage({
|
||||
type: 'player_profile',
|
||||
id: messageId,
|
||||
playerID: playerID,
|
||||
});
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
playerInteraction(playerID: PlayerID, tile: Tile): Promise<PlayerActions> {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.isInitialized) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { GameUpdateViewData } from "../GameView";
|
||||
import { GameConfig, GameID, Turn } from "../Schemas";
|
||||
import { PlayerActions, PlayerID } from "../game/Game";
|
||||
import { PlayerActions, PlayerID, PlayerProfile } from "../game/Game";
|
||||
|
||||
export type WorkerMessageType =
|
||||
| 'heartbeat'
|
||||
@@ -9,7 +9,9 @@ export type WorkerMessageType =
|
||||
| 'turn'
|
||||
| 'game_update'
|
||||
| 'player_actions'
|
||||
| 'player_actions_result';
|
||||
| 'player_actions_result'
|
||||
| 'player_profile'
|
||||
| 'player_profile_result'
|
||||
|
||||
// Base interface for all messages
|
||||
interface BaseWorkerMessage {
|
||||
@@ -55,8 +57,18 @@ export interface PlayerActionsResultMessage extends BaseWorkerMessage {
|
||||
result: PlayerActions;
|
||||
}
|
||||
|
||||
export interface PlayerProfileMessage extends BaseWorkerMessage {
|
||||
type: 'player_profile'
|
||||
playerID: number
|
||||
}
|
||||
|
||||
export interface PlayerProfileResultMessage extends BaseWorkerMessage {
|
||||
type: 'player_profile_result'
|
||||
result: PlayerProfile
|
||||
}
|
||||
|
||||
// Union types for type safety
|
||||
export type MainThreadMessage = HeartbeatMessage | InitMessage | TurnMessage | PlayerActionsMessage
|
||||
export type MainThreadMessage = HeartbeatMessage | InitMessage | TurnMessage | PlayerActionsMessage | PlayerProfileMessage
|
||||
|
||||
// Message send from worker
|
||||
export type WorkerMessage = InitializedMessage | GameUpdateMessage | PlayerActionsResultMessage;
|
||||
export type WorkerMessage = InitializedMessage | GameUpdateMessage | PlayerActionsResultMessage | PlayerProfileResultMessage
|
||||
Reference in New Issue
Block a user