import { ApiCall } from "tsrpc";
import { ReqQianDao, ResQianDao } from "../../../shared/protocols/event/christmas/PtlQianDao";
import {Christmasfun} from "./fun";
import {PublicShared} from "../../../shared/public/public";
import {PlayerFun} from "../../../public/player";
import {HongDianChange} from "../../hongdian/fun";

export default async function (call: ApiCall<ReqQianDao, ResQianDao>) {
    let initCon = await Christmasfun.getCon(call)
    let _con = initCon[call.req.hdid].data.qiandao;
    let _stime = initCon[call.req.hdid].stime
    let _diff = PublicShared.getDiff(_stime)

    let _mydata = await Christmasfun.getMyData(call, call.req.hdid)
    // 循环判断是否有可以领取的
    let _prize = []
    for(let i = 0; i < _diff; i++) {
        if (_mydata.qiandao.includes(i)) continue
        _mydata.qiandao.push(i)
        _prize = _prize.concat(_con[i])
    }
    if (_prize.length <= 0){
        // 没有奖励可以领取
        return call.error('', { code: -1, message: globalThis.lng.yangchengmubiao_2 })
    }
    let _setData = {}
    let _addVal = 0
    // 懒得喊后台和前端修改了，直接取prize里面的对应的经验字段去添加zhanling经验
     for (const p of _prize) {
         // 战令经验
         if (p.t == "shengdanExp") {
             _addVal += p.n
         }
     }
    _mydata["val"] += _addVal
    _setData["val"] = _mydata.val
    _setData["qiandao"] = _mydata.qiandao
    await Christmasfun.setMyData(call.uid, call.req.hdid, { $set: _setData })
    await PlayerFun.sendPrize(call, _prize);
    let changedata = { mydata: _mydata, prize: _prize}
    // 推送红点
    HongDianChange.sendChangeKey(call.uid, ['huodonghd']);
    call.succ(changedata);
}