From dd45bece5366d8a47774e608cb375307408cd732 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Sun, 20 Feb 2022 22:16:09 +0100 Subject: [PATCH] model updates, and fix tests --- .../generated/kafkatorio-schema/schema.d.ts | 40 ++++++++++++++---- .../events/schema/factorioCommonData.kt | 7 ++++ .../events/schema/factorioEventUpdates.kt | 41 ++++++++++++++----- .../events/schema/kafkatorioPacket.kt | 2 + .../events/schema/FactorioEventTest.kt | 13 +++--- .../topology/factorioServerPackets.kt | 18 ++++---- 6 files changed, 90 insertions(+), 31 deletions(-) diff --git a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/schema.d.ts b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/schema.d.ts index 3ba828d..41a6c66 100644 --- a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/schema.d.ts +++ b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/schema.d.ts @@ -1,12 +1,25 @@ // Generated by TypeScriptGenerator - do not edit this file manually -type PacketType = "EVENT" | "CONFIG" | "PROTOTYPES"; +type PacketType = "EVENT" | "CONFIG" | "PROTOTYPES" | "UPDATE"; interface KafkatorioPacket { modVersion: string; packetType: PacketType; } +type FactorioEventUpdateType = "PLAYER" | "MAP_CHUNK" | "ENTITY"; + +interface FactorioEventUpdate { + updateType: FactorioEventUpdateType; +} + +interface FactorioEventUpdatePacket extends KafkatorioPacket { + modVersion: string; + packetType: PacketType; + tick: uint; + update: FactorioEventUpdate; +} + interface FactorioEvent extends KafkatorioPacket { data: FactorioObjectData; eventType: string; @@ -80,33 +93,39 @@ interface MapTile { prototypeName: string; } -type FactorioEventUpdateType = "PLAYER" | "MAP_CHUNK" | "ENTITY"; - -interface FactorioEventUpdate { - updateType: FactorioEventUpdateType; +interface EntityIdentifiers { + name: string; + type: string; + unitNumber: uint | null; } interface PlayerUpdate extends FactorioEventUpdate { + afkTime: uint | null; + bannedReason: string | null; characterUnitNumber: uint | null; chatColour: Colour | null; colour: Colour | null; + diedCause: EntityIdentifiers | null; disconnectReason: string | null; forceIndex: uint | null; index: uint; isAdmin: boolean | null; isConnected: boolean | null; + isRemoved: boolean | null; isShowOnMap: boolean | null; isSpectator: boolean | null; + kickedReason: string | null; lastOnline: uint | null; name: string | null; onlineTime: uint | null; position: MapEntityPosition | null; surfaceIndex: uint | null; tag: string | null; + ticksToRespawn: uint | null; updateType: FactorioEventUpdateType; } -interface EntityUpdate extends FactorioEventUpdate { +interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers { chunkPosition: MapChunkPosition | null; graphicsVariation: number | null; health: float | null; @@ -115,17 +134,20 @@ interface EntityUpdate extends FactorioEventUpdate { lastUser: int | null; localisedDescription: string | null; localisedName: string | null; - name: string | null; + name: string; prototype: string | null; - type: string | null; + type: string; unitNumber: uint; updateType: FactorioEventUpdateType; } interface MapChunkUpdate extends FactorioEventUpdate { chunkPosition: MapChunkPosition; + force: uint | null; isDeleted: boolean | null; + player: uint | null; + robot: EntityIdentifiers | null; surfaceIndex: uint; - tiles: MapTiles | null; + tiles: Map | null; updateType: FactorioEventUpdateType; } diff --git a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioCommonData.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioCommonData.kt index f5cdf9c..0ce9fe3 100644 --- a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioCommonData.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioCommonData.kt @@ -101,3 +101,10 @@ data class MapTile( val prototypeName: PrototypeName, val position: MapTilePosition, ) + + +interface EntityIdentifiers { + val unitNumber: UnitNumber? + val name: String + val type: String +} diff --git a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEventUpdates.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEventUpdates.kt index a109ca7..61b85bc 100644 --- a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEventUpdates.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/factorioEventUpdates.kt @@ -10,6 +10,18 @@ import kotlinx.serialization.json.jsonObject import kotlinx.serialization.json.jsonPrimitive +@Serializable +class FactorioEventUpdatePacket( + override val modVersion: String, + /** game time */ + val tick: Tick, + val update: FactorioEventUpdate, +) : KafkatorioPacket() { + @EncodeDefault + override val packetType: PacketType = PacketType.UPDATE +} + + @Serializable(with = FactorioEventUpdate.Companion.JsonSerializer::class) sealed class FactorioEventUpdate { @@ -68,7 +80,8 @@ data class PlayerUpdate( val colour: Colour? = null, val name: String? = null, - val disconnectReason: String? = null, + val afkTime: Tick? = null, + val ticksToRespawn: Tick? = null, val forceIndex: ForceIndex? = null, val isAdmin: Boolean? = null, val isConnected: Boolean? = null, @@ -79,6 +92,13 @@ data class PlayerUpdate( val position: MapEntityPosition? = null, val surfaceIndex: SurfaceIndex? = null, val tag: String? = null, + val diedCause: EntityIdentifiers? = null, + + val bannedReason: String? = null, + val kickedReason: String? = null, + val disconnectReason: String? = null, + /** `true` when a player is removed (deleted) from the game */ + val isRemoved: Boolean? = null, ) : FactorioEventUpdate() { @EncodeDefault override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.PLAYER @@ -87,14 +107,11 @@ data class PlayerUpdate( @Serializable data class EntityUpdate( - /** - * A [unitNumber] is required for caching, else the Entity Update should be emitted without - * debouncing/throttling - */ - val unitNumber: UnitNumber, + /** A [unitNumber] is required for caching */ + override val unitNumber: UnitNumber, + override val name: String, + override val type: String, - val name: String? = null, - val type: String? = null, val chunkPosition: MapChunkPosition? = null, val graphicsVariation: UShort? = null, val health: Float? = null, @@ -104,7 +121,7 @@ data class EntityUpdate( val localisedDescription: String? = null, val localisedName: String? = null, val prototype: PrototypeName? = null, -) : FactorioEventUpdate() { +) : FactorioEventUpdate(), EntityIdentifiers { @EncodeDefault override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.ENTITY } @@ -115,8 +132,12 @@ data class MapChunkUpdate( val chunkPosition: MapChunkPosition, val surfaceIndex: SurfaceIndex, + val player: PlayerIndex? = null, + val robot: EntityIdentifiers? = null, + val force: ForceIndex? = null, + /** Might be a partial list of updated tiles */ - val tiles: MapTiles? = null, + val tiles: Map? = null, val isDeleted: Boolean? = null, ) : FactorioEventUpdate() { @EncodeDefault diff --git a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/kafkatorioPacket.kt b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/kafkatorioPacket.kt index fd59bc2..7d852f1 100644 --- a/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/kafkatorioPacket.kt +++ b/modules/events-schema/src/commonMain/kotlin/dev/adamko/kafkatorio/events/schema/kafkatorioPacket.kt @@ -21,6 +21,7 @@ sealed class KafkatorioPacket { EVENT, CONFIG, PROTOTYPES, + UPDATE, ; companion object { @@ -51,6 +52,7 @@ object KafkatorioPacketJsonSerializer : JsonContentPolymorphicSerializer FactorioEvent.serializer() KafkatorioPacket.PacketType.CONFIG -> FactorioConfigurationUpdate.serializer() KafkatorioPacket.PacketType.PROTOTYPES -> FactorioPrototypes.serializer() + KafkatorioPacket.PacketType.UPDATE -> FactorioEventUpdatePacket.serializer() } } } diff --git a/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt b/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt index 400b8bf..6597434 100644 --- a/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt +++ b/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/FactorioEventTest.kt @@ -15,6 +15,8 @@ class FactorioEventTest : FunSpec({ { "data": { "objectName": "LuaPlayer", + "force": 1, + "surface": 1, "name": "fredthedeadhead", "characterUnitNumber": 1, "associatedCharactersUnitNumbers": {}, @@ -145,7 +147,8 @@ class FactorioEventTest : FunSpec({ "active": true, "health": 250, "healthRatio": 1, - "surfaceIndex": 1, + "surface": 1, + "force": 1, "unitNumber": 1, "position": { "x": 37.5, @@ -251,19 +254,19 @@ class FactorioEventTest : FunSpec({ surfaceIndex = SurfaceIndex(1u), tiles = listOf( MapTile( - prototypeName = "refined-concrete", + prototypeName = PrototypeName("refined-concrete"), position = MapTilePosition(-65, 10) ), MapTile( - prototypeName = "red-earth", + prototypeName = PrototypeName("red-earth"), position = MapTilePosition(-65, 11) ), MapTile( - prototypeName = "blue-cheese", + prototypeName = PrototypeName("blue-cheese"), position = MapTilePosition(-64, 10) ), MapTile( - prototypeName = "refined-concrete", + prototypeName = PrototypeName("refined-concrete"), position = MapTilePosition(-64, 11) ), ) diff --git a/modules/events-server/src/main/kotlin/dev/adamko/kafkatorio/processor/topology/factorioServerPackets.kt b/modules/events-server/src/main/kotlin/dev/adamko/kafkatorio/processor/topology/factorioServerPackets.kt index d1869d3..9371f9f 100644 --- a/modules/events-server/src/main/kotlin/dev/adamko/kafkatorio/processor/topology/factorioServerPackets.kt +++ b/modules/events-server/src/main/kotlin/dev/adamko/kafkatorio/processor/topology/factorioServerPackets.kt @@ -2,6 +2,7 @@ package dev.adamko.kafkatorio.processor.topology import dev.adamko.kafkatorio.events.schema.FactorioConfigurationUpdate import dev.adamko.kafkatorio.events.schema.FactorioEvent +import dev.adamko.kafkatorio.events.schema.FactorioEventUpdatePacket import dev.adamko.kafkatorio.events.schema.FactorioPrototypes import dev.adamko.kafkatorio.events.schema.KafkatorioPacket import dev.adamko.kafkatorio.processor.KafkatorioTopology @@ -49,13 +50,16 @@ fun splitFactorioServerPacketStream( ) ) { _: FactorioServerId, value: KafkatorioPacket, _: RecordContext -> // println("[$key] sending event:${value.eventType} to topic:${value.data.objectName()}") - when (value) { - is FactorioEvent -> - "kafkatorio.${value.packetType.name}.${value.data.objectName.name}" - is FactorioConfigurationUpdate -> - "kafkatorio.${value.packetType.name}.FactorioConfigurationUpdate" - is FactorioPrototypes -> - "kafkatorio.${value.packetType.name}.all" + + val base = "kafkatorio.${value.packetType.name}" + + val type: String = when (value) { + is FactorioEvent -> value.data.objectName.name + is FactorioConfigurationUpdate -> "FactorioConfigurationUpdate" + is FactorioPrototypes -> "all" + is FactorioEventUpdatePacket -> "all" } + + "$base.$type" } }