more tiles stuff working...

This commit is contained in:
Adam 2022-01-19 18:40:07 +01:00
parent a7a095ecbf
commit 6fe3132f23
8 changed files with 66 additions and 21 deletions

View file

@ -1 +0,0 @@
**/kafkatorio-web-map/**

View file

@ -49,6 +49,7 @@ import org.apache.kafka.streams.TopologyDescription
import org.apache.kafka.streams.kstream.Consumed
import org.apache.kafka.streams.kstream.KStream
import org.apache.kafka.streams.kstream.KTable
import org.apache.kafka.streams.kstream.Suppressed
import org.apache.kafka.streams.processor.RecordContext
import org.apache.kafka.streams.state.QueryableStoreTypes
import org.apache.kafka.streams.state.ReadOnlyKeyValueStore
@ -120,11 +121,11 @@ class KafkatorioTopology(
) { _: String, value: KafkatorioPacket, _: RecordContext ->
// println("[$key] sending event:${value.eventType} to topic:${value.data.objectName()}")
when (value) {
is FactorioEvent ->
is FactorioEvent ->
"kafkatorio.${value.packetType.name}.${value.data.objectName.name}"
is FactorioConfigurationUpdate ->
"kafkatorio.${value.packetType.name}.FactorioConfigurationUpdate"
is FactorioPrototypes ->
is FactorioPrototypes ->
"kafkatorio.${value.packetType.name}.all"
}
}
@ -219,14 +220,14 @@ class KafkatorioTopology(
"kafkatorio.${KafkatorioPacket.PacketType.EVENT}.${FactorioObjectData.ObjectName.LuaTiles}",
Consumed.with(Serdes.String(), jsonMapper.serde<FactorioEvent>())
)
.peek { k, v ->
println("trying to handle MapChunk update... $k: ${v.eventType}")
}
// .peek { k, v ->
// println("trying to handle MapChunk update... $k: ${v.eventType}")
// }
.filter("events.filter.map-tiles") { _: String, event: FactorioEvent -> event.data is MapTiles }
.mapValues("events.extract-map-tiles") { _, event: FactorioEvent -> (event.data as? MapTiles)!! }
.peek { key, value ->
println("MapChunk update $key, tiles count: ${value.tiles.size}")
}
// .peek { key, value ->
// println("MapChunk update $key, tiles count: ${value.tiles.size}")
// }
val chunkTilesUpdateStream: KStream<String, MapTiles> = builder.stream(
@ -262,7 +263,7 @@ class KafkatorioTopology(
luaTilesUpdatesStream
.merge("luaTilesUpdatesStream-and-chunkTilesUpdateStream", chunkTilesUpdateStream)
.flatMapTopicRecords("all-tiles-convert-to-TileUpdateRecord") { _, mapTiles: MapTiles ->
println("all map tiles update ${mapTiles.tiles.size}")
// println("all map tiles update ${mapTiles.tiles.size}")
mapTiles.tiles.map { tile ->
TileUpdateRecord(
mapTiles.surfaceIndex,
@ -288,6 +289,12 @@ class KafkatorioTopology(
.reduce { tileA, tileB ->
tileA.copy(tiles = tileA.tiles + tileB.tiles)
}
.suppress(
Suppressed.untilTimeLimit(
Duration.ofSeconds(30),
Suppressed.BufferConfig.maxRecords(30)
)
)
chunksTable
.toStream()
@ -302,7 +309,7 @@ class KafkatorioTopology(
}
}
@Suppress("KotlinConstantConditions")
// @Suppress("KotlinConstantConditions")
private fun saveMapTilesPng(key: MapChunkDataPosition, chunk: MapChunkData) {
val chunkOriginX = chunk.chunkPosition.position.leftTopTile.x
@ -325,7 +332,10 @@ class KafkatorioTopology(
?.toHexadecimal()
val rgbColour = when (prototypeColour) {
null -> RGBColor(11, 11, 11, 0)
null -> {
println("missing prototype: ${tile.prototypeName}")
RGBColor(11, 11, 11, 0)
}
else -> RGBColor(
prototypeColour.red.roundToInt(),
prototypeColour.green.roundToInt(),
@ -347,7 +357,7 @@ class KafkatorioTopology(
val zoom = 1u
val file =
File("kafkatorio-web-map/s${key.surfaceIndex}/z$zoom/x${key.position.x}/y${key.position.y}.png")
File("src/main/resources/kafkatorio-web-map/s${key.surfaceIndex}/z$zoom/x${key.position.x}/y${key.position.y}.png")
if (file.parentFile.mkdirs()) {
println("created new map tile parentFile directory ${file.absolutePath}")

View file

@ -17,6 +17,7 @@ import org.http4k.routing.bind
import org.http4k.routing.websockets
import org.http4k.server.PolyHandler
import org.http4k.websocket.Websocket
import org.http4k.websocket.WsHandler
import org.http4k.websocket.WsMessage
import org.http4k.websocket.WsStatus
@ -47,7 +48,7 @@ class WebsocketServer {
messages.emit(msg)
}
suspend fun build(): PolyHandler = supervisorScope builder@{
suspend fun build(): WsHandler = supervisorScope builder@{
coroutineContext.job.invokeOnCompletion { e ->
e?.let { serverJob.completeExceptionally(it) } ?: serverJob.complete()
@ -89,10 +90,8 @@ class WebsocketServer {
}
.launchIn(serverScope)
val http = { _: Request -> Response(OK) }
val ws = websockets("/ws" bind ::bindClient)
return@builder PolyHandler(http, ws)
return@builder websockets("/ws" bind ::bindClient)
}
}

View file

@ -2,7 +2,9 @@ package dev.adamko.kafkatorio.processor
import dev.adamko.kafkatorio.processor.config.ApplicationProperties
import dev.adamko.kafkatorio.processor.tileserver.WebMapTileServer
import kotlinx.coroutines.runBlocking
import org.http4k.server.PolyHandler
import org.http4k.server.Undertow
import org.http4k.server.asServer
@ -11,10 +13,16 @@ val appProps = ApplicationProperties()
fun main() = runBlocking {
val wsServer = WebsocketServer()
val tileServer = WebMapTileServer()
val topology = KafkatorioTopology(wsServer)
topology.build()
wsServer.build().asServer(Undertow(9073)).start().block()
PolyHandler(
tileServer.build(),
wsServer.build(),
).asServer(Undertow(9073))
.start()
.block()
}

View file

@ -0,0 +1,19 @@
package dev.adamko.kafkatorio.processor.tileserver
import org.http4k.core.HttpHandler
import org.http4k.routing.ResourceLoader
import org.http4k.routing.RoutingHttpHandler
import org.http4k.routing.bind
import org.http4k.routing.routes
import org.http4k.routing.static
class WebMapTileServer {
private val routes: RoutingHttpHandler = routes(
"/tiles" bind static(ResourceLoader.Classpath("/kafkatorio-web-map"))
)
fun build(): HttpHandler {
return routes
}
}

View file

@ -0,0 +1 @@
kafkatorio-web-map/**

View file

@ -5,13 +5,13 @@
<!-- encoders are assigned the type
ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
<encoder>
<pattern>%cyan(%d{HH:mm:ss.SSS}) %gray([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n
<pattern>%cyan(%d{HH:mm:ss.SSS}) %white([%thread]) %highlight(%-5level) %magenta(%logger{36}) - %msg%n
</pattern>
</encoder>
</appender>
<logger name="org.apache.kafka" level="info" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="STDOUT"/>
</logger>
<root level="info">

View file

@ -1,8 +1,10 @@
package dev.adamko.kafkatorio.webmap
import io.kvision.maps.DefaultTileLayers
import io.kvision.maps.externals.leaflet.geo.CRS
import io.kvision.maps.externals.leaflet.geo.LatLng
import io.kvision.maps.Maps
import io.kvision.maps.Maps.Companion.L
import io.kvision.utils.px
//
@ -24,9 +26,16 @@ fun createFactorioMap() = Maps {
margin = 10.px
configureLeafletMap {
setView(LatLng(0, 0), 13)
setView(LatLng(0, 0), 1)
// DefaultTileLayers.OpenStreetMap.addTo(this)
L.tileLayer(
"""http://localhost:9073/tiles/s1/z1/x{x}/y{y}.png"""
) {
attribution = "kafkatorio"
tms = true
minZoom = 1
}.addTo(this)
with(options) {
crs = CRS.Simple