From b0635d77a5c300c2cba10f659c45504f2e77e408 Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 9 Dec 2020 23:26:09 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=BA=9Bgm=E5=91=BD?= =?UTF-8?q?=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/index.ts | 1 + src/rooms/GeneralRoom.ts | 6 +++ src/rooms/commands/GMCommand.ts | 81 +++++++++++++++++++++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 src/rooms/commands/GMCommand.ts diff --git a/src/index.ts b/src/index.ts index a437974..5cd094f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -22,6 +22,7 @@ initData(); 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'), }); diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 2687ab5..51c7ffe 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -15,6 +15,7 @@ import {Delayed} from "@gamestdio/timer/lib/Delayed"; import {IncomingMessage} from "http"; import {PlayerStateConst} from "../constants/PlayerStateConst"; import {Player} from "./schema/Player"; +import {GMCommand} from "./commands/GMCommand"; export class GeneralRoom extends Room { @@ -69,6 +70,11 @@ export class GeneralRoom extends Room { this.dispatcher.dispatch(new SelectHeroCommand(), {client, heroId: message.heroId}); }); + this.onMessage("gm", (client, message) => { + msgLog('gm command from ', client.sessionId, message); + this.dispatcher.dispatch(new GMCommand(), {client, message}); + }); + this.onMessage("*", (client, type, message) => { // // Triggers when any other type of message is sent, diff --git a/src/rooms/commands/GMCommand.ts b/src/rooms/commands/GMCommand.ts new file mode 100644 index 0000000..f5aff22 --- /dev/null +++ b/src/rooms/commands/GMCommand.ts @@ -0,0 +1,81 @@ +import {Command} from "@colyseus/command"; +import {CardGameState} from "../schema/CardGameState"; +import {Client} from "colyseus"; +import {debugRoom, error} from "../../common/Debug"; + + +export class GMCommand extends Command { + + async execute({client, message} = this.payload) { + if (message.indexOf(':') < 0) { + return; + } + let arr = message.split(':'); + switch (arr[0]) { + case 'changeeffect': + debugRoom(`receive changeeffect command: ${arr[1]}`); + this.changePlayerCards(arr[1]); + break; + case 'changequeue': + this.changeCardQueue(arr[1]); + break; + case 'draw': + this.drawCard(arr[1]); + break; + } + + } + getPlayerByIdx(idx: number) { + let players = [...this.state.players.values()]; + return players[idx]; + } + /** + * 将一个玩家的手牌全变成指定的效果卡 + * changeeffect:0|20011 + * @param msg + */ + changePlayerCards(msg: string) { + if (msg.indexOf('|') < 0) { + error(`错误的GM命令格式: ${msg}`); + return; + } + let arr = msg.split('|'); + let playerIdx = parseInt(arr[0]); + let effectId = parseInt(arr[1]); + let player = this.getPlayerByIdx(playerIdx); + for (let [, card] of player.cards) { + card.effect = effectId; + } + let client = this.room.getClient(player.id); + client.send('sync_card', {}, {afterNextPatch: true}); + } + + /** + * 将牌组中的卡全部变成指定的效果卡 + * changequeue:20011 + * @param msg + */ + changeCardQueue(msg: string) { + let effectId = parseInt(msg); + for (let card of this.state.cardQueue) { + card.effect = effectId; + } + } + + /** + * 抽一定数量的卡 + * draw:0|3 + * @param msg + */ + drawCard(msg: string) { + if (msg.indexOf('|') < 0) { + error(`错误的GM命令格式: ${msg}`); + return; + } + let arr = msg.split('|'); + let playerIdx = parseInt(arr[0]); + let player = this.getPlayerByIdx(playerIdx); + let count = parseInt(arr[1]); + this.room.addCard(player.id, count, 0); + } +}