diff --git a/src/cfg/GameEnv.ts b/src/cfg/GameEnv.ts index a1f1e45..154ea3a 100644 --- a/src/cfg/GameEnv.ts +++ b/src/cfg/GameEnv.ts @@ -78,6 +78,8 @@ export class GameEnv { // 变态机器人的胜率值 public robotLvlExtra: number public robotRateExtra: number + // 等待玩家确认的最长时间 + public maxWaitingTime: number public init(data: Map) { this.initCardNum = data.get(BaseConst.INIT_CARD_NUM).value @@ -123,6 +125,7 @@ export class GameEnv { this.robotRateMid = data.get(BaseConst.ROBOT_RATE_MID).value this.robotRateHigh = data.get(BaseConst.ROBOT_RATE_HIGHT).value this.robotRateExtra = data.get(BaseConst.ROBOT_RATE_EXTRA).value + this.maxWaitingTime = 60 } public getCheatRate(val: number) { if (val < this.robotRateMid) { diff --git a/src/constants/ClockNameConst.ts b/src/constants/ClockNameConst.ts index fc0db60..a4ac92c 100644 --- a/src/constants/ClockNameConst.ts +++ b/src/constants/ClockNameConst.ts @@ -29,4 +29,9 @@ export class ClockNameConst { * @type {string} */ public static readonly EAT_ROUND = 'eat_round' + /** + * 等待玩家发送准备消息 + * @type {string} + */ + public static readonly WAITING_PREPARE = 'waiting_prepare' } diff --git a/src/global.d.ts b/src/global.d.ts index e3dc64a..4ca6edb 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -45,6 +45,13 @@ declare module 'colyseus' { */ getClient(player: string | Player): Client; + /** + * 检查当前真实的客户端连接是否存在 + * @param {string | Player} player + * @return {boolean} + */ + checkClient(player: string | Player): boolean; + // >>>>>>>>> Begin of extend send message <<<<<<<<<<<<< /** diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 6a3cf22..dd5fcd1 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -204,7 +204,7 @@ export class GeneralRoom extends Room { player.online = true } } catch (e) { - debugRoom(`player realy level :${ client.sessionId }, try add robot`) + debugRoom(`player realy level :${ client.sessionId }`) // this.state.players.delete(client.sessionId); } } @@ -242,6 +242,16 @@ export class GeneralRoom extends Room { return result } + checkClient(player: string | Player): boolean { + let result: Client + if (typeof player == 'string') { + result = this.clients.find(client => client.sessionId == player) + } else { + result = this.clients.find(client => client.sessionId == player.id) + } + return !!result + } + /** * 加入当前房间的client数量 * @return {number} diff --git a/src/rooms/commands/GameResultCommand.ts b/src/rooms/commands/GameResultCommand.ts index d2bd872..7537fde 100644 --- a/src/rooms/commands/GameResultCommand.ts +++ b/src/rooms/commands/GameResultCommand.ts @@ -191,8 +191,9 @@ export class GameResultCommand extends Command { error(`所有人都点击了重新开始, 为啥还没开始游戏???`) } } - let time = new GameEnv().gameResultTime * 1000 - this.room.beginSchedule(time, resultTimeOver, ClockNameConst.RESTART_SCHEDULE) + // 20200316 确认, 自动重开功能已经没用了 + // let time = new GameEnv().gameResultTime * 1000 + // this.room.beginSchedule(time, resultTimeOver, ClockNameConst.RESTART_SCHEDULE) let saveData: any try { saveData = (await self.reportGameResult(winner, mvp.id, results.get(mvp).mvpScore, results, this.state.mode)).data.data diff --git a/src/rooms/commands/PlayReadyCommand.ts b/src/rooms/commands/PlayReadyCommand.ts index c992299..aaed605 100644 --- a/src/rooms/commands/PlayReadyCommand.ts +++ b/src/rooms/commands/PlayReadyCommand.ts @@ -1,6 +1,6 @@ import { Command } from '@colyseus/command' import { CardGameState } from '../schema/CardGameState' -import { Client } from 'colyseus' +import { Client, Room } from 'colyseus' import { PlayerStateConst } from '../../constants/PlayerStateConst' import { GameStateConst } from '../../constants/GameStateConst' import { GameEnv } from '../../cfg/GameEnv' @@ -8,6 +8,8 @@ import { SelectHeroCommand } from './SelectHeroCommand' import { HeroCfg } from '../../cfg/parsers/HeroCfg' import { BaseConst } from '../../constants/BaseConst' import { ClockNameConst } from '../../constants/ClockNameConst' +import { debugRoom } from '../../common/Debug' +import { Player } from '../schema/Player' /** * 玩家已准备 @@ -18,17 +20,47 @@ export class PlayReadyCommand extends Command= this.room.maxClients) { - this.room.stopSchedule(ClockNameConst.RESTART_SCHEDULE) - this.room.stopSchedule(ClockNameConst.WAITING_PLAYER) + // 比大小, 确定先手 // return [new PrepareCommand()]; // let i = 0; @@ -36,36 +68,42 @@ export class PlayReadyCommand extends Command = global.$cfg.get(BaseConst.HERO) - let heroArr: HeroCfg[] = [...heroMap.values()] - let hero: HeroCfg = heroArr.randomOne() - heroId = hero.id - } - let targetClient = self.room.getClient(curPlayer) - self.room.dispatcher.dispatch(new SelectHeroCommand(), { - client: targetClient, - heroId - }) - } - } - } - let time = new GameEnv().pickHeroTime * 1000 - this.room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO) + + self.room.stopSchedule(ClockNameConst.WAITING_PREPARE) + await beginChangeHero(self.room) } } } +const beginChangeHero = async function (room: Room) { + /** + * 超时后随机选一个英雄 + */ + let pickHeroTimeOut = function () { + for (let [, curPlayer] of room.state.players) { + if (curPlayer.state == PlayerStateConst.PLAYER_READY) { + let heroId: number + if (curPlayer.heros) { + heroId = curPlayer.heros.randomOne() + } else { + let heroMap: Map = global.$cfg.get(BaseConst.HERO) + let heroArr: HeroCfg[] = [...heroMap.values()] + let hero: HeroCfg = heroArr.randomOne() + heroId = hero.id + } + let targetClient = room.getClient(curPlayer) + room.dispatcher.dispatch(new SelectHeroCommand(), { + client: targetClient, + heroId + }) + } + } + } + await room.setPrivate(true) + room.stopSchedule(ClockNameConst.RESTART_SCHEDULE) + room.stopSchedule(ClockNameConst.WAITING_PLAYER) + room.state.updateGameState(GameStateConst.CHANGE_HERO) + let time = new GameEnv().pickHeroTime * 1000 + room.beginSchedule(time, pickHeroTimeOut, ClockNameConst.PICK_HERO) +}