Add alteration on item consuming.
This commit is contained in:
parent
6e1aaa10d3
commit
4e013aa358
5 changed files with 83 additions and 1 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue