From 8331047a9b2f7976f9e1ec6f17c2ca37bf0b1d22 Mon Sep 17 00:00:00 2001 From: evanpelle Date: Sat, 7 Sep 2024 21:23:38 -0700 Subject: [PATCH] have client create player id --- src/client/Client.ts | 1 + src/client/ClientGame.ts | 18 ++++++++++-------- src/core/Schemas.ts | 1 + src/core/execution/BotSpawner.ts | 1 + src/core/execution/ExecutionManager.ts | 2 +- src/core/execution/SpawnExecution.ts | 2 +- 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/client/Client.ts b/src/client/Client.ts index afb090ad2..490afd84b 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -122,6 +122,7 @@ class Client { this.game = createClientGame( getUsername(), uuidv4(), + uuidv4(), clientIP, lobby.id, getConfig(), diff --git a/src/client/ClientGame.ts b/src/client/ClientGame.ts index 9e983b2c9..b3bd7354c 100644 --- a/src/client/ClientGame.ts +++ b/src/client/ClientGame.ts @@ -12,7 +12,7 @@ import {TerrainRenderer} from "./graphics/TerrainRenderer"; -export function createClientGame(name: string, clientID: ClientID, ip: string | null, gameID: GameID, config: Config, terrainMap: TerrainMap): ClientGame { +export function createClientGame(name: string, clientID: ClientID, playerID: PlayerID, ip: string | null, gameID: GameID, config: Config, terrainMap: TerrainMap): ClientGame { let eventBus = new EventBus() let game = createGame(terrainMap, eventBus, config) let terrainRenderer = new TerrainRenderer(game) @@ -21,6 +21,7 @@ export function createClientGame(name: string, clientID: ClientID, ip: string | return new ClientGame( name, clientID, + playerID, ip, gameID, eventBus, @@ -45,9 +46,11 @@ export class ClientGame { private isProcessingTurn = false + constructor( public playerName: string, private id: ClientID, + private playerID: PlayerID, private clientIP: string | null, private gameID: GameID, private eventBus: EventBus, @@ -88,13 +91,11 @@ export class ClientGame { if (!this.isActive) { this.start() } - this.sendIntent( - { - type: "updateName", - name: this.playerName, - clientID: this.id - } - ) + this.sendIntent({ + type: "updateName", + name: this.playerName, + clientID: this.id + }) } if (message.type == "turn") { this.addTurn(message.turn) @@ -279,6 +280,7 @@ export class ClientGame { this.sendIntent({ type: "spawn", clientID: this.id, + playerID: this.playerID, name: this.playerName, playerType: PlayerType.Human, x: cell.x, diff --git a/src/core/Schemas.ts b/src/core/Schemas.ts index 602dd2ca1..78501519e 100644 --- a/src/core/Schemas.ts +++ b/src/core/Schemas.ts @@ -55,6 +55,7 @@ export const AttackIntentSchema = BaseIntentSchema.extend({ export const SpawnIntentSchema = BaseIntentSchema.extend({ type: z.literal('spawn'), + playerID: z.string(), name: z.string(), playerType: PlayerTypeSchema, x: z.number(), diff --git a/src/core/execution/BotSpawner.ts b/src/core/execution/BotSpawner.ts index 124a2582b..b22ed3365 100644 --- a/src/core/execution/BotSpawner.ts +++ b/src/core/execution/BotSpawner.ts @@ -39,6 +39,7 @@ export class BotSpawner { } return { type: 'spawn', + playerID: this.random.nextID(), name: botName, playerType: PlayerType.Bot, x: tile.cell().x, diff --git a/src/core/execution/ExecutionManager.ts b/src/core/execution/ExecutionManager.ts index 3891adcec..65533f8c5 100644 --- a/src/core/execution/ExecutionManager.ts +++ b/src/core/execution/ExecutionManager.ts @@ -34,7 +34,7 @@ export class Executor { ) } else if (intent.type == "spawn") { return new SpawnExecution( - new PlayerInfo(intent.name, intent.playerType, intent.clientID, this.random.nextID()), + new PlayerInfo(intent.name, intent.playerType, intent.clientID, intent.playerID), new Cell(intent.x, intent.y) ) } else if (intent.type == "boat") { diff --git a/src/core/execution/SpawnExecution.ts b/src/core/execution/SpawnExecution.ts index 1f50bb474..a72459638 100644 --- a/src/core/execution/SpawnExecution.ts +++ b/src/core/execution/SpawnExecution.ts @@ -24,7 +24,7 @@ export class SpawnExecution implements Execution { return } - const existing = this.mg.players().find(p => p.clientID() != null && p.clientID() == this.playerInfo.clientID) + const existing = this.mg.players().find(p => p.id() == this.playerInfo.id) if (existing) { existing.tiles().forEach(t => existing.relinquish(t)) getSpawnCells(this.mg, this.cell).forEach(c => {