增加一些gm命令

This commit is contained in:
zhl 2020-12-09 23:26:09 +08:00
parent dd14a4d184
commit b0635d77a5
3 changed files with 88 additions and 0 deletions

View File

@ -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'),
});

View File

@ -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,

View File

@ -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<CardGameState, {client: Client, message: string}> {
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);
}
}