import { ApiCall } from "tsrpc";
import { PataFun } from "../../public/pata";
import { PlayerFun } from "../../public/player";
import { ReqSaoDang, ResSaoDang } from "../../shared/protocols/pata/PtlSaoDang";
import { PlayerShared } from '../../shared/public/player';
import { PublicShared } from "../../shared/public/public";
import { HongDianChange } from "../hongdian/fun";

export default async function (call: ApiCall<ReqSaoDang, ResSaoDang>) {
    let changeInfo = await PataFun.getInfo(call.uid);
    let maxNum = G.gc.com.pata_saodang.value;

    // 扫荡次数不足
    if ((changeInfo?.saodangNum || 0) >= (maxNum + (PlayerShared.getMingDaoBuff(call.conn.gud, 'zccg_sd') || 0))) return call.error(globalThis.lng.pata_fight_1);

    // 未通关过关卡
    if (changeInfo.lv <= 1) return call.error(globalThis.lng.pata_getprize_3);

    let _guankaid = changeInfo.lv - 1;
    let _prize: atn[] = G.gc.pata[_guankaid].sdprize;
    for (let index = 0; index < call.req.num - 1; index++) {
        _prize = _prize.concat(G.gc.pata[_guankaid].sdprize);
    }
    _prize = PublicShared.mergePrize(_prize);

    changeInfo.saodangNum = call.req.num + (changeInfo?.saodangNum || 0);
    // 改变的数据
    let setData = {
        saodangNum: changeInfo.saodangNum
    };
    // 设置数据
    await PataFun.changeInfo(call.uid, changeInfo.sid, { $set: setData });
    _prize = await PlayerFun.sendPrize(call, _prize);

    let data = {
        mydata: changeInfo,
        prize: _prize
    };
    HongDianChange.sendChangeKey(call.uid, ['taskhd', 'huodonghd']);
    call.succ(data);
}