Files
ZYZ/shared/pubUtils/dictionary/DicGacha.ts
T
2021-04-21 21:10:26 +08:00

54 lines
1.5 KiB
TypeScript

import { readJsonFile, parseNumberList, parseGoodStr, decodeArrayListStr } from '../util'
import { FILENAME } from '../../consts'
import { RewardInter } from '../interface';
export interface DicGacha {
// 抽卡id
readonly id: number;
// 抽卡次数 1次,10次
readonly count: number[];
// 免费次数
readonly free: { day: number, count: number };
// 消耗的招募券
readonly cost: RewardInter[];
// 概率
readonly percent: { id: number, percent: number }[];
}
const str = readJsonFile(FILENAME.DIC_GACHA);
let arr = JSON.parse(str);
export const dicGacha = new Map<number, DicGacha>();
arr.forEach(o => {
o.count = parseNumberList(o.count);
o.free = parseFree(o.free);
o.cost = parseGoodStr(o.cost);
o.percent = parsePercent(o.percent);
dicGacha.set(o.id, o);
});
function parseFree(str: string) {
let arr = parseNumberList(str);
if(arr.length > 0 && arr.length != 2) {
throw new Error('dic_gacha free format err');
}
if(arr.length == 0) {
return { day: 0, count: 0 }
} else {
return { day: arr[0], count: arr[1] }
}
}
function parsePercent(str: string) {
let result = new Array<{ id: number, percent: number }>();
if (!str) return result;
let decodeArr = decodeArrayListStr(str);
for (let [id, percent] of decodeArr) {
if (isNaN(parseInt(id)) || isNaN(parseInt(percent))) {
throw new Error('data table format wrong');
}
result.push({ id: parseInt(id), percent: parseInt(percent) });
}
return result
}