From 7c5a74762fd62dc8a11b8b975c06238adf7ac76e Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 14 Jan 2021 13:25:22 +0800 Subject: [PATCH] =?UTF-8?q?player=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E7=94=A8=E4=BA=8E=E6=A0=87=E8=AE=B0=E5=BA=A7=E6=AC=A1=E5=8F=B7?= =?UTF-8?q?=E7=9A=84=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.d.ts | 7 +++++++ src/rooms/GeneralRoom.ts | 19 ++++++++++++++----- src/rooms/commands/EatConfirmCommand.ts | 4 +++- src/rooms/commands/GMCommand.ts | 14 +++++--------- src/rooms/commands/NextTurnCommand.ts | 4 +++- src/rooms/commands/OnJoinCommand.ts | 6 +++--- src/rooms/commands/PlayReadyCommand.ts | 10 +++++----- src/rooms/commands/SelectHeroCommand.ts | 1 - src/rooms/schema/Player.ts | 8 ++++++-- 9 files changed, 46 insertions(+), 27 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index 4a027dd..fc5afeb 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -282,6 +282,13 @@ declare module "colyseus" { * @param srcPlayer */ getOppositePlayer(srcPlayer: string|Player): Player; + + /** + * 根据index获取玩家 + * @param {number} idx + * @return {Player} + */ + getPlayerByIdx(idx: number): Player; } } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 6b47f8b..0a41f7d 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -257,7 +257,17 @@ export class GeneralRoom extends Room { getAssistClient(sessionId: string): RobotClient { return this.assistMap.get(sessionId); } - + getPlayerByIdx(idx: number) { + for (let [, player] of this.state.players) { + if (player.idx == idx) { + return player; + } + } + } + /** + * 获取对位玩家 + * @param srcPlayer + */ getOppositePlayer(srcPlayer: string | Player): Player { let playerId; if (typeof srcPlayer === 'string') { @@ -268,9 +278,8 @@ export class GeneralRoom extends Room { if (!this.state.players.has(playerId)) { return null; } - const sessionIds = [...this.state.players.keys()]; - let idx = sessionIds.indexOf(playerId); - let playerId2 = sessionIds[(2 + idx) % 4]; - return this.state.players.get(playerId2); + let idx = this.state.players.get(playerId).idx; + let opposIdx = (2 + idx) % 4; + return this.getPlayerByIdx(opposIdx); } } diff --git a/src/rooms/commands/EatConfirmCommand.ts b/src/rooms/commands/EatConfirmCommand.ts index 4bb3a68..1268ffa 100644 --- a/src/rooms/commands/EatConfirmCommand.ts +++ b/src/rooms/commands/EatConfirmCommand.ts @@ -19,7 +19,9 @@ export class EatConfirmCommand extends Command a.idx - b.idx); + const sessionIds = players.map(p => p.id); // 将当前轮玩家移至第一位 let pids: string[] = sessionIds.moveElement(-sessionIds.indexOf(this.state.currentTurn)); diff --git a/src/rooms/commands/GMCommand.ts b/src/rooms/commands/GMCommand.ts index 4cdcdd3..b0797a7 100644 --- a/src/rooms/commands/GMCommand.ts +++ b/src/rooms/commands/GMCommand.ts @@ -45,10 +45,6 @@ export class GMCommand extends Command 2) { @@ -208,7 +204,7 @@ export class GMCommand extends Command { async execute(){ this.state.updateGameState(GameStateConst.STATE_BEGIN_DRAW); - const sessionIds = [...this.state.players.keys()]; + const players = [...this.state.players.values()]; + players.sort((a, b) => a.idx - b.idx); + const sessionIds = players.map(p => p.id); if (!this.state.currentTurn) { this.state.round = 0; } diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index bdfe9ad..aee1a82 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -13,9 +13,9 @@ export class OnJoinCommand extends Command { execute({client, accountId} = this.payload) { - let team = (this.state.players.size == 1 || this.state.players.size == 2)? 1 : 0; - // 实际的team会在PlayReadyCommand中设置 - let player = new Player(client.sessionId, 0, team); + let count = this.state.players.size; + let team = (count == 1 || count == 2)? 1 : 0; + let player = new Player(client.sessionId, count, team); if (accountId && accountId == 'robot') { accountId = `robot_${client.sessionId}`; } else if (!accountId) { diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index 6f60ceb..6df7e3b 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -30,11 +30,11 @@ export class PlayReadyCommand extends Command(); - constructor(id: string, heroId: number, team: number) { + constructor(id: string, idx: number, team: number) { super(); this.id = id; this.state = PlayerStateConst.PLAYER_NORMAL; this.hp = 0; - this.heroId = heroId; + this.idx = idx; + this.heroId = 0; this.team = team; this.countTotal = 0; this.countPresent = 0;