import { ApiCall } from "tsrpc";
import { EmailFun } from "../../public/email";
import { WangZheRongYaofun } from "../../public/wzry";
import { ReqOpen, ResOpen } from "../../shared/protocols/wzry/PtlOpen";
import { PublicShared } from "../../shared/public/public";
import { Rank } from "../../public/rank/rank";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    let status = await WangZheRongYaofun.getWangZheStatus()
    let _mydata = await WangZheRongYaofun.getMyData(call)
    let _bmnum = await WangZheRongYaofun.getBaoMingNum()
    let zkey = await PublicShared.getToWeek()

    // 赛期结束无法清除此数据，这里做个自动刷新
    if (status.status == 3 && _mydata.isdldrefre == 0 && _mydata.isbm) {
        let _res = await G.clientCross.callApi('wzry/DldRefre', { uid: _mydata.uid })
        if (_res.isSucc) {
            let _enemy = _res.res.info
            G.mongodb.cPlayerInfo("wzry").updateOne({ uid: call.uid, type: 'wzry' }, { $set: { info: _enemy, isdldrefre: 1, zkey: zkey } })
            _mydata.info = _enemy
        }
    }

    let _wzFight = await WangZheRongYaofun.getMyFight(call) || {}
    if (Object.keys(_wzFight).length > 1) {
        _mydata.isjjzs = 1
    }

    let mygroup = {}
    // @ts-ignore
    let [_tmpNum, deep, rank, jifen] = [_wzFight?.groupinfo?.order || 0, _wzFight.deep || 0, _wzFight.rank || -1, _wzFight.jifen || 0]
    mygroup = {
        "256": Math.ceil(_tmpNum / 4),
        "64": deep >= 3 ? Math.ceil(_tmpNum / 32) : 0,
        "8": deep >= 6 ? 1 : 0
    }
    let _res = await G.clientCross.callApi('wzry/Rank', { gud: call.conn.gud })

    if (!_res.res){
        _res.res = { rankList: [], myRank: { rank: -1, player: {}, valArr: [ 0 ] } }
    }

    _mydata.rank = _res.res.myRank.rank
    _mydata.jifen = _res.res.myRank.valArr[0]

    call.succ({
        status: status,
        mydata: _mydata,
        zkey: zkey,
        bmnum: _bmnum,
        time: G.time,
        mygroup: mygroup
    })
}