card_svr/src/rooms/commands/OnJoinCommand.ts
2020-12-25 18:10:41 +08:00

45 lines
1.9 KiB
TypeScript

import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {Player} from "../schema/Player";
import {Client} from "colyseus";
import {GameStateConst} from "../../constants/GameStateConst";
import {GameEnv} from "../../cfg/GameEnv";
/**
* 玩家成功加入房间
*/
export class OnJoinCommand extends Command<CardGameState, {
client: Client
}> {
execute({client} = 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);
this.state.players.set(client.sessionId, player);
this.room.addAssistClient(client.sessionId);
let self = this;
if (this.room.clientCount() == 1) {
// 正常的匹配逻辑进入的第一个玩家, 开启定时, 超过设定时间人没齐的话, 添加机器人
let timeOutWaitingPlayer = function () {
let count = self.room.maxClients - self.room.clientCount();
if (count > 0) {
for (let i = 0; i < count; i++) {
self.room.addRobot();
}
}
}
let time = new GameEnv().waitingPlayerTime * 1000;
self.room.beginSchedule(time, timeOutWaitingPlayer, 'waiting_player');
} else if (this.room.clientCount() > 1 && this.room.clientCount() < this.room.maxClients) {
let moreTime = new GameEnv().waitingPlayerOnePlus * 1000;
self.room.addScheduleTime(moreTime, 'play_join', 'waiting_player')
}
if (this.state.players.size >= this.room.maxClients) {
this.room.lock().then(() => {});
this.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE);
}
this.room.bUserJoin(`${client.sessionId}`, {except: client});
}
}