Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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 @@ -10,12 +10,16 @@ import tech.thatgravyboat.skyblockapi.api.events.info.CurrencyUpdateEvent
import tech.thatgravyboat.skyblockapi.api.events.info.ScoreboardUpdateEvent
import tech.thatgravyboat.skyblockapi.api.events.info.TabWidget
import tech.thatgravyboat.skyblockapi.api.events.info.TabWidgetChangeEvent
import tech.thatgravyboat.skyblockapi.api.events.misc.DebugBuilder
import tech.thatgravyboat.skyblockapi.api.location.SkyBlockIsland
import tech.thatgravyboat.skyblockapi.utils.ApiDebug
import tech.thatgravyboat.skyblockapi.utils.extentions.parseFormattedDouble
import tech.thatgravyboat.skyblockapi.utils.extentions.parseFormattedLong
import tech.thatgravyboat.skyblockapi.utils.regex.RegexGroup
import tech.thatgravyboat.skyblockapi.utils.regex.RegexUtils.anyFound
import tech.thatgravyboat.skyblockapi.utils.regex.RegexUtils.anyMatch
import kotlin.math.abs
import kotlin.math.pow
import kotlin.reflect.KMutableProperty0

enum class PurseType(scoreboardName: String? = null) {
Expand Down Expand Up @@ -45,6 +49,8 @@ object CurrencyAPI {
)
private val soulflowRegex = widgetGroup.create("profile.soulflow", " Soulflow: (?<soulflow>[\\d,.kmb]+)")
private val gemsRegex = widgetGroup.create("gems", " Gems: (?<gems>(?i)[\\d,.kmb]+)")
private val sowdustWidgetRegex = widgetGroup.create("sowdust", "(?i) Sowdust: (?<sowdust>[\\d,.]+)")
private val copperWidgetRegex = widgetGroup.create("copper", "(?i) Copper: (?<copper>[\\d,.]+)")

private val currencyGroup = RegexGroup.SCOREBOARD.group("currency")
private val purseRegex = currencyGroup.create("purse", "^(?<type>Purse|Piggy): (?<purse>(?i)[\\d,.kmb]+)")
Expand Down Expand Up @@ -107,9 +113,13 @@ object CurrencyAPI {
this.soulflow = soulflow.parseFormattedLong()
}
}

TabWidget.AREA -> {
gemsRegex.findCurrency(event.new, "gems", ::gems, CurrencyEvent::Gems)
sowdustWidgetRegex.findCurrencyChecked(event.new, "sowdust", ::sowdust, CurrencyEvent::SowDust)
copperWidgetRegex.findCurrencyChecked(event.new, "copper", ::copper, CurrencyEvent::Copper)
}

else -> return
}
}
Expand All @@ -124,8 +134,8 @@ object CurrencyAPI {
if (SkyBlockIsland.JERRYS_WORKSHOP.inIsland()) {
northStarsRegex.findCurrency(event.added, "northstars", ::northStars, CurrencyEvent::NorthStars)
} else if (SkyBlockIsland.GARDEN.inIsland()) {
copperRegex.findCurrency(event.added, "copper", ::copper, CurrencyEvent::Copper)
sowdustRegex.findCurrency(event.added, "sowdust", ::sowdust, CurrencyEvent::SowDust)
copperRegex.findCurrencyChecked(event.added, "copper", ::copper, CurrencyEvent::Copper)
sowdustRegex.findCurrencyChecked(event.added, "sowdust", ::sowdust, CurrencyEvent::SowDust)
}
purseRegex.anyFound(event.added, "type", "purse") { (type, purse) ->
this.purseType = PurseType.fromName(type)
Expand All @@ -142,6 +152,43 @@ object CurrencyAPI {
crossinline event: (Long, Long) -> CurrencyUpdateEvent<Long>,
) = anyFound(added, group) { (newValue) -> property.set(post(newValue, property(), event)) }

private val multipliers = mapOf(
'k' to 1_000.0,
'm' to 1_000_000.0,
'b' to 1_000_000_000.0,
)

private inline fun Regex.findCurrencyChecked(
added: List<String>,
group: String,
property: KMutableProperty0<Long>,
crossinline event: (Long, Long) -> CurrencyUpdateEvent<Long>,
) = anyFound(added, group) { (newValueString) ->
val current = property()
val parsed = newValueString.parseFormattedLong()

if (current == parsed) return@anyFound

val lastChar = newValueString.lastOrNull()?.lowercaseChar()
if (lastChar == null || lastChar !in multipliers.keys) {
property.set(post(parsed, current, event))
return@anyFound
}

val multiplier = multipliers[lastChar] ?: 1.0

val numericPart = newValueString.dropLast(1)
val decimalIndex = numericPart.indexOf('.')
val decimals = if (decimalIndex == -1) 0 else numericPart.length - decimalIndex - 1

val precision = (multiplier / 10.0.pow(decimals.toDouble())).toLong()
val diff = abs(current - parsed)

if (diff >= precision) {
property.set(post(parsed, current, event))
}
}

// Helper method for specifically parsing longs from a string, since most of them are like this
private inline fun post(new: String, old: Long, event: (Long, Long) -> CurrencyUpdateEvent<Long>): Long {
return post(new.parseFormattedLong(), old, event)
Expand All @@ -151,4 +198,20 @@ object CurrencyAPI {
if (new != old) event(new, old).post()
return new
}

@ApiDebug("Currency")
internal fun debug(builder: DebugBuilder) = with(builder) {
field("purse", purse)
field("purseType", purseType)
field("personalBank", personalBank)
field("coopBank", coopBank)
field("bank", bank)
field("motes", motes)
field("bits", bits)
field("copper", copper)
field("sowdust", sowdust)
field("northStars", northStars)
field("gems", gems)
field("soulflow", soulflow)
}
}