import { ApiCall } from "tsrpc";
import { ReqRank, ResRank } from "../../cross/protocols/clsl/PtlRank";
import { Rank } from '../../public/rank/rank';
import { PublicShared } from "../../shared/public/public";
import { RankClslCross } from "../../public/rank/rank_clsl";

export default async function (call: ApiCall<ReqRank, ResRank>) {
    let group_time = G.gc.clsl_com.divideTime;
    let week_zero_time = PublicShared.getToWeekMondayZeroTime();

    if (G.time < week_zero_time + group_time) {
        call.succ({ rankList: [], myRank: { rank: -1, player: {}, valArr: [] } })
        return
    }

    let a = await G.mongodb.collection('clslCrossUser').findOne({
        uid: call.req.uid
    });
    
    if (!a || !a.group){
        call.succ({ rankList: [], myRank: { rank: -1, player: {}, valArr: [] } })
        return
    }

    let rank: Rank;
    if (Rank.list[`clslCross_${a.group}`]) {
        rank = Rank.list[`clslCross_${a.group}`]
    }
    else {
        rank = new RankClslCross(a.group)
    }

    let page = call.req.page || 0
    let offset = call.req.offset || -1 // 预防未查询到的调用，按原逻辑查询全部，避免引起未知问题
    let { min, max } = Rank.pageToMin(page, offset)
    call.succ(await rank.getRankList(call.req.uid, { min, max }));
}