import {formatNpcData} from '../../shared/fightControl/fightFun';
import {joinFightData} from '../../shared/fightControl/fightType';
import {player} from '../../shared/protocols/user/type';

function getNpc(lv: number, fort: string) {
    let rang = G.gc.slzd.forts[fort].npc.find(v => lv >= v.lvs[0] && lv <= v.lvs[1]);
    return formatNpcData(rang.npcs.random());
}

export async function getEnemy(player: player, fort: string) {
    let enemy: joinFightData[] = [];

    let roles = await G.mongodb.collection('slzdUser').aggregate([
        {$match: {uid: {$ne: player.uid}, ghid: {$ne: player.ghId}, ["val."+fort] : {$exists:true}  }},
        {$project: {
            _id: 0,
            info: 1
        }},
        {$sample: {size: 5}}
    ]).toArray();

    for (let index = 0; index < 6; index++) {
        if (!roles[index]) {
            enemy.push(getNpc(player.lv, fort));
        } else {
            enemy.push(roles[index].info);
        }
    }

    return enemy;
}