diff --git a/src/index.ts b/src/index.ts index 267f178..4fc5072 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 diff --git a/src/robot/RobotClient.ts b/src/robot/RobotClient.ts index 03a6cae..9dfdc3f 100644 --- a/src/robot/RobotClient.ts +++ b/src/robot/RobotClient.ts @@ -81,7 +81,7 @@ export class RobotClient implements Client { } leave(code?: number, data?: string): void { - + this.ref.emit('close'); } raw(data: ArrayLike, options?: ISendOptions): void { diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index fbbcd82..bf7c565 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -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) { diff --git a/src/rooms/commands/EatConfirmCommand.ts b/src/rooms/commands/EatConfirmCommand.ts index f362dff..3c55351 100644 --- a/src/rooms/commands/EatConfirmCommand.ts +++ b/src/rooms/commands/EatConfirmCommand.ts @@ -65,9 +65,10 @@ export class EatConfirmCommand extends Command { 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 { 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个人都点击了重开, 理论上不存在这种情况 diff --git a/src/rooms/commands/GiveUpCommand.ts b/src/rooms/commands/GiveUpCommand.ts index c64b727..4f3fb4a 100644 --- a/src/rooms/commands/GiveUpCommand.ts +++ b/src/rooms/commands/GiveUpCommand.ts @@ -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 { 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});