From 2e44956441fd8a589993360ed66f18ef950cb49a Mon Sep 17 00:00:00 2001 From: HiZe Date: Fri, 31 May 2024 01:35:19 +0200 Subject: [PATCH] Backend: Use SkillAPI in SkillExpGainEvent (#1788) Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com> --- .../at/hannibal2/skyhanni/api/SkillAPI.kt | 96 ++++++++++++------- .../skyhanni/data/SkillExperience.kt | 11 --- .../skyhanni/events/SkillExpGainEvent.kt | 4 +- 3 files changed, 67 insertions(+), 44 deletions(-) diff --git a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt index e5c9067cdad5..842b9ec84d38 100644 --- a/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt +++ b/src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt @@ -7,6 +7,7 @@ import at.hannibal2.skyhanni.events.DebugDataCollectEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent import at.hannibal2.skyhanni.events.SecondPassedEvent +import at.hannibal2.skyhanni.events.SkillExpGainEvent import at.hannibal2.skyhanni.events.SkillOverflowLevelupEvent import at.hannibal2.skyhanni.features.skillprogress.SkillProgress import at.hannibal2.skyhanni.features.skillprogress.SkillType @@ -27,6 +28,7 @@ import at.hannibal2.skyhanni.utils.NumberUtil.formatLong import at.hannibal2.skyhanni.utils.NumberUtil.formatLongOrUserError import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimalIfNecessary import at.hannibal2.skyhanni.utils.SimpleTimeMark +import at.hannibal2.skyhanni.utils.StringUtils.matchMatcher import at.hannibal2.skyhanni.utils.StringUtils.removeColor import at.hannibal2.skyhanni.utils.TabListData import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern @@ -53,11 +55,15 @@ object SkillAPI { ) private val skillTabPattern by patternGroup.pattern( "skill.tab", - "(?:§e§lSkills: §r§a)?\\s*(?\\w+) (?\\d+): §r§.(?.+)%$" + " (?\\w+)(?: (?\\d+))?: §r§a(?[0-9.]+)%" ) private val maxSkillTabPattern by patternGroup.pattern( "skill.tab.max", - "(?:§e§lSkills: §r§a)?\\s*(?\\w+) (?\\d+): §r§c§lMAX\$" + " (?\\w+) (?\\d+): §r§c§lMAX" + ) + private val skillTabNoPercentPattern by patternGroup.pattern( + "skill.tab.nopercent", + " §r§a(?\\w+)(?: (?\\d+))?: §r§e(?[0-9,.]+)§r§6/§r§e(?[0-9kmb]+)" ) var skillXPInfoMap = mutableMapOf() @@ -120,6 +126,9 @@ object SkillAPI { skillPercentPattern -> handleSkillPatternPercent(matcher, skillType) skillMultiplierPattern -> handleSkillPatternMultiplier(matcher, skillType, skillInfo) } + + SkillExpGainEvent(skillType, matcher.group("gained").formatDouble()).postAndCatch() + showDisplay = true lastUpdate = SimpleTimeMark.now() skillXp.lastUpdate = SimpleTimeMark.now() @@ -274,48 +283,71 @@ object SkillAPI { } private fun handleSkillPatternPercent(matcher: Matcher, skillType: SkillType) { - var tablistLevel = 0 + var current = 0L + var needed = 0L + var xpPercentage = 0.0 + var isPercentPatternFound = false + var tablistLevel: Int? = null + for (line in TabListData.getTabList()) { - var levelMatcher = skillTabPattern.matcher(line) - if (levelMatcher.matches()) { - val type = levelMatcher.group("type") - if (type != skillType.displayName) continue - tablistLevel = levelMatcher.group("level").toInt() - if (levelMatcher.group("type").lowercase() != activeSkill?.lowercaseName) tablistLevel = 0 - } else { - levelMatcher = maxSkillTabPattern.matcher(line) - if (levelMatcher.matches()) { - tablistLevel = levelMatcher.group("level").toInt() - if (levelMatcher.group("type").lowercase() != activeSkill?.lowercaseName) tablistLevel = 0 + skillTabPattern.matchMatcher(line) { + if (group("type") == skillType.displayName) { + tablistLevel = group("level").toInt() + isPercentPatternFound = true + if (group("type").lowercase() != activeSkill?.lowercaseName) tablistLevel = null + } + } + + maxSkillTabPattern.matchMatcher(line) { + tablistLevel = group("level").toInt() + if (group("type").lowercase() != activeSkill?.lowercaseName) tablistLevel = null + } + + skillTabNoPercentPattern.matchMatcher(line) { + if (group("type") == skillType.displayName) { + tablistLevel = group("level").toInt() + current = group("current").formatLong() + needed = group("needed").formatLong() + isPercentPatternFound = false + return@matchMatcher } } + xpPercentage = matcher.group("progress").formatDouble() } + val existingLevel = getSkillInfo(skillType) ?: SkillInfo() - val xpPercentage = matcher.group("progress").formatDouble() - val levelXp = calculateLevelXp(existingLevel.level - 1) - val nextLevelDiff = levelArray.getOrNull(tablistLevel)?.toDouble() ?: 7_600_000.0 - val nextLevelProgress = nextLevelDiff * xpPercentage / 100 - val totalXp = levelXp + nextLevelProgress - val (_, currentOverflow, currentMaxOverflow, totalOverflow) = getSkillInfo( - tablistLevel, - nextLevelProgress.toLong(), - nextLevelDiff.toLong(), - totalXp.toLong() - ) + tablistLevel?.let { level -> + if (isPercentPatternFound) { + val levelXp = calculateLevelXp(existingLevel.level - 1) + val nextLevelDiff = levelArray.getOrNull(level)?.toDouble() ?: 7_600_000.0 + val nextLevelProgress = nextLevelDiff * xpPercentage / 100 + val totalXp = levelXp + nextLevelProgress + updateSkillInfo(existingLevel, level, nextLevelProgress.toLong(), nextLevelDiff.toLong(), totalXp.toLong(), matcher.group("gained")) + } else { + val exactLevel = getLevelExact(needed) + val levelXp = calculateLevelXp(existingLevel.level - 1).toLong() + current + updateSkillInfo(existingLevel, exactLevel, current, needed, levelXp, matcher.group("gained")) + } + storage?.set(skillType, existingLevel) + } + } + + private fun updateSkillInfo(existingLevel: SkillInfo, level: Int, currentXp: Long, maxXp: Long, totalXp: Long, gained: String) { + val (levelOverflow, currentOverflow, currentMaxOverflow, totalOverflow) = getSkillInfo(level, currentXp, maxXp, totalXp) + existingLevel.apply { - this.totalXp = totalXp.toLong() - this.currentXp = nextLevelProgress.toLong() - this.currentXpMax = nextLevelDiff.toLong() - this.level = tablistLevel + this.totalXp = totalXp + this.currentXp = currentXp + this.currentXpMax = maxXp + this.level = level this.overflowTotalXp = totalOverflow this.overflowCurrentXp = currentOverflow this.overflowCurrentXpMax = currentMaxOverflow - this.overflowLevel = tablistLevel + this.overflowLevel = levelOverflow - this.lastGain = matcher.group("gained") + this.lastGain = gained } - storage?.set(skillType, existingLevel) } private fun handleSkillPatternMultiplier(matcher: Matcher, skillType: SkillType, skillInfo: SkillInfo) { diff --git a/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt b/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt index 439a35e72111..25d48a525e30 100644 --- a/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt +++ b/src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt @@ -3,7 +3,6 @@ package at.hannibal2.skyhanni.data import at.hannibal2.skyhanni.events.ActionBarUpdateEvent import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent import at.hannibal2.skyhanni.events.ProfileJoinEvent -import at.hannibal2.skyhanni.events.SkillExpGainEvent import at.hannibal2.skyhanni.utils.ItemUtils.getLore import at.hannibal2.skyhanni.utils.ItemUtils.name import at.hannibal2.skyhanni.utils.LorenzUtils @@ -24,10 +23,6 @@ class SkillExperience { "inventory", ".* §e(?.*)§6/.*" ) - private val actionBarLowLevelPattern by patternGroup.pattern( - "actionbarlow", - ".*§3+(?.+) (?.*) \\((?.*)%\\).*" - ) @SubscribeEvent fun onProfileJoin(event: ProfileJoinEvent) { @@ -46,11 +41,6 @@ class SkillExperience { val baseExp = getExpForLevel(nextLevel - 1) val totalExp = baseExp + overflow skillExp[skill] = totalExp - SkillExpGainEvent(skill).postAndCatch() - } - actionBarLowLevelPattern.matchMatcher(event.actionBar) { - val skill = group("skill").lowercase() - SkillExpGainEvent(skill).postAndCatch() } } @@ -186,4 +176,3 @@ class SkillExperience { ) } } - diff --git a/src/main/java/at/hannibal2/skyhanni/events/SkillExpGainEvent.kt b/src/main/java/at/hannibal2/skyhanni/events/SkillExpGainEvent.kt index c6c7be5ccc9f..09365a7baa9c 100644 --- a/src/main/java/at/hannibal2/skyhanni/events/SkillExpGainEvent.kt +++ b/src/main/java/at/hannibal2/skyhanni/events/SkillExpGainEvent.kt @@ -1,4 +1,6 @@ package at.hannibal2.skyhanni.events +import at.hannibal2.skyhanni.features.skillprogress.SkillType + // does not know how much exp is there, also gets called multiple times -class SkillExpGainEvent(val skill: String) : LorenzEvent() +class SkillExpGainEvent(val skill: SkillType, val gained: Double) : LorenzEvent()