import { exp, index } from "mathjs";
import { rankInfo } from "../../../shared/protocols/type";
import { CollectionPlayerInfo } from "../../../module/collection_palyerInfo";
import { get } from "http";
import { fightResult } from "../../../shared/fightControl/fightType";
import { FightFun } from "../../../public/fight";

/**更新跨服里黑帮争霸的玩家数据 */
export async function updateHbzbCrossUser(info: rankInfo, isNew = false) {
    let callRes = await G.clientCross.callApi('hbzb/UpdateHbzbCrossUser', { uid: info.player.uid, user: info, isNew: isNew });
    return callRes;
}

/**获取指定uid的数据 */
export async function getHbzbData(uid:string) {
    let dbData = await G.mongodb.cPlayerInfo('hbzb').findOne({ uid: uid, type: 'hbzb' });
    return dbData;
}


/**获取前200名玩家的uid */
export async function getToper200RankUid(){
    let uids = []
    let arr = await G.mongodb.collection("playerInfo").find(
        { "type": "hbzb" }
     ).sort(
        { "data.jifen": -1 }
     ).project({
        uid:1
     }).limit(200).toArray();

     arr.map(i=>{
        uids.push( i.uid );
     })
     return uids;
}

/**获取我的排名 */
export async function getMyRank(uid:string) : Promise<number>{
    let top200 = await getToper200RankUid();
    let myRank = top200.indexOf(uid);
    if(myRank > -1){
        myRank += 1;
    }
    return myRank;
}

/**获取前50名玩家信息 */
export async function getToper50RankInfo(uid?:string){
    let rank = {
        "myRank":{},
        "rankList" : []
    }
    let arr = await G.mongodb.collection("playerInfo").find(
        { "type": "hbzb", "data.jifen":{$gt:0} },
    ).sort(
        { "data.jifen": -1 }
    ).limit(50).toArray();
    
    //需要到跨服表去拉数据
    let uids = [];
    if(uid){
        uids.push(uid);
    }

    arr.map(i=>{
        uids.push( i.uid );
    });


    let callRes = await G.clientCross.callApi('hbzb/GetUser', { uids: uids });
    if(!callRes.isSucc){
        //todo 健壮性处理
    }
    let myRank = -1;
    arr.map((i,index)=>{
        rank.rankList.push( {
            player: callRes.res[i.uid]?.data?.player || {},
            roles: callRes.res[i.uid]?.data?.roles || {},
            utime: i.data.refreshTime,
            rank : (index+1),
            //积分在本地取，而不是跨服里的
            valArr: [i.data.jifen]
        });

        if(uid){
            if(i.uid == uid){
                myRank = rank.rankList.length;
            }
        }
    })

    if(uid){
        let myData = await getHbzbData(uid) as any;
        rank.myRank = {
            player: callRes.res[uid]?.data?.player || {},
            rank: myRank,
            valArr: [myData.data.jifen]
        }
    }

    return rank;
}

export function getWinScore(myMaxPower:number, enemyMaxPower:number){
    //let myMaxPower = call.conn.gud?.maxpower || call.conn.gud?.power;
    //let enemyMaxPower = enemy.data.player.maxpower || enemy.data.player.power;
    let score = Math.floor((enemyMaxPower-myMaxPower)/50000+29);
    //scroe需要在30到35之间
    if (score < 30) score = 30;
    if (score > 35) score = 35;
    return score;
}