diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/cleaners/skyblock/member.ts | 14 | ||||
-rw-r--r-- | src/cleaners/skyblock/stats.ts | 49 | ||||
-rw-r--r-- | src/database.ts | 37 | ||||
-rw-r--r-- | src/hypixel.ts | 3 |
4 files changed, 59 insertions, 44 deletions
diff --git a/src/cleaners/skyblock/member.ts b/src/cleaners/skyblock/member.ts index 424bb9b..fe86e2e 100644 --- a/src/cleaners/skyblock/member.ts +++ b/src/cleaners/skyblock/member.ts @@ -1,18 +1,18 @@ -import { CleanProfileStats, cleanProfileStats } from './stats' +import { cleanCollections, Collection } from './collections' import { cleanInventories, Inventories } from './inventory' import { cleanFairySouls, FairySouls } from './fairysouls' import { cleanObjectives, Objective } from './objectives' +import { CleanFullProfileBasicMembers } from './profile' +import { cleanProfileStats, StatItem } from './stats' import { CleanMinion, cleanMinions } from './minions' +import { cleanSlayers, SlayerData } from './slayers' +import { cleanVisitedZones, Zone } from './zones' import { cleanSkills, Skill } from './skills' import * as cached from '../../hypixelCached' -import { CleanFullProfile, CleanFullProfileBasicMembers } from './profile' import { Included } from '../../hypixel' import { CleanPlayer } from '../player' +import { CleanRank } from '../rank' import { Bank } from './bank' -import { cleanVisitedZones, Zone } from './zones' -import { cleanCollections, Collection } from './collections' -import { cleanSlayers, SlayerData } from './slayers' -import { cleanRank, CleanRank } from '../rank' export interface CleanBasicMember { uuid: string @@ -24,7 +24,7 @@ export interface CleanBasicMember { export interface CleanMember extends CleanBasicMember { purse: number - stats: CleanProfileStats + stats: StatItem[] rawHypixelStats?: { [ key: string ]: number } minions: CleanMinion[] fairy_souls: FairySouls diff --git a/src/cleaners/skyblock/stats.ts b/src/cleaners/skyblock/stats.ts index 7d8da57..efb79bd 100644 --- a/src/cleaners/skyblock/stats.ts +++ b/src/cleaners/skyblock/stats.ts @@ -59,22 +59,53 @@ export function categorizeStat(statNameRaw: string): StatCategory { } } -export interface CleanProfileStats { - [ category: string ]: { - [ stat: string ]: any - total?: any - } +export const statUnits = { + time: ['_best_time', '_best_time_2'], + date: ['first_join'], + coins: ['purse'] +} + +export interface StatItem { + rawName: string + value: number + categorizedName: string + category: string + unit: string +} + +export function getStatUnit(name: string): string { + for (const [ unitName, statMatchers ] of Object.entries(statUnits)) { + for (const statMatch of statMatchers) { + let trailingEnd = statMatch[0] === '_' + let trailingStart = statMatch.substr(-1) === '_' + if ( + (trailingStart && name.startsWith(statMatch)) + || (trailingEnd && name.endsWith(statMatch)) + || (name == statMatch) + ) + return unitName + } + } } -export function cleanProfileStats(data: any): CleanProfileStats { + +export function cleanProfileStats(data: any): StatItem[] { // TODO: add type for statsRaw (probably in hypixelApi.ts since its coming from there) - const stats: CleanProfileStats = {} + const stats: StatItem[] = [] + const rawStats = data?.stats ?? {} + for (const statNameRaw in rawStats) { const statValue = rawStats[statNameRaw] let { category: statCategory, name: statName } = categorizeStat(statNameRaw) - if (!stats[statCategory]) stats[statCategory] = {} - stats[statCategory][statName || 'total'] = statValue + stats.push({ + categorizedName: statName ?? 'total', + value: statValue, + rawName: statNameRaw, + category: statCategory, + unit: getStatUnit(statNameRaw) ?? null + }) } + return stats } diff --git a/src/database.ts b/src/database.ts index 6e5a41b..cfa95ac 100644 --- a/src/database.ts +++ b/src/database.ts @@ -2,15 +2,15 @@ * Store data about members for leaderboards */ -import * as constants from './constants' -import * as cached from './hypixelCached' -import { Collection, Db, MongoClient } from 'mongodb' -import NodeCache from 'node-cache' +import { categorizeStat, getStatUnit } from './cleaners/skyblock/stats' +import { CleanFullProfile } from './cleaners/skyblock/profile' import { CleanMember } from './cleaners/skyblock/member' +import { Collection, Db, MongoClient } from 'mongodb' import { CleanPlayer } from './cleaners/player' +import * as cached from './hypixelCached' +import * as constants from './constants' import { shuffle, sleep } from './util' -import { CleanFullProfile } from './cleaners/skyblock/profile' -import { categorizeStat } from './cleaners/skyblock/stats' +import NodeCache from 'node-cache' import Queue from 'queue-promise' import { debug } from '.' @@ -40,11 +40,6 @@ const reversedLeaderboards = [ 'first_join', '_best_time', '_best_time_2' ] -const leaderboardUnits = { - time: ['_best_time', '_best_time_2'], - date: ['first_join'], - coins: ['purse'] -} let client: MongoClient let database: Db @@ -154,21 +149,6 @@ function isLeaderboardReversed(name: string): boolean { return false } -function getLeaderboardUnit(name: string): string { - for (const [ unitName, leaderboardMatchers ] of Object.entries(leaderboardUnits)) { - for (const leaderboardMatch of leaderboardMatchers) { - let trailingEnd = leaderboardMatch[0] === '_' - let trailingStart = leaderboardMatch.substr(-1) === '_' - if ( - (trailingStart && name.startsWith(leaderboardMatch)) - || (trailingEnd && name.endsWith(leaderboardMatch)) - || (name == leaderboardMatch) - ) - return unitName - } - } -} - async function fetchMemberLeaderboardRaw(name: string): Promise<DatabaseLeaderboardItem[]> { if (cachedRawLeaderboards.has(name)) return cachedRawLeaderboards.get(name) @@ -195,6 +175,7 @@ interface Leaderboard { list: LeaderboardItem[] } +/** Fetch a leaderboard that ranks members, as opposed to profiles */ export async function fetchMemberLeaderboard(name: string): Promise<Leaderboard> { const leaderboardRaw = await fetchMemberLeaderboardRaw(name) const fetchLeaderboardPlayer = async(item: DatabaseLeaderboardItem): Promise<LeaderboardItem> => { @@ -210,7 +191,7 @@ export async function fetchMemberLeaderboard(name: string): Promise<Leaderboard> const leaderboard = await Promise.all(promises) return { name: name, - unit: getLeaderboardUnit(name) ?? null, + unit: getStatUnit(name) ?? null, list: leaderboard } } @@ -234,7 +215,7 @@ export async function fetchMemberLeaderboardSpots(player: string, profile: strin name: leaderboardName, positionIndex: leaderboardPositionIndex, value: applicableAttributes[leaderboardName], - + unit: getStatUnit(leaderboardName) ?? null }) } diff --git a/src/hypixel.ts b/src/hypixel.ts index 979b57f..723feea 100644 --- a/src/hypixel.ts +++ b/src/hypixel.ts @@ -183,8 +183,11 @@ export async function fetchMemberProfileUncached(playerUuid: string, profileUuid null, { mainMemberUuid: playerUuid } ) + + // queue updating the leaderboard positions for the member, eventually for (const member of profile.members) queueUpdateDatabaseMember(member, profile) + return profile } |