修改游戏重启逻辑

This commit is contained in:
zhl 2020-12-11 21:20:52 +08:00
parent ee488c85e5
commit 735897848c
6 changed files with 14 additions and 6 deletions

View File

@ -23,7 +23,7 @@ const server = http.createServer(app);
const gameServer = new Server({
server,
// driver: new MongooseDriver('mongodb://127.0.0.1/card-development'),
driver: new MongooseDriver('mongodb://192.168.100.24/card-development-y'),
driver: new MongooseDriver('mongodb://192.168.100.24/card-development'),
});
// register your room handlers

View File

@ -81,7 +81,7 @@ export class RobotClient implements Client {
}
leave(code?: number, data?: string): void {
this.ref.emit('close');
}
raw(data: ArrayLike<number>, options?: ISendOptions): void {

View File

@ -148,6 +148,7 @@ export class GeneralRoom extends Room {
debugRoom(`begin schedule: `, name, millisecond / 1000);
if (this.mainClock?.active) {
error(`当前已存在进行中的mainClock: ${this.mainClock['args']}`);
this.mainClock.clear();
}
this.mainClock = this.clock.setTimeout(handler, millisecond , name);
}
@ -191,6 +192,7 @@ export class GeneralRoom extends Room {
const options = this.reservedSeats[sessionId];
delete this.reservedSeats[sessionId];
this.clients.push(client);
client.ref.once('close', this['_onLeave'].bind(this, client));
// client.ref.on('message', this.onMessage.bind(this, client));
const reconnection = this.reconnections[sessionId];
if (reconnection) {

View File

@ -65,9 +65,10 @@ export class EatConfirmCommand extends Command<CardGameState, { timeUp: boolean
*/
// 吃牌逻辑
if (player) {
debugRoom(`有人吃牌了: ${player}, 场上牌: ${this.state.cards.size}, isFirst: ${isFirst}, timeUp: ${timeUp}`);
debugRoom(`有人吃牌了: ${player.id}, 场上牌: ${this.state.cards.size}, isFirst: ${isFirst}, timeUp: ${timeUp}`);
}
if (player != null && (isFirst || timeUp)) {
debugRoom(`真的开始吃牌了: ${player.id}, 场上牌: ${this.state.cards.size}, isFirst: ${isFirst}, timeUp: ${timeUp}`);
// 如果有吃牌计时的话,停止吃牌计时
this.room.stopSchedule();
// if (this.room.mainClock?.active) {

View File

@ -16,12 +16,11 @@ export class GameResultCommand extends Command<CardGameState, {}> {
this.room.bGameResult({});
this.state.updateGameState(GameStateConst.STATE_GAME_OVER);
this.resetAllState();
//TODO: 启动定时, 时间到后, 踢出没离开且没点重玩的玩家, 并将房间设为非private
//启动定时, 时间到后, 踢出没离开且没点重玩的玩家, 并将房间设为非private
let self = this;
let resultTimeOver = async function () {
let restartCount = 0;
// 踢出没离开并没点击重新开始的玩家
debugRoom(`restart_schedule 倒计时结束, 有 ${restartCount} 人点击重玩`)
debugRoom(`restart_schedule 倒计时结束, 有 ${self.state.restartCount} 人点击重玩`)
if (self.state.restartCount == 0) {
// 没有任何人点重新开始, 则解散房间
await self.room.disconnect();
@ -30,9 +29,11 @@ export class GameResultCommand extends Command<CardGameState, {}> {
for (let [,player] of self.state.players) {
if (player.state !== PlayerStateConst.PLAYER_READY) {
let client = self.room.getClient(player);
debugRoom(`清除没点击重新开始的玩家 ${client.sessionId}`);
client.leave();
}
}
await self.room.unlock();
await self.room.setPrivate(false);
} else { // 如果4个人都点击了重开, 理论上不存在这种情况

View File

@ -2,12 +2,16 @@ import {Command} from "@colyseus/command";
import {CardGameState} from "../schema/CardGameState";
import {Client} from "colyseus";
import {EatConfirmCommand} from "./EatConfirmCommand";
import {GameStateConst} from "../../constants/GameStateConst";
/**
*
*/
export class GiveUpCommand extends Command<CardGameState, {client: Client}> {
execute({client} = this.payload) {
if (this.state.gameState != GameStateConst.STATE_BEGIN_EAT) {
return;
}
if (!this.state.tmpActionMap.has(client.sessionId)) {
this.state.tmpActionMap.set(client.sessionId, 0);
this.room.broadcast('give_up_eat_s2c', {player: client.sessionId});