import {ApiCall} from "tsrpc";
import {JJCFun} from '../../public/jjc';
import {ReqOpen, ResOpen} from "../../shared/protocols/jjc/PtlOpen";
import {PublicShared} from '../../shared/public/public';
import {UserFun} from "../../public/user";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {
    const dbData = await JJCFun.getMyData(call.uid);
    const {_id, uid, ...data} = dbData;

    let _ipDate = {}
    if (dbData._id == null || await JJCFun.getRankSortByUid(call.uid) == -1) {
        await JJCFun.addRankInfo({player: call.conn.gud});
        if (dbData._id == null) _ipDate = {...data}
    }

    if (PublicShared.getToDayZeroTime() > data.resetTime) {
        Object.assign(data, {resetTime: G.time, receivedArr: [], buyFightNum: 0, useFightNum: 0});
        G.mongodb.collection('jjc').updateOne(
            {uid: call.uid},
            {$set: {resetTime: G.time, receivedArr: [], buyFightNum: 0, useFightNum: 0}},
        );
    }
    data.recordWin = data.recordWin || 0;
    let enemy = await JJCFun.randomEnemy(call.uid)

    call.succ({
        ...data,
        ...enemy
    });

    UserFun.activeHeadFrame(call.uid, 'jjc_rank', data["rank"] + 1);
    JJCFun.checkUpdatePlayer()

}