全局:加载json方法全部改为最新

This commit is contained in:
luying
2021-05-06 20:25:05 +08:00
parent 2ef00e8179
commit 0315269387
36 changed files with 401 additions and 920 deletions
+43 -150
View File
@@ -65,103 +65,6 @@ export function generateNum(len: number) {
return code;
}
export function decodeStr(type, str, splitForm = '|') {
if (str == '&' || !str) {
return []
} else {
return str.split(splitForm).map(cur => {
return decodeStrSingle(type, cur);
});
};
}
export function decodeStrSingle(type, str) { //单层,返回对象而不是数组
let last = str.substr(str.length - 1, 1);
if (last == '&') str = str.substr(0, str.length - 1);
if (str == '&' || !str) {
return {}
} else {
let arr = str.split('&');
let result: any;
switch (type) {
case 'fixReward': {
let [id, count] = arr;
if (isNaN(id) || isNaN(count)) throw new Error('data table format wrong');
result = { id: parseInt(id), count: parseInt(count) };
break;
}
case 'conditionReward': {
let [id, count, condition] = arr;
if (isNaN(id) || isNaN(count)) throw new Error('data table format wrong');
result = { id: parseInt(id), count: parseInt(count), condition: parseInt(condition) };
break;
}
case 'randomReward': {
let [id, count, frequency] = arr;
if (isNaN(id) || isNaN(count)) throw new Error('data table format wrong');
result = { id: parseInt(id), count: parseInt(count), frequency: parseInt(frequency) };
break;
}
case 'suitLevel': {
let [min, max] = arr;
if (isNaN(min) || isNaN(max)) throw new Error('data table format wrong');
result = { min: parseInt(min), max: parseInt(max) };
break;
}
case 'suitLevelSingle': {
let [min, max] = arr;
if (max) {
if (isNaN(min) || isNaN(max)) throw new Error('data table format wrong');
result = { min: parseInt(min), max: parseInt(max) };
break;
} else {
if (isNaN(min)) throw new Error('data table format wrong');
result = { min: parseInt(min) };
break;
}
}
case 'attribute': {
let [id, value] = arr;
if (isNaN(id) || isNaN(value)) throw new Error('data table format wrong');
result = { id: parseInt(id), value: parseInt(value) };
break;
}
case 'point': {
let [x] = arr;
if (isNaN(x)) throw new Error('data table format wrong');
result = x;
break;
}
case 'decimalReward': {
let [id, count] = arr;
if (isNaN(id) || isNaN(count)) throw new Error('data table format wrong');
result = { id: parseInt(id), count: parseFloat(count) };
break;
}
case 'towerTaskCondition': {
let [type, param, cnt] = arr;
if (isNaN(type) || isNaN(param) || isNaN(cnt)) throw new Error('data table format wrong');
result = { type: parseInt(type), param: parseInt(param), cnt: parseInt(cnt) };
break;
}
case 'possibility': {
let [id, weight] = arr;
if (isNaN(id) || isNaN(weight)) throw new Error('data table format wrong');
result = { id: parseInt(id), weight: parseInt(weight) };
break;
}
case 'cost': {
let [id, count] = arr;
if (isNaN(id) || isNaN(count)) throw new Error('data table format wrong');
result = { id: parseInt(id), count: parseInt(count) };
break;
}
}
return result;
};
}
/**
* 将 | 分隔的字符串解析为数组,如:a|b|c 解析为[a, b, c]
* @param str 要解析的字符串
@@ -211,32 +114,6 @@ export async function calculateSumCE(roleId: string, type: number, param: { num?
return sum;
}
export function getRandomByLen(arr: Array<any>): any {
let len = arr.length;
return arr[Math.floor(Math.random() * len)]
}
export function getRandomIndexByLen(len: number) {
return Math.floor(Math.random() * len);
}
export function getRandomWithWeight<T extends { weight: number }>(randomList: T[]): { dic: T, index: number } {
let len = randomList.reduce((pre, cur) => {
return pre + cur.weight || 1;
}, 0);
let index = Math.floor(Math.random() * len);
let result = { dic: null, index: -1 };
for (let i = 0; i < randomList.length; i++) {
let { weight = 0 } = randomList[i];
if (index < weight) {
result.dic = randomList[i];
result.index = i;
break;
}
index -= weight;
}
return result
}
/**
* 传入两个时间,返回按照时间差计算,第二个时间比第一个晚几天
* @param preTime 之前的时间
@@ -329,6 +206,45 @@ export function getRandEelm<T>(source: Array<T> = [], cnt = 1): Array<T> {
return source.filter((_item, idx) => idxs.has(idx));
}
/**
* 从一个数组中随机返回1元素
* @param source
*/
export function getRandSingleEelm<T>(source: Array<T>): T {
let len = source.length;
return source[Math.floor(Math.random() * len)]
}
/**
* 随机数组中一个index
* @param len
*/
export function getRandSingleIndex(len: number) {
return Math.floor(Math.random() * len);
}
/**
* 从一个数组根据权重随机一个元素
* @param randomList
*/
export function getRandEelmWithWeight<T extends { weight: number }>(randomList: T[]): { dic: T, index: number } {
let len = randomList.reduce((pre, cur) => {
return pre + cur.weight || 1;
}, 0);
let index = Math.floor(Math.random() * len);
let result = { dic: null, index: -1 };
for (let i = 0; i < randomList.length; i++) {
let { weight = 0 } = randomList[i];
if (index < weight) {
result.dic = randomList[i];
result.index = i;
break;
}
index -= weight;
}
return result
}
/**
* 不改变原数组长度,将内部元素打乱
* @param source
@@ -360,7 +276,6 @@ export function getRandValueByMinMax(min: number, max: number, decimal = 2): num
}
export function resResult(status: { code: number, simStr: string }, data = null, customMsg = '') {
const { code, simStr } = status;
if (code !== STATUS.SUCCESS.code) {
@@ -452,13 +367,10 @@ export function calculateNum(ratio: { A: number, B: number }, params: { num: num
}
}
export function ratioReward(rewardStr: string, ratio: number): string {
let rewards = decodeIdCntArrayStr(rewardStr, ratio);
let res = '';
for (let [k, v] of rewards) {
res += `${k}&${v}|`;
}
return res.substring(0, res.length - 1);
export function ratioReward(reward: {id: number, count: number}[], ratio: number): {id: number, count: number}[] {
return reward.map(cur => {
return {id: cur.id, count: cur.count * ratio}
});
}
export function getItems(str: string) {
@@ -606,25 +518,6 @@ export function reduceCe(ce: number = 0) {
return Math.floor(ce / HERO_CE_RATIO / HERO_CE_RATIO)
}
export function getRandNum(min, max) {
let randNum = min + Math.floor(Math.random() * (max - min + 1));
return randNum;
}
export function getRandomArr(allarr, ranNum) {
let arr = deepCopy(allarr);
var result = [];
if (arr.length <= ranNum) {
result = result.concat(arr);
return result;
}
for (var i = 0; i < ranNum; i++) {
var ran = Math.floor(Math.random() * arr.length);
result.push(arr.splice(ran, 1)[0]);
};
return result;
}
// 获取全部属性
export function getAllAttrStage() {