diff --git a/client/src/scenes/GameScene.ts b/client/src/scenes/GameScene.ts index 235b594..315eb32 100644 --- a/client/src/scenes/GameScene.ts +++ b/client/src/scenes/GameScene.ts @@ -39,7 +39,9 @@ export class GameScene extends Scene { private respawnText: Phaser.GameObjects.Text | undefined = undefined; private endText: Phaser.GameObjects.Text | undefined = undefined; private disconnectText: Phaser.GameObjects.Text | undefined = undefined; + private serverRequestBuffer: number = Date.now(); + static SERVER_REQ_BUFFER_LENGTH = 500; static NAME = "scene-game"; constructor() { @@ -334,10 +336,16 @@ export class GameScene extends Scene { state.players .filter((p) => p.id === this.currentUserID && p.nickname !== sessionStorage.getItem("bullet-mania-nickname")) .forEach(() => { - this.connection?.writeJson({ - type: ClientMessageType.SetNickname, - nickname: sessionStorage.getItem("bullet-mania-nickname"), - }); + if ( + sessionStorage.getItem("bullet-mania-nickname") && + Date.now() - this.serverRequestBuffer > GameScene.SERVER_REQ_BUFFER_LENGTH + ) { + this.serverRequestBuffer = Date.now(); + this.connection?.writeJson({ + type: ClientMessageType.SetNickname, + nickname: sessionStorage.getItem("bullet-mania-nickname"), + }); + } }); // calc leaderboard diff --git a/server/server.ts b/server/server.ts index 49c8888..96eae4d 100644 --- a/server/server.ts +++ b/server/server.ts @@ -236,7 +236,7 @@ const store: Application = { if (message.type === ClientMessageType.SetNickname) { player.nickname = message.nickname; - updateRoomConfig(game, roomId); + updateRoomConfig(game, roomId, player); } else if (message.type === ClientMessageType.SetDirection) { player.direction = message.direction; } else if (message.type === ClientMessageType.SetAngle) { @@ -471,11 +471,17 @@ async function endGameCleanup(roomId: string, game: InternalState, winningPlayer }, 10000); } -async function updateRoomConfig(game: InternalState, roomId: string) { +async function updateRoomConfig(game: InternalState, roomId: string, playerNewNickname?: InternalPlayer) { const roomConfig: RoomConfig = { capacity: 0, winningScore: game.winningScore, - playerNicknameMap: Object.fromEntries(game.players.map((player) => [player.id, player.nickname ?? player.id])), + playerNicknameMap: Object.fromEntries(game.players.map((player) => { + if (playerNewNickname && player.id == playerNewNickname.id) { + return [player.id, playerNewNickname.nickname ?? player.id]; + } else { + return [player.id, player.nickname ?? player.id]; + } + })), isGameEnd: game.isGameEnd, winningPlayerId: game.winningPlayerId, };