From fcc0b9de5362a8ed6005c761e9fe00ab6b0671e6 Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 2 Mar 2021 17:12:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E5=A4=9A=E6=AC=A1=E5=90=83?= =?UTF-8?q?=E7=89=8C=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cfg/RoomOptions.ts | 4 +- src/rooms/GeneralRoom.ts | 8 ++- src/rooms/commands/DiscardCommand.ts | 10 ++-- src/rooms/commands/DrawCommand.ts | 32 +----------- src/rooms/commands/ManualTurnEndCommand.ts | 33 +++++++++++++ src/rooms/commands/SelectPetCommand.ts | 2 + src/utils/assistant.util.ts | 2 +- src/utils/clock.util.ts | 57 ++++++++++++++++++++++ 8 files changed, 107 insertions(+), 41 deletions(-) create mode 100644 src/rooms/commands/ManualTurnEndCommand.ts create mode 100644 src/utils/clock.util.ts diff --git a/src/cfg/RoomOptions.ts b/src/cfg/RoomOptions.ts index de66a69..49c5be6 100644 --- a/src/cfg/RoomOptions.ts +++ b/src/cfg/RoomOptions.ts @@ -24,7 +24,7 @@ export class RoomOptions { * @param opt */ public multipleEat(opt?: any) { - return true + return false } /** @@ -32,7 +32,7 @@ export class RoomOptions { * @param opt */ public autoDiscard(opt?: any) { - return false + return true } } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index ed4dd59..4bcfbe7 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -21,6 +21,7 @@ import { RobotClient } from '../robot/RobotClient' import { ChangePetCommand } from './commands/ChangePetCommand' import { createRobot } from '../common/WebApi' import { Service } from '../service/Service' +import { ManualTurnEndCommand } from './commands/ManualTurnEndCommand' export class GeneralRoom extends Room { dispatcher = new Dispatcher(this) @@ -143,6 +144,11 @@ export class GeneralRoom extends Room { this.broadcast('broadcast_s2c', message, { except: client }) }) + this.onMessage('turn_end_c2s', (client) => { + msgLog('turn_end_c2s from ', client.sessionId) + this.dispatcher.dispatch(new ManualTurnEndCommand(), { client }) + }) + this.onMessage('*', (client, type, message) => { // // Triggers when any other type of message is sent, @@ -285,7 +291,7 @@ export class GeneralRoom extends Room { return -1 } clock.pause() - return clock.time - clock.elapsedTime + return clock.elapsedTime } /** diff --git a/src/rooms/commands/DiscardCommand.ts b/src/rooms/commands/DiscardCommand.ts index 5938aeb..6ea422b 100644 --- a/src/rooms/commands/DiscardCommand.ts +++ b/src/rooms/commands/DiscardCommand.ts @@ -11,6 +11,7 @@ import { Wait } from './Wait' import { StateTypeEnum } from '../enums/StateTypeEnum' import { RoomOptions } from '../../cfg/RoomOptions' import { ClockNameConst } from '../../constants/ClockNameConst' +import { stopDrawCardClock } from '../../utils/clock.util' /** * 出牌 @@ -106,13 +107,8 @@ export class DiscardCommand extends Command= 0) { - let maxTime = new GameEnv().maxDiscardTime * 1000 - let count = elapsedTime - maxTime - let newCount = player.extraTime - Math.max(count, 0) - player.extraTime = Math.max(newCount, 0) - } + stopDrawCardClock(this.room, player) + for (let card of tmpCards) { this.state.cards.set(card.id + '', card) player.cardQueue.push(card) diff --git a/src/rooms/commands/DrawCommand.ts b/src/rooms/commands/DrawCommand.ts index 1f425b4..56acf9f 100644 --- a/src/rooms/commands/DrawCommand.ts +++ b/src/rooms/commands/DrawCommand.ts @@ -1,11 +1,7 @@ import { Command } from '@colyseus/command' import { CardGameState } from '../schema/CardGameState' import { GameEnv } from '../../cfg/GameEnv' -import { debugRoom } from '../../common/Debug' -import { DiscardCommand } from './DiscardCommand' -import { ClockNameConst } from '../../constants/ClockNameConst' -import { RoomOptions } from '../../cfg/RoomOptions' -import { TurnEndCommand } from './TurnEndCommand' +import { addDrawCardClock } from '../../utils/clock.util' /** * 正常的抽卡 @@ -14,30 +10,6 @@ export class DrawCommand extends Command { async execute() { let sessionId = this.state.currentTurn this.room.addCard(sessionId, new GameEnv().roundDrawNum, 0) - let maxTime = new GameEnv().maxDiscardTime * 1000 - let player = this.state.players.get(sessionId) - let self = this - let timeOverDraw = function () { - if (sessionId == self.state.currentTurn) { - if (new RoomOptions().autoDiscard()) { - let client = self.room.getClient(sessionId) - let card = player.cards.values().next().value - debugRoom('出牌时间到, 自动出牌: ', card.id) - player.extraTime = 0 - if (client) { - self.room.dispatcher.dispatch(new DiscardCommand(), { - client, - cards: [card.id], - dtype: 1 - }) - } - } else { - debugRoom('出牌时间到, 下个玩家开始') - self.room.dispatcher.dispatch(new TurnEndCommand()) - } - - } - } - this.room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD) + addDrawCardClock(this.room) } } diff --git a/src/rooms/commands/ManualTurnEndCommand.ts b/src/rooms/commands/ManualTurnEndCommand.ts new file mode 100644 index 0000000..95b2da8 --- /dev/null +++ b/src/rooms/commands/ManualTurnEndCommand.ts @@ -0,0 +1,33 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { TurnEndCommand } from './TurnEndCommand' +import { Client } from 'colyseus' +import { stopDrawCardClock } from '../../utils/clock.util' +import { GameStateConst } from '../../constants/GameStateConst' + +/** + * 客户端主动结束一轮 + */ +export class ManualTurnEndCommand extends Command { + + execute({ client } = this.payload) { + if (this.state.currentTurn !== client.sessionId) { + this.room.send(client, 'turn_end_s2c', { + errcode: 2, + errmsg: `发送消息的玩家不是当前玩家` + }) + return + } + if (this.state.gameState !== GameStateConst.STATE_BEGIN_DRAW) { + this.room.send(client, 'turn_end_s2c', { + errcode: 3, + errmsg: `当前状态不适合停止` + }) + return + } + const player = this.state.players.get(client.sessionId) + stopDrawCardClock(this.room, player) + return [new TurnEndCommand()] + } + +} diff --git a/src/rooms/commands/SelectPetCommand.ts b/src/rooms/commands/SelectPetCommand.ts index e2d46e4..98ab887 100644 --- a/src/rooms/commands/SelectPetCommand.ts +++ b/src/rooms/commands/SelectPetCommand.ts @@ -9,6 +9,7 @@ import assistantUtil from '../../utils/assistant.util' import { GameStateConst } from '../../constants/GameStateConst' import { debugRoom } from '../../common/Debug' import { ClockNameConst } from '../../constants/ClockNameConst' +import { addDrawCardClock } from '../../utils/clock.util' /** * 选择随从或者法术 @@ -103,6 +104,7 @@ export class SelectPetCommand extends Command 0) { - if (cur != tmp[i - 1] + 1) { + if (cur != tmp[tmp.length - 1] + 1) { tmp.length = 0 } tmp.push(cur) diff --git a/src/utils/clock.util.ts b/src/utils/clock.util.ts new file mode 100644 index 0000000..661b27f --- /dev/null +++ b/src/utils/clock.util.ts @@ -0,0 +1,57 @@ +import { Room } from 'colyseus' +import { RoomOptions } from '../cfg/RoomOptions' +import { debugRoom } from '../common/Debug' +import { DiscardCommand } from '../rooms/commands/DiscardCommand' +import { TurnEndCommand } from '../rooms/commands/TurnEndCommand' +import { ClockNameConst } from '../constants/ClockNameConst' +import { GameEnv } from '../cfg/GameEnv' +import { Player } from '../rooms/schema/Player' + +/** + * 添加出牌计时器 + * @param {Room} room + */ +export function addDrawCardClock(room: Room) { + const sessionId = room.state.currentTurn + const maxTime = new GameEnv().maxDiscardTime * 1000 + const player = room.state.players.get(sessionId) + let timeOverDraw = function () { + if (sessionId == room.state.currentTurn) { + if (new RoomOptions().autoDiscard()) { + const client = room.getClient(sessionId) + const card = player.cards.values().next().value + debugRoom('出牌时间到, 自动出牌: ', card.id) + player.extraTime = 0 + if (client && card) { + room.dispatcher.dispatch(new DiscardCommand(), { + client, + cards: [card.id], + dtype: 1 + }) + } else { + debugRoom('出牌时间到, 玩家没有手牌') + room.dispatcher.dispatch(new TurnEndCommand()) + } + } else { + debugRoom('出牌时间到, 下个玩家开始') + room.dispatcher.dispatch(new TurnEndCommand()) + } + } + } + room.beginSchedule(maxTime + player.extraTime, timeOverDraw, ClockNameConst.DRAW_CARD) +} + +/** + * 停止出牌计时, 并更新玩家灵活时间 + * @param {Room} room + * @param {Player} player + */ +export function stopDrawCardClock(room: Room, player: Player) { + let elapsedTime = room.stopSchedule(ClockNameConst.DRAW_CARD) + if (elapsedTime >= 0) { + let maxTime = new GameEnv().maxDiscardTime * 1000 + let count = elapsedTime - maxTime + let newCount = player.extraTime - Math.max(count, 0) + player.extraTime = Math.max(newCount, 0) + } +}