From 47c47690a74cfa7bc473a8b511bf0ebbef48d63d Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Fri, 9 Sep 2022 17:33:48 +0200 Subject: [PATCH] refactor so files match contents --- .../dev/adamko/kafkatorio/webmap/App.kt | 19 -------- .../webmap/{map.kt => FactorioMap.kt} | 0 .../dev/adamko/kafkatorio/webmap/main.kt | 31 ++++++++++++ .../webmap/state/FactorioGameState.kt | 47 +++++++++++++++++++ .../state/{playerState.kt => PlayerState.kt} | 27 ----------- .../adamko/kafkatorio/webmap/state/state.kt | 32 ------------- 6 files changed, 78 insertions(+), 78 deletions(-) rename modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/{map.kt => FactorioMap.kt} (100%) create mode 100644 modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/main.kt create mode 100644 modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/FactorioGameState.kt rename modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/{playerState.kt => PlayerState.kt} (71%) delete mode 100644 modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/state.kt diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/App.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/App.kt index fdb7f84..532527b 100644 --- a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/App.kt +++ b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/App.kt @@ -38,8 +38,6 @@ import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach -val rootJob = Job() - object App : Application() { @@ -111,20 +109,3 @@ object App : Application() { } } - - -fun main() { - startApplication( - { - Link.useDataNavigoForLinks = true - App - }, - module.hot, - BootstrapModule, -// BootstrapCssModule, - BootstrapIconsModule, - FontAwesomeModule, - ChartModule, - CoreModule, - ) -} diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/map.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/FactorioMap.kt similarity index 100% rename from modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/map.kt rename to modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/FactorioMap.kt diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/main.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/main.kt new file mode 100644 index 0000000..05fbac7 --- /dev/null +++ b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/main.kt @@ -0,0 +1,31 @@ +package dev.adamko.kafkatorio.webmap + +import io.kvision.BootstrapIconsModule +import io.kvision.BootstrapModule +import io.kvision.ChartModule +import io.kvision.CoreModule +import io.kvision.FontAwesomeModule +import io.kvision.html.Link +import io.kvision.module +import io.kvision.startApplication +import kotlinx.coroutines.Job + + +val rootJob = Job() + + +fun main() { + startApplication( + { + Link.useDataNavigoForLinks = true + App + }, + module.hot, + BootstrapModule, +// BootstrapCssModule, + BootstrapIconsModule, + FontAwesomeModule, + ChartModule, + CoreModule, + ) +} diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/FactorioGameState.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/FactorioGameState.kt new file mode 100644 index 0000000..f4a0946 --- /dev/null +++ b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/FactorioGameState.kt @@ -0,0 +1,47 @@ +package dev.adamko.kafkatorio.webmap.state + +import dev.adamko.kafkatorio.schema.common.FactorioServerId +import dev.adamko.kafkatorio.schema.common.PlayerIndex +import dev.adamko.kafkatorio.webmap.FactorioMap +import io.kvision.maps.externals.leaflet.layer.LayerGroup + + +data class FactorioGameState( + val serverId: FactorioServerId, + val map: FactorioMap = FactorioMap(serverId), + val players: Map = emptyMap(), +) { + + val playerIconsLayer: LayerGroup + get() = map.playerIconsLayer + + operator fun plus(action: SiteAction.FactorioUpdate): FactorioGameState = + when (action) { + is SiteAction.FactorioUpdate.Player -> handlePlayerUpdate(action) + } +} + + +private fun FactorioGameState.handlePlayerUpdate( + update: SiteAction.FactorioUpdate.Player, +): FactorioGameState { + + val previous = players[update.data.key.index] + ?: PlayerState(update.data.key.index, update.tick) + + val updated = previous.update(update.tick, update.data) + + if (previous.mapMarker != updated.mapMarker) { + if (previous.mapMarker != null) { + println("removing player icon ${previous.index}") + playerIconsLayer.removeLayer(previous.mapMarker) + } + if (updated.mapMarker != null) { + println("adding player icon ${updated.index}") + playerIconsLayer.addLayer(updated.mapMarker) + } + } + + val updatedPlayersState = players + (previous.index to updated) + return copy(players = updatedPlayersState) +} diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/playerState.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/PlayerState.kt similarity index 71% rename from modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/playerState.kt rename to modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/PlayerState.kt index b46b3b5..6c7c399 100644 --- a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/playerState.kt +++ b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/PlayerState.kt @@ -7,33 +7,6 @@ import io.kvision.maps.Maps import io.kvision.maps.externals.leaflet.layer.vector.CircleMarker import kotlinx.js.jso - -fun handlePlayerUpdate( - gameState: FactorioGameState, - update: SiteAction.FactorioUpdate.Player, -): FactorioGameState { - - val previous = gameState.players[update.data.key.index] - ?: PlayerState(update.data.key.index, update.tick) - - val updated = previous.update(update.tick, update.data) - - if (previous.mapMarker != updated.mapMarker) { - if (previous.mapMarker != null) { - println("removing player icon ${previous.index}") - gameState.playerIconsLayer.removeLayer(previous.mapMarker) - } - if (updated.mapMarker != null) { - println("adding player icon ${updated.index}") - gameState.playerIconsLayer.addLayer(updated.mapMarker) - } - } - - val updatedPlayersState = gameState.players + (previous.index to updated) - return gameState.copy(players = updatedPlayersState) -} - - data class PlayerState( val index: PlayerIndex, val lastTick: Tick, diff --git a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/state.kt b/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/state.kt deleted file mode 100644 index 206d5b3..0000000 --- a/modules/web-map/src/main/kotlin/dev/adamko/kafkatorio/webmap/state/state.kt +++ /dev/null @@ -1,32 +0,0 @@ -package dev.adamko.kafkatorio.webmap.state - -import dev.adamko.kafkatorio.schema.common.FactorioServerId -import dev.adamko.kafkatorio.schema.common.PlayerIndex -import dev.adamko.kafkatorio.webmap.FactorioMap -import io.kvision.maps.externals.leaflet.layer.LayerGroup - - -//fun createFactorioReduxStore(): ReduxStore = -// createReduxStore( -// ::factorioGameReducer, -// FactorioGameState( -// map = FactorioMap(), -// players = emptyMap(), -// ) -// ) - - -data class FactorioGameState( - val serverId: FactorioServerId, - val map: FactorioMap = FactorioMap(serverId), - val players: Map = emptyMap(), -) { - - val playerIconsLayer: LayerGroup - get() = map.playerIconsLayer - - operator fun plus(action: SiteAction.FactorioUpdate): FactorioGameState = - when (action) { - is SiteAction.FactorioUpdate.Player -> handlePlayerUpdate(this, action) - } -}