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: De-Duplication #3063

Open
wants to merge 7 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
@@ -1,6 +1,7 @@
package at.hannibal2.skyhanni.data.jsonobjects.repo

import at.hannibal2.skyhanni.utils.NEUInternalName
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName
import com.google.gson.annotations.Expose
import com.google.gson.annotations.SerializedName

Expand All @@ -11,4 +12,18 @@ data class ItemsJson(
@Expose @SerializedName("lava_fishing_rods") val lavaFishingRods: List<NEUInternalName>,
@Expose @SerializedName("water_fishing_rods") val waterFishingRods: List<NEUInternalName>,
@Expose @SerializedName("book_bundle_amount") val bookBundleAmount: Map<String, Int>,
@Expose @SerializedName("value_calculation_data") val valueCalculationData: ItemValueCalculationDataJson,
)

data class ItemValueCalculationDataJson(
@Expose @SerializedName("always_active_enchants") val alwaysActiveEnchants: Map<String, AlwaysActiveEnchantJson>,
@Expose @SerializedName("only_tier_one_prices") val onlyTierOnePrices: List<String>,
@Expose @SerializedName("only_tier_five_prices") val onlyTierFivePrices: List<String>,
)

data class AlwaysActiveEnchantJson(
@Expose val level: Int,
@Expose val items: List<String>,
) {
val internalNames = items.map { it.toInternalName() }
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ import at.hannibal2.skyhanni.events.InventoryOpenEvent
import at.hannibal2.skyhanni.events.InventoryUpdatedEvent
import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent
import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent
import at.hannibal2.skyhanni.features.inventory.EssenceShopHelper.essenceUpgradePattern
import at.hannibal2.skyhanni.features.inventory.EssenceShopHelper.maxedUpgradeLorePattern
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.EssenceUtils
import at.hannibal2.skyhanni.utils.ItemUtils.createItemStack
import at.hannibal2.skyhanni.utils.ItemUtils.getLore
import at.hannibal2.skyhanni.utils.LorenzUtils
import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal
import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.groupOrNull
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import net.minecraft.item.ItemStack
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
Expand All @@ -36,6 +33,13 @@ object CarnivalShopHelper {
private const val CUSTOM_STACK_LOCATION = 8
private val NAME_TAG_ITEM by lazy { "NAME_TAG".toInternalName().getItemStack().item }

/**
* All upgrades will appear in slots 11 -> 15
*
* Filter out items outside of these bounds
*/
private val SLOT_RANGE = 11..15

private var repoEventShops = mutableListOf<EventShop>()
private var currentProgress: EventShopProgress? = null
private var currentEventType: String = ""
Expand Down Expand Up @@ -246,26 +250,7 @@ object CarnivalShopHelper {
}

private fun processEventShopUpgrades(inventoryItems: Map<Int, ItemStack>) {
/**
* All upgrades will appear in slots 11 -> 15
*
* Filter out items outside of these bounds
*/
val upgradeStacks = inventoryItems.filter { it.key in 11..15 && it.value.item != null }
// TODO remove duplicate code fragment with EssenceShopHelper
val purchasedUpgrades: MutableMap<String, Int> = buildMap {
for (value in upgradeStacks.values) {
// Right now Carnival and Essence Upgrade patterns are 'in-sync'
// This may change in the future, and this would then need its own pattern
essenceUpgradePattern.matchMatcher(value.displayName) {
val upgradeName = groupOrNull("upgrade") ?: return
val nextUpgradeRoman = groupOrNull("tier") ?: return
val nextUpgrade = nextUpgradeRoman.romanToDecimal()
val isMaxed = value.getLore().any { loreLine -> maxedUpgradeLorePattern.matches(loreLine) }
put(upgradeName, if (isMaxed) nextUpgrade else nextUpgrade - 1)
}
}
}.toMutableMap()
val purchasedUpgrades = EssenceUtils.extractPurchasedUpgrades(inventoryItems, SLOT_RANGE)
currentProgress = EventShopProgress(currentEventType, purchasedUpgrades)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import at.hannibal2.skyhanni.events.render.gui.ReplaceItemEvent
import at.hannibal2.skyhanni.features.inventory.bazaar.BazaarApi
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.EssenceUtils
import at.hannibal2.skyhanni.utils.ItemPriceSource
import at.hannibal2.skyhanni.utils.ItemPriceUtils.getPrice
import at.hannibal2.skyhanni.utils.ItemUtils.createItemStack
Expand All @@ -23,7 +24,6 @@ import at.hannibal2.skyhanni.utils.NEUInternalName.Companion.toInternalName
import at.hannibal2.skyhanni.utils.NEUItems.getItemStack
import at.hannibal2.skyhanni.utils.NumberUtil.addSeparators
import at.hannibal2.skyhanni.utils.NumberUtil.formatInt
import at.hannibal2.skyhanni.utils.NumberUtil.romanToDecimal
import at.hannibal2.skyhanni.utils.RegexUtils.firstMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.groupOrNull
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
Expand All @@ -40,6 +40,21 @@ object EssenceShopHelper {
private const val CUSTOM_STACK_LOCATION = 8
private val GOLD_NUGGET_ITEM by lazy { "GOLD_NUGGET".toInternalName().getItemStack().item }

/**
* Essence Upgrade Bounds
* Undead -> 10 to 20
* Wither -> 10 to 16
* Dragon -> 19 to 33
* Spider -> 19 to 25
* Crimson -> 20 to 33
* Ice -> 21 to 32
* Gold -> 19 to 25
* Diamond -> 19 to 25
*
* Filter out items that fall outside the bounds of 10 - 33
*/
private val SLOT_RANGE = 10..33

private var essenceShops = mutableListOf<EssenceShop>()
private var currentProgress: EssenceShopProgress? = null
private var currentEssenceType: String = ""
Expand Down Expand Up @@ -240,34 +255,7 @@ object EssenceShopHelper {
}

private fun processEssenceShopUpgrades(essenceName: String, inventoryItems: Map<Int, ItemStack>) {
/**
* Essence Upgrade Bounds
* Undead -> 10 to 20
* Wither -> 10 to 16
* Dragon -> 19 to 33
* Spider -> 19 to 25
* Crimson -> 20 to 33
* Ice -> 21 to 32
* Gold -> 19 to 25
* Diamond -> 19 to 25
*
* Filter out items that fall outside the bounds of 10 - 33
*/
val upgradeStacks = inventoryItems.filter { it.key in 10..33 && it.value.item != null }
// TODO remove duplicate code fragment with CarnivalShopHelper
val purchasedUpgrades: MutableMap<String, Int> = buildMap {
for (value in upgradeStacks.values) {
// Right now Carnival and Essence Upgrade patterns are 'in-sync'
// This may change in the future, and this would then need its own pattern
essenceUpgradePattern.matchMatcher(value.displayName) {
val upgradeName = groupOrNull("upgrade") ?: return
val nextUpgradeRoman = groupOrNull("tier") ?: return
val nextUpgrade = nextUpgradeRoman.romanToDecimal()
val isMaxed = value.getLore().any { loreLine -> maxedUpgradeLorePattern.matches(loreLine) }
put(upgradeName, if (isMaxed) nextUpgrade else nextUpgrade - 1)
}
}
}.toMutableMap()
val purchasedUpgrades = EssenceUtils.extractPurchasedUpgrades(inventoryItems, SLOT_RANGE)
currentProgress = EssenceShopProgress(essenceName, purchasedUpgrades)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.ConfigUpdaterMigrator
import at.hannibal2.skyhanni.config.features.misc.EstimatedItemValueConfig
import at.hannibal2.skyhanni.data.jsonobjects.repo.ItemValueCalculationDataJson
import at.hannibal2.skyhanni.data.jsonobjects.repo.ItemsJson
import at.hannibal2.skyhanni.events.ConfigLoadEvent
import at.hannibal2.skyhanni.events.GuiRenderEvent
Expand Down Expand Up @@ -49,6 +50,9 @@ object EstimatedItemValue {
var bookBundleAmount = mapOf<String, Int>()
private var currentlyShowing = false

var itemValueCalculationData: ItemValueCalculationDataJson? = null
private set

fun isCurrentlyShowing() = currentlyShowing && Minecraft.getMinecraft().currentScreen != null

@HandleEvent
Expand All @@ -61,6 +65,7 @@ object EstimatedItemValue {
fun onRepoReload(event: RepositoryReloadEvent) {
val data = event.getConstant<ItemsJson>("Items")
bookBundleAmount = data.bookBundleAmount
itemValueCalculationData = data.valueCalculationData
}

@HandleEvent(onlyOnSkyblock = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package at.hannibal2.skyhanni.features.misc.items

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.ReforgeAPI
import at.hannibal2.skyhanni.features.misc.discordrpc.DiscordRPCManager
import at.hannibal2.skyhanni.features.nether.kuudra.KuudraAPI
import at.hannibal2.skyhanni.features.nether.kuudra.KuudraAPI.getKuudraTier
import at.hannibal2.skyhanni.features.nether.kuudra.KuudraAPI.isKuudraArmor
Expand All @@ -10,8 +11,8 @@ import at.hannibal2.skyhanni.test.command.ErrorManager
import at.hannibal2.skyhanni.utils.CollectionUtils.addOrPut
import at.hannibal2.skyhanni.utils.CollectionUtils.sorted
import at.hannibal2.skyhanni.utils.CollectionUtils.sortedDesc
import at.hannibal2.skyhanni.utils.EssenceItemUtils
import at.hannibal2.skyhanni.utils.EssenceItemUtils.getEssencePrices
import at.hannibal2.skyhanni.utils.EssenceUtils
import at.hannibal2.skyhanni.utils.EssenceUtils.getEssencePrices
import at.hannibal2.skyhanni.utils.ItemPriceUtils.getNpcPriceOrNull
import at.hannibal2.skyhanni.utils.ItemPriceUtils.getPriceOrNull
import at.hannibal2.skyhanni.utils.ItemPriceUtils.getRawCraftCostOrNull
Expand Down Expand Up @@ -491,7 +492,7 @@ object EstimatedItemValueCalculator {
private fun calculateStarPrice(
internalName: NEUInternalName,
inputStars: Int,
): Pair<EssenceItemUtils.EssenceUpgradePrice, Pair<Int, Int>>? {
): Pair<EssenceUtils.EssenceUpgradePrice, Pair<Int, Int>>? {
var totalStars = inputStars
val (price, maxStars) = if (internalName.isKuudraArmor()) {
val tier = (internalName.getKuudraTier() ?: 0) - 1
Expand All @@ -501,17 +502,17 @@ object EstimatedItemValueCalculator {

val removed = internalName.removeKuudraTier().asString()
var maxStars = 0
var finalPrice: EssenceItemUtils.EssenceUpgradePrice? = null
var finalPrice: EssenceUtils.EssenceUpgradePrice? = null

val tiers = mutableMapOf<NEUInternalName, Int>()

for ((id, _) in EssenceItemUtils.itemPrices) {
for ((id, _) in EssenceUtils.itemPrices) {
if (!id.contains(removed)) continue
tiers[id] = (id.getKuudraTier() ?: 0) - 1

}
for ((id, _) in tiers.sorted()) {
val prices = EssenceItemUtils.itemPrices[id].orEmpty()
val prices = EssenceUtils.itemPrices[id].orEmpty()
maxStars += prices.size
if (remainingStars <= 0) continue

Expand All @@ -523,9 +524,8 @@ object EstimatedItemValueCalculator {

finalPrice to maxStars
} else {
if (totalStars == 0) return null

val prices = internalName.getEssencePrices() ?: return null
val prices = internalName.getEssencePrices()
if (totalStars == 0 || prices == null) return null

(getPriceFor(prices, totalStars) ?: return null) to prices.size
}
Expand All @@ -535,10 +535,10 @@ object EstimatedItemValueCalculator {
}

private fun getPriceFor(
prices: Map<Int, EssenceItemUtils.EssenceUpgradePrice>,
prices: Map<Int, EssenceUtils.EssenceUpgradePrice>,
totalStars: Int,
): EssenceItemUtils.EssenceUpgradePrice? {
var totalEssencePrice: EssenceItemUtils.EssencePrice? = null
): EssenceUtils.EssenceUpgradePrice? {
var totalEssencePrice: EssenceUtils.EssencePrice? = null
var totalCoinPrice = 0L
val totalItemPrice = mutableMapOf<NEUInternalName, Int>()

Expand All @@ -555,7 +555,7 @@ object EstimatedItemValueCalculator {
}
}
totalEssencePrice ?: return null
return EssenceItemUtils.EssenceUpgradePrice(totalEssencePrice, totalCoinPrice, totalItemPrice)
return EssenceUtils.EssenceUpgradePrice(totalEssencePrice, totalCoinPrice, totalItemPrice)
}

private fun addMasterStars(stack: ItemStack, list: MutableList<String>): Double {
Expand Down Expand Up @@ -587,18 +587,27 @@ object EstimatedItemValueCalculator {
return price
}

private fun addDrillUpgrades(stack: ItemStack, list: MutableList<String>): Double {
val drillUpgrades = stack.getDrillUpgrades() ?: return 0.0

private fun getMapAndTotalFromExtra(
extraList: List<NEUInternalName>
): Pair<Double, MutableMap<String, Double>> {
var totalPrice = 0.0
val map = mutableMapOf<String, Double>()
for (internalName in drillUpgrades) {
for (internalName in extraList) {
val name = internalName.itemName
val price = internalName.getPriceOrNull(config.priceSource.get()) ?: continue

totalPrice += price
val format = price.shortFormat()
map[" $name §7(§6$format§7)"] = price
}
val totalPrice = map.values.sum()
return totalPrice to map
}


private fun addDrillUpgrades(stack: ItemStack, list: MutableList<String>): Double {
val drillUpgrades = stack.getDrillUpgrades() ?: return 0.0

val (totalPrice, map) = getMapAndTotalFromExtra(drillUpgrades)
if (map.isNotEmpty()) {
list.add("§7Drill upgrades: §6" + totalPrice.shortFormat())
list += map.sortedDesc().keys
Expand Down Expand Up @@ -666,15 +675,7 @@ object EstimatedItemValueCalculator {
private fun addAbilityScrolls(stack: ItemStack, list: MutableList<String>): Double {
val abilityScrolls = stack.getAbilityScrolls() ?: return 0.0

val map = mutableMapOf<String, Double>()
for (internalName in abilityScrolls) {
val name = internalName.itemName
val price = internalName.getPriceOrNull(config.priceSource.get()) ?: continue

val format = price.shortFormat()
map[" $name §7(§6$format§7)"] = price
}
val totalPrice = map.values.sum()
val (totalPrice, map) = getMapAndTotalFromExtra(abilityScrolls)
if (map.isNotEmpty()) {
list.add("§7Ability Scrolls: §6" + totalPrice.shortFormat())
list += map.sortedDesc().keys
Expand Down Expand Up @@ -720,51 +721,24 @@ object EstimatedItemValueCalculator {
return price
}

// TODO repo
private val hasAlwaysScavenger = listOf(
"CRYPT_DREADLORD_SWORD".toInternalName(),
"ZOMBIE_SOLDIER_CUTLASS".toInternalName(),
"CONJURING_SWORD".toInternalName(),
"EARTH_SHARD".toInternalName(),
"ZOMBIE_KNIGHT_SWORD".toInternalName(),
"SILENT_DEATH".toInternalName(),
"ZOMBIE_COMMANDER_WHIP".toInternalName(),
"ICE_SPRAY_WAND".toInternalName(),
)

private val hasAlwaysReplenish = listOf(
"ADVANCED_GARDENING_HOE".toInternalName(),
"ADVANCED_GARDENING_AXE".toInternalName()
)

private fun addEnchantments(stack: ItemStack, list: MutableList<String>): Double {
val enchantments = stack.getEnchantments() ?: return 0.0

val map = mutableMapOf<String, Double>()

// todo use repo
val tieredEnchants = listOf("compact", "cultivating", "champion", "expertise", "hecatomb", "toxophilite")

@Suppress("PropertyWrapping")
val onlyTierOnePrices = listOf("ultimate_chimera", "ultimate_fatal_tempo", "smoldering", "ultimate_flash", "divine_gift")
val onlyTierFivePrices = listOf("ferocious_mana", "hardened_mana", "mana_vampire", "strong_mana")

val internalName = stack.getInternalName()
for ((rawName, rawLevel) in enchantments) {
// efficiency 1-5 is cheap, 6-10 is handled by silex
if (rawName == "efficiency") continue

if (rawName == "scavenger" && rawLevel == 5 && internalName in hasAlwaysScavenger) {
continue
}

if (rawName == "replenish" && rawLevel == 1 && internalName in hasAlwaysReplenish) {
continue
val isAlwaysActive = EstimatedItemValue.itemValueCalculationData?.alwaysActiveEnchants.orEmpty().entries.any {
it.key == rawName && it.value.level == rawLevel && it.value.internalNames.contains(internalName)
}
if (isAlwaysActive) continue

var level = rawLevel
var multiplier = 1
if (rawName in onlyTierOnePrices) {
if (rawName in EstimatedItemValue.itemValueCalculationData?.onlyTierOnePrices.orEmpty()) {

when (rawLevel) {
2 -> multiplier = 2
Expand All @@ -774,7 +748,7 @@ object EstimatedItemValueCalculator {
}
level = 1
}
if (rawName in onlyTierFivePrices) {
if (rawName in EstimatedItemValue.itemValueCalculationData?.onlyTierFivePrices.orEmpty()) {
when (rawLevel) {
6 -> multiplier = 2
7 -> multiplier = 4
Expand All @@ -789,7 +763,7 @@ object EstimatedItemValueCalculator {
if (internalName.startsWith("ENCHANTED_BOOK_BUNDLE_")) {
multiplier = EstimatedItemValue.bookBundleAmount.getOrDefault(rawName, 5)
}
if (rawName in tieredEnchants) level = 1
if (rawName in DiscordRPCManager.stackingEnchants.keys) level = 1

val enchantmentName = "$rawName;$level".toInternalName()
val singlePrice = enchantmentName.getPriceOrNull(config.priceSource.get()) ?: continue
Expand Down
Loading
Loading