71 lines
2.4 KiB
TypeScript
71 lines
2.4 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,
|
|
accountId: string,
|
|
seat?: number,
|
|
}> {
|
|
execute({client, accountId, seat} = this.payload) {
|
|
let count = this.state.players.size;
|
|
if (count >= this.room.maxClients) {
|
|
return;
|
|
}
|
|
if (accountId && accountId == 'robot') {
|
|
accountId = `robot_${client.sessionId}`;
|
|
} else if (!accountId) {
|
|
accountId = `player_${client.sessionId}`;
|
|
}
|
|
let team = 0;
|
|
let idx = count;
|
|
if (seat != undefined) {
|
|
idx = +seat;
|
|
} else {
|
|
team = (count == 1 || count == 2) ? 1 : 0;
|
|
}
|
|
let player = new Player(client.sessionId, idx, team);
|
|
player.accountId = accountId;
|
|
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.stopSchedule('waiting_player');
|
|
this.room.lock().then(() => {
|
|
});
|
|
this.state.updateGameState(GameStateConst.STATE_WAIT_PREPARE);
|
|
} else if (this.state.players.size < this.room.maxClients
|
|
&& this.state.players.size >= this.room.maxClients - this.room.robotCount) {
|
|
for (let i = 0; i < this.room.robotCount; i++) {
|
|
this.room.robotCount --;
|
|
self.room.addRobot();
|
|
}
|
|
}
|
|
|
|
this.room.bUserJoin(`${client.sessionId}`, {except: client});
|
|
}
|
|
|
|
}
|