Add server synchronization feature.
This commit is contained in:
parent
53c6aede27
commit
fa49d8ed22
15 changed files with 154 additions and 48 deletions
|
|
@ -41,7 +41,7 @@ class AlterationStore(
|
|||
|
||||
fun alterationsFlow(): StateFlow<List<Alteration>> = alterationFlow
|
||||
|
||||
private fun updateAlterationFlow() {
|
||||
fun updateAlterationFlow() {
|
||||
alterationFlow.value = try {
|
||||
load()
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ class CampaignStore(
|
|||
|
||||
fun campaignFlow(): StateFlow<Campaign> = campaignFlow
|
||||
|
||||
private fun updateCampaignFlow() {
|
||||
fun updateCampaignFlow() {
|
||||
campaignFlow.value = try {
|
||||
load()
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ class CharacterSheetStore(
|
|||
|
||||
fun characterSheetsFlow(): StateFlow<List<CharacterSheet>> = characterSheetsFlow
|
||||
|
||||
private suspend fun updateCharacterFlow() {
|
||||
fun updateCharacterFlow() {
|
||||
characterSheetsFlow.value = try {
|
||||
load()
|
||||
} catch (exception: Exception) {
|
||||
|
|
@ -54,7 +54,7 @@ class CharacterSheetStore(
|
|||
JsonCodingException::class,
|
||||
JsonConversionException::class,
|
||||
)
|
||||
suspend fun load(
|
||||
fun load(
|
||||
directory: File = this.directory,
|
||||
): List<CharacterSheet> {
|
||||
return directory
|
||||
|
|
|
|||
|
|
@ -40,7 +40,7 @@ class InventoryStore(
|
|||
|
||||
fun inventoryFlow(): StateFlow<Map<String, Inventory>> = inventoryFlow
|
||||
|
||||
private suspend fun updateInventoryFlow() {
|
||||
fun updateInventoryFlow() {
|
||||
inventoryFlow.value = try {
|
||||
load()
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ class ItemStore(
|
|||
|
||||
fun itemsFlow(): StateFlow<List<Item>> = itemFlow
|
||||
|
||||
private fun updateItemsFlow() {
|
||||
fun updateItemsFlow() {
|
||||
itemFlow.value = try {
|
||||
load()
|
||||
} catch (exception: Exception) {
|
||||
|
|
|
|||
|
|
@ -33,18 +33,7 @@ class TagStore(
|
|||
val scope = CoroutineScope(Dispatchers.IO + Job())
|
||||
// load the initial data
|
||||
scope.launch {
|
||||
update(
|
||||
flow = alterationTagsFlow,
|
||||
file = alterationFile(),
|
||||
)
|
||||
update(
|
||||
flow = characterTagsFlow,
|
||||
file = characterFile(),
|
||||
)
|
||||
update(
|
||||
flow = itemTagsFlow,
|
||||
file = itemFile(),
|
||||
)
|
||||
updateTagFlow()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -52,6 +41,21 @@ class TagStore(
|
|||
fun characterTags(): StateFlow<Map<String, TagJson>> = characterTagsFlow
|
||||
fun itemTags(): StateFlow<Map<String, TagJson>> = itemTagsFlow
|
||||
|
||||
fun updateTagFlow() {
|
||||
update(
|
||||
flow = alterationTagsFlow,
|
||||
file = alterationFile(),
|
||||
)
|
||||
update(
|
||||
flow = characterTagsFlow,
|
||||
file = characterFile(),
|
||||
)
|
||||
update(
|
||||
flow = itemTagsFlow,
|
||||
file = itemFile(),
|
||||
)
|
||||
}
|
||||
|
||||
private fun update(
|
||||
flow: MutableStateFlow<Map<String, TagJson>>,
|
||||
file: File,
|
||||
|
|
|
|||
|
|
@ -1,15 +1,22 @@
|
|||
package com.pixelized.server.lwa.server
|
||||
|
||||
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
|
||||
import com.pixelized.server.lwa.model.character.CharacterSheetStore
|
||||
import com.pixelized.server.lwa.model.inventory.InventoryService
|
||||
import com.pixelized.server.lwa.model.inventory.InventoryStore
|
||||
import com.pixelized.server.lwa.model.item.ItemService
|
||||
import com.pixelized.server.lwa.model.item.ItemStore
|
||||
import com.pixelized.server.lwa.model.tag.TagService
|
||||
import com.pixelized.server.lwa.model.tag.TagStore
|
||||
import com.pixelized.shared.lwa.model.campaign.factory.CampaignJsonFactory
|
||||
import com.pixelized.shared.lwa.protocol.websocket.ApiSynchronisation
|
||||
import com.pixelized.shared.lwa.protocol.websocket.CampaignEvent
|
||||
import com.pixelized.shared.lwa.protocol.websocket.CharacterSheetEvent
|
||||
import com.pixelized.shared.lwa.protocol.websocket.GameAdminEvent
|
||||
import com.pixelized.shared.lwa.protocol.websocket.GameMasterEvent
|
||||
import com.pixelized.shared.lwa.protocol.websocket.RollEvent
|
||||
import com.pixelized.shared.lwa.protocol.websocket.SocketMessage
|
||||
|
|
@ -23,6 +30,12 @@ class Engine(
|
|||
val inventoryService: InventoryService,
|
||||
val tagService: TagService,
|
||||
val campaignJsonFactory: CampaignJsonFactory,
|
||||
private val campaignStore: CampaignStore,
|
||||
private val characterStore: CharacterSheetStore,
|
||||
private val alterationStore: AlterationStore,
|
||||
private val itemStore: ItemStore,
|
||||
private val inventoryStore: InventoryStore,
|
||||
private val tagStore: TagStore,
|
||||
) {
|
||||
val webSocket = MutableSharedFlow<SocketMessage>()
|
||||
|
||||
|
|
@ -94,6 +107,23 @@ class Engine(
|
|||
is CampaignEvent.UpdateScene -> Unit
|
||||
}
|
||||
|
||||
is GameAdminEvent -> when (message) {
|
||||
is GameAdminEvent.ServerSynchronization -> {
|
||||
characterStore.updateCharacterFlow()
|
||||
campaignStore.updateCampaignFlow()
|
||||
alterationStore.updateAlterationFlow()
|
||||
itemStore.updateItemsFlow()
|
||||
inventoryStore.updateInventoryFlow()
|
||||
tagStore.updateTagFlow()
|
||||
|
||||
webSocket.emit(
|
||||
value = GameAdminEvent.ServerSynchronization(
|
||||
timestamp = System.currentTimeMillis(),
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
is ApiSynchronisation -> Unit // Nothing to do there.
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue