import { ApiCall } from "tsrpc";
import { ReqFindEnemy, ResFindEnemy } from "../../cross/protocols/clsl/PtlFindEnemy";
import { formatNpcData } from '../../shared/fightControl/fightFun';

export default async function (call: ApiCall<ReqFindEnemy, ResFindEnemy>) {
    let my = await G.mongodb.collection('clslCrossUser').findOne({ uid: call.req.uid });
    let starConf = getStarConf(my.allStar);
    let others = await G.mongodb.collection('clslCrossUser').find({ uid: { $nin: [call.req.uid] } }).toArray();
    let enemy = others.random();

    call.succ({
        allStar: enemy?.allStar || my.allStar,
        info: enemy?.info || formatNpcData(starConf.npc)
    });
}

const endConf = Object.values(G.gc.clsl_dan).slice(-1)[0];

function getStarConf(star = 0) {
    return G.gc.clsl_dan[star] || endConf;
}