From 1550c5c5f63aedddfea96ffd637891ffb7449e50 Mon Sep 17 00:00:00 2001 From: GregHib Date: Sun, 4 Aug 2024 14:35:37 +0100 Subject: [PATCH] Add diango code definitions --- data/definitions/holiday-codes.yml | 3 + .../gregs/voidps/engine/EngineModules.kt | 1 + .../data/config/DiangoCodeDefinition.kt | 25 +++++++++ .../data/definition/DiangoCodeDefinitions.kt | 56 +++++++++++++++++++ .../gregs/voidps/world/map/draynor/Diango.kts | 21 +++++-- game/src/main/resources/game.properties | 1 + game/src/test/resources/test.properties | 1 + 7 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 data/definitions/holiday-codes.yml create mode 100644 engine/src/main/kotlin/world/gregs/voidps/engine/data/config/DiangoCodeDefinition.kt create mode 100644 engine/src/main/kotlin/world/gregs/voidps/engine/data/definition/DiangoCodeDefinitions.kt diff --git a/data/definitions/holiday-codes.yml b/data/definitions/holiday-codes.yml new file mode 100644 index 000000000..10e67be96 --- /dev/null +++ b/data/definitions/holiday-codes.yml @@ -0,0 +1,3 @@ +flagstaff_of_festivities: + code: flagstaff + add: [ flagstaff_of_festivities ] \ No newline at end of file diff --git a/engine/src/main/kotlin/world/gregs/voidps/engine/EngineModules.kt b/engine/src/main/kotlin/world/gregs/voidps/engine/EngineModules.kt index c19e69384..1eadb7cda 100644 --- a/engine/src/main/kotlin/world/gregs/voidps/engine/EngineModules.kt +++ b/engine/src/main/kotlin/world/gregs/voidps/engine/EngineModules.kt @@ -101,6 +101,7 @@ val engineModule = module { single(createdAtStart = true) { PrayerDefinitions().load() } single(createdAtStart = true) { GearDefinitions().load() } single(createdAtStart = true) { ItemOnItemDefinitions().load() } + single(createdAtStart = true) { DiangoCodeDefinitions().load() } single(createdAtStart = true) { AccountDefinitions().load() } single(createdAtStart = true) { HuntModeDefinitions().load() } single(createdAtStart = true) { CategoryDefinitions().load() } diff --git a/engine/src/main/kotlin/world/gregs/voidps/engine/data/config/DiangoCodeDefinition.kt b/engine/src/main/kotlin/world/gregs/voidps/engine/data/config/DiangoCodeDefinition.kt new file mode 100644 index 000000000..36ca586b0 --- /dev/null +++ b/engine/src/main/kotlin/world/gregs/voidps/engine/data/config/DiangoCodeDefinition.kt @@ -0,0 +1,25 @@ +package world.gregs.voidps.engine.data.config + +import world.gregs.voidps.engine.entity.item.Item + +/** + * Codes to redeem holiday items from django + * @param code the items code + * @param add the items added with this code + */ +data class DiangoCodeDefinition( + val code: String = "", + val add: List = emptyList(), +) { + + companion object { + + @Suppress("UNCHECKED_CAST") + operator fun invoke(map: Map) = DiangoCodeDefinition( + code = map["code"] as? String ?: EMPTY.code, + add = map["add"] as? List ?: EMPTY.add, + ) + + val EMPTY = DiangoCodeDefinition() + } +} \ No newline at end of file diff --git a/engine/src/main/kotlin/world/gregs/voidps/engine/data/definition/DiangoCodeDefinitions.kt b/engine/src/main/kotlin/world/gregs/voidps/engine/data/definition/DiangoCodeDefinitions.kt new file mode 100644 index 000000000..069a2f674 --- /dev/null +++ b/engine/src/main/kotlin/world/gregs/voidps/engine/data/definition/DiangoCodeDefinitions.kt @@ -0,0 +1,56 @@ +package world.gregs.voidps.engine.data.definition + +import com.github.michaelbull.logging.InlineLogger +import world.gregs.voidps.engine.data.config.DiangoCodeDefinition +import world.gregs.voidps.engine.entity.item.Item +import world.gregs.voidps.engine.get +import world.gregs.voidps.engine.getProperty +import world.gregs.voidps.engine.timedLoad +import world.gregs.yaml.Yaml +import world.gregs.yaml.read.YamlReaderConfiguration + +class DiangoCodeDefinitions { + + private lateinit var definitions: Map + + fun get(code: String) = definitions[code] ?: DiangoCodeDefinition.EMPTY + + @Suppress("UNCHECKED_CAST") + fun load(yaml: Yaml = get(), path: String = getProperty("diangoCodeDefinitionsPath"), itemDefinitions: ItemDefinitions? = null): DiangoCodeDefinitions { + timedLoad("diango code definition") { + val config = object : YamlReaderConfiguration(2, 2) { + override fun add(list: MutableList, value: Any, parentMap: String?) { + super.add(list, if (value is Map<*, *>) { + val id = value["item"] as String + if (itemDefinitions != null && !itemDefinitions.contains(id)) { + logger.warn { "Invalid diango item id: $id" } + } + Item(id, value["amount"] as? Int ?: 1) + } else { + Item(value as String, amount = 1) + }, parentMap) + } + override fun set(map: MutableMap, key: String, value: Any, indent: Int, parentMap: String?) { + if (key == "<<") { + map.putAll(value as Map) + return + } + if (indent == 0) { + super.set(map, key, DiangoCodeDefinition(value as Map), indent, parentMap) + } else { + super.set(map, key, value, indent, parentMap) + } + } + } + val definitions = yaml.load(path, config) as Map + this.definitions = definitions + definitions.size + } + return this + } + + companion object { + private val logger = InlineLogger() + } + +} \ No newline at end of file diff --git a/game/src/main/kotlin/world/gregs/voidps/world/map/draynor/Diango.kts b/game/src/main/kotlin/world/gregs/voidps/world/map/draynor/Diango.kts index a1c0c0aa5..c42bf90c1 100644 --- a/game/src/main/kotlin/world/gregs/voidps/world/map/draynor/Diango.kts +++ b/game/src/main/kotlin/world/gregs/voidps/world/map/draynor/Diango.kts @@ -2,9 +2,11 @@ package world.gregs.voidps.world.map.draynor import world.gregs.voidps.engine.client.message import world.gregs.voidps.engine.client.ui.open +import world.gregs.voidps.engine.data.definition.DiangoCodeDefinitions import world.gregs.voidps.engine.entity.character.npc.npcOperate -import world.gregs.voidps.engine.inv.add +import world.gregs.voidps.engine.inject import world.gregs.voidps.engine.inv.inventory +import world.gregs.voidps.engine.inv.transact.operation.AddItem.add import world.gregs.voidps.world.interact.dialogue.Chuckle import world.gregs.voidps.world.interact.dialogue.Happy import world.gregs.voidps.world.interact.dialogue.Neutral @@ -36,12 +38,21 @@ npcOperate("Holiday-items", "diango") { player.open("diangos_item_retrieval") } +val codeDefinitions: DiangoCodeDefinitions by inject() + npcOperate("Redeem-code", "diango") { val code = stringEntry("Please enter your code.") - if (code == "flagstaff") { - player.inventory.add("flagstaff_of_festivities") - player.message("Your code has been succesfully processed.") - } else { + val definition = codeDefinitions.get(code) + if (definition.code.isBlank()) { player.message("Your code was not valid. Please check it and try again.") + return@npcOperate + } + val success = player.inventory.transaction { + for (item in definition.add) { + add(item.id, item.amount) + } + } + if (success) { + player.message("Your code has been successfully processed.") } } \ No newline at end of file diff --git a/game/src/main/resources/game.properties b/game/src/main/resources/game.properties index 781c724a0..65a1a8a44 100644 --- a/game/src/main/resources/game.properties +++ b/game/src/main/resources/game.properties @@ -67,6 +67,7 @@ spellDefinitionsPath=./data/definitions/spells.yml patrolDefinitionsPath=./data/definitions/patrols.yml prayerDefinitionsPath=./data/definitions/prayers.yml itemOnItemDefinitionsPath=./data/definitions/item-on-item.yml +diangoCodeDefinitionsPath=./data/definitions/holiday-codes.yml gearDefinitionsPath=./data/definitions/gear-sets.yml enumDefinitionsPath=./data/definitions/enums.yml structDefinitionsPath=./data/definitions/structs.yml diff --git a/game/src/test/resources/test.properties b/game/src/test/resources/test.properties index bdfecc8e0..18d61c085 100644 --- a/game/src/test/resources/test.properties +++ b/game/src/test/resources/test.properties @@ -30,6 +30,7 @@ renderEmoteDefinitionsPath=../data/definitions/render-emotes.yml midiDefinitionsPath=../data/definitions/midis.yml jingleDefinitionsPath=../data/definitions/jingles.yml itemOnItemDefinitionsPath=../data/definitions/item-on-item.yml +diangoCodeDefinitionsPath=../data/definitions/holiday-codes.yml spellDefinitionsPath=../data/definitions/spells.yml patrolDefinitionsPath=../data/definitions/patrols.yml prayerDefinitionsPath=../data/definitions/prayers.yml