添加字典表解析

This commit is contained in:
luying
2020-12-15 15:00:41 +08:00
parent 2a268b2158
commit 87cdd7a80f
33 changed files with 1462 additions and 47 deletions
+54 -2
View File
@@ -6,6 +6,9 @@ import { RoleModel } from '../db/Role';
import { PvpDefenseModel } from '../db/PvpDefense';
import Actor from './actor';
import fs = require('fs');
import path = require('path');
const moment = require('moment');
export function genCode(len) {
@@ -115,6 +118,7 @@ const moment = require('moment');
};
}
/**
* 将 | 分隔的字符串解析为数组,如:a|b|c 解析为[a, b, c]
* @param str 要解析的字符串
@@ -123,12 +127,20 @@ export function decodeArrayStr(str: string, splitForm='|') {
let last = str.substr(str.length-1, 1);
if(last == '&') str = str.substr(0, str.length - 1);
if(str == '') {
return []
return new Array<string>()
} else {
return str.split(splitForm);
}
}
/**
* 将 | 和 & 分隔的字符串解析为 Map,,如:a&b&c|c&d&f 解析为 [[a,b,c], [c,d,f]]
* @param str 要解析的字符串
*/
export function decodeArrayListStr(str: string) {
return decodeArrayStr(str).map(cur => decodeArrayStr(cur, '&'));
}
/**
* 将 | 和 & 分隔的字符串解析为 Map,,如:a&b|c&d|e&f 解析为 Map {a=>b, c=>d, e=>f}
* @param str 要解析的字符串
@@ -434,4 +446,44 @@ export function deepCopy (obj) {
}
}
return target;
};
};
export function readJsonFile(fileName: string) {
const folder = 'jsons';
return fs.readFileSync(path.resolve(__dirname, `../resource/${folder}/${fileName}.json`)).toString('utf8').replace(/^\uFEFF/, '');
}
export function readWarJsonFileList() {
const folder = 'warJsons';
return fs.readdirSync(__dirname + `/../resource/${folder}`)
.map(file => {
return fs.readFileSync(path.resolve(__dirname, `../resource/${folder}/${file}`)).toString('utf8').replace(/^\uFEFF/, '');
});
}
// 字典表常用解析方法
// 解析物品 {"id": number, "count": number} 格式
export function parseReward(str: string) {
let result = new Array<{id: number, count: number}>();
if(!str) return result;
let decodeArr = decodeArrayListStr(str);
for(let [id, count] of decodeArr) {
if(isNaN(parseInt(id)) || isNaN(parseInt(count))) {
throw new Error('data table format wrong');
}
result.push({id: parseInt(id), count: parseInt(count)});
}
return result
}
// 数字列表
export function parseNumberList(str: string) {
let res = new Array<number>();
if(!str) return res;
let arr = decodeArrayStr(str);
for(let g of arr) {
if(g === "") continue;
res.push(parseInt(g));
}
return res;
}