aboutsummaryrefslogtreecommitdiff
path: root/src/cleaners/skyblock
diff options
context:
space:
mode:
authormat <27899617+mat-1@users.noreply.github.com>2021-02-13 14:13:42 -0600
committermat <27899617+mat-1@users.noreply.github.com>2021-02-13 14:13:42 -0600
commit52e38809212133ef673d11bfa96ba3bb43c3644c (patch)
treef54408afd41cc64b64d5e82a3ad814b1bb55d4a7 /src/cleaners/skyblock
parenta23103ec24128f2e24b93ad101ade6dfdd4758c3 (diff)
downloadskyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.gz
skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.tar.bz2
skyblock-api-52e38809212133ef673d11bfa96ba3bb43c3644c.zip
move stuff into src folder
Diffstat (limited to 'src/cleaners/skyblock')
-rw-r--r--src/cleaners/skyblock/minions.ts70
-rw-r--r--src/cleaners/skyblock/stats.ts73
2 files changed, 143 insertions, 0 deletions
diff --git a/src/cleaners/skyblock/minions.ts b/src/cleaners/skyblock/minions.ts
new file mode 100644
index 0000000..da69634
--- /dev/null
+++ b/src/cleaners/skyblock/minions.ts
@@ -0,0 +1,70 @@
+import { maxMinion } from '../../hypixel'
+
+export interface CleanMinion {
+ name: string,
+ levels: boolean[]
+}
+
+
+/**
+ * Clean the minions provided by Hypixel
+ * @param minionsRaw The minion data provided by the Hypixel API
+ */
+export function cleanMinions(minionsRaw: string[]): CleanMinion[] {
+ const minions: CleanMinion[] = []
+ for (const minionRaw of minionsRaw ?? []) {
+ // do some regex magic to get the minion name and level
+ // examples of potential minion names: CLAY_11, PIG_1, MAGMA_CUBE_4
+ const minionName = minionRaw.split(/_\d/)[0].toLowerCase()
+ const minionLevel = parseInt(minionRaw.split(/\D*_/)[1])
+ let matchingMinion = minions.find(m => m.name === minionName)
+ if (!matchingMinion) {
+ // if the minion doesnt already exist in the minions array, then create it
+ matchingMinion = {
+ name: minionName,
+ levels: new Array(maxMinion).fill(false)
+ }
+ minions.push(matchingMinion)
+ }
+ while (minionLevel > matchingMinion.levels.length)
+ // if hypixel increases the minion level, this will increase with it
+ matchingMinion.levels.push(false)
+
+ // set the minion at that level to true
+ matchingMinion.levels[minionLevel - 1] = true
+ }
+ return minions
+}
+
+/**
+ * Combine multiple arrays of minions into one, useful when getting the minions for members
+ * @param minions An array of arrays of minions
+ */
+export function combineMinionArrays(minions: CleanMinion[][]): CleanMinion[] {
+ const resultMinions: CleanMinion[] = []
+
+ for (const memberMinions of minions) {
+ for (const minion of memberMinions) {
+ // this is a reference, so we can directly modify the attributes for matchingMinionReference
+ // and they'll be in the resultMinions array
+ const matchingMinionReference = resultMinions.find(m => m.name === minion.name)
+ if (!matchingMinionReference) {
+ // if the minion name isn't already in the array, add it!
+ resultMinions.push(minion)
+ } else {
+
+ // This should never happen, but in case the length of `minion.levels` is longer than
+ // `matchingMinionReference.levels`, then it should be extended to be equal length
+ while (matchingMinionReference.levels.length < minion.levels.length)
+ matchingMinionReference.levels.push(null)
+
+ for (let i = 0; i < minion.levels.length; i++) {
+ if (minion.levels[i])
+ matchingMinionReference.levels[i] = true
+ }
+ }
+ }
+ }
+
+ return resultMinions
+} \ No newline at end of file
diff --git a/src/cleaners/skyblock/stats.ts b/src/cleaners/skyblock/stats.ts
new file mode 100644
index 0000000..07d9133
--- /dev/null
+++ b/src/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
+}