From cb7b1cef6d3d861adc2f723eb20cc6e8eb811cd0 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 3 Dec 2020 14:57:28 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=B0=81=E8=A3=85=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E4=B8=8B=E5=8F=91=E6=B6=88=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/constants/GameStateConst.ts | 2 ++ src/global.d.ts | 19 +++++++++++++------ src/index.ts | 1 + src/rooms/GeneralRoom.ts | 10 ++++++++++ src/rooms/MSender.ts | 8 ++++++++ src/rooms/commands/NextTurnCommand.ts | 3 ++- src/rooms/commands/OnJoinCommand.ts | 2 +- src/rooms/commands/PartResultCommand.ts | 16 ++++++++++++++++ src/utils/game.util.ts | 12 +++++++++--- tsconfig.json | 1 + 10 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 src/rooms/MSender.ts create mode 100644 src/rooms/commands/PartResultCommand.ts diff --git a/src/constants/GameStateConst.ts b/src/constants/GameStateConst.ts index 3ebd7db..742dfe5 100644 --- a/src/constants/GameStateConst.ts +++ b/src/constants/GameStateConst.ts @@ -16,6 +16,8 @@ export class GameStateConst { public static readonly DETERMINE_TURN = 6; // 选英雄阶段 public static readonly CHANGE_HERO = 7; + // 游戏中的结算轮 + public static readonly STATE_ROUND_RESULT = 8; // 游戏结束 public static readonly STATE_GAME_OVER = 9; diff --git a/src/global.d.ts b/src/global.d.ts index 4bcf86d..5fe9e91 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -9,12 +9,19 @@ declare global { } } } - -declare module colyseus { - namespace Colyseus { - class Room { - testFun(param1: string): void; - } +/** + * GeneralRoom 扩展方法 + */ +import { Room } from "colyseus"; +declare module "colyseus" { + interface Room { + /** + * 广播玩家加入房间 + * @param data + * @param options + */ + bUserJoin(data?: any, options?: any): void; } } + diff --git a/src/index.ts b/src/index.ts index 6771c59..f9ae7b5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -9,6 +9,7 @@ import { GeneralRoom } from "./rooms/GeneralRoom"; import {MongooseDriver} from "colyseus/lib/matchmaker/drivers/MongooseDriver"; import {initData} from "./common/GConfig"; +require('./rooms/MSender'); const port = Number(process.env.PORT || 2567); const app = express() diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 0a9d3a6..3e230cc 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -81,4 +81,14 @@ export class GeneralRoom extends Room { this.dispatcher.stop(); } + /** + * begin of message + */ + + + /** + * end of message + */ + + } diff --git a/src/rooms/MSender.ts b/src/rooms/MSender.ts new file mode 100644 index 0000000..4640ed9 --- /dev/null +++ b/src/rooms/MSender.ts @@ -0,0 +1,8 @@ +import { Room } from "colyseus"; + +const room = Room.prototype; + +room.bUserJoin = function (data, options?: any) { + this.broadcast("player_join", data, options); +} + diff --git a/src/rooms/commands/NextTurnCommand.ts b/src/rooms/commands/NextTurnCommand.ts index 5a5aea5..44826cf 100644 --- a/src/rooms/commands/NextTurnCommand.ts +++ b/src/rooms/commands/NextTurnCommand.ts @@ -1,6 +1,7 @@ import {Command} from "@colyseus/command"; import {CardGameState} from "../schema/CardGameState"; import {DrawCommand} from "./DrawCommand"; +import {GameStateConst} from "../../constants/GameStateConst"; /** * 下一轮 @@ -8,7 +9,7 @@ import {DrawCommand} from "./DrawCommand"; export class NextTurnCommand extends Command { execute() { - this.state.gameState = 2; + this.state.gameState = GameStateConst.STATE_BEGIN_DRAW; this.state.subTurn = ''; const sessionIds = [...this.state.players.keys()]; this.state.currentTurn = (this.state.currentTurn) diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index c8828eb..bb57af3 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -21,7 +21,7 @@ export class OnJoinCommand extends Command { + + execute() { + this.state.gameState = GameStateConst.STATE_ROUND_RESULT; + } + +} diff --git a/src/utils/game.util.ts b/src/utils/game.util.ts index af4cd36..cda52c6 100644 --- a/src/utils/game.util.ts +++ b/src/utils/game.util.ts @@ -2,14 +2,14 @@ import {Card} from "../rooms/schema/Card"; import arrUtil from "./array.util"; import {Player} from "../rooms/schema/Player"; -import {singleton} from "../common/Singleton"; -import {GameEnv} from "../cfg/GameEnv"; import {BaseConst} from "../constants/BaseConst"; import {SystemCardCfg} from "../cfg/parsers/SystemCardCfg"; import {EffectCardCfg} from "../cfg/parsers/EffectCardCfg"; let gameUtil = { - // TODO: 根据配表生成牌组 + /** + * 游戏开始时, 初始化卡组 + */ initCardQue() { let cards: Array = []; let numCfgMap: Map = global.$cfg.get(BaseConst.SYSTEMCARD); @@ -34,6 +34,12 @@ let gameUtil = { arrUtil.randomSort(cards); return cards; }, + /** + * 根据配表中的权重, 获取一个效果卡的id + * @param weightArr 配表中的权重列表 + * @param effCfgMap 配表中的所有效果卡的配置 + * @param countMap 当前已生成的效果卡数量 + */ getRandomEffect(weightArr: number[][], effCfgMap: Map, countMap: Map) { let total = 0; let tmpArr:number[][] = []; diff --git a/tsconfig.json b/tsconfig.json index 6549b4f..fd67a3e 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -14,6 +14,7 @@ "strict": true, "strictNullChecks": false, "esModuleInterop": true, + "moduleResolution": "node", "experimentalDecorators": true } } From fc68018a3246707fd41465d3f491d699fe42e9d4 Mon Sep 17 00:00:00 2001 From: zhl Date: Thu, 3 Dec 2020 15:19:48 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E9=80=89=E6=8B=A9=E8=8B=B1=E9=9B=84?= =?UTF-8?q?=E6=97=B6=E5=A2=9E=E5=8A=A0=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/global.d.ts | 15 ++++++++++++++- src/rooms/GeneralRoom.ts | 2 +- src/rooms/MSender.ts | 8 ++++++++ src/rooms/commands/OnJoinCommand.ts | 2 +- src/rooms/commands/SelectHeroCommand.ts | 14 +++++++++++--- src/rooms/logic/Handler/PetHandler.ts | 6 +++--- src/utils/game.util.ts | 2 +- 7 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index 5fe9e91..ed7553c 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -12,7 +12,7 @@ declare global { /** * GeneralRoom 扩展方法 */ -import { Room } from "colyseus"; +import {Client, Room} from "colyseus"; declare module "colyseus" { interface Room { /** @@ -21,6 +21,19 @@ declare module "colyseus" { * @param options */ bUserJoin(data?: any, options?: any): void; + + /** + * 给指定用户下发英雄选择结果 + * @param client + * @param data + */ + sSelectHero(client: Client, data?: any); + + /** + * 广播英雄选择结果 + * @param data + */ + bSelectHero(data?: any); } } diff --git a/src/rooms/GeneralRoom.ts b/src/rooms/GeneralRoom.ts index 3e230cc..2eccc3e 100644 --- a/src/rooms/GeneralRoom.ts +++ b/src/rooms/GeneralRoom.ts @@ -53,7 +53,7 @@ export class GeneralRoom extends Room { this.onMessage("select_hero_c2s", (client, message) => { console.log('select_hero from ', client.sessionId, message); - this.dispatcher.dispatch(new SelectHeroCommand(), {client, heroId: message.heroId}); + this.dispatcher.dispatch(new SelectHeroCommand(), {client, heroId: message.heroId, battle: this.battleMan}); }); this.onMessage("*", (client, type, message) => { diff --git a/src/rooms/MSender.ts b/src/rooms/MSender.ts index 4640ed9..ce9dd90 100644 --- a/src/rooms/MSender.ts +++ b/src/rooms/MSender.ts @@ -6,3 +6,11 @@ room.bUserJoin = function (data, options?: any) { this.broadcast("player_join", data, options); } +room.sSelectHero = function (client, data) { + this.send(client, 'select_hero_s2c', data); +} + +room.bSelectHero = function(data) { + this.broadcast("select_hero_s2c", data); +} + diff --git a/src/rooms/commands/OnJoinCommand.ts b/src/rooms/commands/OnJoinCommand.ts index bb57af3..1f57cd6 100644 --- a/src/rooms/commands/OnJoinCommand.ts +++ b/src/rooms/commands/OnJoinCommand.ts @@ -4,6 +4,7 @@ import {Player} from "../schema/Player"; import {Client} from "colyseus"; import {GameStateConst} from "../../constants/GameStateConst"; import { BattleHandler } from "rooms/logic/Handler/BattleHandler"; +import {BaseConst} from "../../constants/BaseConst"; /** * 玩家成功加入房间 @@ -16,7 +17,6 @@ export class OnJoinCommand extends Command= this.room.maxClients) { this.room.lock(); this.state.gameState = GameStateConst.STATE_WAIT_PREPARE; diff --git a/src/rooms/commands/SelectHeroCommand.ts b/src/rooms/commands/SelectHeroCommand.ts index cea1f86..9e4cd1f 100644 --- a/src/rooms/commands/SelectHeroCommand.ts +++ b/src/rooms/commands/SelectHeroCommand.ts @@ -3,16 +3,24 @@ import {CardGameState} from "../schema/CardGameState"; import {Client} from "colyseus"; import {PlayerStateConst} from "../../constants/PlayerStateConst"; import {BeginGameCommand} from "./BeginGameCommand"; +import {BattleHandler} from "../logic/Handler/BattleHandler"; +import {BaseConst} from "../../constants/BaseConst"; /** * 选择英雄 */ -export class SelectHeroCommand extends Command { - execute({ client, heroId } = this.payload) { +export class SelectHeroCommand extends Command { + execute({ client, heroId, battle} = this.payload) { let player = this.state.players.get(client.sessionId); + const heroMap = global.$cfg.get(BaseConst.HERO); + if (!heroMap || !heroMap.has(heroId)) { + this.room.sSelectHero(client, {errcode: 1, errmsg: '无法找到对应英雄的配置'}); + return; + } player.heroId = heroId; player.state = PlayerStateConst.PLAYER_SELECT_HERO; - this.room.broadcast('select_hero_s2c', {errocode: 0, errmsg: '', player: client.sessionId, heroId: heroId}); + battle.addPlayer(player); + this.room.bSelectHero({errocode: 0, errmsg: '', player: client.sessionId, heroId: heroId}); let readyCount = 0; for (let [sessionId, player] of this.state.players) { if (player.state === PlayerStateConst.PLAYER_SELECT_HERO) { diff --git a/src/rooms/logic/Handler/PetHandler.ts b/src/rooms/logic/Handler/PetHandler.ts index 25923c3..d547918 100644 --- a/src/rooms/logic/Handler/PetHandler.ts +++ b/src/rooms/logic/Handler/PetHandler.ts @@ -10,7 +10,7 @@ export class PetHandler { private _owner: PlayerHandler; _id: number; _cfg: UnitCfg; - _exskills: number[]; + _exskills: number[] = []; _baseap: number; @@ -23,7 +23,7 @@ export class PetHandler { public init(apet: Pet, owner: PlayerHandler){ this._pet = apet; this._owner = owner; - }; + }; public addGroupAttr(attrstr: string, value: number){ }; @@ -48,4 +48,4 @@ export class PetHandler { } }); } -} \ No newline at end of file +} diff --git a/src/utils/game.util.ts b/src/utils/game.util.ts index cda52c6..0f45822 100644 --- a/src/utils/game.util.ts +++ b/src/utils/game.util.ts @@ -52,7 +52,7 @@ let gameUtil = { for (let data of tmpArr) { if (data[1] >= num ) { let count = countMap.has(data[0]) ? countMap.get(data[0]) : 0; - if (count < effCfgMap.get(data[0]).count) { + if (count <= effCfgMap.get(data[0]).count) { effid = effCfgMap.get(data[0]).id; countMap.set(effid, count + 1); break;