Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backend: Bits Event #2815

Open
wants to merge 4 commits into
base: beta
Choose a base branch
from
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 @@ -21,6 +21,9 @@ class PlayerSpecificStorage {
@Expose
var gardenCommunityUpgrade: Int = -1

@Expose
var fameRank: String = "New Player"

@Expose
var nextCityProjectParticipationTime: SimpleTimeMark = getSimpleTimeMark(farPast()).it

Expand Down
Empty file.
100 changes: 52 additions & 48 deletions src/main/java/at/hannibal2/skyhanni/data/BitsAPI.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package at.hannibal2.skyhanni.data

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.data.FameRanks.getFameRankByNameOrNull
import at.hannibal2.skyhanni.events.BitsAvailableUpdateEvent
import at.hannibal2.skyhanni.events.BitsUpdateEvent
import at.hannibal2.skyhanni.events.InventoryFullyOpenedEvent
import at.hannibal2.skyhanni.events.LorenzChatEvent
Expand All @@ -29,18 +28,18 @@ import kotlin.time.Duration.Companion.days
@SkyHanniModule
object BitsAPI {
private val profileStorage get() = ProfileStorageData.profileSpecific?.bits
private val playerStorage get() = SkyHanniMod.feature.storage
private val playerStorage get() = ProfileStorageData.playerSpecific

var bits: Int
get() = profileStorage?.bits ?: 0
private set(value) {
profileStorage?.bits = value
}
private var currentFameRank: FameRank?
get() = getFameRankByNameOrNull(playerStorage.currentFameRank)
var fameRank: FameRank?
get() = playerStorage?.fameRank?.let(FameRanks::getByInternalName)
private set(value) {
if (value != null) {
playerStorage.currentFameRank = value.name
playerStorage?.fameRank = value.internalName
}
}
var bitsAvailable: Int
Expand All @@ -55,7 +54,7 @@ object BitsAPI {
profileStorage?.boosterCookieExpiryTime = value
}

private const val defaultCookieBits = 4800
private const val DEFAULT_COOKIE_BITS = 4800

private val bitsDataGroup = RepoPattern.group("data.bits")

Expand Down Expand Up @@ -201,15 +200,17 @@ object BitsAPI {
}
}

private fun updateBits(bits: Int, modifyAvailable: Boolean = true) {
if (bits > this.bits) {
val difference = bits - this.bits
if (modifyAvailable) bitsAvailable -= difference
this.bits = bits
sendBitsGainEvent(difference)
private fun updateBits(amount: Int) {
val diff = amount - bits
if (diff == 0) return

if (diff > 0) {
bitsAvailable -= diff
bits = amount
sendBitsGainEvent(diff)
} else {
this.bits = bits
sendBitsSpentEvent()
bits = amount
sendBitsSpentEvent(diff)
}
}

Expand All @@ -229,13 +230,13 @@ object BitsAPI {
fameRankUpPattern.matchMatcher(message) {
val rank = group("rank")

currentFameRank = getFameRankByNameOrNull(rank)
fameRank = FameRanks.getByName(rank)
?: return ErrorManager.logErrorWithData(
FameRankNotFoundException(rank),
"FameRank $rank not found",
"Rank" to rank,
"Message" to message,
"FameRanks" to FameRanks.fameRanks,
"FameRanks" to FameRanks.fameRanksMap,
)

return
Expand All @@ -250,7 +251,7 @@ object BitsAPI {
}
}

fun bitsPerCookie(): Int = (defaultCookieBits * (currentFameRank?.bitsMultiplier ?: 1.0)).toInt()
fun bitsPerCookie(): Int = (DEFAULT_COOKIE_BITS * (fameRank?.bitsMultiplier ?: 1.0)).toInt()

@SubscribeEvent
fun onInventoryOpen(event: InventoryFullyOpenedEvent) {
Expand Down Expand Up @@ -296,42 +297,44 @@ object BitsAPI {
var foundFameRankStack = false
var foundBitsStack = false
var foundCookieStack = false
items@ for (item in stacks.values.reversed()) {
items@ for (item in stacks.values.toList().asReversed()) {
if (foundFameRankStack && foundBitsStack && foundCookieStack) return
if (!foundFameRankStack && fameRankGuiStackPattern.matches(item.displayName)) {
foundFameRankStack = true
lore@ for (line in item.getLore()) {
fameRankCommunityShopPattern.matchMatcher(line) {
val rank = group("rank")

currentFameRank = getFameRankByNameOrNull(rank)
?: return ErrorManager.logErrorWithData(
FameRankNotFoundException(rank),
"FameRank $rank not found",
"Rank" to rank,
"Lore" to item.getLore(),
"FameRanks" to FameRanks.fameRanks,
)

continue@lore
}
for (pattern in listOf(fameRankCommunityShopPattern, fameRankSbMenuPattern)) {
pattern.matchMatcher(line) {
val rank = group("rank")

fameRank = FameRanks.getByName(rank)
?: return ErrorManager.logErrorWithData(
FameRankNotFoundException(rank),
"FameRank $rank not found",
"Rank" to rank,
"Lore" to item.getLore(),
"FameRanks" to FameRanks.fameRanksMap,
)

continue@lore
}

fameRankSbMenuPattern.matchMatcher(line) {
val rank = group("rank")
fameRankSbMenuPattern.matchMatcher(line) {
val rank = group("rank")

currentFameRank = getFameRankByNameOrNull(rank)
?: return ErrorManager.logErrorWithData(
FameRankNotFoundException(rank),
"FameRank $rank not found",
"Rank" to rank,
"Lore" to item.getLore(),
"FameRanks" to FameRanks.fameRanks,
)
fameRank = FameRanks.getByName(rank)
?: return ErrorManager.logErrorWithData(
FameRankNotFoundException(rank),
"FameRank $rank not found",
"Rank" to rank,
"Lore" to item.getLore(),
"FameRanks" to FameRanks.fameRanksMap,
)

continue@lore
continue@lore
}
}
continue@items
}
continue@items
}
if (!foundBitsStack && bitsStackPattern.matches(item.displayName)) {
foundBitsStack = true
Expand All @@ -342,7 +345,7 @@ object BitsAPI {
if (!foundBits) bitsPurseMenuPattern.findMatcher(line) {
foundBits = true
val amount = group("amount").formatInt()
updateBits(amount, false)
updateBits(amount)

continue@lore
}
Expand Down Expand Up @@ -386,8 +389,9 @@ object BitsAPI {
private fun sendBitsGainEvent(difference: Int) =
BitsUpdateEvent.BitsGain(bits, bitsAvailable, difference).post()

private fun sendBitsSpentEvent() = BitsUpdateEvent.BitsSpent(bits, bitsAvailable).post()
private fun sendBitsAvailableGainedEvent() = BitsUpdateEvent.BitsAvailableGained(bits, bitsAvailable).post()
private fun sendBitsSpentEvent(difference: Int) =
BitsUpdateEvent.BitsSpent(bits, bitsAvailable, difference).post()
private fun sendBitsAvailableGainedEvent() = BitsAvailableUpdateEvent(bitsAvailable).post()

fun isEnabled() = LorenzUtils.inSkyBlock && !LorenzUtils.isOnAlphaServer && profileStorage != null

Expand Down
14 changes: 9 additions & 5 deletions src/main/java/at/hannibal2/skyhanni/data/FameRanks.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,26 @@ import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

@SkyHanniModule
object FameRanks {
var fameRanks = emptyMap<String, FameRank>()
var fameRanksMap = mapOf<String, FameRank>()
private set

fun getFameRankByNameOrNull(name: String) = fameRanks[name]
fun getByName(name: String) = fameRanksMap.values.find { it.name.equals(name, true) }

fun getByInternalName(internalName: String) = fameRanksMap[internalName]

@SubscribeEvent
fun onRepoReload(event: RepositoryReloadEvent) {
val ranks = event.getConstant<FameRankJson>("FameRank")
fameRanks = ranks.fameRank.values.map { FameRank(it.name, it.fameRequired, it.bitsMultiplier, it.votes) }
.associateBy { it.name }
fameRanksMap = ranks.fameRank.map { (internalName, rank) ->
FameRank(rank.name, rank.fameRequired, rank.bitsMultiplier, rank.votes, internalName)
}.associateBy(FameRank::internalName)
}
}

data class FameRank(
val name: String,
val fameRequired: Int,
val bitsMultiplier: Double,
val electionVotes: Int
val electionVotes: Int,
val internalName: String,
)
7 changes: 4 additions & 3 deletions src/main/java/at/hannibal2/skyhanni/events/BitsUpdateEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package at.hannibal2.skyhanni.events

import at.hannibal2.skyhanni.api.event.SkyHanniEvent

open class BitsUpdateEvent(val bits: Int, val bitsAvailable: Int, val difference: Int = 0) : SkyHanniEvent() {
open class BitsUpdateEvent(val bits: Int, val bitsAvailable: Int, val difference: Int) : SkyHanniEvent() {
class BitsGain(bits: Int, bitsAvailable: Int, difference: Int) : BitsUpdateEvent(bits, bitsAvailable, difference)
class BitsSpent(bits: Int, bitsAvailable: Int) : BitsUpdateEvent(bits, bitsAvailable)
class BitsAvailableGained(bits: Int, bitsAvailable: Int) : BitsUpdateEvent(bits, bitsAvailable)
class BitsSpent(bits: Int, bitsAvailable: Int, difference: Int) : BitsUpdateEvent(bits, bitsAvailable, difference)
}

class BitsAvailableUpdateEvent(val bitsAvailable: Int) : SkyHanniEvent()
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ object NoBitsWarning {

@HandleEvent
fun onBitsGain(event: BitsUpdateEvent.BitsGain) {
if (isWarningEnabled() && event.bitsAvailable == 0) {
if (config.bitsGainChatMessage && event.bitsAvailable == 0) {

ChatUtils.clickableChat(
"§bNo Bits Available! §eClick to buy booster cookies on the bazaar.",
Expand All @@ -33,7 +33,7 @@ object NoBitsWarning {
if (config.notificationSound) SoundUtils.repeatSound(100, 10, createSound("note.pling", 0.6f))
}

if (isChatMessageEnabled()) {
if (config.enableWarning) {
if (event.bits < config.threshold) return
ChatUtils.chat("You have gained §b${event.difference.addSeparators()} §eBits.")
}
Expand All @@ -45,7 +45,4 @@ object NoBitsWarning {
event.move(40, "misc.noBitsWarning.enabled", "misc.bits.enableWarning")
event.move(40, "misc.noBitsWarning.notificationSound", "misc.bits.notificationSound")
}

private fun isChatMessageEnabled() = LorenzUtils.inSkyBlock && config.bitsGainChatMessage
private fun isWarningEnabled() = LorenzUtils.inSkyBlock && config.enableWarning
}
Loading