Skip to content

Commit

Permalink
Add diango code definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
GregHib committed Aug 4, 2024
1 parent accf2c5 commit 1550c5c
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 5 deletions.
3 changes: 3 additions & 0 deletions data/definitions/holiday-codes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
flagstaff_of_festivities:
code: flagstaff
add: [ flagstaff_of_festivities ]
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Item> = emptyList(),
) {

companion object {

@Suppress("UNCHECKED_CAST")
operator fun invoke(map: Map<String, Any>) = DiangoCodeDefinition(
code = map["code"] as? String ?: EMPTY.code,
add = map["add"] as? List<Item> ?: EMPTY.add,
)

val EMPTY = DiangoCodeDefinition()
}
}
Original file line number Diff line number Diff line change
@@ -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<String, DiangoCodeDefinition>

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<Any>, 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<String, Any>, key: String, value: Any, indent: Int, parentMap: String?) {
if (key == "<<") {
map.putAll(value as Map<String, Any>)
return
}
if (indent == 0) {
super.set(map, key, DiangoCodeDefinition(value as Map<String, Any>), indent, parentMap)
} else {
super.set(map, key, value, indent, parentMap)
}
}
}
val definitions = yaml.load<Any>(path, config) as Map<String, DiangoCodeDefinition>
this.definitions = definitions
definitions.size
}
return this
}

companion object {
private val logger = InlineLogger()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.")
}
}
1 change: 1 addition & 0 deletions game/src/main/resources/game.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions game/src/test/resources/test.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 1550c5c

Please sign in to comment.