From eb085651d05522a6796976db2a492da3f92b051a Mon Sep 17 00:00:00 2001 From: zhl Date: Wed, 2 Dec 2020 23:20:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=85=8D=E7=BD=AE=E7=9A=84?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/cfg/parsers/SystemCardCfg.ts | 8 ++++++++ src/common/DataParser.ts | 26 +++++++++++++++++--------- src/rooms/schema/Card.ts | 16 ++++++++++++---- src/utils/game.util.ts | 13 +++++++++---- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/cfg/parsers/SystemCardCfg.ts b/src/cfg/parsers/SystemCardCfg.ts index b3c7652..ce444b0 100644 --- a/src/cfg/parsers/SystemCardCfg.ts +++ b/src/cfg/parsers/SystemCardCfg.ts @@ -6,10 +6,18 @@ export class SystemCardCfg implements Cfg { public count: number; public point: number; public weight: string; + public weightArr: []; public decode(data: any) { this.id = data.id; this.typeId = data.type_id; this.point = data.point; this.weight = data.weight; + let arr = this.weight.split('|'); + this.weightArr = []; + for (let str of arr) { + let subArr = str.split(':'); + // @ts-ignore + this.weightArr.push([parseInt(subArr[0]), parseInt(subArr[1])]); + } } } diff --git a/src/common/DataParser.ts b/src/common/DataParser.ts index dd941ad..a7ae1c5 100644 --- a/src/common/DataParser.ts +++ b/src/common/DataParser.ts @@ -4,7 +4,7 @@ import {GameEnv} from "../cfg/GameEnv"; import {BaseConst} from "../constants/BaseConst"; const $cfg = new Map(); - +const jsonPath = 'configs'; export var DataParser = (function (){ const parsers: { [index: string]: ConfigDataParser } = {}; @@ -13,13 +13,21 @@ export var DataParser = (function (){ regCommonParser(key: string, CfgCreator: { new (): Cfg }, idkey = "id") { regParser(key, (data: any[]): any => { if (!data) return; - let dict = {}; - + let dict = new Map(); + for (let i = 0, len = data.length; i < len; i++) { + let obj = data[i]; + let to = new CfgCreator(); + to.decode(obj); + if (dict.has(to.id)) { + console.error(`配置${key}的数据有误,唯一标识 id 有重复值:${to.id}`) + process.abort(); + } + dict.set(to.id, to); + } return dict; }); }, loadAll() { - let jsonPath = `configs`; let fileArr = jetpack.list(jsonPath); for (let f of fileArr) { let key = f.replace('_tbl.json', ''); @@ -27,11 +35,10 @@ export var DataParser = (function (){ let json = jetpack.read(`${jsonPath}/${f}`, 'json'); if (parser && json){ if (Array.isArray(json)) { - let map = new Map(); - for (let d of json) { - map.set(d.id, d); + let data = parser(json); + if (data) { // 支持一些void的情况 + $cfg.set(key, data); } - $cfg.set(key, map); } else { $cfg.set(key, json); } @@ -41,7 +48,7 @@ export var DataParser = (function (){ Object.assign(global, { $cfg: $cfg }) - } + }, } /** * 注册配置解析 @@ -68,4 +75,5 @@ export interface Cfg * @param {*} [local] 没有接口,但是需要本地赋值的数据 */ decode?: { (local?: any):void }; + id?: number; } diff --git a/src/rooms/schema/Card.ts b/src/rooms/schema/Card.ts index 36e4370..6ab9f0a 100644 --- a/src/rooms/schema/Card.ts +++ b/src/rooms/schema/Card.ts @@ -2,7 +2,7 @@ import {Schema, type, filter} from "@colyseus/schema"; export class Card extends Schema { - constructor(number: number, type: string, id: number) { + constructor(number: number, type: number, id: number, effect: number) { super(); this.number = number; this.type = type; @@ -30,8 +30,16 @@ export class Card extends Schema { @type("boolean") played: boolean = false; /** - * 种类 + * 类型 + * 1: 普通牌 + * 2: 效果强化卡 + * 3: 点数加倍卡 */ - @type("string") - type: string; + @type("number") + type: number; + /** + * 对应的效果强化牌id + */ + @type("number") + effect: number; } diff --git a/src/utils/game.util.ts b/src/utils/game.util.ts index 4e1a0eb..aa150e7 100644 --- a/src/utils/game.util.ts +++ b/src/utils/game.util.ts @@ -4,18 +4,23 @@ 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"; let gameUtil = { // TODO: 根据配表生成牌组 initCardQue() { let cards: Array = []; + let numCfgMap: Map = global.$cfg.get(BaseConst.SYSTEMCARD); + let effCfgMap = global.$cfg.get(BaseConst.EFFECTCARD); let nums: Array = []; let types : Array = []; - for (let i = 0; i < 12; i ++) { - for (let j = 0; j < 16; j ++) { - nums.push(i); + for (let [id, cfg] of numCfgMap) { + for (let i = 0; i < cfg.count; i++) { + nums.push(cfg.point); } } + arrUtil.randomSort(nums); for (let i = 0; i < 8; i ++) { for (let j = 0; j < 24; j ++) { @@ -24,7 +29,7 @@ let gameUtil = { } arrUtil.randomSort(types); for (let i = 0; i < nums.length; i++) { - cards.push(new Card(nums[i] + 1, types[i] + '', i)); + cards.push(new Card(nums[i] + 1, types[i], i, i)); } arrUtil.randomSort(cards); return cards;