import { filter } from "mathjs";
import { ApiCall } from "tsrpc";
import { FightFun } from '../../public/fight';
import { PlayerFun } from '../../public/player';
import { Rank } from '../../public/rank/rank';
import { fightResult } from '../../shared/fightControl/fightType';
import { ReqFight, ResFight } from "../../shared/protocols/tanxian/PtlFight";
import { PublicShared } from '../../shared/public/public';
import { HongDianChange } from "../hongdian/fun";
import {RankKfjs} from "../../public/rank/rank_kfjs";

export default async function (call: ApiCall<ReqFight, ResFight>) {


    const conf = G.gc.tanxian[call.conn.gud.mapId + 1];

    if (!conf) return call.error(globalThis.lng.tanxian_2);
    if (call.conn.gud.lv < conf.needLv) return call.error(globalThis.lng.tanxian_3);

    // 增加 推关饰品
    let _addDrop = Object.values(G.gc.tanxian_shiwu).filter(ele => {
        return ele.level[0] <= call.conn.gud.lv && call.conn.gud.lv <= ele.level[1]
    })
    let _passPrize = conf.passPrize.map(_n => { return _n })
    _passPrize.push(_addDrop[0].diaoluo)

    let prize = _passPrize.map(id => PublicShared.randomDropGroup(id)).reduce((a, b) => a.concat(b));
    let result: fightResult = await FightFun.fightNpc(call, conf.npcId, 'tanxian');

    if (result.winSide == 0) {
        await PlayerFun.addAttr(call, { mapId: call.conn.gud.mapId + 1 });
        await PlayerFun.sendPrize(call, prize);

        Rank.list['tanxian'].addNew({
            player: call.conn.gud,
            valArr: [call.conn.gud.mapId]
        });

        // (await call.conn.gonghui)?.addExp(20, call.uid);

        let rankKfjs = RankKfjs(4)
        rankKfjs.setDataAndCheck({
            player: call.conn.gud,
            valArr: [call.conn.gud.mapId]
        });
    }
    HongDianChange.sendChangeKey(call.uid, ['taskhd', 'huodonghd', 'jijinhd', 'huobanzhaomuhd'])

    call.succ({
        prize: result.winSide == 0 ? prize : [],
        result: result
    });


}