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"; import {Delayed} from "@gamestdio/timer/lib/Delayed"; import {IncomingMessage} from "http"; import {BaseConst} from "../constants/BaseConst"; import {PlayerStateConst} from "../constants/PlayerStateConst"; export class GeneralRoom extends Room { dispatcher = new Dispatcher(this); maxClients = 4; clientMap = new Map(); battleMan = new BattleHandler(); // 用于游戏过程中各种计时器, 使用该计时器的前提是, 只针对当前操作玩家 mainClock: Delayed; async onAuth (client:Client, options: any, request: IncomingMessage) { console.log(options); // TODO: 验证用户信息 // client.auth.accountId = options.accountId; // client.auth.sessionId = options.sessionId; return true; } 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, dtype: 0}); }); 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) { let data = { client: client }; this.dispatcher.dispatch(new OnJoinCommand(), data); this.clientMap.set(client.sessionId, client); } //TODO: 掉线逻辑 async onLeave (client: Client, consented: boolean) { this.state.players.get(client.sessionId).state = PlayerStateConst.PLAYER_OFFLINE; try { if (consented) { throw new Error("consented leave"); } await this.allowReconnection(client, 60); this.state.players.get(client.sessionId).state = PlayerStateConst.PLAYER_NORMAL; } catch (e) { this.state.players.delete(client.sessionId); } } onDispose() { this.dispatcher.stop(); } getClient(sessionId: string) { return this.clients.find(client => client.sessionId == sessionId ); } }