diff --git a/server/src/main/kotlin/Module.kt b/server/src/main/kotlin/Module.kt index 2b0345d..9e4443a 100644 --- a/server/src/main/kotlin/Module.kt +++ b/server/src/main/kotlin/Module.kt @@ -1,3 +1,4 @@ +import com.pixelized.server.lwa.logics.ItemUsageLogic import com.pixelized.server.lwa.model.alteration.AlterationService import com.pixelized.server.lwa.model.alteration.AlterationStore import com.pixelized.server.lwa.model.campaign.CampaignService @@ -24,6 +25,7 @@ val serverModuleDependencies engineDependencies, storeDependencies, serviceDependencies, + logicsDependencies, ) val toolsDependencies @@ -60,3 +62,8 @@ val serviceDependencies singleOf(::ItemService) singleOf(::TagService) } + +val logicsDependencies + get() = module { + singleOf(::ItemUsageLogic) + } diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/logics/ItemUsageLogic.kt b/server/src/main/kotlin/com/pixelized/server/lwa/logics/ItemUsageLogic.kt new file mode 100644 index 0000000..094958e --- /dev/null +++ b/server/src/main/kotlin/com/pixelized/server/lwa/logics/ItemUsageLogic.kt @@ -0,0 +1,37 @@ +package com.pixelized.server.lwa.logics + +import com.pixelized.server.lwa.model.character.CharacterSheetService +import com.pixelized.server.lwa.model.inventory.InventoryService + +class ItemUsageLogic( + private val characterSheetService: CharacterSheetService, + private val inventoryService: InventoryService, +) { + suspend fun consumeInventoryItem( + characterSheetId: String, + inventoryId: String, + ): List { + val inventoryItem = inventoryService.getInventoryItem( + characterSheetId = characterSheetId, + inventoryId = inventoryId, + ) + val item = inventoryService.getItem( + itemId = inventoryItem.itemId, + ) + // equip the item form the inventory + inventoryService.consumeInventoryItem( + characterSheetId = characterSheetId, + inventoryId = inventoryId, + ) + // if consume didn't throw then add the alteration to the character + val alterations = item.alterations + alterations.forEach { alterationId -> + characterSheetService.updateAlteration( + characterSheetId = characterSheetId, + alterationId = alterationId, + active = true, + ) + } + return alterations + } +} \ No newline at end of file diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/model/inventory/InventoryService.kt b/server/src/main/kotlin/com/pixelized/server/lwa/model/inventory/InventoryService.kt index 9c0ba11..e9538af 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/model/inventory/InventoryService.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/model/inventory/InventoryService.kt @@ -5,6 +5,7 @@ import com.pixelized.server.lwa.server.exception.BusinessException import com.pixelized.shared.lwa.model.inventory.Inventory import com.pixelized.shared.lwa.model.inventory.InventoryJson import com.pixelized.shared.lwa.model.inventory.factory.InventoryJsonFactory +import com.pixelized.shared.lwa.model.item.Item import com.pixelized.shared.lwa.protocol.rest.ApiPurseJson import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -106,6 +107,30 @@ class InventoryService( return inventoryId } + @Throws + suspend fun getInventoryItem( + characterSheetId: String, + inventoryId: String, + ): Inventory.Item { + // get the inventory of the character, if none create one. + val inventory = inventoryStore.inventoryFlow().value[characterSheetId] + ?: Inventory.empty(characterSheetId = characterSheetId) + // Guard case. + return inventory.items + .firstOrNull { it.inventoryId == inventoryId } + ?: throw BusinessException( + message = "InventoryItem (id:$inventoryId) not found in Inventory(characterSheetId:$characterSheetId).", + ) + } + + @Throws + suspend fun getItem( + itemId: String, + ): Item { + return itemStore.item(itemId = itemId) + ?: throw BusinessException(message = "Item (id:$itemId) not found.") + } + @Throws suspend fun changeInventoryItemCount( characterSheetId: String, diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt index 55810bc..88f90d0 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/Engine.kt @@ -1,5 +1,6 @@ package com.pixelized.server.lwa.server +import com.pixelized.server.lwa.logics.ItemUsageLogic import com.pixelized.server.lwa.model.alteration.AlterationService import com.pixelized.server.lwa.model.alteration.AlterationStore import com.pixelized.server.lwa.model.campaign.CampaignService @@ -30,6 +31,7 @@ class Engine( val inventoryService: InventoryService, val tagService: TagService, val campaignJsonFactory: CampaignJsonFactory, + val itemUsageLogic: ItemUsageLogic, private val campaignStore: CampaignStore, private val characterStore: CharacterSheetStore, private val alterationStore: AlterationStore, diff --git a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/inventory/PUT_ConsumeInventoryItem.kt b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/inventory/PUT_ConsumeInventoryItem.kt index 322c553..bb0e64f 100644 --- a/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/inventory/PUT_ConsumeInventoryItem.kt +++ b/server/src/main/kotlin/com/pixelized/server/lwa/server/rest/inventory/PUT_ConsumeInventoryItem.kt @@ -6,6 +6,7 @@ import com.pixelized.server.lwa.utils.extentions.exception import com.pixelized.server.lwa.utils.extentions.inventoryId import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation +import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent import io.ktor.server.response.respond import io.ktor.server.routing.RoutingContext @@ -16,7 +17,7 @@ fun Engine.consumeInventoryItem(): suspend RoutingContext.() -> Unit { val characterSheetId = call.queryParameters.characterSheetId val inventoryId = call.queryParameters.inventoryId // add the item to the inventory. - inventoryService.consumeInventoryItem( + val alterationIds = itemUsageLogic.consumeInventoryItem( characterSheetId = characterSheetId, inventoryId = inventoryId, ) @@ -30,6 +31,16 @@ fun Engine.consumeInventoryItem(): suspend RoutingContext.() -> Unit { characterSheetId = characterSheetId, ), ) + alterationIds.forEach { + webSocket.emit( + value = CharacterSheetEvent.UpdateAlteration( + timestamp = System.currentTimeMillis(), + characterSheetId = characterSheetId, + alterationId = it, + active = true, + ) + ) + } } catch (exception: Exception) { call.exception( exception = exception,