Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Add ability to manually define item changes #12

Open
wants to merge 2 commits into
base: master
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 @@ -17,6 +17,7 @@ data class ApiConfig (
val npcLocations: String = "https://raw.githubusercontent.com/Wynntils/Data-Storage/master/npc-locations.json",

val wynnItems: String = "https://api.wynncraft.com/public_api.php?action=itemDB&category=all",
val wynnItemChanges: String = "https://raw.githubusercontent.com/Wynntils/Data-Storage/master/item-changes.json",

val wynnGuildInfo: String = "https://api.wynncraft.com/public_api.php?action=guildStats&command=",

Expand Down
26 changes: 23 additions & 3 deletions src/main/kotlin/com/wynntils/athena/routes/caches/ItemListCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ class ItemListCache: DataCache {
val input = connection.getInputStream().readBytes().toString(StandardCharsets.UTF_8).asJSON<JSONObject>()
if (!input.containsKey("items")) throw UnexpectedCacheResponse()

val changelog = URL(apiConfig.wynnItemChanges).openConnection()
changelog.setRequestProperty("User-Agent", generalConfig.userAgent)
changelog.readTimeout = 20000
changelog.connectTimeout = 20000

val changelogInput = changelog.getInputStream().readBytes().toString(StandardCharsets.UTF_8).asJSON<JSONObject>()
val changes = changelogInput.getOrCreate<JSONObject>("changes")
val newItems = changelogInput.getOrCreate<JSONArray>("items")

val result = JSONOrderedObject()
val items = result.getOrCreate<JSONArray>("items")

Expand All @@ -41,13 +50,18 @@ class ItemListCache: DataCache {
for (i in originalItems) {
val item = i as JSONObject

// store all item material types
// convert item and apply changes, if necessary
val converted = ItemManager.convertItem(item)
if (changes.containsKey(converted["displayName"])) {
ItemManager.updateItem(converted, changes)
}

// store all item material types
if (converted["itemInfo"] != null) {
val itemInfo = converted["itemInfo"] as JSONOrderedObject
val typeArray = materialTypes.getOrCreate<JSONArray>(itemInfo["type"] as String);
val typeArray = materialTypes.getOrCreate<JSONArray>(itemInfo["type"] as String)

val material = itemInfo["material"];
val material = itemInfo["material"]
if (material != null && !typeArray.contains(material)) typeArray.add(material)
}

Expand All @@ -56,6 +70,12 @@ class ItemListCache: DataCache {
items.add(converted)
}

// add new items from changelog
for (i in newItems) {
val item = i as JSONObject
items.add(item)
}

val wynnBuilder = URL(apiConfig.wynnBuilderIDs).openConnection()
wynnBuilder.setRequestProperty("User-Agent", generalConfig.userAgent)
wynnBuilder.readTimeout = 20000
Expand Down
55 changes: 47 additions & 8 deletions src/main/kotlin/com/wynntils/athena/routes/managers/ItemManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,6 @@ object ItemManager {
else "PERCENTAGE"
}

fun ignoreZero(input: Any?): Any? {
if (input == null) return null

if (input is Number) return if (input.toInt() == 0) null else input
else if (input is String) return if (input.isEmpty() || input == "0-0") return null else input
return input
}

// outside tags
result["displayName"] = if (input.containsKey("displayName")) input["displayName"] else (input["name"] as String).replace("֎", "") // cancer has ֎ in it name for a random reason
result["tier"] = (input["tier"] as String).toUpperCase()
Expand Down Expand Up @@ -123,6 +115,45 @@ object ItemManager {
return result
}

fun updateItem(item: JSONOrderedObject, changelog: JSONObject) {
fun updateCategory(item: JSONOrderedObject, changes: JSONObject, category: String) {
if (changes.containsKey(category)) {
val original = item.getOrCreate<JSONOrderedObject>(category)
val changed = changes[category] as JSONObject
for (key in changed.keys) {
original[key] = ignoreZero(changed[key])
}
original.cleanNull()
}
}

val itemName = item["displayName"]
val changes = changelog[itemName] as JSONObject

updateCategory(item, changes, "requirements")
updateCategory(item, changes, "damageTypes")
updateCategory(item, changes, "defenseTypes")

// must be handled separately since structure is different
if (changes.containsKey("statuses")) {
val statuses = item.getOrCreate<JSONOrderedObject>("statuses")
val changed = changes["statuses"] as JSONObject
for (key in changed.keys) {
val changedStatus = changed[key] as JSONObject
val value = changedStatus["baseValue"] as Number
if (value.toInt() == 0) {
statuses.remove(key)
continue
}

val status = statuses.getOrCreate<JSONOrderedObject>(key as String)
status["type"] = changedStatus["type"]
status["isFixed"] = changedStatus["isFixed"]
status["baseValue"] = value
}
}
}

fun getIdentificationOrder(): JSONOrderedObject {
val result = JSONOrderedObject()

Expand Down Expand Up @@ -338,4 +369,12 @@ object ItemManager {
}
}

private fun ignoreZero(input: Any?): Any? {
if (input == null) return null

if (input is Number) return if (input.toInt() == 0) null else input
else if (input is String) return if (input.isEmpty() || input == "0-0") return null else input
return input
}

}