Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import gg.skytils.skytilsmod.Skytils.Companion.failPrefix
import gg.skytils.skytilsmod.Skytils.Companion.mc
import gg.skytils.skytilsmod.core.API
import gg.skytils.skytilsmod.utils.*
import gg.skytils.skytilsmod.utils.NumberUtil.roundToPrecision
import kotlinx.coroutines.launch
import org.incendo.cloud.annotations.Argument
import org.incendo.cloud.annotations.Command
Expand Down Expand Up @@ -128,6 +129,11 @@ object CataCommand {
)

val secrets = playerResponse?.achievements?.getOrDefault("skyblock_treasure_hunter", 0) ?: 0
val comps = cataData.tier_completions.values
val masterComps = masterCataData?.tier_completions?.values ?: emptyList()
val runs = comps.sum() - (cataData.tier_completions["total"] ?: 0.0) +
masterComps.sum() - (masterCataData?.tier_completions?.get("total") ?: 0.0)
Comment on lines +134 to +135
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

There's a potential for division by zero here if runs is equal to 0.0. If runs is 0.0 and profileData.dungeons?.secrets is greater than 0, the division (profileData.dungeons?.secrets ?: 0.0) / runs would result in Infinity. The roundToPrecision(2) method then converts Infinity to a very large finite number, which would be misleading when formatted and displayed to the user. If profileData.dungeons?.secrets is 0.0 and runs is 0.0, the result is NaN, which roundToPrecision(2) converts to 0.0 (which is acceptable in this specific sub-case).

How about explicitly checking if runs is greater than zero before performing the division to prevent this and ensure a more accurate representation (e.g., 0 secrets/run if there are no runs)?

Suggested change
val runs = comps.sum() - (cataData.tier_completions["total"] ?: 0.0) +
masterComps.sum() - (masterCataData?.tier_completions?.get("total") ?: 0.0)
val secretsFound = profileData.dungeons?.secrets ?: 0.0
val secretsPerRun = if (runs > 0.0) {
(secretsFound / runs).roundToPrecision(2)
} else {
0.0 // Default to 0.0 secrets/run if there are no runs to avoid division by zero
}

val secretsPerRun = ((profileData.dungeons?.secrets ?: 0.0) / runs).roundToPrecision(2)

val classAvgOverflow = (archLevel + bersLevel + healerLevel + mageLevel + tankLevel) / 5.0
val classAvgCapped =
Expand Down Expand Up @@ -268,6 +274,7 @@ object CataCommand {
component
.append("§a§l➜ Miscellaneous:\n")
.append(" §aTotal Secrets Found: §l➡ §e${NumberUtil.nf.format(secrets)}\n")
.append(" §aSecrets/Run: §l➡ §e${NumberUtil.nf.format(secretsPerRun)}\n")
.append(
" §aBlood Mobs Killed: §l➡ §e${
NumberUtil.nf.format(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import gg.skytils.skytilsmod.Skytils.Companion.failPrefix
import gg.skytils.skytilsmod.Skytils.Companion.mc
import gg.skytils.skytilsmod.core.API
import gg.skytils.skytilsmod.utils.*
import gg.skytils.skytilsmod.utils.NumberUtil.roundToPrecision
import gg.skytils.skytilsmod.utils.NumberUtil.toRoman
import gg.skytils.skytilsmod.utils.SkillUtils.level
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -104,6 +105,12 @@ object PartyFinderStats {
val name = playerResponse.formattedName

val secrets = playerResponse.achievements.getOrDefault("skyblock_treasure_hunter", 0)
val comps = cataData.tier_completions.values
val masterComps = masterCataData?.tier_completions?.values ?: emptyList()
val runs = comps.sum() - (cataData.tier_completions["total"] ?: 0.0) +
masterComps.sum() - (masterCataData?.tier_completions?.get("total") ?: 0.0)
Comment on lines +108 to +111
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

This block of logic for calculating comps, masterComps, runs, and secretsPerRun appears to be similar to the logic in CataCommand.kt (lines 132-136).

To improve maintainability and adhere to the DRY (Don't Repeat Yourself) principle, could we extract this calculation into a shared utility function? This function could potentially reside in a relevant utility class (e.g., DungeonUtils or a new PlayerStatsUtils) and take cataData, masterCataData, and profileData.dungeons (or the specific secrets count) as parameters, returning the calculated secretsPerRun.

This would also centralize the fix for the potential division by zero issue.

val secretsPerRun = ((profileData.dungeons?.secrets ?: 0.0) / runs).roundToPrecision(2)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

Similar to the CataCommand.kt file, there's a potential for division by zero if runs is 0.0. This can lead to secretsPerRun being a misleadingly large number (if secrets > 0 and runs = 0, Infinity is rounded) or 0.0 (if secrets = 0 and runs = 0, NaN is rounded to 0.0).

It's important to handle the case where runs is zero to prevent incorrect data display. Consider checking runs > 0.0 before division, as suggested for CataCommand.kt.

                    val dungeonSecrets = profileData.dungeons?.secrets ?: 0.0
                    val secretsPerRun = if (runs > 0.0) {
                        (dungeonSecrets / runs).roundToPrecision(2)
                    } else {
                        0.0 // Default to 0.0 secrets/run if there are no runs
                    }


val component = UMessage("&2&m--------------------------------\n").append(
"$name §8» §dCata §9${
NumberUtil.nf.format(cataLevel)
Expand Down Expand Up @@ -240,6 +247,7 @@ object PartyFinderStats {
UTextComponent("§5Miscellanous: §7(Hover)\n\n").setHoverText(
"""
#§aTotal Secrets Found: §l§e${NumberUtil.nf.format(secrets)}
#§aSecrets Per Run: §l§e${NumberUtil.nf.format(secretsPerRun)}
#§aBlood Mobs Killed: §l§e${NumberUtil.nf.format(bloodMobsKilled)}
#§dMagical Power: §l§e$magicalPower
""".trimMargin("#")
Expand Down