Skip to content

Commit

Permalink
Backend: Use SkillAPI in SkillExpGainEvent (#1788)
Browse files Browse the repository at this point in the history
Co-authored-by: CalMWolfs <94038482+CalMWolfs@users.noreply.github.com>
  • Loading branch information
superhize and CalMWolfs authored May 30, 2024
1 parent 23ce43b commit 2e44956
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 44 deletions.
96 changes: 64 additions & 32 deletions src/main/java/at/hannibal2/skyhanni/api/SkillAPI.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -53,11 +55,15 @@ object SkillAPI {
)
private val skillTabPattern by patternGroup.pattern(
"skill.tab",
"(?:§e§lSkills: §r§a)?\\s*(?<type>\\w+) (?<level>\\d+): §r§.(?<progress>.+)%$"
" (?<type>\\w+)(?: (?<level>\\d+))?: §r§a(?<progress>[0-9.]+)%"
)
private val maxSkillTabPattern by patternGroup.pattern(
"skill.tab.max",
"(?:§e§lSkills: §r§a)?\\s*(?<type>\\w+) (?<level>\\d+): §r§c§lMAX\$"
" (?<type>\\w+) (?<level>\\d+): §r§c§lMAX"
)
private val skillTabNoPercentPattern by patternGroup.pattern(
"skill.tab.nopercent",
" §r§a(?<type>\\w+)(?: (?<level>\\d+))?: §r§e(?<current>[0-9,.]+)§r§6/§r§e(?<needed>[0-9kmb]+)"
)

var skillXPInfoMap = mutableMapOf<SkillType, SkillXPInfo>()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand Down
11 changes: 0 additions & 11 deletions src/main/java/at/hannibal2/skyhanni/data/SkillExperience.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,10 +23,6 @@ class SkillExperience {
"inventory",
".* §e(?<number>.*)§6/.*"
)
private val actionBarLowLevelPattern by patternGroup.pattern(
"actionbarlow",
".*§3+(?<add>.+) (?<skill>.*) \\((?<percentage>.*)%\\).*"
)

@SubscribeEvent
fun onProfileJoin(event: ProfileJoinEvent) {
Expand All @@ -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()
}
}

Expand Down Expand Up @@ -186,4 +176,3 @@ class SkillExperience {
)
}
}

Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 2e44956

Please sign in to comment.