import { ApiCall } from "tsrpc";
import { rankOpenfun } from "../../api_s2c/rank/ApiOpen";
import { Reqranking, Resranking } from "../../monopoly/protocols/games/Ptlranking";
import { NotifiCationFun } from "../../public/notification";
import { PayFun } from "../../public/pay";

export default async function (call: ApiCall<Reqranking, Resranking>) {
    // 设置开区时间
    let _num = call.req.skip ? 999 : call.req.num
    let _rankType = call.req.rankid

    // 无此玩家  随便取一个玩家
    let gud = await G.mongodb.collection('user').findOne({});
    if (!gud) {
        return call.error('', { code: 1002, message: globalThis.lng.wuciwanjia });
    }
    let rescall = PayFun.getCall(gud);
    // 排行榜数据
    let _rankData = await rankOpenfun(rescall, [_rankType])
    // 排行
    let _renkList = _rankData[_rankType]["rankList"]
    let _resData = []

    for (let index = 0; index < _renkList.length; index++) {
        const element = _renkList[index];
        let _tmp: Resranking["data"][0] = {
            ranking: index + 1,
            id: element.player.uid,
            name: element.player.name,
            rankkingValue: (element.valArr ? element.valArr[0] : await NotifiCationFun.getFmtRankVal(_rankType, element)).toString()
        }
        _resData.push(_tmp)
    }

    call.succ({
        code: 0,
        message: globalThis.lng.huoqupaihang,
        data: _resData
    });
}