aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/io/github/moulberry/notenoughupdates/miscfeatures/profileviewer/bestiary/BestiaryData.kt
blob: cf11b35506b2e29a0ad903848fae48c29467ff94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2023-2024 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.miscfeatures.profileviewer.bestiary

import com.google.gson.JsonObject
import io.github.moulberry.notenoughupdates.util.Constants
import io.github.moulberry.notenoughupdates.util.ItemUtils
import io.github.moulberry.notenoughupdates.util.Utils
import io.github.moulberry.notenoughupdates.util.roundToDecimals
import kotlin.math.min

object BestiaryData {
    private val categoriesToParse = listOf(
        "dynamic",
        "hub",
        "farming_1",
        "garden",
        "combat_1",
        "combat_3",
        "crimson_isle",
        "mining_2",
        "mining_3",
        "crystal_hollows",
        "foraging_1",
        "spooky_festival",
        "mythological_creatures",
        "jerry",
        "kuudra",
        "catacombs",
        "fishing"
    )

    /**
     * Calculates the sum of all individual tiers for this profile
     *
     * @param computedCategories List of parsed categories
     * @see BestiaryPage.parseBestiaryData
     */
    @JvmStatic
    fun calculateTotalBestiaryTiers(computedCategories: List<Category>): Int {
        var tiers = 0.0
        computedCategories.forEach {
            tiers += countTotalLevels(it)
        }
        return tiers.toInt()
    }

    /**
     * Calculate the skyblock xp awarded for the given bestiary progress
     */
    @JvmStatic
    fun calculateBestiarySkyblockXp(profileInfo: JsonObject): Int {
        val totalTiers = calculateTotalBestiaryTiers(parseBestiaryData(profileInfo))
        var skyblockXp = 0

        val slayingTask = Constants.SBLEVELS.getAsJsonObject("slaying_task") ?: return 0
        val xpPerTier = (slayingTask.get("bestiary_family_xp") ?: return 0).asInt
        val xpPerMilestone = slayingTask.get("bestiary_milestone_xp").asInt
        val maxXp = slayingTask.get("bestiary_progress").asInt

        skyblockXp += totalTiers * xpPerTier

        val milestones = (totalTiers / 100)
        skyblockXp += milestones * xpPerMilestone

        return min(skyblockXp, maxXp)
    }

    private fun countTotalLevels(category: Category): Int {
        var levels = 0
        for (mob in category.mobs) {
            levels += mob.mobLevelData.level
        }
        category.subCategories.forEach { levels += countTotalLevels(it) }
        return levels
    }

    /**
     * Checks if a user profile has migrated.
     *
     * @param profileInfo skyblock profile information
     */
    fun hasMigrated(profileInfo: JsonObject): Boolean {
        val bestiaryObject = profileInfo.getAsJsonObject("bestiary") ?: return false
        return if (bestiaryObject.has("migration")) {
            bestiaryObject.get("migration").asBoolean
        } else {
            // The account is new and therefore already uses the new format
            true
        }
    }

    /**
     * Parse the bestiary data for the profile. Categories are taken from the `constants/bestiary.json` repo file
     *
     * @param profileInfo the JsonObject containing the bestiary data
     */
    @JvmStatic
    fun parseBestiaryData(profileInfo: JsonObject): MutableList<Category> {
        if (!hasMigrated(profileInfo) || Constants.BESTIARY == null) {
            return mutableListOf()
        }

        val parsedCategories = mutableListOf<Category>()

        val apiKills = profileInfo.getAsJsonObject("bestiary")!!.getAsJsonObject("kills") ?: return mutableListOf()
        val apiDeaths = profileInfo.getAsJsonObject("bestiary").getAsJsonObject("deaths") ?: return mutableListOf()
        val killsMap: HashMap<String, Int> = HashMap()
        for (entry in apiKills.entrySet()) {
            if (entry.key == "last_killed_mob") {
                continue
            }
            killsMap[entry.key] = entry.value.asString.toIntOrNull() ?: -1
        }
        val deathsMap: HashMap<String, Int> = HashMap()
        for (entry in apiDeaths.entrySet()) {
            deathsMap[entry.key] = entry.value.asString.toIntOrNull() ?: -1
        }

        for (categoryId in categoriesToParse) {
            val categoryData = Constants.BESTIARY.getAsJsonObject(categoryId)
            if (categoryData != null) {
                parsedCategories.add(parseCategory(categoryData, categoryId, killsMap, deathsMap))
            } else {
                Utils.showOutdatedRepoNotification("bestiary.json missing or outdated")
            }
        }

        return parsedCategories
    }

