aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cleaners/skyblock/minions.ts44
-rw-r--r--src/constants.ts39
-rw-r--r--src/hypixel.ts4
3 files changed, 68 insertions, 19 deletions
diff --git a/src/cleaners/skyblock/minions.ts b/src/cleaners/skyblock/minions.ts
index c77b6c5..3067fd0 100644
--- a/src/cleaners/skyblock/minions.ts
+++ b/src/cleaners/skyblock/minions.ts
@@ -1,6 +1,5 @@
import typedHypixelApi from 'typed-hypixel-api'
import * as constants from '../../constants.js'
-import { maxMinion } from '../../hypixel.js'
export interface CleanMinion {
name: string,
@@ -14,19 +13,28 @@ export interface CleanMinion {
*/
export async function cleanMinions(member: typedHypixelApi.SkyBlockProfileMember): Promise<CleanMinion[]> {
const minions: CleanMinion[] = []
- const processedMinionNames: Set<string> = new Set()
+ const processedMinionIds: Set<string> = new Set()
+
+ const maxMinionTiers = await constants.fetchMaxMinionTiers()
+ let updatedMaxMinionTiers = false
for (const minionRaw of member?.crafted_generators ?? []) {
// 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 minionId = minionRaw.split(/_\d/)[0].toLowerCase()
const minionLevel = parseInt(minionRaw.split(/\D*_/)[1])
- let matchingMinion = minions.find(m => m.name === minionName)
+
+ if (minionLevel > (maxMinionTiers[minionId] ?? -1)) {
+ maxMinionTiers[minionId] = minionLevel
+ updatedMaxMinionTiers = true
+ }
+
+ let matchingMinion = minions.find(m => m.name === minionId)
if (!matchingMinion) {
// if the minion doesnt already exist in the minions array, then create it
matchingMinion = {
- name: minionName,
- levels: new Array(maxMinion).fill(false)
+ name: minionId,
+ levels: new Array(maxMinionTiers[minionId]).fill(false)
}
minions.push(matchingMinion)
}
@@ -36,29 +44,35 @@ export async function cleanMinions(member: typedHypixelApi.SkyBlockProfileMember
// set the minion at that level to true
matchingMinion.levels[minionLevel - 1] = true
- processedMinionNames.add(minionName)
+ processedMinionIds.add(minionId)
}
const allMinionNames = new Set(await constants.fetchMinions())
- for (const minionName of processedMinionNames) {
+ for (const minionName of processedMinionIds) {
if (!allMinionNames.has(minionName)) {
- constants.addMinions(Array.from(processedMinionNames))
+ constants.addMinions(Array.from(processedMinionIds))
break
}
}
- for (const minionName of allMinionNames) {
- if (!processedMinionNames.has(minionName)) {
- processedMinionNames.add(minionName)
+ for (const minionId of allMinionNames) {
+ if (!processedMinionIds.has(minionId)) {
+ processedMinionIds.add(minionId)
minions.push({
- name: minionName,
- levels: new Array(maxMinion).fill(false)
+ name: minionId,
+ levels: new Array(maxMinionTiers[minionId]).fill(false)
})
}
}
- return minions.sort((a, b) => a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))
+ if (updatedMaxMinionTiers) {
+ await constants.addMaxMinionTiers(maxMinionTiers)
+ }
+
+ minions.sort((a, b) => a.name > b.name ? 1 : (a.name < b.name ? -1 : 0))
+
+ return minions
}
/**
diff --git a/src/constants.ts b/src/constants.ts
index 14254ed..a112c96 100644
--- a/src/constants.ts
+++ b/src/constants.ts
@@ -267,6 +267,45 @@ export async function addCrops(addingCrops: string[]): Promise<void> {
await constants.addJSONConstants('crops.json', addingCrops, 'crop')
}
+export async function fetchMaxMinionTiers(): Promise<Record<string, number>> {
+ return await constants.fetchJSONConstant('max_minion_tiers.json')
+}
+
+export async function addMaxMinionTiers(addingTiers: Record<string, number>): Promise<void> {
+ let file: GithubFile = await fetchFile('max_minion_tiers.json')
+ if (!file.path)
+ return
+
+ let maxTiers: Record<string, number>
+ try {
+ maxTiers = JSON.parse(file.content)
+ } catch {
+ // invalid json, set it as an empty array and continue
+ console.warn('Invalid max minion tiers file, resetting to empty')
+ maxTiers = {}
+ }
+
+ let updated = false
+
+ for (const [minionId, potentialMaxTier] of Object.entries(addingTiers)) {
+ if (potentialMaxTier > (maxTiers[minionId] ?? -1)) {
+ maxTiers[minionId] = potentialMaxTier
+ updated = true
+ }
+ }
+
+ if (!updated) return
+
+ const commitMessage = 'Update max minion tiers'
+ try {
+ await editFile(file, commitMessage, JSON.stringify(maxTiers, null, 2))
+ } catch {
+ // the file probably changed or something, try again
+ file = await fetchFile('max_minion_tiers.json')
+ await editFile(file, commitMessage, JSON.stringify(maxTiers, null, 2))
+ }
+}
+
interface constantValues {
max_minions?: number
diff --git a/src/hypixel.ts b/src/hypixel.ts
index c6bfb3c..d7a5887 100644
--- a/src/hypixel.ts
+++ b/src/hypixel.ts
@@ -33,10 +33,6 @@ export type Included = 'profiles' | 'player' | 'stats' | 'inventories' | undefin
// the interval at which the "last_save" parameter updates in the hypixel api, this is 3 minutes
export const saveInterval = 60 * 3 * 1000
-// the highest level a minion can be
-// TODO: have this be auto updated
-export const maxMinion = 12
-
/**
* Send a request to api.hypixel.net using a random key, clean it up to be more useable, and return it
*/