import {ApiCall} from "tsrpc";
import {ReqOpen, ResOpen} from "../../shared/protocols/kaifujingsai/PtlOpen";
import {RankKfjs} from "../../public/rank/rank_kfjs";
import kfjsFun from "../../public/kaifujingsai";
import {Rank} from "../../public/rank/rank";
import {PublicShared} from "../../shared/public/public";

export default async function (call: ApiCall<ReqOpen, ResOpen>) {

    let status = kfjsFun.getStatus(call.req.type)
    if (status.type == -1) return call.errorCode(-1)

    const {min, max} = Rank.pageToMin(call.req.page, call.req.offset)

    let rankKfjs = RankKfjs(status.type)

    let endTime = PublicShared.getToDayZeroTime(PublicShared.getToDayZeroTime(G.openTime) + status.conf.showDay[1] * 86400)

    call.succ({
        ...status,
        ...(await rankKfjs.getRankList(call.uid, {min: min, max: max})),
        endTime,
        record: (await G.mongodb.cEvent('kaifujingsai').findOne({uid: call.uid, type: 'kaifujingsai'}))?.record || {}
    });
}
