import {ApiCall} from "tsrpc";
import {FightFun} from "../../public/fight";
import {PataFun} from '../../public/pata';
import {PlayerFun} from '../../public/player';
import {Rank} from '../../public/rank/rank';
import {fightResult} from "../../shared/fightControl/fightType";
import {ReqFight, ResFight} from "../../shared/protocols/pata/PtlFight";
import {md_redPoint_check} from '../gongyu/mingdao/ApiOpen';
import {HongDianChange} from "../hongdian/fun";
import {RankKfjs} from "../../public/rank/rank_kfjs";

export default async function (call: ApiCall<ReqFight, ResFight>) {
    let changeInfo = await PataFun.getInfo(call.uid);
    let maxNum = G.gc.common.patacom.patamax;
    let con = G.gc.pata;
    let prize = [];

    // 挑战次数不足
    if (maxNum > 0 && changeInfo.useNum > maxNum) {
        return call.error(globalThis.lng.pata_fight_1);
    }

    // 参数错误
    if (!con[changeInfo.lv]) {
        return call.error(globalThis.lng.pata_fight_2);
    }
    let result: fightResult = await FightFun.fightNpc(call, con[changeInfo.lv]["npcid"], 'pata');

    if (result.winSide == 0) {
        changeInfo.useNum += 1;
        changeInfo.lv += 1;
        // 改变的数据
        let setData = {
            useNum: changeInfo.useNum,
            lv: changeInfo.lv
        };
        // 设置数据
        await PataFun.changeInfo(call.uid, changeInfo.sid, {$set: setData});
        prize = await PlayerFun.sendPrize(call, con[changeInfo.lv - 1].prize);

        Rank.list['zccg'].addNew({
            player: call.conn.gud,
            valArr: [changeInfo.lv]
        });

        let rankKfjs = RankKfjs(5)
        rankKfjs.setDataAndCheck({
            player: call.conn.gud,
            valArr: [changeInfo.lv-1]
        });

        md_redPoint_check(call.conn, 'zccg');
    }

    let data = {
        mydata: changeInfo,
        prize: prize,
        result: result
    };

    HongDianChange.sendChangeKey(call.uid, ['taskhd', 'huodonghd', 'jijinhd', 'patahd']);

    call.succ(data);
}