thread-split: create PlayerProfile transfer flow

This commit is contained in:
evanpelle
2025-01-13 11:08:43 -08:00
committed by Evan
parent e76837c75a
commit fed18e471c
5 changed files with 39 additions and 17 deletions
+11 -2
View File
@@ -105,10 +105,19 @@ export class GameRunner {
return actions
}
public playerProfile(playerID: number): PlayerProfile {
return {
relations: this.game.players().filter(p => p.smallID() == playerID)[0]?.allRelationsSorted()
const player = this.game.players().filter(p => p.smallID() == playerID)[0];
if (!player) {
throw new Error(`player with id ${playerID} not found`);
}
return {
relations: Object.fromEntries(
player.allRelationsSorted()
.map(({ player, relation }) => [player.smallID(), relation])
)
};
}
private canBoat(myPlayer: Player, tile: Tile): boolean {
+6 -1
View File
@@ -1,4 +1,4 @@
import { GameUpdates, GameUpdateType, MapPos, MessageType, NameViewData, Player, PlayerActions, PlayerUpdate, Tile, TileUpdate, Unit, UnitUpdate } from './game/Game';
import { GameUpdates, GameUpdateType, MapPos, MessageType, NameViewData, Player, PlayerActions, PlayerProfile, PlayerUpdate, Tile, TileUpdate, Unit, UnitUpdate } from './game/Game';
import { Config } from "./configuration/Config";
import { Alliance, AllianceRequest, AllPlayers, Cell, DefenseBonus, EmojiMessage, Execution, ExecutionView, Game, Gold, MutableTile, Nation, PlayerID, PlayerInfo, PlayerType, Relation, TerrainMap, TerrainTile, TerrainType, TerraNullius, Tick, UnitInfo, UnitType } from "./game/Game";
import { ClientID } from "./Schemas";
@@ -205,6 +205,11 @@ export class PlayerView implements Player {
relation(other: Player): Relation {
return Relation.Neutral
}
profile(): Promise<PlayerProfile> {
return this.game.worker.playerProfile(this.smallID())
}
allRelationsSorted(): { player: Player; relation: Relation; }[] {
return []
}
+5 -6
View File
@@ -5,6 +5,7 @@ import {
WorkerMessage,
InitializedMessage,
PlayerActionsResultMessage,
PlayerProfileResultMessage,
} from './WorkerMessages';
const ctx: Worker = self as any;
@@ -83,19 +84,17 @@ ctx.addEventListener('message', async (e: MessageEvent<MainThreadMessage>) => {
}
try {
const actions = (await gameRunner).playerActions(message.playerID, message.x, message.y)
const profile = (await gameRunner).playerProfile(message.playerID)
sendMessage({
type: 'player_actions_result',
type: 'player_profile_result',
id: message.id,
result: actions
} as PlayerActionsResultMessage);
result: profile
} as PlayerProfileResultMessage);
} catch (error) {
console.error('Failed to check borders:', error);
throw error;
}
break;
default:
console.warn('Unknown message :', message);
}
+2 -2
View File
@@ -29,7 +29,7 @@ export class WorkerClient {
break;
case 'initialized':
case 'player_actions_result':
default:
if (message.id && this.messageHandlers.has(message.id)) {
const handler = this.messageHandlers.get(message.id)!;
handler(message);
@@ -91,7 +91,7 @@ export class WorkerClient {
});
}
playerInfo(playerID: number): Promise<PlayerProfile> {
playerProfile(playerID: number): Promise<PlayerProfile> {
return new Promise((resolve, reject) => {
if (!this.isInitialized) {
reject(new Error('Worker not initialized'));