From 43ccef0d8822f0a574615140ac9f1ab48cd0994a Mon Sep 17 00:00:00 2001 From: zhl Date: Tue, 30 Mar 2021 13:05:17 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=AF=94=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E7=9A=84=E6=B8=B8=E6=88=8F=E6=A8=A1=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/GameStateConst.ts | 4 ++ src/rooms/commands/BeginGameCommand.ts | 7 ++- src/rooms/commands/DiceCommand.ts | 55 +++++++++++++++++++++++ src/rooms/commands/DiceDrawCommand.ts | 10 +++++ src/rooms/commands/DiceNextTurnCommand.ts | 12 +++++ src/rooms/commands/DiceResultCommand.ts | 43 ++++++++++++++++++ src/rooms/commands/DiceTurnEndCommand.ts | 9 ++++ src/rooms/commands/EatCardCommand.ts | 2 +- src/rooms/commands/EatConfirmCommand.ts | 12 ++--- src/rooms/schema/CardGameState.ts | 2 +- 10 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 src/rooms/commands/DiceCommand.ts create mode 100644 src/rooms/commands/DiceDrawCommand.ts create mode 100644 src/rooms/commands/DiceNextTurnCommand.ts create mode 100644 src/rooms/commands/DiceResultCommand.ts create mode 100644 src/rooms/commands/DiceTurnEndCommand.ts diff --git a/src/constants/GameStateConst.ts b/src/constants/GameStateConst.ts index 742dfe5..0ff2f50 100644 --- a/src/constants/GameStateConst.ts +++ b/src/constants/GameStateConst.ts @@ -20,5 +20,9 @@ export class GameStateConst { public static readonly STATE_ROUND_RESULT = 8; // 游戏结束 public static readonly STATE_GAME_OVER = 9; + // 比大小游戏 + public static readonly STATE_DICE_TURN = 10; + // 比大小结果 + public static readonly STATE_DICE_RESULT = 11; } diff --git a/src/rooms/commands/BeginGameCommand.ts b/src/rooms/commands/BeginGameCommand.ts index 733d450..5053eb3 100644 --- a/src/rooms/commands/BeginGameCommand.ts +++ b/src/rooms/commands/BeginGameCommand.ts @@ -5,6 +5,7 @@ import gameUtil from '../../utils/game.util' import { GameEnv } from '../../cfg/GameEnv' import { NextTurnCommand } from './NextTurnCommand' import { debugRoom } from '../../common/Debug' +import { DiceNextTurnCommand } from './DiceNextTurnCommand' /** * 开始游戏 @@ -62,7 +63,11 @@ export class BeginGameCommand extends Command { const cardChangeTime = new GameEnv().cardChangeTime await this.delay(cardChangeTime * 1000) if (this.state.gameState == GameStateConst.STATE_CHANGE_CARD) { - return [new NextTurnCommand()] + if (this.state.mode === 4) { + return [new NextTurnCommand()] + } else { + return [new DiceNextTurnCommand()] + } } } diff --git a/src/rooms/commands/DiceCommand.ts b/src/rooms/commands/DiceCommand.ts new file mode 100644 index 0000000..89bd68b --- /dev/null +++ b/src/rooms/commands/DiceCommand.ts @@ -0,0 +1,55 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { Client } from 'colyseus' +import { error } from '../../common/Debug' +import { DiceResultCommand } from './DiceResultCommand' +import { GameStateConst } from '../../constants/GameStateConst' + +/** + * 出一张牌比大小 + */ +export class DiceCommand extends Command { + async execute({ client, cards } = this.payload) { + let tmpActionMap = this.state.tmpActionMap + const player = this.state.players.get(client.sessionId) + if (this.state.gameState !== GameStateConst.STATE_DICE_TURN) { + this.room.send(client, 'dice_card_s2c', { + errcode: 11, + errmsg: '不是出牌轮' + }) + return + } + if (tmpActionMap.has(player.id)) { + this.room.send(client, 'dice_card_s2c', { + errcode: 12, + errmsg: '本轮已经出牌' + }) + return + } + + let cardIds = [] + for (let id of cards) { + if (player.cards.has(id + '')) { + if (!player.cards.get(id + '').number) { + error(`${ player.id } 的手牌 ${ id } 数据有问题`) + continue + } + const card = player.cards.get(id + '').clone() + player.cards.delete(id + '') + cardIds.push(card) + } else { + error(`${ player.id } 出的牌 ${ id } 在手牌中不存在`) + this.room.send(client, 'dice_card_s2c', { + errcode: 12, + errmsg: `要出的牌 ${ id } 在手牌中不存在` + }) + return + } + } + + this.state.tmpActionMap.set(client.sessionId, cardIds) + return [new DiceResultCommand().setPayload({ timeUp: false })] + + } + +} diff --git a/src/rooms/commands/DiceDrawCommand.ts b/src/rooms/commands/DiceDrawCommand.ts new file mode 100644 index 0000000..aa88724 --- /dev/null +++ b/src/rooms/commands/DiceDrawCommand.ts @@ -0,0 +1,10 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' + +export class DiceDrawCommand extends Command { + async execute() { + for (let [id] of this.state.players) { + this.room.addCard(id, 2, 0) + } + } +} diff --git a/src/rooms/commands/DiceNextTurnCommand.ts b/src/rooms/commands/DiceNextTurnCommand.ts new file mode 100644 index 0000000..4990ea1 --- /dev/null +++ b/src/rooms/commands/DiceNextTurnCommand.ts @@ -0,0 +1,12 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { GameStateConst } from '../../constants/GameStateConst' +import { DiceDrawCommand } from './DiceDrawCommand' + +export class DiceNextTurnCommand extends Command { + async execute() { + this.state.round += 1 + this.state.updateGameState(GameStateConst.STATE_DICE_TURN) + return [new DiceDrawCommand()] + } +} diff --git a/src/rooms/commands/DiceResultCommand.ts b/src/rooms/commands/DiceResultCommand.ts new file mode 100644 index 0000000..4ff370f --- /dev/null +++ b/src/rooms/commands/DiceResultCommand.ts @@ -0,0 +1,43 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { GameStateConst } from '../../constants/GameStateConst' +import { DiceTurnEndCommand } from './DiceTurnEndCommand' +import { Card } from '../schema/Card' +import { CardType } from '../../cfg/enums/CardType' + + +export class DiceResultCommand extends Command { + async execute({timeUp} = this.payload) { + const tmpActionMap = this.state.tmpActionMap + if (tmpActionMap.size >= this.room.maxClients) { + // 开始比较 + this.state.updateGameState(GameStateConst.STATE_DICE_RESULT) + let max = 0 + let winner + let resultMap: any = {} + let cardIds = [] + for (let [pid, cardArr] of tmpActionMap) { + const cards = cardArr as Card[] + let ap = 0 + let doubleCount = 1 + for (const card of cards) { + if (card.type == CardType.general || card.type == CardType.variable_unit) { + ap += card.number + } else { + doubleCount += 1 + } + cardIds.push(card.id) + } + ap = ap * doubleCount + resultMap.push([pid, cardIds]) + if (ap > max) { + max = ap + winner = pid + } + } + this.room.broadcast('dice_result_s2c', {winner, tatal: max, results: resultMap}) + await this.delay(2 * 1000) + return [new DiceTurnEndCommand()]; + } + } +} diff --git a/src/rooms/commands/DiceTurnEndCommand.ts b/src/rooms/commands/DiceTurnEndCommand.ts new file mode 100644 index 0000000..f290c21 --- /dev/null +++ b/src/rooms/commands/DiceTurnEndCommand.ts @@ -0,0 +1,9 @@ +import { Command } from '@colyseus/command' +import { CardGameState } from '../schema/CardGameState' +import { DiceNextTurnCommand } from './DiceNextTurnCommand' + +export class DiceTurnEndCommand extends Command { + async execute() { + return [new DiceNextTurnCommand()] + } +} diff --git a/src/rooms/commands/EatCardCommand.ts b/src/rooms/commands/EatCardCommand.ts index 65bb5d8..16b870a 100644 --- a/src/rooms/commands/EatCardCommand.ts +++ b/src/rooms/commands/EatCardCommand.ts @@ -25,7 +25,7 @@ export class EatCardCommand extends Command = new Map() + tmpActionMap: Map = new Map() /** * 点击重开的玩家数量 */