// 藏宝图掉落率
import { decodeArrayListStr, readFileAndParse } from '../util'
import { FILENAME } from '../../consts'

export interface DicBlueprtPossibility {

    // 君主等级下限
    readonly min: number;
    // 君主等级上限
    readonly max: number;
    // 掉落概率
    readonly possibility: Array<{id: number, weight: number}>;

}

export const dicBlueprtPossibility = new Array<DicBlueprtPossibility>();
export function loadBlueprtPossibility() {
    dicBlueprtPossibility.splice(0, dicBlueprtPossibility.length);
    let arr = readFileAndParse(FILENAME.DIC_BLUEPRT_POSSIBILITY);
    arr.forEach(o => {
        o.possibility = parsePossibility(o.possibility);
        dicBlueprtPossibility.push(o);
    });
}


function parsePossibility(str: string) {
    let result = new Array<{id: number, weight: number}>();
    if(!str) return result;
    let decodeArr = decodeArrayListStr(str);
    for(let [id, weight] of decodeArr) {
        if(isNaN(parseInt(id)) || isNaN(parseInt(weight))) {
            throw new Error('data table format wrong');
        }
        result.push({id: parseInt(id), weight: parseInt(weight)});
    } 
    return result
}