From 02eb5c1766f825b11ca695177c789f26c98df5a7 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 18 Mar 2021 17:05:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=AF=E5=90=83=E7=89=8C=E5=8F=98=E6=9B=B4?= =?UTF-8?q?=E5=90=8E,=20=E5=A2=9E=E5=8A=A0=E5=B9=BF=E6=92=AD=E6=B6=88?= =?UTF-8?q?=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/Extend.ts | 16 ++++++++++++++++ src/global.d.ts | 7 +++++++ src/message/EatCard.ts | 18 ++++++++++++++++++ src/rooms/MSender.ts | 9 +++++++++ src/rooms/commands/DiscardCommand.ts | 8 ++++++++ src/rooms/commands/TurnEndCommand.ts | 3 +++ 6 files changed, 61 insertions(+) create mode 100644 src/message/EatCard.ts diff --git a/src/common/Extend.ts b/src/common/Extend.ts index 3251380..2e91e76 100644 --- a/src/common/Extend.ts +++ b/src/common/Extend.ts @@ -889,6 +889,12 @@ interface Map { * @param value */ inc?(key: K, value: V): this; + + /** + * 获取Map中第一个值 + * @return {Map} + */ + getFirst?(): Map | null; } Object.defineProperties(Map.prototype, { @@ -900,5 +906,15 @@ Object.defineProperties(Map.prototype, { this.set(key, value); } } + }, + getFirst: { + value: function () { + if (this.size > 0) { + return this.values().next().value + } else { + return null + } + }, + writable: true } }); diff --git a/src/global.d.ts b/src/global.d.ts index 8b2d6a2..c20e83b 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -187,6 +187,13 @@ declare module 'colyseus' { */ bMsgQueue(datas: IMsg[], options?: any): void; + /** + * 广播可吃牌变更新消息 + * @param data + * @param options + */ + bEatChange(data: any, options?: {except: Client}): void; + send(client: Client, type: string, data?: any): void; diff --git a/src/message/EatCard.ts b/src/message/EatCard.ts new file mode 100644 index 0000000..a86ef0b --- /dev/null +++ b/src/message/EatCard.ts @@ -0,0 +1,18 @@ +import { Card } from '../rooms/schema/Card' + +export class EatCard{ + id: number + effect: number + owner: string + + constructor() { + } + + static fromCard(card: Card) { + let result = new EatCard() + result.id = card.id + result.effect = card.effect + result.owner = card.owner + return result + } +} diff --git a/src/rooms/MSender.ts b/src/rooms/MSender.ts index 32d869f..b5c13c5 100644 --- a/src/rooms/MSender.ts +++ b/src/rooms/MSender.ts @@ -240,6 +240,15 @@ Object.defineProperties(Room.prototype, { this.broadcast('player_dead_s2c', data, options); } }, + bEatChange: { + value: function (data: any, options?: {except: Client}) { + let bOptions: any = {afterNextPatch: true} + if (options?.except) { + bOptions.except = options?.except + } + this.broadcast('eatcard_change_s2c', data, bOptions); + } + }, send: { value: function (client: Client, type: string, data?: any) { diff --git a/src/rooms/commands/DiscardCommand.ts b/src/rooms/commands/DiscardCommand.ts index 9333bd8..62d5ea9 100644 --- a/src/rooms/commands/DiscardCommand.ts +++ b/src/rooms/commands/DiscardCommand.ts @@ -13,6 +13,7 @@ import { RULE_CANEAT, RULE_SINGLEEAT } from '../../cfg/RoomOptions' import { ClockNameConst } from '../../constants/ClockNameConst' import { stopDrawCardClock } from '../../utils/clock.util' import { CardType } from '../../cfg/enums/CardType' +import { EatCard } from '../../message/EatCard' /** * 出牌 @@ -108,6 +109,7 @@ export class DiscardCommand extends Command 0) { + let card = this.state.cards.values().next().value + old = EatCard.fromCard(card) + } for (let [key, val] of this.state.cards) { this.state.cards.delete(key) } let card = tmpCards[0] card.round = this.state.round this.state.cards.set(card.id + '', card) + this.room.bEatChange({type: 'replace', old, current: EatCard.fromCard(card)}) } /** * 这说明是当前轮正常出牌, diff --git a/src/rooms/commands/TurnEndCommand.ts b/src/rooms/commands/TurnEndCommand.ts index 834b5ef..6fb9e70 100644 --- a/src/rooms/commands/TurnEndCommand.ts +++ b/src/rooms/commands/TurnEndCommand.ts @@ -5,6 +5,7 @@ import { GameResultCommand } from './GameResultCommand' import gameUtil from '../../utils/game.util' import { StateTypeEnum } from '../enums/StateTypeEnum' import { RULE_EATFLOW, RULE_EATSELF } from '../../cfg/RoomOptions' +import { EatCard } from '../../message/EatCard' /** * 一轮结束 @@ -48,12 +49,14 @@ export class TurnEndCommand extends Command { //如果可以吃自己出的牌, 那么等到下一轮自己出完牌再清空 if (targetCard.owner === this.state.currentTurn && targetCard.round != this.state.round) { this.state.cards.clear() + this.room.bEatChange({type: 'remove', old: EatCard.fromCard(targetCard), current: null}) } } else { // 如果不能吃自己的牌, 那么在下一回合自己轮开始前清空自己的牌 const nextPid = this.room.nextPlayer(this.state.currentTurn) if (targetCard.owner == nextPid) { this.state.cards.clear() + this.room.bEatChange({type: 'remove', old: EatCard.fromCard(targetCard), current: null}) } }