diff options
author | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 13:56:19 -0600 |
---|---|---|
committer | mat <27899617+mat-1@users.noreply.github.com> | 2021-02-13 13:56:19 -0600 |
commit | a23103ec24128f2e24b93ad101ade6dfdd4758c3 (patch) | |
tree | c4d543a2003062a7c213d37f5d06a826c2a88798 /cleaners/skyblock/stats.ts | |
parent | d7b9d1fc5cfc8c648604eb1d178091873ea698a6 (diff) | |
download | skyblock-api-a23103ec24128f2e24b93ad101ade6dfdd4758c3.tar.gz skyblock-api-a23103ec24128f2e24b93ad101ade6dfdd4758c3.tar.bz2 skyblock-api-a23103ec24128f2e24b93ad101ade6dfdd4758c3.zip |
Basic profile stats
Diffstat (limited to 'cleaners/skyblock/stats.ts')
-rw-r--r-- | cleaners/skyblock/stats.ts | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/cleaners/skyblock/stats.ts b/cleaners/skyblock/stats.ts new file mode 100644 index 0000000..07d9133 --- /dev/null +++ b/cleaners/skyblock/stats.ts @@ -0,0 +1,73 @@ +const statCategories: { [ key: string ]: string[] | null } = { // sorted in order of importance + 'deaths': ['deaths_', 'deaths'], + 'kills': ['kills_', 'kills'], + 'fishing': ['items_fished_', 'items_fished'], + 'auctions': ['auctions_'], + 'races': ['_best_time'], + 'misc': null // everything else goes here +} + +interface statCategory { + category: string, + name: string +} + +function categorizeStat(statNameRaw: string): statCategory { + // 'deaths_void' + for (const statCategory in statCategories) { + // 'deaths' + const statCategoryMatchers = statCategories[statCategory] + if (statCategoryMatchers == null) { + // If it's null, just go with this. Can only ever be 'misc' + return { + category: statCategory, + name: statNameRaw + } + } + for (const categoryMatch of statCategoryMatchers) { + // ['deaths_'] + let trailingEnd = categoryMatch[0] == '_' + let trailingStart = categoryMatch.substr(-1) == '_' + if (trailingStart && statNameRaw.startsWith(categoryMatch)) { + return { + category: statCategory, + name: statNameRaw.substr(categoryMatch.length) + } + } else if (trailingEnd && statNameRaw.endsWith(categoryMatch)) { + return { + category: statCategory, + name: statNameRaw.substr(0, categoryMatch.length) + } + } else if (statNameRaw == categoryMatch) { + // if it matches exactly, we don't know the name. will be defaulted to category later on + return { + category: statCategory, + name: null + } + } + } + } + // this should never happen, as it'll default to misc and return if nothing is found + return { + category: null, + name: statNameRaw + } +} + +export interface CleanProfileStats { + [ category: string ]: { + [ stat: string ]: any + total?: any + } +} + +export function cleanProfileStats(statsRaw): CleanProfileStats { + // TODO: add type for statsRaw (probably in hypixelApi.ts since its coming from there) + const stats: CleanProfileStats = {} + for (let statNameRaw in statsRaw) { + let { category: statCategory, name: statName } = categorizeStat(statNameRaw) + if (!stats[statCategory]) stats[statCategory] = {} + stats[statCategory][statName || 'total'] = statsRaw[statNameRaw] + } + return stats +} |