Add the alteration system to the server & link the app on it.
This commit is contained in:
parent
4ed11660c3
commit
29747dcb5c
83 changed files with 1797 additions and 811 deletions
|
|
@ -1,3 +1,5 @@
|
|||
import com.pixelized.server.lwa.model.alteration.AlterationService
|
||||
import com.pixelized.server.lwa.model.alteration.AlterationStore
|
||||
import com.pixelized.server.lwa.model.campaign.CampaignService
|
||||
import com.pixelized.server.lwa.model.campaign.CampaignStore
|
||||
import com.pixelized.server.lwa.model.character.CharacterSheetService
|
||||
|
|
@ -26,12 +28,14 @@ val storeDependencies
|
|||
get() = module {
|
||||
singleOf(::CharacterSheetStore)
|
||||
singleOf(::CampaignStore)
|
||||
singleOf(::AlterationStore)
|
||||
}
|
||||
|
||||
val serviceDependencies
|
||||
get() = module {
|
||||
singleOf(::CharacterSheetService)
|
||||
singleOf(::CampaignService)
|
||||
singleOf(::AlterationService)
|
||||
}
|
||||
|
||||
val factoryDependencies
|
||||
|
|
|
|||
|
|
@ -0,0 +1,56 @@
|
|||
package com.pixelized.server.lwa.model.alteration
|
||||
|
||||
import com.pixelized.shared.lwa.model.alteration.AlterationJson
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.model.campaign.CampaignJsonFactory
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.SharingStarted
|
||||
import kotlinx.coroutines.flow.map
|
||||
import kotlinx.coroutines.flow.stateIn
|
||||
|
||||
class AlterationService(
|
||||
private val store: AlterationStore,
|
||||
private val campaignJsonFactory: CampaignJsonFactory,
|
||||
) {
|
||||
private val scope = CoroutineScope(Dispatchers.IO + Job())
|
||||
private val alterations = store.alterationsFlow()
|
||||
private val actives = store.activeFlow()
|
||||
.map { data ->
|
||||
data.mapKeys { it: Map.Entry<String, List<String>> ->
|
||||
campaignJsonFactory.convertFromV1(characterInstanceIdJson = it.key)
|
||||
}
|
||||
}.stateIn(
|
||||
scope = scope,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = emptyMap()
|
||||
)
|
||||
|
||||
fun alterations(): List<AlterationJson> {
|
||||
return alterations.value
|
||||
}
|
||||
|
||||
fun active(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
): List<String> {
|
||||
return actives.value[characterInstanceId] ?: emptyList()
|
||||
}
|
||||
|
||||
fun isAlterationActive(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
alterationId: String,
|
||||
): Boolean {
|
||||
return actives.value[characterInstanceId]?.contains(alterationId) ?: false
|
||||
}
|
||||
|
||||
suspend fun toggleActiveAlteration(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
alterationId: String,
|
||||
): Boolean {
|
||||
return store.toggleActiveAlteration(
|
||||
characterInstanceId = characterInstanceId,
|
||||
alterationId = alterationId,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,144 @@
|
|||
package com.pixelized.server.lwa.model.alteration
|
||||
|
||||
import com.pixelized.shared.lwa.alterationsPath
|
||||
import com.pixelized.shared.lwa.model.alteration.AlterationJson
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.model.campaign.CampaignJsonFactory
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.serialization.encodeToString
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
|
||||
class AlterationStore(
|
||||
private val campaignJsonFactory: CampaignJsonFactory,
|
||||
private val json: Json,
|
||||
) {
|
||||
private val directory = File(alterationsPath()).also { it.mkdirs() }
|
||||
private val alterationsFlow = MutableStateFlow<List<AlterationJson>>(emptyList())
|
||||
private val activeFlow = MutableStateFlow<Map<String, List<String>>>(emptyMap())
|
||||
|
||||
init {
|
||||
// build a coroutine scope for async calls
|
||||
val scope = CoroutineScope(Dispatchers.IO + Job())
|
||||
// load the initial data
|
||||
scope.launch {
|
||||
updateAlterations()
|
||||
updateActiveAlterations()
|
||||
}
|
||||
}
|
||||
|
||||
fun alterationsFlow(): StateFlow<List<AlterationJson>> = alterationsFlow
|
||||
|
||||
fun activeFlow(): StateFlow<Map<String, List<String>>> = activeFlow
|
||||
|
||||
private fun updateAlterations() {
|
||||
alterationsFlow.value = loadAlterations()
|
||||
}
|
||||
|
||||
private fun updateActiveAlterations() {
|
||||
activeFlow.value = loadActiveAlterations()
|
||||
}
|
||||
|
||||
private fun loadAlterations(): List<AlterationJson> {
|
||||
return try {
|
||||
val alterationFile = file()
|
||||
val json = alterationFile.readText(charset = Charsets.UTF_8)
|
||||
if (json.isBlank()) error("alterations file is empty")
|
||||
this.json.decodeFromString<List<AlterationJson>>(json)
|
||||
} catch (exception: Exception) {
|
||||
// TODO log exception
|
||||
emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
private fun loadActiveAlterations(): Map<String, List<String>> {
|
||||
val mainFile = file()
|
||||
val jsonExt = ".json"
|
||||
return directory
|
||||
.listFiles()
|
||||
?.filter { file ->
|
||||
// guard ignore the main alteration file and non json files.
|
||||
file.name != mainFile.name && file.name.contains(jsonExt)
|
||||
}
|
||||
?.mapNotNull { file ->
|
||||
// read the alteration file.
|
||||
val json = try {
|
||||
file.readText(charset = Charsets.UTF_8)
|
||||
} catch (exception: Exception) {
|
||||
throw FileReadException(root = exception)
|
||||
}
|
||||
try {
|
||||
val alterationIds = this.json.decodeFromString<List<String>>(json)
|
||||
val characterInstanceId = file.name.dropLast(n = jsonExt.length)
|
||||
characterInstanceId to alterationIds
|
||||
} catch (exception: Exception) {
|
||||
// TODO log exception
|
||||
throw JsonConversionException(root = exception)
|
||||
}
|
||||
}
|
||||
?.toMap()
|
||||
?: emptyMap()
|
||||
}
|
||||
|
||||
fun toggleActiveAlteration(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
alterationId: String,
|
||||
): Boolean {
|
||||
val id = campaignJsonFactory.convertToJson(id = characterInstanceId)
|
||||
// toggle the activation state
|
||||
val characterActiveAlterationIds = activeFlow.value[id]
|
||||
?.toMutableList()
|
||||
?.toggle(alterationId = alterationId)
|
||||
?: listOf(alterationId)
|
||||
// build the json string to save
|
||||
val json = try {
|
||||
this.json.encodeToString(characterActiveAlterationIds)
|
||||
} catch (exception: Exception) {
|
||||
throw JsonConversionException(root = exception)
|
||||
}
|
||||
// write the file
|
||||
try {
|
||||
val file = file(id = id)
|
||||
file.writeText(
|
||||
text = json,
|
||||
charset = Charsets.UTF_8,
|
||||
)
|
||||
} catch (exception: Exception) {
|
||||
throw FileWriteException(root = exception)
|
||||
}
|
||||
// Update the dataflow.
|
||||
activeFlow.value = activeFlow.value.toMutableMap().also {
|
||||
it[id] = characterActiveAlterationIds
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun file(): File {
|
||||
return File("${alterationsPath()}alterations.json")
|
||||
}
|
||||
|
||||
private fun file(
|
||||
id: String,
|
||||
): File {
|
||||
return File("${alterationsPath()}$id.json")
|
||||
}
|
||||
|
||||
private fun MutableList<String>.toggle(alterationId: String): MutableList<String> {
|
||||
if (contains(alterationId)) {
|
||||
remove(alterationId)
|
||||
} else {
|
||||
add(alterationId)
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
||||
sealed class AlterationStoreException(root: Exception) : Exception(root)
|
||||
class JsonConversionException(root: Exception) : AlterationStoreException(root)
|
||||
class FileWriteException(root: Exception) : AlterationStoreException(root)
|
||||
class FileReadException(root: Exception) : AlterationStoreException(root)
|
||||
}
|
||||
|
|
@ -4,6 +4,7 @@ import com.pixelized.shared.lwa.model.campaign.Campaign
|
|||
import com.pixelized.shared.lwa.model.campaign.CampaignJson
|
||||
import com.pixelized.shared.lwa.model.campaign.CampaignJsonFactory
|
||||
import com.pixelized.shared.lwa.model.campaign.character
|
||||
import com.pixelized.shared.lwa.model.campaign.npc
|
||||
import com.pixelized.shared.lwa.usecase.CampaignUseCase
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
|
|
@ -23,11 +24,6 @@ class CampaignService(
|
|||
private val campaign: Campaign get() = campaignFlow.value
|
||||
|
||||
private val campaignFlow = store.campaignFlow()
|
||||
.stateIn(
|
||||
scope = scope,
|
||||
started = SharingStarted.Eagerly,
|
||||
initialValue = Campaign.EMPTY,
|
||||
)
|
||||
|
||||
private val campaignJsonFlow: StateFlow<CampaignJson> = campaignFlow
|
||||
.map { factory.convertToJson(it) }
|
||||
|
|
@ -41,13 +37,15 @@ class CampaignService(
|
|||
return campaignJsonFlow.value
|
||||
}
|
||||
|
||||
suspend fun addCharacter(characterId: String): Boolean {
|
||||
suspend fun addCharacter(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
): Boolean {
|
||||
// fetch all the current campaign character
|
||||
val characters = campaign.characters.toMutableMap()
|
||||
// check if the character is in the campaign.
|
||||
if (characters.containsKey(characterId)) return false
|
||||
if (characters.containsKey(characterInstanceId)) return false
|
||||
// update the corresponding character
|
||||
characters[characterId] = campaign.character(id = characterId)
|
||||
characters[characterInstanceId] = campaign.character(id = characterInstanceId)
|
||||
// save the campaign to the disk + update the flow.
|
||||
store.save(
|
||||
campaign = campaign.copy(characters = characters)
|
||||
|
|
@ -55,13 +53,15 @@ class CampaignService(
|
|||
return true
|
||||
}
|
||||
|
||||
suspend fun removeCharacter(characterId: String): Boolean {
|
||||
suspend fun removeCharacter(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
): Boolean {
|
||||
// fetch all the current campaign character
|
||||
val characters = campaign.characters.toMutableMap()
|
||||
// check if the character is in the campaign.
|
||||
if (characters.containsKey(characterId).not()) return false
|
||||
if (characters.containsKey(characterInstanceId).not()) return false
|
||||
// update the corresponding character
|
||||
characters.remove(characterId)
|
||||
characters.remove(characterInstanceId)
|
||||
// save the campaign to the disk + update the flow.
|
||||
store.save(
|
||||
campaign = campaign.copy(characters = characters)
|
||||
|
|
@ -69,46 +69,50 @@ class CampaignService(
|
|||
return true
|
||||
}
|
||||
|
||||
suspend fun addNpc(npcId: String): Boolean {
|
||||
suspend fun addNpc(
|
||||
npcInstanceId: Campaign.CharacterInstance.Id,
|
||||
): Boolean {
|
||||
// fetch all the current campaign character
|
||||
val characters = campaign.npcs.toMutableMap()
|
||||
val npcs = campaign.npcs.toMutableMap()
|
||||
// check if the character is in the campaign.
|
||||
if (characters.containsKey(npcId)) return false
|
||||
if (npcs.containsKey(npcInstanceId)) return false
|
||||
// update the corresponding character
|
||||
characters[npcId] = campaign.character(id = npcId)
|
||||
npcs[npcInstanceId] = campaign.npc(id = npcInstanceId)
|
||||
// save the campaign to the disk + update the flow.
|
||||
store.save(
|
||||
campaign = campaign.copy(npcs = characters)
|
||||
campaign = campaign.copy(npcs = npcs)
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
suspend fun removeNpc(npcId: String): Boolean {
|
||||
suspend fun removeNpc(
|
||||
npcInstanceId: Campaign.CharacterInstance.Id,
|
||||
): Boolean {
|
||||
// fetch all the current campaign character
|
||||
val characters = campaign.npcs.toMutableMap()
|
||||
val npcs = campaign.npcs.toMutableMap()
|
||||
// check if the character is in the campaign.
|
||||
if (characters.containsKey(npcId).not()) return false
|
||||
if (npcs.containsKey(npcInstanceId).not()) return false
|
||||
// update the corresponding character
|
||||
characters.remove(npcId)
|
||||
npcs.remove(npcInstanceId)
|
||||
// save the campaign to the disk + update the flow.
|
||||
store.save(
|
||||
campaign = campaign.copy(npcs = characters)
|
||||
campaign = campaign.copy(npcs = npcs)
|
||||
)
|
||||
return true
|
||||
}
|
||||
|
||||
// Data manipulation through WebSocket.
|
||||
|
||||
suspend fun updateCharacteristic(
|
||||
characterId: String,
|
||||
suspend fun updateCharacterCharacteristic(
|
||||
characterInstanceId: Campaign.CharacterInstance.Id,
|
||||
characteristic: Campaign.CharacterInstance.Characteristic,
|
||||
value: Int,
|
||||
) {
|
||||
// fetch all the current campaign character
|
||||
val characters = campaign.characters.toMutableMap()
|
||||
// update the corresponding character using the use case.
|
||||
characters[characterId] = useCase.updateCharacteristic(
|
||||
character = campaign.character(id = characterId),
|
||||
characters[characterInstanceId] = useCase.updateCharacteristic(
|
||||
character = campaign.character(id = characterInstanceId),
|
||||
characteristic = characteristic,
|
||||
value = value,
|
||||
)
|
||||
|
|
@ -117,4 +121,23 @@ class CampaignService(
|
|||
campaign = campaign.copy(characters = characters)
|
||||
)
|
||||
}
|
||||
|
||||
suspend fun updateNpcCharacteristic(
|
||||
npcInstanceId: Campaign.CharacterInstance.Id,
|
||||
characteristic: Campaign.CharacterInstance.Characteristic,
|
||||
value: Int,
|
||||
) {
|
||||
// fetch all the current campaign character
|
||||
val npcs = campaign.npcs.toMutableMap()
|
||||
// update the corresponding character using the use case.
|
||||
npcs[npcInstanceId] = useCase.updateCharacteristic(
|
||||
character = campaign.npc(id = npcInstanceId),
|
||||
characteristic = characteristic,
|
||||
value = value,
|
||||
)
|
||||
// save the campaign to the disk + update the flow.
|
||||
store.save(
|
||||
campaign = campaign.copy(npcs = npcs)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
|
@ -18,22 +18,30 @@ class CampaignStore(
|
|||
private val factory: CampaignJsonFactory,
|
||||
private val json: Json,
|
||||
) {
|
||||
private val directory = File(campaignPath()).also { it.mkdirs() }
|
||||
|
||||
private val flow = MutableStateFlow(value = Campaign.EMPTY)
|
||||
|
||||
init {
|
||||
// create the directory if needed.
|
||||
File(campaignPath()).also { it.mkdirs() }
|
||||
// build a coroutine scope for async calls
|
||||
val scope = CoroutineScope(Dispatchers.IO + Job())
|
||||
// load the initial data
|
||||
scope.launch {
|
||||
flow.value = load()
|
||||
update()
|
||||
}
|
||||
}
|
||||
|
||||
fun campaignFlow(): StateFlow<Campaign> = flow
|
||||
|
||||
suspend fun update() {
|
||||
flow.value = load()
|
||||
}
|
||||
|
||||
suspend fun load(): Campaign {
|
||||
return try {
|
||||
val json = file().readText(charset = Charsets.UTF_8)
|
||||
if (json.isBlank()) Campaign.EMPTY
|
||||
if (json.isBlank()) error("Campaign file is empty")
|
||||
val campaign = this.json.decodeFromString<CampaignJson>(json)
|
||||
factory.convertFromJson(campaign)
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
package com.pixelized.server.lwa.server
|
||||
|
||||
import com.pixelized.server.lwa.model.alteration.AlterationService
|
||||
import com.pixelized.server.lwa.model.campaign.CampaignService
|
||||
import com.pixelized.server.lwa.model.character.CharacterSheetService
|
||||
import com.pixelized.shared.lwa.model.campaign.CampaignJsonFactory
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RollMessage
|
||||
|
|
@ -12,6 +14,8 @@ import kotlinx.coroutines.flow.MutableSharedFlow
|
|||
class Engine(
|
||||
val characterService: CharacterSheetService,
|
||||
val campaignService: CampaignService,
|
||||
val alterationService: AlterationService,
|
||||
val campaignJsonFactory: CampaignJsonFactory,
|
||||
) {
|
||||
val webSocket = MutableSharedFlow<Message>()
|
||||
|
||||
|
|
@ -20,11 +24,13 @@ class Engine(
|
|||
|
||||
is RollMessage -> Unit // Nothing to do here.
|
||||
|
||||
is UpdatePlayerCharacteristicMessage -> campaignService.updateCharacteristic(
|
||||
characterId = data.characterId,
|
||||
characteristic = data.characteristic,
|
||||
value = data.value,
|
||||
)
|
||||
is UpdatePlayerCharacteristicMessage -> {
|
||||
campaignService.updateCharacterCharacteristic(
|
||||
characterInstanceId = campaignJsonFactory.convertFromV1(characterInstanceIdJson = data.characterInstanceId),
|
||||
characteristic = campaignJsonFactory.convertFromV1(characteristicJson = data.characteristic),
|
||||
value = data.value,
|
||||
)
|
||||
}
|
||||
|
||||
is UpdateSkillUsageMessage -> characterService.updateCharacterSkillUsage(
|
||||
characterId = data.characterId,
|
||||
|
|
@ -36,6 +42,8 @@ class Engine(
|
|||
is RestSynchronisation.CharacterUpdate -> Unit // Handle in the Rest
|
||||
|
||||
is RestSynchronisation.CharacterDelete -> Unit // Handle in the Rest
|
||||
|
||||
is RestSynchronisation.ToggleActiveAlteration -> Unit // Handle in the Rest
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,9 @@ package com.pixelized.server.lwa.server
|
|||
|
||||
import com.pixelized.server.lwa.extention.decodeFromFrame
|
||||
import com.pixelized.server.lwa.extention.encodeToFrame
|
||||
import com.pixelized.server.lwa.server.rest.alteration.getActiveAlteration
|
||||
import com.pixelized.server.lwa.server.rest.alteration.getAlteration
|
||||
import com.pixelized.server.lwa.server.rest.alteration.putActiveAlteration
|
||||
import com.pixelized.server.lwa.server.rest.campaign.deleteCampaignCharacter
|
||||
import com.pixelized.server.lwa.server.rest.campaign.deleteCampaignNpc
|
||||
import com.pixelized.server.lwa.server.rest.campaign.getCampaign
|
||||
|
|
@ -143,6 +146,20 @@ class LocalServer {
|
|||
)
|
||||
}
|
||||
}
|
||||
route(path = "/alterations") {
|
||||
get(
|
||||
path = "",
|
||||
body = engine.getAlteration(),
|
||||
)
|
||||
get(
|
||||
path = "/active",
|
||||
body = engine.getActiveAlteration(),
|
||||
)
|
||||
put(
|
||||
path = "/active/toggle",
|
||||
body = engine.putActiveAlteration(),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
|
|
|||
|
|
@ -0,0 +1,26 @@
|
|||
package com.pixelized.server.lwa.server.rest.alteration
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import io.ktor.server.response.respond
|
||||
|
||||
fun Engine.getActiveAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
// get the query parameter
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
// build the character instance id.
|
||||
val id = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
// fetch the data from the service
|
||||
val data = id?.let { alterationService.active(it) } ?: emptyList()
|
||||
// respond to the client.
|
||||
call.respond(data)
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
package com.pixelized.server.lwa.server.rest.alteration
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import io.ktor.server.response.respond
|
||||
|
||||
fun Engine.getAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
call.respond(alterationService.alterations())
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,63 @@
|
|||
package com.pixelized.server.lwa.server.rest.alteration
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import io.ktor.http.HttpStatusCode
|
||||
import io.ktor.server.request.receive
|
||||
import io.ktor.server.response.respondText
|
||||
|
||||
fun Engine.putActiveAlteration(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
// fetch the query parameters
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
val alterationId = call.receive<String>()
|
||||
|
||||
// build the characterInstanceId from the parameters
|
||||
val characterInstanceId = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
// Update the alteration
|
||||
val updated = characterInstanceId?.let {
|
||||
alterationService.toggleActiveAlteration(
|
||||
characterInstanceId = it,
|
||||
alterationId = alterationId,
|
||||
)
|
||||
} ?: false
|
||||
|
||||
// build the Http response & send it
|
||||
val code = when (updated) {
|
||||
true -> HttpStatusCode.Accepted
|
||||
else -> HttpStatusCode.UnprocessableEntity
|
||||
}
|
||||
call.respondText(
|
||||
text = "$code",
|
||||
status = code,
|
||||
)
|
||||
|
||||
// share the modification to all client through the websocket.
|
||||
characterInstanceId?.let {
|
||||
webSocket.emit(
|
||||
Message(
|
||||
from = "Server",
|
||||
value = RestSynchronisation.ToggleActiveAlteration(
|
||||
characterId = campaignJsonFactory.convertToJson(id = it),
|
||||
alterationId = alterationId,
|
||||
active = alterationService.isAlterationActive(
|
||||
characterInstanceId = it,
|
||||
alterationId = alterationId
|
||||
),
|
||||
),
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,7 @@
|
|||
package com.pixelized.server.lwa.server.rest.campaign
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import io.ktor.http.HttpStatusCode
|
||||
|
|
@ -8,7 +9,16 @@ import io.ktor.server.response.respondText
|
|||
|
||||
fun Engine.deleteCampaignCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
val id = call.queryParameters["id"]
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
val id = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val updated = id?.let { campaignService.removeCharacter(it) } ?: false
|
||||
val code = when (updated) {
|
||||
true -> HttpStatusCode.Accepted
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.pixelized.server.lwa.server.rest.campaign
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import io.ktor.http.HttpStatusCode
|
||||
|
|
@ -8,7 +9,16 @@ import io.ktor.server.response.respondText
|
|||
|
||||
fun Engine.deleteCampaignNpc(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
val id = call.queryParameters["id"]
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
val id = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val updated = id?.let { campaignService.removeNpc(it) } ?: false
|
||||
val code = when (updated) {
|
||||
true -> HttpStatusCode.Accepted
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.pixelized.server.lwa.server.rest.campaign
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import io.ktor.http.HttpStatusCode
|
||||
|
|
@ -8,7 +9,16 @@ import io.ktor.server.response.respondText
|
|||
|
||||
fun Engine.putCampaignCharacter(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
val id = call.queryParameters["id"]
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
val id = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
val updated = id?.let { campaignService.addCharacter(it) } ?: false
|
||||
val code = when (updated) {
|
||||
true -> HttpStatusCode.Accepted
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
package com.pixelized.server.lwa.server.rest.campaign
|
||||
|
||||
import com.pixelized.server.lwa.server.Engine
|
||||
import com.pixelized.shared.lwa.model.campaign.Campaign
|
||||
import com.pixelized.shared.lwa.protocol.websocket.Message
|
||||
import com.pixelized.shared.lwa.protocol.websocket.payload.RestSynchronisation
|
||||
import io.ktor.http.HttpStatusCode
|
||||
|
|
@ -8,16 +9,30 @@ import io.ktor.server.response.respondText
|
|||
|
||||
fun Engine.putCampaignNpc(): suspend io.ktor.server.routing.RoutingContext.() -> Unit {
|
||||
return {
|
||||
val id = call.queryParameters["id"]
|
||||
val characterSheetId = call.queryParameters["characterSheetId"]
|
||||
val instanceId = call.queryParameters["instanceId"]?.toIntOrNull()
|
||||
|
||||
val id = if (characterSheetId != null && instanceId != null) {
|
||||
Campaign.CharacterInstance.Id(
|
||||
characterSheetId = characterSheetId,
|
||||
instanceId = instanceId
|
||||
)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
val updated = id?.let { campaignService.addNpc(it) } ?: false
|
||||
|
||||
val code = when (updated) {
|
||||
true -> HttpStatusCode.Accepted
|
||||
else -> HttpStatusCode.UnprocessableEntity
|
||||
}
|
||||
|
||||
call.respondText(
|
||||
text = "$code",
|
||||
status = code,
|
||||
)
|
||||
|
||||
webSocket.emit(
|
||||
Message(
|
||||
from = "Server",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue