improve event-schema polymorphism with enums

This commit is contained in:
Adam 2022-01-08 23:09:27 +01:00
parent 3875b9c309
commit 51e0aff823
18 changed files with 120 additions and 67 deletions

View file

@ -56,7 +56,6 @@ fun main() {
"ws://localhost:9073/ws",
reduxStore,
)
App(
reduxStore = reduxStore,
wsService = wsService

View file

@ -2,9 +2,8 @@ package dev.adamko.kafkatorio.webmap
import dev.adamko.kafkatorio.events.schema.FactorioEvent
import dev.adamko.kafkatorio.events.schema.PlayerData
import externals.leaflet.geo.LatLng
import externals.leaflet.layer.overlay.Tooltip
import externals.leaflet.layer.vector.CircleMarker
import io.kvision.maps.externals.leaflet.layer.overlay.Tooltip
import io.kvision.maps.externals.leaflet.layer.vector.CircleMarker
import io.kvision.maps.Maps
import io.kvision.maps.Maps.Companion.L
import io.kvision.redux.ReduxStore
@ -12,6 +11,7 @@ import io.kvision.redux.createReduxStore
import io.kvision.utils.obj
import redux.RAction
fun createFactorioReduxStore(): ReduxStore<FactorioGameState, FactorioUpdate> =
createReduxStore(
::factorioGameReducer,
@ -21,6 +21,7 @@ fun createFactorioReduxStore(): ReduxStore<FactorioGameState, FactorioUpdate> =
)
)
data class FactorioGameState(
val kvMaps: Maps,
val players: Map<String, PlayerState>,
@ -32,6 +33,7 @@ sealed class FactorioUpdate : RAction {
FactorioUpdate()
}
fun factorioGameReducer(state: FactorioGameState, update: FactorioUpdate): FactorioGameState =
when (update) {
is FactorioUpdate.PlayerUpdate -> {
@ -77,7 +79,7 @@ data class PlayerState(
fillColor = color
fillOpacity = 1f
radius = 10
className = playerData.objectName
className = playerData.objectName.name
// interactive = false
}

View file

@ -1,13 +1,20 @@
package dev.adamko.kafkatorio.webmap
import dev.adamko.kafkatorio.events.schema.ConsoleChatMessage
import dev.adamko.kafkatorio.events.schema.EntityData
import dev.adamko.kafkatorio.events.schema.FactorioConfigurationUpdate
import dev.adamko.kafkatorio.events.schema.FactorioEvent
import dev.adamko.kafkatorio.events.schema.FactorioObjectData
import dev.adamko.kafkatorio.events.schema.FactorioMapChunk
import dev.adamko.kafkatorio.events.schema.FactorioMapTile
import dev.adamko.kafkatorio.events.schema.KafkatorioPacket
import dev.adamko.kafkatorio.events.schema.PlayerData
import dev.adamko.kafkatorio.events.schema.SurfaceData
import io.kvision.redux.ReduxStore
import kotlinx.serialization.decodeFromString
import org.w3c.dom.MessageEvent
import org.w3c.dom.WebSocket
class WebsocketService(
private val wsUrl: String,
private val reduxStore: ReduxStore<FactorioGameState, FactorioUpdate>,
@ -24,22 +31,28 @@ class WebsocketService(
if (
data != null
&& data.trim().startsWith("{")
&& data.trim().endsWith("}")
&& data.trim().run { startsWith("{") && endsWith("}") }
) {
println(data.replace('\n', ' '))
val event: FactorioEvent<FactorioObjectData> = jsonMapper.decodeFromString(data)
when (val eventData = event.data) {
is PlayerData -> {
reduxStore.dispatch(FactorioUpdate.PlayerUpdate(event, eventData))
when (val event: KafkatorioPacket = jsonMapper.decodeFromString(data)) {
is FactorioEvent<*> -> {
when (val eventData = event.data) {
is PlayerData ->
reduxStore.dispatch(FactorioUpdate.PlayerUpdate(event, eventData))
is ConsoleChatMessage -> {}
is EntityData -> {}
is FactorioMapChunk -> {}
is FactorioMapTile -> {}
is SurfaceData -> {}
}
}
else -> {}
is FactorioConfigurationUpdate -> {}
}
} else {
println(data)
}
}