// 粮草先行道具
import { readFileAndParse, parseNumberList } from '../util'
import { FILENAME } from '../../consts'
const _ = require('lodash');

export interface DicRaceActivity {

    // 粮草先行道具id
    readonly id: number;
    // 类型 1-道具 2-事件
    readonly type: number;
    // 事件类型 1-好事件 2-坏事件
    readonly effectType: number;
    // 效果值
    readonly effect: number[];
    // 持续时间
    readonly continueTime: number;
    // 持续距离
    readonly continueDistance: number;
    // 生效时间
    readonly effectTime: number;
    // 可使用数量
    readonly useCount: number[];
}

type KeysEnum<T> = { [P in keyof Required<T>]: true };
const DicRaceKeys: KeysEnum<DicRaceActivity> = {id: true, type: true, effectType: true, effect: true, continueTime: true, continueDistance: true, effectTime: true, useCount: true};

export const dicRaceActivity = new Map<number, DicRaceActivity>();
export const dicRaceTypes = new Map<number, Map<number, number[]>>();
export function loadRaceActivity() {
    dicRaceActivity.clear();
    dicRaceTypes.clear();
    
    let arr = readFileAndParse(FILENAME.DIC_RACE_ACTIVITY);

    arr.forEach(o => {
        o.effectTime = o.effecttime;
        o.effect = parseNumberList(o.effect);
        o.useCount = parseNumberList(o.useCount);
        dicRaceActivity.set(o.id, _.pick(o, Object.keys(DicRaceKeys)));
        if(!dicRaceTypes.has(o.type)) {
            dicRaceTypes.set(o.type, new Map<number, number[]>());
        }
        if(!dicRaceTypes.get(o.type).has(o.effectType)) {
            dicRaceTypes.get(o.type).set(o.effectType, []);
        }
        dicRaceTypes.get(o.type).get(o.effectType).push(o.id);
    });
    arr = undefined;
}