Add alteration on item consuming.

This commit is contained in:
Thomas Andres Gomez 2025-10-18 22:32:31 +02:00
parent 6e1aaa10d3
commit 4e013aa358
5 changed files with 83 additions and 1 deletions

View file

@ -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.AlterationService
import com.pixelized.server.lwa.model.alteration.AlterationStore import com.pixelized.server.lwa.model.alteration.AlterationStore
import com.pixelized.server.lwa.model.campaign.CampaignService import com.pixelized.server.lwa.model.campaign.CampaignService
@ -24,6 +25,7 @@ val serverModuleDependencies
engineDependencies, engineDependencies,
storeDependencies, storeDependencies,
serviceDependencies, serviceDependencies,
logicsDependencies,
) )
val toolsDependencies val toolsDependencies
@ -60,3 +62,8 @@ val serviceDependencies
singleOf(::ItemService) singleOf(::ItemService)
singleOf(::TagService) singleOf(::TagService)
} }
val logicsDependencies
get() = module {
singleOf(::ItemUsageLogic)
}

View file

@ -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<String> {
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
}
}

View file

@ -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.Inventory
import com.pixelized.shared.lwa.model.inventory.InventoryJson import com.pixelized.shared.lwa.model.inventory.InventoryJson
import com.pixelized.shared.lwa.model.inventory.factory.InventoryJsonFactory 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 com.pixelized.shared.lwa.protocol.rest.ApiPurseJson
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -106,6 +107,30 @@ class InventoryService(
return inventoryId 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 @Throws
suspend fun changeInventoryItemCount( suspend fun changeInventoryItemCount(
characterSheetId: String, characterSheetId: String,

View file

@ -1,5 +1,6 @@
package com.pixelized.server.lwa.server 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.AlterationService
import com.pixelized.server.lwa.model.alteration.AlterationStore import com.pixelized.server.lwa.model.alteration.AlterationStore
import com.pixelized.server.lwa.model.campaign.CampaignService import com.pixelized.server.lwa.model.campaign.CampaignService
@ -30,6 +31,7 @@ class Engine(
val inventoryService: InventoryService, val inventoryService: InventoryService,
val tagService: TagService, val tagService: TagService,
val campaignJsonFactory: CampaignJsonFactory, val campaignJsonFactory: CampaignJsonFactory,
val itemUsageLogic: ItemUsageLogic,
private val campaignStore: CampaignStore, private val campaignStore: CampaignStore,
private val characterStore: CharacterSheetStore, private val characterStore: CharacterSheetStore,
private val alterationStore: AlterationStore, private val alterationStore: AlterationStore,

View file

@ -6,6 +6,7 @@ import com.pixelized.server.lwa.utils.extentions.exception
import com.pixelized.server.lwa.utils.extentions.inventoryId import com.pixelized.server.lwa.utils.extentions.inventoryId
import com.pixelized.shared.lwa.protocol.rest.APIResponse import com.pixelized.shared.lwa.protocol.rest.APIResponse
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation 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.response.respond
import io.ktor.server.routing.RoutingContext import io.ktor.server.routing.RoutingContext
@ -16,7 +17,7 @@ fun Engine.consumeInventoryItem(): suspend RoutingContext.() -> Unit {
val characterSheetId = call.queryParameters.characterSheetId val characterSheetId = call.queryParameters.characterSheetId
val inventoryId = call.queryParameters.inventoryId val inventoryId = call.queryParameters.inventoryId
// add the item to the inventory. // add the item to the inventory.
inventoryService.consumeInventoryItem( val alterationIds = itemUsageLogic.consumeInventoryItem(
characterSheetId = characterSheetId, characterSheetId = characterSheetId,
inventoryId = inventoryId, inventoryId = inventoryId,
) )
@ -30,6 +31,16 @@ fun Engine.consumeInventoryItem(): suspend RoutingContext.() -> Unit {
characterSheetId = characterSheetId, characterSheetId = characterSheetId,
), ),
) )
alterationIds.forEach {
webSocket.emit(
value = CharacterSheetEvent.UpdateAlteration(
timestamp = System.currentTimeMillis(),
characterSheetId = characterSheetId,
alterationId = it,
active = true,
)
)
}
} catch (exception: Exception) { } catch (exception: Exception) {
call.exception( call.exception(
exception = exception, exception = exception,