import {Client, Room} from "colyseus"; import {IMsg} from "../message/IMsg"; import {PetInfoMsg} from "../message/PetInfo"; import {SkillInfoMsg} from "../message/SkillInfo"; import {Pet} from "./schema/Pet"; import {PartResultMsg} from "../message/PartResult"; import {RemovePetMsg} from "../message/RemovePetMsg"; import {error} from "../common/Debug"; import {Player} from "./schema/Player"; /** * 一些封装了的下发消息的方法 */ Object.defineProperties(Room.prototype, { /** * 广播玩家加入房间 * @param data * @param options */ bUserJoin: { value: function (data?: any, options?: any) { this.broadcast("player_join", data, options); }, writable: true }, bUserLeft: { value: function (data?: any) { this.broadcast("player_left", data); }, writable: true }, /** * 给指定用户下发英雄选择结果 * @param client * @param data */ sSelectHero: { value: function (client: Client, data?: any) { this.send(client, 'select_hero_s2c', data); }, writable: true }, /** * 广播英雄选择结果 * @param data */ bSelectHero: { value: function (data?: any) { this.broadcast("select_hero_s2c", data); }, writable: true }, /** * 单独下发一个偷卡信息 * @param data */ sDrawCard: { value: function (client: Client, data?: any) { this.send(client, 'draw_card_s2c', data); } }, /** * 广播一个抽卡信息 * @param data * @param options */ bDrawCard: { value: function (data?: any, options?: any) { this.broadcast('draw_card_s2c', data, options); } }, /** * 增加一个随从 * @param data */ bAddPet: { value: function (data?: PetInfoMsg) { let obj = data.data; let pid = obj.player; let player = this.state.players.get(pid); let pet = player.pets.get(obj.pos + ''); pet.id = obj.id; pet.ap = obj.ap; pet.extAp = obj.extAp; pet.harmReduce = obj.harmReduce; pet.skills.length = 0; pet.state = 1; pet.em = obj.em; pet.silence = obj.silence; pet.effectCount = obj.effectCount; pet.point = obj.point; pet.hps = obj.hps; if (obj.skills) { pet.skills.length = 0; for (let s of obj.skills) { pet.skills.push(s); } } if (obj.extSkills) { pet.extSkills.length = 0; for (let s of obj.extSkills) { pet.extSkills.push(s); } } if (obj.buffs) { pet.buffs.length = 0; for (let s of obj.buffs) { pet.buffs.push(s); } } this.broadcast("pet_info_s2c", data); } }, /** * 发送给个人的消息列表 * @param client * @param data */ sMsgQueue: { value: function (client:Client, datas?: IMsg) { this.send(client, "msg_queue_s2c", datas); } }, /** * 广播的消息列表 * 例: * let datas:IMsg[] = []; * datas.push({errcode: 0, type: '消息类型', data: {pid: 1}}); * this.room.bMsgQueue(datas); * @param datas * @param options */ bMsgQueue: { value: function (datas?: IMsg, options?: any) { this.broadcast("msg_queue_s2c", datas, options); } }, /** * 施放一个技能 * @param data */ bCastSkill: { value: function (data?: SkillInfoMsg) { this.broadcast("cast_skill_s2c", data); } }, /** * 广播偷卡信息 * @param data * @param options */ bStealCard: { value: function (data?: any, options?: any) { this.broadcast("steal_card_s2c", data, options); } }, /** * 单独下发一个偷卡信息 * @param data * @param options */ sStealCard: { value: function (client: Client, data?: any) { this.send(client, "steal_card_s2c", data, {afterNextPatch: true}); } }, /** * 广播一个弃卡信息 * @param data * @param options */ bRemoveCard: { value: function (client: Client, data?: any) { this.send(client, "remove_card_s2c", data); } }, /** * 移除随从 * @param data * @param options */ bRemovePet: { value: function (data?: RemovePetMsg, options?: any) { let obj = data.data; let pid = obj.player; let player = this.state.players.get(pid); if (!player) { error(`bRemovePet no player found: ${pid}`); return; } let pet = player.pets.get(obj.pos + ''); if (!pet) { error(`bRemovePet no pet found: ${pid}, pos: ${obj.pos}`); return; } pet.ap = 0; pet.state = 2; player.pets.set(obj.pos + '', pet); this.broadcast('remove_pet_s2c', data, options); } }, /** * 广播游戏进行中的决斗结果 * @param data * @param options */ bPartResult: { value: function (data?: PartResultMsg, options?: any) { this.broadcast('part_result_s2c', data, options); } }, /** * 广播游戏最终结果 * @param data * @param options */ bGameResult: { value: function (data?: any, options?: any) { this.broadcast('game_result_s2c', data, options); } }, sChangeCard: { value: function (client: Client, data?: any) { this.send(client, "change_card_s2c", data); } }, sNeedChangePet: { value: function (player: string | Player, data?: any) { let client = this.getClient(player); this.send(client, "change_pet_s2c", data); } }, bPlayerDead: { value: function (data: any, options?: any) { 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) { if (client) { client.send(type, data); } else { let assistClient = this.getAssistClient(client.sessionId); assistClient && assistClient.send(type, data); } } } });