import { ApiCall } from "tsrpc";
import { ReqUpdateFight, ResUpdateFight } from "../../cross/protocols/wzry/PtlUpdateFight";
import { Rank } from "../../public/rank/rank";

export default async function (call: ApiCall<ReqUpdateFight, ResUpdateFight>) {
    if (call.req.setdata) {
        await G.mongodb.collection('wzry_fight').updateOne({ uid: call.req.uid }, { $set: call.req.setdata })
        if(call.req.setdata.jifen) {
            let data = {
                valArr: [call.req.setdata.jifen],
                ...call.req.setdata.data
            }
            Rank.list.wzryCross.addNew(data)
        }
    }

    if (call.req.isjingcai) {
        await G.mongodb.collection('crosskv').updateOne({ type: "wzryallnum" }, { $inc: { num: call.req.isjingcai } }, { upsert: true })
    }

    // 设置删除跨服邮件的标识符，标识已经被拉倒本服生效
    if (call.req?.isdelemail?.isdel) {
        let _list = []
        for (let index = 0; index < call.req.isdelemail.email.length; index++) {
            const element = call.req.isdelemail.email[index];
            _list.push(G.mongodb.conversionId(element))
        }
        await G.mongodb.collection('email').updateMany({ _id: { $in: _list } }, {$set: { isdel: 1 }})
    }
    call.succ({})
}