Add inventory managment to server.

This commit is contained in:
Thomas Andres Gomez 2025-04-12 22:36:25 +02:00
parent 04b203239d
commit 4f33492b23
18 changed files with 499 additions and 7 deletions

View file

@ -7,6 +7,8 @@ import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonV1Factory
import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonV2Factory
import com.pixelized.shared.lwa.model.characterSheet.factory.CharacterSheetJsonFactory
import com.pixelized.shared.lwa.model.characterSheet.factory.CharacterSheetJsonV1Factory
import com.pixelized.shared.lwa.model.inventory.factory.InventoryJsonFactory
import com.pixelized.shared.lwa.model.inventory.factory.InventoryJsonFactoryV1
import com.pixelized.shared.lwa.model.item.factory.ItemJsonFactory
import com.pixelized.shared.lwa.model.item.factory.ItemJsonFactoryV1
import com.pixelized.shared.lwa.model.tag.TagJsonFactory
@ -52,6 +54,8 @@ val factoryDependencies
factoryOf(::TagJsonFactory)
factoryOf(::ItemJsonFactory)
factoryOf(::ItemJsonFactoryV1)
factoryOf(::InventoryJsonFactory)
factoryOf(::InventoryJsonFactoryV1)
}
val parserDependencies

View file

@ -0,0 +1,30 @@
package com.pixelized.shared.lwa.model.inventory
data class Inventory(
val characterSheetId: String,
val purse: Purse,
val items: List<Item>,
) {
data class Purse(
val gold: Int,
val silver: Int,
val copper: Int,
)
data class Item(
val itemId: String,
val count: Int,
)
companion object {
fun empty(characterSheetId: String) = Inventory(
characterSheetId = characterSheetId,
purse = Purse(
gold = 0,
silver = 0,
copper = 0,
),
items = emptyList(),
)
}
}

View file

@ -0,0 +1,8 @@
package com.pixelized.shared.lwa.model.inventory
import kotlinx.serialization.Serializable
@Serializable
sealed interface InventoryJson {
val characterSheetId: String
}

View file

@ -0,0 +1,24 @@
package com.pixelized.shared.lwa.model.inventory
import kotlinx.serialization.Serializable
@Serializable
data class InventoryJsonV1(
override val characterSheetId: String,
val purse: PurseJson,
val items: List<ItemJson>,
) : InventoryJson {
@Serializable
data class PurseJson(
val gold: Int,
val silver: Int,
val copper: Int,
)
@Serializable
data class ItemJson(
val itemId: String,
val count: Int,
)
}

View file

@ -0,0 +1,19 @@
package com.pixelized.shared.lwa.model.inventory.factory
import com.pixelized.shared.lwa.model.inventory.Inventory
import com.pixelized.shared.lwa.model.inventory.InventoryJson
import com.pixelized.shared.lwa.model.inventory.InventoryJsonV1
class InventoryJsonFactory(
private val v1: InventoryJsonFactoryV1,
) {
fun convertFromJson(json: InventoryJson): Inventory {
return when (json) {
is InventoryJsonV1 -> v1.convertFromJson(json = json)
}
}
fun convertToJson(inventory: Inventory): InventoryJson {
return v1.convertToJson(inventory = inventory)
}
}

View file

@ -0,0 +1,41 @@
package com.pixelized.shared.lwa.model.inventory.factory
import com.pixelized.shared.lwa.model.inventory.Inventory
import com.pixelized.shared.lwa.model.inventory.InventoryJsonV1
class InventoryJsonFactoryV1 {
fun convertFromJson(json: InventoryJsonV1): Inventory {
return Inventory(
characterSheetId = json.characterSheetId,
purse = Inventory.Purse(
gold = json.purse.gold,
silver = json.purse.silver,
copper = json.purse.copper,
),
items = json.items.map {
Inventory.Item(
itemId = it.itemId,
count = it.count,
)
},
)
}
fun convertToJson(inventory: Inventory): InventoryJsonV1 {
return InventoryJsonV1(
characterSheetId = inventory.characterSheetId,
purse = InventoryJsonV1.PurseJson(
gold = inventory.purse.gold,
silver = inventory.purse.silver,
copper = inventory.purse.copper,
),
items = inventory.items.map {
InventoryJsonV1.ItemJson(
itemId = it.itemId,
count = it.count,
)
},
)
}
}

View file

@ -49,4 +49,19 @@ sealed interface ApiSynchronisation : SocketMessage {
override val timestamp: Long,
val itemId: String,
) : ItemApiSynchronisation
@Serializable
sealed interface InventoryApiSynchronisation : ApiSynchronisation, CharacterSheetIdMessage
@Serializable
data class InventoryUpdate(
override val timestamp: Long,
override val characterSheetId: String,
) : InventoryApiSynchronisation
@Serializable
data class InventoryDelete(
override val timestamp: Long,
override val characterSheetId: String,
) : InventoryApiSynchronisation
}

View file

@ -73,4 +73,14 @@ class PathProvider(
OperatingSystem.Macintosh -> "${storePath(os = os, app = app)}tags/"
}
}
fun inventoryPath(
os: OperatingSystem = this.operatingSystem,
app: String = this.appName,
): String {
return when (os) {
OperatingSystem.Windows -> "${storePath(os = os, app = app)}inventory\\"
OperatingSystem.Macintosh -> "${storePath(os = os, app = app)}inventory/"
}
}
}