diff options
author | efefury <69400149+efefury@users.noreply.github.com> | 2023-01-31 22:04:33 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-31 23:04:33 +0100 |
commit | 7d34b5456f9978d063b9098ab095256109d522c2 (patch) | |
tree | 3752f4adb11ef6bf77c017102343121141e3571b /src/main/kotlin/io | |
parent | 291860435d5487562b122aaddbe57c178a8734de (diff) | |
download | NotEnoughUpdates-7d34b5456f9978d063b9098ab095256109d522c2.tar.gz NotEnoughUpdates-7d34b5456f9978d063b9098ab095256109d522c2.tar.bz2 NotEnoughUpdates-7d34b5456f9978d063b9098ab095256109d522c2.zip |
Levels page in Profile Viewer (#562)
* ughidontwannafigureouthowtousethis
* not finished
hgdt<
* Make use of new collections api in collectionspage
* final commit HOPEFULLY
* fix infer v1
* fix mithril powder going over cap
my beloved
* Formatting :thumbsup:
* More Formatting :thumbsup:
important
* ea sports
its in the game
* maybe fix cache
* change location of weight and networth and config option for weght
requested by alea
* Fix jump at CAP powder
* Code formatting and deleted unused variables.
* Used KUUDRA_TIERS array from CrimsonIslePage in SlayingTaskLevel.
* Used SLAYERS array from PROFILEVIEWER in SlayingTaskLevel.
* Intellij Code Cleanup.
* revert fun things
Please efe put formatting in another PR.
* fix issues
idk
* Import fixes and formatting. And an unnecessary empty space in 2.1.1 for jani.
* fix nea issue
---------
Co-authored-by: nea <nea@nea.moe>
Co-authored-by: jani270 <jani270@gmx.de>
Co-authored-by: hannibal2 <24389977+hannibal00212@users.noreply.github.com>
Diffstat (limited to 'src/main/kotlin/io')
-rw-r--r-- | src/main/kotlin/io/github/moulberry/notenoughupdates/util/hypixelapi/Collection.kt | 159 |
1 files changed, 159 insertions, 0 deletions
diff --git a/src/main/kotlin/io/github/moulberry/notenoughupdates/util/hypixelapi/Collection.kt b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/hypixelapi/Collection.kt new file mode 100644 index 00000000..b2c7fcec --- /dev/null +++ b/src/main/kotlin/io/github/moulberry/notenoughupdates/util/hypixelapi/Collection.kt @@ -0,0 +1,159 @@ +/* + * Copyright (C) 2023 NotEnoughUpdates contributors + * + * This file is part of NotEnoughUpdates. + * + * NotEnoughUpdates is free software: you can redistribute it + * and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + * + * NotEnoughUpdates is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>. + */ + +package io.github.moulberry.notenoughupdates.util.hypixelapi + +import com.google.gson.JsonArray +import com.google.gson.JsonObject +import io.github.moulberry.notenoughupdates.NotEnoughUpdates +import java.math.BigInteger +import java.util.concurrent.CompletableFuture +import kotlin.collections.List +import kotlin.collections.Map +import kotlin.collections.component1 +import kotlin.collections.component2 +import kotlin.collections.filter +import kotlin.collections.flatMap +import kotlin.collections.groupBy +import kotlin.collections.mapNotNull +import kotlin.collections.mapValues +import kotlin.collections.maxOf +import kotlin.collections.sumOf +import kotlin.collections.toList +import kotlin.collections.toMap +import kotlin.collections.toSet + +data class ProfileCollectionInfo( + val collections: Map<String, CollectionInfo>, + val craftedGenerators: Map<String, Int>, +) { + data class CollectionInfo( + val collection: Collection, + val totalCollectionCount: BigInteger, + val personalCollectionCount: BigInteger, + val unlockedTiers: List<CollectionTier>, + ) + + class CollectionMetadata internal constructor() { + lateinit var collections: Map<String, CollectionCategory> + private set + val allCollections by lazy { collections.values.flatMap { it.items.toList() }.toMap() } + } + + class CollectionCategory internal constructor() { + lateinit var items: Map<String, Collection> + private set + } + + class Collection internal constructor() { + lateinit var name: String + private set + var maxTiers: Int = -1 + private set + lateinit var tiers: List<CollectionTier> + private set + + override fun toString(): String { + return "Collection(name=$name, maxTiers=$maxTiers, tiers=$tiers)" + } + } + + class CollectionTier internal constructor() { + var tier: Int = -1 + private set + var amountRequired: Int = -1 + private set + lateinit var unlocks: List<String> + private set + + override fun toString(): String { + return "CollectionTier(tier=$tier, amountRequired=$amountRequired, unlocks=$unlocks)" + } + } + + + companion object { + + + val generatorPattern = "^([^0-9]+)_([0-9]+)$".toRegex() + + val hypixelCollectionInfo: CompletableFuture<CollectionMetadata> by lazy { + NotEnoughUpdates.INSTANCE.manager.apiUtils + .newHypixelApiRequest("resources/skyblock/collections") + .requestJson() + .thenApply { + NotEnoughUpdates.INSTANCE.manager.gson.fromJson(it, CollectionMetadata::class.java) + } + } + + fun getCollectionData( + profileData: JsonObject, + mainPlayer: String, + collectionData: CollectionMetadata + ): ProfileCollectionInfo? { + val mainPlayerUUID = mainPlayer.replace("-", "") + val members = profileData["members"] as? JsonObject ?: return null + val mainPlayerData = + (members[mainPlayerUUID] as? JsonObject ?: return null) + val mainPlayerCollection = mainPlayerData["collection"] as? JsonObject ?: return null + val memberCollections = members.entrySet().mapNotNull { (uuid, data) -> + if (data !is JsonObject) return null + data["collection"] as? JsonObject + } + val generators = members.entrySet().mapNotNull { (uuid, data) -> + if (data !is JsonObject) return null + data["crafted_generators"] as? JsonArray + }.flatMap { it.toList() } + return ProfileCollectionInfo( + collectionData.allCollections.mapValues { (name, collection) -> + val totalCollection = memberCollections.sumOf { it[name]?.asBigInteger ?: BigInteger.ZERO } + val personalCollection = mainPlayerCollection[name]?.asBigInteger ?: BigInteger.ZERO + CollectionInfo( + collection, + totalCollection, + personalCollection, + collection.tiers.filter { BigInteger.valueOf(it.amountRequired.toLong()) <= totalCollection } + ) + }, + generators.toSet() + .mapNotNull { + val pattern = generatorPattern.matchEntire(it.asString) ?: return@mapNotNull null + pattern.groupValues[1] to pattern.groupValues[2].toInt() + } + .groupBy { it.first } + .mapValues { + it.value.maxOf { it.second } + } + .toMap() + ) + } + + /** + * This should be the json object returned by /skyblock/profiles at profiles.<somenumber>. (aka the root tag + * should contain profile_id, members, cute_name, etc.) + */ + @JvmStatic + fun getCollectionData(profileData: JsonObject, mainPlayer: String): CompletableFuture<ProfileCollectionInfo?> { + return hypixelCollectionInfo.thenApply { + getCollectionData(profileData, mainPlayer, it) + } + } + } + +} |