card_svr/src/rooms/GeneralRoom.ts
2020-12-04 14:40:23 +08:00

90 lines
3.3 KiB
TypeScript

import { Room, Client } from "colyseus";
import { CardGameState } from "./schema/CardGameState";
import { OnJoinCommand } from "./commands/OnJoinCommand";
import { PlayReadyCommand} from "./commands/PlayReadyCommand";
import { Dispatcher } from "@colyseus/command";
import {DiscardCommand} from "./commands/DiscardCommand";
import {NextSubCommand} from "./commands/NextSubCommand";
import {SelectPetCommand} from "./commands/SelectPetCommand";
import {ChangeCardCommand} from "./commands/ChangeCardCommand";
import {SelectHeroCommand} from "./commands/SelectHeroCommand";
import {EatCardCommand} from "./commands/EatCardCommand";
import {GiveUpCommand} from "./commands/GiveUpCommand";
import {BattleHandler} from "./logic/Handler/BattleHandler";
import {debugRoom} from "../common/Debug";
export class GeneralRoom extends Room {
dispatcher = new Dispatcher(this);
maxClients = 4;
clientMap = new Map();
battleMan = new BattleHandler();
onCreate (options: any) {
let cs = new CardGameState();
this.setState(cs);
this.battleMan.init(cs, this);
this.clock.start();
this.state.gameSate = 0;
this.onMessage("play_ready_c2s", (client, message) => {
debugRoom('play_ready from ', client.sessionId, message);
this.dispatcher.dispatch(new PlayReadyCommand(), {client});
});
this.onMessage("change_card_c2s", (client, message) => {
debugRoom('change_card from ', client.sessionId, message);
this.dispatcher.dispatch(new ChangeCardCommand(), {client, cards: message.cards});
});
this.onMessage("discard_card_c2s", (client, message) => {
debugRoom('discard_card from ', client.sessionId, message);
this.dispatcher.dispatch(new DiscardCommand(), {client, cards: message.cards});
});
this.onMessage("eat_card_c2s", (client, message) => {
debugRoom('eat_card from ', client.sessionId, message);
this.dispatcher.dispatch(new EatCardCommand(), {client, cards: message.cards, target: message.target});
});
this.onMessage("give_up_eat_c2s", (client, message) => {
debugRoom('give_up_take from ', client.sessionId, message);
this.dispatcher.dispatch(new GiveUpCommand(), {client});
});
this.onMessage("select_pet_c2s", (client, message) => {
debugRoom('select_pet from ', client.sessionId, message);
this.dispatcher.dispatch(new SelectPetCommand(), {client, cardId: message.card, playerId: message.player, pos: message.pos, effCards: message.effCards });
});
this.onMessage("select_hero_c2s", (client, message) => {
debugRoom('select_hero from ', client.sessionId, message);
this.dispatcher.dispatch(new SelectHeroCommand(), {client, heroId: message.heroId});
});
this.onMessage("*", (client, type, message) => {
//
// Triggers when any other type of message is sent,
// excluding "action", which has its own specific handler defined above.
//
debugRoom(client.sessionId, "sent", type, message);
});
}
onJoin (client: Client, options: any) {
this.dispatcher.dispatch(new OnJoinCommand(), {
client: client
});
this.clientMap.set(client.sessionId, client);
}
onLeave (client: Client, consented: boolean) {
}
onDispose() {
this.dispatcher.stop();
}
getClient(sessionId: string) {
return this.clients.find(client => client.sessionId == sessionId );
}
}