import { rankType } from '../../shared/protocols/rank/PtlOpen';
import { rankInfo } from '../../shared/protocols/type';
import { PublicShared } from '../../shared/public/public';
import { Rank } from './rank';


export class RankClslCross extends Rank {
    group: string;
    minStar = Object.values(G.gc.clsl_dan)[Object.keys(G.gc.clsl_dan).length - 1].allStar;

    constructor(group: string = 'group0') {
        super({ group });
    }

    getType(): rankType { return `clslCross_${this.group}` as rankType }

    getValArr(info: rankInfo): number | string {
        // 星级
        let star = info?.valArr[0] || 0;

        // 战力
        let zhanli1 = info?.valArr[1] || 0;

        // 将星级作为整数部分，战力作为小数部分长度为10位 进行拼接
        let val = `${star}.${zhanli1.toString().padStart(10, '0')}`;

        return Number(val)
    }

    async getRankData(uid: string) {
        let data: rankInfo;
        return (await this.db.findOne({ "idKey": uid, "type": this.getType() }))?.data
    }

    async addNew(info: rankInfo) {
        // 积分大于配置的最小参数，更新数据
        if (info.valArr[0] >= this.minStar) {
            this.setRankData(info.player[this.findKey], info)
            this.db.updateOne({ type: this.type, idKey: info.player[this.findKey] }, { $set: { data: info } }, { upsert: true });
        }
    }
}

/**
 * 从林猎手 排行榜检测初始化
*/
export async function RankClslCrossInit() {
    let week = PublicShared.getToWeek();
    let group_time = G.gc.clsl_com.divideTime;
    let week_zero_time = PublicShared.getToWeekMondayZeroTime();

    if (G.time > week_zero_time + group_time) {
        let groups = await G.mongodb.collection('clslCrossGroup').findOne({ week: week });
        Object.keys(groups.groups).forEach(group => {
            new RankClslCross(group);
        })
    }
}