    /**
     * Parse one individual category, including potential subcategories
     */
    private fun parseCategory(
        categoryData: JsonObject,
        categoryId: String,
        killsMap: HashMap<String, Int>,
        deathsMap: HashMap<String, Int>
    ): Category {
        val categoryName = categoryData["name"].asString
        val computedMobs: MutableList<Mob> = mutableListOf()
        val categoryIconData = categoryData["icon"].asJsonObject

        val categoryIcon = if (categoryIconData.has("skullOwner")) {
            Utils.createSkull(
                categoryName, categoryIconData["skullOwner"].asString, categoryIconData["texture"].asString
            )
        } else {
            ItemUtils.createItemStackFromId(categoryIconData["item"].asString, categoryName)
        }
        if (categoryData.has("hasSubcategories")) { // It must have some subcategories
            val subCategories: MutableList<Category> = mutableListOf()

            val reserved = listOf("name", "icon", "hasSubcategories")
            for (entry in categoryData.entrySet()) {
                if (!reserved.contains(entry.key)) {
                    subCategories.add(
                        parseCategory(
                            entry.value.asJsonObject,
                            "${categoryId}_${entry.key}",
                            killsMap,
                            deathsMap
                        )
                    )
                }
            }
            return Category(categoryId, categoryName, categoryIcon, emptyList(), subCategories, calculateFamilyDataOfSubcategories(subCategories))
        } else {
            val categoryMobs = categoryData["mobs"].asJsonArray.map { it.asJsonObject }

            for (mobData in categoryMobs) {
                val mobName = mobData["name"].asString
                val mobIcon = if (mobData.has("skullOwner")) {
                    Utils.createSkull(
                        mobName, mobData["skullOwner"].asString, mobData["texture"].asString
                    )
                } else {
                    ItemUtils.createItemStackFromId(mobData["item"].asString, mobName)
                }

                val cap = mobData["cap"].asDouble
                val bracket = mobData["bracket"].asInt

                var kills = 0.0
                var deaths = 0.0

                // The mobs array contains the individual names returned by the API
                val mobsArray = mobData["mobs"].asJsonArray.map { it.asString }
                for (s in mobsArray) {
                    kills += killsMap.getOrDefault(s, 0)
                    deaths += deathsMap.getOrDefault(s, 0)
                }

                val levelData = calculateLevel(bracket, kills, cap)
                computedMobs.add(Mob(mobName, mobIcon, kills, deaths, levelData))
            }
            return Category(categoryId, categoryName, categoryIcon, computedMobs, emptyList(), calculateFamilyData(computedMobs))
        }
    }

    /**
     * Calculates the level for a given mob
     *
     * @param bracket applicable bracket number
     * @param kills number of kills the player has on that mob type
     * @param cap maximum kill limit for the mob
     */
    private fun calculateLevel(bracket: Int, kills: Double, cap: Double): MobLevelData {
        val bracketData =
            Constants.BESTIARY["brackets"].asJsonObject[bracket.toString()].asJsonArray.map { it.asDouble }
        var maxLevel = false
        var progress = 0.0
        var effKills = 0.0
        var effReq = 0.0

        val effectiveKills = if (kills >= cap) {
            maxLevel = true
            cap
        } else {
            kills
        }

        val totalProgress = (effectiveKills / cap * 100).roundToDecimals(1)

        var level = 0
        for (requiredKills in bracketData) {
            if (effectiveKills >= requiredKills) {
                level++