import { ApiCall } from "tsrpc";
import { WangZheRongYaofun } from "../../public/wzry";
import { ReqDldRefre, ResDldRefre } from "../../shared/protocols/wzry/PtlDldRefre";

export default async function (call: ApiCall<ReqDldRefre, ResDldRefre>) {
    let status = await WangZheRongYaofun.getWangZheStatus();
    if (status.status != 3) {
        // 未到大乱斗时间
        return call.error('', { code: -1/* , message: globalThis.lng.wzry_5  */ });
    }

    let _mydata = await WangZheRongYaofun.getMyData(call);
    if (!_mydata.isbm) {
        // 未报名
        return call.error('', { code: -2/* , message: globalThis.lng.wzry_7 */ });
    }

    if (_mydata.dldinfo.dldrefrenum >= G.gc.wangzherongyao.wangzhe.dldrefrenum) {
        // 次数不足
        return call.error('', { code: -3/* , message: globalThis.lng.wzry_6  */ });
    }

    let _enemy = (await G.clientCross.callApi('wzry/DldRefre', { uid: _mydata.uid })).res.info;
    G.mongodb.cPlayerInfo("wzry").updateOne({ uid: call.uid, type: 'wzry' }, { $set: { info: _enemy }, $inc: { 'dldinfo.dldrefrenum': 1 } });
    _mydata.info = _enemy;

    let _wzFight = await WangZheRongYaofun.getMyFight(call) || {};
    // @ts-ignore
    let [rank, jifen] = [_wzFight?.rank || -1, _wzFight?.jifen || 0];

    _mydata.rank = rank;
    _mydata.jifen = jifen;

    _mydata.dldinfo.dldrefrenum += 1;
    call.succ({ mydata: _mydata });
}