// 兵种表
import { readFileAndParse, parseNumberList } from '../util'
import { FILENAME, } from '../../consts'
const _ = require('lodash');

export interface DicSystemOpenTime {
    // 唯一id
    readonly id: number;
    // 开启关卡id
    readonly warId: number;
    // 开启玩家等级
    readonly lv: number;
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicSystemOpenTimeKeys: KeysEnum<DicSystemOpenTime> = {id: true, warId: true, lv: true};

export const dicSystemOpenTime = new Map<number, DicSystemOpenTime>();

export function loadSystemOpenTime() {
    dicSystemOpenTime.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_SYSTEM_OPEN_TIME);

    arr.forEach(o => {
        let { warId, lv } = parseLimit(o.limit);
        o.warId = warId;
        o.lv = lv;
        dicSystemOpenTime.set(o.id, _.pick(o, Object.keys(DicSystemOpenTimeKeys)));
    });
    arr = undefined;
}

function parseLimit(str: string) {
    let arr = parseNumberList(str);
    if(arr[0] == 0) {
        return { warId: arr[1], lv: 0 }
    }
    return { warId: 0, lv: arr[0] }
}