card_svr/src/rooms/commands/PlayReadyCommand.ts

62 lines
2.5 KiB
TypeScript

import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {Client} from "colyseus";
import {PlayerStateConst} from "../../constants/PlayerStateConst";
import {GameStateConst} from "../../constants/GameStateConst";
import {GameEnv} from "../../cfg/GameEnv";
import {SelectHeroCommand} from "./SelectHeroCommand";
import {HeroCfg} from "../../cfg/parsers/HeroCfg";
import {BaseConst} from "../../constants/BaseConst";
/**
* 玩家已准备
*/
export class PlayReadyCommand extends Command<CardGameState, {
client: Client
}> {
async execute({client}: { client: Client }) {
this.state.players.get(client.sessionId).state = PlayerStateConst.PLAYER_READY;
this.room.broadcast("player_ready_s2c", {player: client.sessionId}, {except: client});
let readyCount = 0;
for (let [, player] of this.state.players) {
if (player.state === PlayerStateConst.PLAYER_READY) {
readyCount++;
}
}
// 如果所有人的状态都为已准备状态, 则开始发牌
if (readyCount >= this.room.maxClients) {
this.room.stopSchedule('restart_schedule');
this.room.stopSchedule('waiting_player');
// 比大小, 确定先手
// return [new PrepareCommand()];
// let i = 0;
// for (let [,player] of this.state.players) {
// player.team = (i == 1 || i == 2) ? 1 : 0;
// i += 1;
// }
await this.room.setPrivate(true);
this.room.state.updateGameState(GameStateConst.CHANGE_HERO);
let self = this;
/**
* 超时后随机选一个英雄
*/
let pickHeroTimeOut = function () {
for (let [, curPlayer] of self.state.players) {
if (curPlayer.state == PlayerStateConst.PLAYER_READY ) {
let heroMap: Map<number, HeroCfg> = global.$cfg.get(BaseConst.HERO);
let heroArr: HeroCfg[] = [...heroMap.values()];
let hero: HeroCfg = heroArr.randomOne();
let targetClient = self.room.getClient(curPlayer);
self.room.dispatcher.dispatch(new SelectHeroCommand(), {client: targetClient, heroId: hero.id});
}
}
}
let time = new GameEnv().pickHeroTime * 1000;
this.room.beginSchedule(time, pickHeroTimeOut, 'pick_hero');
}
}
}