完善配置的加载

This commit is contained in:
zhl 2020-12-02 23:20:24 +08:00
parent 576f22786a
commit eb085651d0
4 changed files with 46 additions and 17 deletions

View File

@ -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])]);
}
}
}

View File

@ -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;
}

View File

@ -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;
}

View File

@ -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<Card> = [];
let numCfgMap: Map<number, SystemCardCfg> = global.$cfg.get(BaseConst.SYSTEMCARD);
let effCfgMap = global.$cfg.get(BaseConst.EFFECTCARD);
let nums: Array<number> = [];
let types : Array<number> = [];
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;