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;