/**
* Store data about members for leaderboards
*/
import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats.js'
import { CleanFullProfile } from './cleaners/skyblock/profile.js'
import { SLAYER_TIERS } from './cleaners/skyblock/slayers.js'
import { Binary, Collection, Db, MongoClient, WithId } from 'mongodb'
import { CleanMember } from './cleaners/skyblock/member.js'
import * as cached from './hypixelCached.js'
import * as constants from './constants.js'
import { isUuid, letterFromColorCode, minecraftColorCodes, shuffle, sleep } from './util.js'
import * as discord from './discord.js'
import NodeCache from 'node-cache'
import { v4 as uuid4 } from 'uuid'
import { debug } from './index.js'
import Queue from 'queue-promise'
import { RANK_COLORS } from './cleaners/rank.js'
import { cleanItemId } from './cleaners/skyblock/itemId.js'
import { periodicallyFetchRecentlyEndedAuctions } from './hypixel.js'
// don't update the user for 3 minutes
const recentlyUpdated = new NodeCache({
stdTTL: 60 * 3,
checkperiod: 60,
useClones: false,
})
// don't add stuff to the queue within the same 5 minutes
const recentlyQueued = new NodeCache({
stdTTL: 60 * 5,
checkperiod: 60,
useClones: false,
})
interface DatabaseMemberLeaderboardItem {
uuid: string
profile: string
/** The color code of this player's rank */
color: string
username: string
stats: Record<string, number>
lastUpdated: Date
}
interface DatabaseProfileLeaderboardItem {
uuid: string
/** The color codes of the players ranks */
colors: string[]
/** An array of uuids for each player in the profile */
players: string[]
usernames: string[]
stats: Record<string, number>
lastUpdated: Date
}
interface MemberRawLeaderboardItem {
uuid: string
profile: string
color: string
username: string
value: number
}
interface ProfileRawLeaderboardItem {
uuid: string
/** An array of uuids for each player in the profile */
players: string[]
colors: string[]
usernames: string[]
value: number
}
interface MemberLeaderboardItem {
player: LeaderboardBasicPlayer
profileUuid: string
value: number
}
interface ProfileLeaderboardItem {
players: LeaderboardBasicPlayer[]
profileUuid: string
value: number
}
export const cachedRawLeaderboards: Map<string, (MemberRawLeaderboardItem | ProfileRawLeaderboardItem)[]> = new Map()
const LEADERBOARD_MAX = 100
const reversedLeaderboards = [
'first_join', 'last_save',
'_best_time', '_best_time_2',
'fastest_coop_join', 'fastest_target_practice'
]
let client: MongoClient
let database: Db
interface SessionSchema {
_id?: string
refresh_token: string
discord_user