Skip to content

Commit

Permalink
Backend: Compacting CFStats.updateDisplay (#3056)
Browse files Browse the repository at this point in the history
Co-authored-by: hannibal2 <[email protected]>
Co-authored-by: Cal <[email protected]>
  • Loading branch information
3 people authored Dec 15, 2024
1 parent 2b542b3 commit 9fe4db7
Showing 1 changed file with 121 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import at.hannibal2.skyhanni.utils.ClipboardUtils
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.toRoman
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderables
import at.hannibal2.skyhanni.utils.RenderUtils.renderRenderable
import at.hannibal2.skyhanni.utils.StringUtils.removeColor
import at.hannibal2.skyhanni.utils.TimeUtils.format
import at.hannibal2.skyhanni.utils.renderables.Renderable
Expand All @@ -29,7 +29,7 @@ object ChocolateFactoryStats {
private val config get() = ChocolateFactoryAPI.config
private val profileStorage get() = ChocolateFactoryAPI.profileStorage

private var display = listOf<Renderable>()
private var display: Renderable? = null

@SubscribeEvent
fun onSecondPassed(event: SecondPassedEvent) {
Expand All @@ -43,45 +43,66 @@ object ChocolateFactoryStats {
if (!ChocolateFactoryAPI.inChocolateFactory && !ChocolateFactoryAPI.chocolateFactoryPaused) return
if (!config.statsDisplay) return

config.position.renderRenderables(display, posLabel = "Chocolate Factory Stats")
display?.let {
config.position.renderRenderable(it, posLabel = "Chocolate Factory Stats")
}
}

@Suppress("LongMethod", "CyclomaticComplexMethod")
fun updateDisplay() {
val profileStorage = profileStorage ?: return

val perSecond = ChocolateFactoryAPI.chocolatePerSecond
val perMinute = perSecond * 60
val perHour = perMinute * 60
val perDay = perHour * 24
val map = buildMap {
put(ChocolateFactoryStat.HEADER, "§6§lChocolate Factory ${ChocolateFactoryAPI.currentPrestige.toRoman()}")

val maxSuffix = if (ChocolateFactoryAPI.isMax()) " §cMax!" else ""
put(ChocolateFactoryStat.CURRENT, "§eCurrent Chocolate: §6${ChocolateAmount.CURRENT.formatted}$maxSuffix")
put(ChocolateFactoryStat.THIS_PRESTIGE, "§eThis Prestige: §6${ChocolateAmount.PRESTIGE.formatted}")
put(ChocolateFactoryStat.ALL_TIME, "§eAll-time: §6${ChocolateAmount.ALL_TIME.formatted}")

addProduction()

put(ChocolateFactoryStat.MULTIPLIER, "§eChocolate Multiplier: §6${profileStorage.chocolateMultiplier}")
put(ChocolateFactoryStat.BARN, "§eBarn: §6${ChocolateFactoryBarnManager.barnStatus()}")

addLeaderboard()

put(ChocolateFactoryStat.EMPTY, "")
put(ChocolateFactoryStat.EMPTY_2, "")
put(ChocolateFactoryStat.EMPTY_3, "")
put(ChocolateFactoryStat.EMPTY_4, "")
put(ChocolateFactoryStat.EMPTY_5, "")

addTimeTower()

put(
ChocolateFactoryStat.RAW_PER_SECOND,
"§eRaw Per Second: §6${profileStorage.rawChocPerSecond.addSeparators()}",
)

addPrestige()

val upgradeAvailableAt = ChocolateAmount.CURRENT.formattedTimeUntilGoal(profileStorage.bestUpgradeCost)
put(ChocolateFactoryStat.TIME_TO_BEST_UPGRADE, "§eBest Upgrade: $upgradeAvailableAt")

addHitman()
}
val text = config.statsDisplayList.filter { it.shouldDisplay() }.flatMap { map[it]?.split("\n").orEmpty() }
display = createDisplay(text)
}

private fun MutableMap<ChocolateFactoryStat, String>.addLeaderboard() {
val position = ChocolateFactoryAPI.leaderboardPosition
val positionText = position?.addSeparators() ?: "???"
val percentile = ChocolateFactoryAPI.leaderboardPercentile
val percentileText = percentile?.let { "§7Top §a$it%" }.orEmpty()
val leaderboard = "#$positionText $percentileText"
ChocolatePositionChange.update(position, leaderboard)
HoppityEventSummary.updateCfPosition(position, percentile)
put(ChocolateFactoryStat.LEADERBOARD_POS, "§ePosition: §b$leaderboard")
}

val timeTowerInfo = if (ChocolateFactoryTimeTowerManager.timeTowerActive()) {
"§d§lActive"
} else {
"§6${ChocolateFactoryTimeTowerManager.timeTowerCharges()}"
}

val timeTowerFull = ChocolateFactoryTimeTowerManager.timeTowerFullTimeMark()

val prestigeEstimate = ChocolateAmount.PRESTIGE.formattedTimeUntilGoal(ChocolateFactoryAPI.chocolateForPrestige)
val chocolateUntilPrestigeCalculation =
ChocolateFactoryAPI.chocolateForPrestige - ChocolateAmount.PRESTIGE.chocolate()

var chocolateUntilPrestige = "§6${chocolateUntilPrestigeCalculation.addSeparators()}"

if (chocolateUntilPrestigeCalculation <= 0) {
chocolateUntilPrestige = "§aPrestige Available"
}

val upgradeAvailableAt = ChocolateAmount.CURRENT.formattedTimeUntilGoal(profileStorage.bestUpgradeCost)
private fun MutableMap<ChocolateFactoryStat, String>.addHitman() {
val profileStorage = ChocolateFactoryStats.profileStorage ?: return

val hitmanStats = profileStorage.hitmanStats
val availableHitmanEggs = hitmanStats.availableEggs?.takeIf { it > 0 }?.toString() ?: "§7None"
Expand All @@ -98,96 +119,91 @@ object ChocolateFactoryStats {
val hitmanFullString = if (openSlotsNow == 0) "§7Cooldown..."
else hitmanFullTime.takeIf { it > Duration.ZERO }?.format() ?: "§cFull Now"
val hitmanSlotsFull = "${if (hitmanFullEventInhibited) "§c" else "§b"}$hitmanFullString"
put(ChocolateFactoryStat.HITMAN_HEADER, "§c§lRabbit Hitman")
put(ChocolateFactoryStat.AVAILABLE_HITMAN_EGGS, "§eAvailable Hitman Eggs: §6$availableHitmanEggs")
put(ChocolateFactoryStat.OPEN_HITMAN_SLOTS, "§eOpen Hitman Slots: §6$openSlotsNow")
put(ChocolateFactoryStat.HITMAN_SLOT_COOLDOWN, "§eHitman Slot Cooldown: §b$hitmanSingleSlotCd")
put(ChocolateFactoryStat.HITMAN_ALL_SLOTS, "§eAll Hitman Slots Cooldown: §b$hitmanAllSlotsCd")

if (HoppityAPI.isHoppityEvent()) {
put(ChocolateFactoryStat.HITMAN_FULL_SLOTS, "§eFull Hitman Slots: §b$hitmanSlotsFull")
put(ChocolateFactoryStat.HITMAN_28_SLOTS, "§e$purchasedSlots Hitman Claims: $hitmanAllClaimReady")
}
}

val map = buildMap {
put(ChocolateFactoryStat.HEADER, "§6§lChocolate Factory ${ChocolateFactoryAPI.currentPrestige.toRoman()}")

val maxSuffix = if (ChocolateFactoryAPI.isMax()) {
" §cMax!"
} else ""
put(ChocolateFactoryStat.CURRENT, "§eCurrent Chocolate: §6${ChocolateAmount.CURRENT.formatted}$maxSuffix")
put(ChocolateFactoryStat.THIS_PRESTIGE, "§eThis Prestige: §6${ChocolateAmount.PRESTIGE.formatted}")
put(ChocolateFactoryStat.ALL_TIME, "§eAll-time: §6${ChocolateAmount.ALL_TIME.formatted}")

put(ChocolateFactoryStat.PER_SECOND, "§ePer Second: §6${perSecond.addSeparators()}")
put(ChocolateFactoryStat.PER_MINUTE, "§ePer Minute: §6${perMinute.addSeparators()}")
put(ChocolateFactoryStat.PER_HOUR, "§ePer Hour: §6${perHour.addSeparators()}")
put(ChocolateFactoryStat.PER_DAY, "§ePer Day: §6${perDay.addSeparators()}")

put(ChocolateFactoryStat.MULTIPLIER, "§eChocolate Multiplier: §6${profileStorage.chocolateMultiplier}")
put(ChocolateFactoryStat.BARN, "§eBarn: §6${ChocolateFactoryBarnManager.barnStatus()}")

put(ChocolateFactoryStat.LEADERBOARD_POS, "§ePosition: §b$leaderboard")
private fun MutableMap<ChocolateFactoryStat, String>.addPrestige() {
val allTime = ChocolateAmount.ALL_TIME.chocolate()
val nextChocolateMilestone = ChocolateFactoryAPI.getNextMilestoneChocolate(allTime)
val amountUntilNextMilestone = nextChocolateMilestone - allTime
val amountFormat = amountUntilNextMilestone.addSeparators()
val maxMilestoneEstimate = ChocolateAmount.ALL_TIME.formattedTimeUntilGoal(nextChocolateMilestone)
val prestigeEstimate = ChocolateAmount.PRESTIGE.formattedTimeUntilGoal(ChocolateFactoryAPI.chocolateForPrestige)
val chocolateUntilPrestigeCalculation = ChocolateFactoryAPI.chocolateForPrestige - ChocolateAmount.PRESTIGE.chocolate()

put(ChocolateFactoryStat.EMPTY, "")
put(ChocolateFactoryStat.EMPTY_2, "")
put(ChocolateFactoryStat.EMPTY_3, "")
put(ChocolateFactoryStat.EMPTY_4, "")
put(ChocolateFactoryStat.EMPTY_5, "")
var chocolateUntilPrestige = "§6${chocolateUntilPrestigeCalculation.addSeparators()}"

put(ChocolateFactoryStat.TIME_TOWER, "§eTime Tower: §6$timeTowerInfo")
put(
ChocolateFactoryStat.TIME_TOWER_FULL,
if (ChocolateFactoryTimeTowerManager.timeTowerFull()) {
"§eFull Tower Charges: §a§lNow\n" +
"§eHappens at: §a§lNow"
} else {
"§eFull Tower Charges: §b${timeTowerFull.timeUntil().format()}\n" +
"§eHappens at: §b${timeTowerFull.formattedDate("EEEE, MMM d h:mm a")}"
},
)
put(
ChocolateFactoryStat.RAW_PER_SECOND,
"§eRaw Per Second: §6${profileStorage.rawChocPerSecond.addSeparators()}",
if (chocolateUntilPrestigeCalculation <= 0) {
chocolateUntilPrestige = "§aPrestige Available"
}
val prestigeData = when {
!ChocolateFactoryAPI.isMaxPrestige() -> mapOf(
ChocolateFactoryStat.TIME_TO_PRESTIGE to "§eTime To Prestige: $prestigeEstimate",
ChocolateFactoryStat.CHOCOLATE_UNTIL_PRESTIGE to "§eChocolate To Prestige: §6$chocolateUntilPrestige",
)

val allTime = ChocolateAmount.ALL_TIME.chocolate()
val nextChocolateMilestone = ChocolateFactoryAPI.getNextMilestoneChocolate(allTime)
val amountUntilNextMilestone = nextChocolateMilestone - allTime
val amountFormat = amountUntilNextMilestone.addSeparators()
val maxMilestoneEstimate = ChocolateAmount.ALL_TIME.formattedTimeUntilGoal(nextChocolateMilestone)
val prestigeData = when {
!ChocolateFactoryAPI.isMaxPrestige() -> mapOf(
ChocolateFactoryStat.TIME_TO_PRESTIGE to "§eTime To Prestige: $prestigeEstimate",
ChocolateFactoryStat.CHOCOLATE_UNTIL_PRESTIGE to "§eChocolate To Prestige: §6$chocolateUntilPrestige"
)
amountUntilNextMilestone >= 0 -> mapOf(
ChocolateFactoryStat.TIME_TO_PRESTIGE to "§eTime To Next Milestone: $maxMilestoneEstimate",
ChocolateFactoryStat.CHOCOLATE_UNTIL_PRESTIGE to "§eChocolate To Next Milestone: §6$amountFormat"
)
else -> emptyMap()
}
putAll(prestigeData)

put(ChocolateFactoryStat.TIME_TO_BEST_UPGRADE, "§eBest Upgrade: $upgradeAvailableAt")
amountUntilNextMilestone >= 0 -> mapOf(
ChocolateFactoryStat.TIME_TO_PRESTIGE to "§eTime To Next Milestone: $maxMilestoneEstimate",
ChocolateFactoryStat.CHOCOLATE_UNTIL_PRESTIGE to "§eChocolate To Next Milestone: §6$amountFormat",
)

put(ChocolateFactoryStat.HITMAN_HEADER, "§c§lRabbit Hitman")
put(ChocolateFactoryStat.AVAILABLE_HITMAN_EGGS, "§eAvailable Hitman Eggs: §6$availableHitmanEggs")
put(ChocolateFactoryStat.OPEN_HITMAN_SLOTS, "§eOpen Hitman Slots: §6$openSlotsNow")
put(ChocolateFactoryStat.HITMAN_SLOT_COOLDOWN, "§eHitman Slot Cooldown: §b$hitmanSingleSlotCd")
put(ChocolateFactoryStat.HITMAN_ALL_SLOTS, "§eAll Hitman Slots Cooldown: §b$hitmanAllSlotsCd")
else -> emptyMap()
}
putAll(prestigeData)
}

if (HoppityAPI.isHoppityEvent()) {
put(ChocolateFactoryStat.HITMAN_FULL_SLOTS, "§eFull Hitman Slots: §b$hitmanSlotsFull")
put(ChocolateFactoryStat.HITMAN_28_SLOTS, "§e$purchasedSlots Hitman Claims: $hitmanAllClaimReady")
}
private fun MutableMap<ChocolateFactoryStat, String>.addTimeTower() {
val timeTowerInfo = if (ChocolateFactoryTimeTowerManager.timeTowerActive()) {
"§d§lActive"
} else {
"§6${ChocolateFactoryTimeTowerManager.timeTowerCharges()}"
}
val text = config.statsDisplayList.filter { it.shouldDisplay() }.flatMap { map[it]?.split("\n").orEmpty() }
put(ChocolateFactoryStat.TIME_TOWER, "§eTime Tower: §6$timeTowerInfo")

display = listOf(
Renderable.clickAndHover(
Renderable.verticalContainer(text.map(Renderable::string)),
tips = listOf("§bCopy to Clipboard!"),
onClick = {
val list = text.toMutableList()
list.add(0, "${LorenzUtils.getPlayerName()}'s Chocolate Factory Stats")

ClipboardUtils.copyToClipboard(list.joinToString("\n") { it.removeColor() })
},
),
val timeTowerFull = ChocolateFactoryTimeTowerManager.timeTowerFullTimeMark()
put(
ChocolateFactoryStat.TIME_TOWER_FULL,
if (ChocolateFactoryTimeTowerManager.timeTowerFull()) {
"§eFull Tower Charges: §a§lNow\n" + "§eHappens at: §a§lNow"
} else {
"§eFull Tower Charges: §b${
timeTowerFull.timeUntil().format()
}\n" + "§eHappens at: §b${timeTowerFull.formattedDate("EEEE, MMM d h:mm a")}"
},
)
}

private fun createDisplay(text: List<String>) = Renderable.clickAndHover(
Renderable.verticalContainer(text.map(Renderable::string)),
tips = listOf("§bCopy to Clipboard!"),
onClick = {
val list = text.toMutableList()
list.add(0, "${LorenzUtils.getPlayerName()}'s Chocolate Factory Stats")

ClipboardUtils.copyToClipboard(list.joinToString("\n") { it.removeColor() })
},
)

private fun MutableMap<ChocolateFactoryStat, String>.addProduction() {
val perSecond = ChocolateFactoryAPI.chocolatePerSecond
val perMinute = perSecond * 60
val perHour = perMinute * 60
val perDay = perHour * 24
put(ChocolateFactoryStat.PER_SECOND, "§ePer Second: §6${perSecond.addSeparators()}")
put(ChocolateFactoryStat.PER_MINUTE, "§ePer Minute: §6${perMinute.addSeparators()}")
put(ChocolateFactoryStat.PER_HOUR, "§ePer Hour: §6${perHour.addSeparators()}")
put(ChocolateFactoryStat.PER_DAY, "§ePer Day: §6${perDay.addSeparators()}")
}

@HandleEvent
fun onConfigFix(event: ConfigUpdaterMigrator.ConfigFixEvent) {
event.transform(42, "event.chocolateFactory.statsDisplayList") { element ->
Expand Down

0 comments on commit 9fe4db7

Please sign in to comment.