Add a research page, and keep the topics that views are rebuilt from

The mod sends the research state of a force (researched technologies,
levels, current research, progress and queue) whenever research changes,
and every minute for the progress. events-server-web keeps the latest state
and serves it at /kafkatorio/data/servers/{id}/research.

The web map has a research page (/servers/{id}/research, linked from the
header), with:
- progress, the current research and the queue
- the science packs needed for everything that's left
- every technology, filterable by status, science pack and name, with its
  cost (including levelled technologies' count formulas)
- details: the total cost to reach a technology, its prerequisites, what it
  leads to and what it unlocks

The technology tree comes from export_building_icons.py, which now also
exports technologies.json, items.json and technology/science pack icons.

Kafka deleted data older than 7 days, but the building, resource and
research views are rebuilt by reading their topics from the beginning, so
buildings disappeared after a restart. These topics are now kept forever.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Lazzexus 2026-11-03 10:46:09 +01:00
parent 930d746336
commit 8770a0617b
23 changed files with 1517 additions and 11 deletions

View file

@ -5,6 +5,7 @@ import dev.adamko.kafkatorio.processor.core.launchTopology
import dev.adamko.kafkatorio.server.processor.topology.broadcastToWebsocket
import dev.adamko.kafkatorio.server.processor.topology.saveMapTiles
import dev.adamko.kafkatorio.server.web.buildings.BuildingService
import dev.adamko.kafkatorio.server.web.research.ResearchService
import dev.adamko.kafkatorio.server.web.resources.ResourcePatchService
import dev.adamko.kafkatorio.server.web.surfaces.SurfaceService
import dev.adamko.kafkatorio.server.web.websocket.WebmapWebsocketServer
@ -23,6 +24,7 @@ suspend fun main(): Unit = coroutineScope {
val resourcePatchService = ResourcePatchService(appProps)
val buildingService = BuildingService(appProps)
val surfaceService = SurfaceService(appProps)
val researchService = ResearchService(appProps)
launch {
websocketBroadcaster(wsServer)
@ -48,6 +50,10 @@ suspend fun main(): Unit = coroutineScope {
surfaceService.consume()
}
launch {
researchService.consume()
}
launch {
startWebServer(
appProps = appProps,
@ -55,6 +61,7 @@ suspend fun main(): Unit = coroutineScope {
resourcePatchService = resourcePatchService,
buildingService = buildingService,
surfaceService = surfaceService,
researchService = researchService,
)
}
}

View file

@ -0,0 +1,37 @@
package dev.adamko.kafkatorio.server.web.research
import dev.adamko.kafkatorio.processor.config.ApplicationProperties
import dev.adamko.kafkatorio.processor.config.topicName
import dev.adamko.kafkatorio.schema.common.FactorioServerId
import dev.adamko.kafkatorio.schema.packets.ResearchUpdate
import dev.adamko.kafkatorio.server.web.kafka.followTopic
import java.util.concurrent.ConcurrentHashMap
/** Keeps the latest research state of each force, for the research page. */
class ResearchService(
private val appProps: ApplicationProperties,
) {
private val research = ConcurrentHashMap<Pair<FactorioServerId, String>, ResearchUpdate>()
/** The mod sends the research state when research changes, and regularly with the progress. */
suspend fun consume(): Unit = followTopic(
appProps,
ResearchUpdate::class.topicName,
clientId = "kafkatorio.events-server-web.research",
) { serverId, data ->
if (data is ResearchUpdate) research[serverId to data.forceName] = data
}
fun research(serverId: FactorioServerId, forceName: String = PLAYER_FORCE): ResearchUpdate? =
research[serverId to forceName]
companion object {
/** The force that players are in, by default */
const val PLAYER_FORCE = "player"
}
}

View file

@ -9,7 +9,9 @@ import dev.adamko.kafkatorio.processor.config.ApplicationProperties
import dev.adamko.kafkatorio.server.web.config.jsonMapper
import dev.adamko.kafkatorio.server.web.buildings.BuildingService
import dev.adamko.kafkatorio.server.web.resources.ResourcePatchService
import dev.adamko.kafkatorio.server.web.research.ResearchService
import dev.adamko.kafkatorio.server.web.surfaces.SurfaceService
import dev.adamko.kafkatorio.schema.packets.ResearchUpdate
import dev.adamko.kafkatorio.schema.common.MapSurface
import dev.adamko.kafkatorio.schema.common.MapBuilding
import dev.adamko.kafkatorio.schema.common.MapEntityPosition
@ -38,6 +40,7 @@ internal fun Routing.factorioServersRoutes(
resourcePatchService: ResourcePatchService,
buildingService: BuildingService,
surfaceService: SurfaceService,
researchService: ResearchService,
) {
get<FactorioServers> {
@ -51,6 +54,15 @@ internal fun Routing.factorioServersRoutes(
call.respond(jsonMapper.encodeToJsonElement(ListSerializer(MapSurface.serializer()), surfaces))
}
get<FactorioServer.Research> { request ->
val research = researchService.research(request.server.factorioServerId)
if (research == null) {
call.respond(HttpStatusCode.NotFound)
} else {
call.respond(jsonMapper.encodeToJsonElement(ResearchUpdate.serializer(), research))
}
}
get<FactorioServer.MapData.Tile> { tile ->
handleTileRequest(tile, appProps)
}
@ -158,6 +170,13 @@ data class FactorioServer(
)
}
/** The research state of the players' force */
@Serializable
@Resource("research")
data class Research(
val server: FactorioServer,
)
@Serializable
@Resource("players")
data class Players(

View file

@ -3,6 +3,7 @@ package dev.adamko.kafkatorio.server.web.rest
import dev.adamko.kafkatorio.processor.config.ApplicationProperties
import dev.adamko.kafkatorio.server.web.buildings.BuildingService
import dev.adamko.kafkatorio.server.web.resources.ResourcePatchService
import dev.adamko.kafkatorio.server.web.research.ResearchService
import dev.adamko.kafkatorio.server.web.surfaces.SurfaceService
import io.ktor.http.ContentType
import io.ktor.http.HttpStatusCode
@ -30,6 +31,7 @@ internal fun Application.webmapTileServer(
resourcePatchService: ResourcePatchService,
buildingService: BuildingService,
surfaceService: SurfaceService,
researchService: ResearchService,
) {
install(AutoHeadResponse)
@ -59,6 +61,6 @@ internal fun Application.webmapTileServer(
}
routing {
factorioServersRoutes(appProps, resourcePatchService, buildingService, surfaceService)
factorioServersRoutes(appProps, resourcePatchService, buildingService, surfaceService, researchService)
}
}

View file

@ -3,6 +3,7 @@ package dev.adamko.kafkatorio.server.web
import dev.adamko.kafkatorio.processor.config.ApplicationProperties
import dev.adamko.kafkatorio.server.web.buildings.BuildingService
import dev.adamko.kafkatorio.server.web.research.ResearchService
import dev.adamko.kafkatorio.server.web.resources.ResourcePatchService
import dev.adamko.kafkatorio.server.web.surfaces.SurfaceService
import dev.adamko.kafkatorio.server.web.rest.webmapTileServer
@ -19,6 +20,7 @@ internal suspend fun startWebServer(
resourcePatchService: ResourcePatchService,
buildingService: BuildingService,
surfaceService: SurfaceService,
researchService: ResearchService,
): Unit = coroutineScope {
embeddedServer(
@ -28,7 +30,7 @@ internal suspend fun startWebServer(
webmapWebsocketServer(wsServer)
webmapTileServer(appProps, resourcePatchService, buildingService, surfaceService)
webmapTileServer(appProps, resourcePatchService, buildingService, surfaceService, researchService)
}.start(wait = true)
}