From 1fbe0d87b0d7a22341a0d9ff9eecb566fa445f42 Mon Sep 17 00:00:00 2001 From: Adam <897017+aSemy@users.noreply.github.com> Date: Mon, 14 Mar 2022 00:01:51 +0100 Subject: [PATCH] fix EntityIdentifiers deserialization bug (kxs has polymorphism problems with interfaces) --- .../generated/kafkatorio-schema/update.d.ts | 6 +- .../events/schema/factorioCommonData.kt | 10 +++ .../events/schema/factorioEventUpdates.kt | 6 +- .../events/schema/UpdatePacketsTest.kt | 71 +++++++++++++++++-- 4 files changed, 82 insertions(+), 11 deletions(-) diff --git a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts index ed61598..058fb45 100644 --- a/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts +++ b/modules/events-mod/src/main/typescript/generated/kafkatorio-schema/update.d.ts @@ -28,7 +28,7 @@ interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey { characterUnitNumber: uint | null; chatColour: Colour | null; colour: Colour | null; - diedCause: EntityIdentifiers | null; + diedCause: EntityIdentifiersData | null; disconnectReason: string | null; eventCounts: { [key: string]: uint } | null; forceIndex: uint | null; @@ -55,7 +55,7 @@ interface EntityUpdateKey extends EntityIdentifiers, FactorioEventUpdateKey { unitNumber: uint; } -interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey { +interface EntityUpdate extends FactorioEventUpdate, EntityUpdateKey { chunkPosition: MapChunkPosition | null; eventCounts: { [key: string]: uint } | null; graphicsVariation: uint8 | null; @@ -83,7 +83,7 @@ interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey { force: uint | null; isDeleted: boolean | null; player: uint | null; - robot: EntityIdentifiers | null; + robot: EntityIdentifiersData | null; surfaceIndex: uint; tileDictionary: MapTileDictionary | 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 65b7e36..fb91302 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 @@ -102,6 +102,16 @@ interface EntityIdentifiers { } +// instant of EntityIdentifiers, because kxs needs a concrete instance +@Serializable +data class EntityIdentifiersData( + override val unitNumber: UnitNumber? = null, + override val name: String, + /** The prototype-type of the entity. */ + override val type: String, +) : EntityIdentifiers + + /** * Size-optimised 2D map of x/y tile positions and prototype-name. * 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 e930361..2e7441d 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 @@ -113,7 +113,7 @@ data class PlayerUpdate( val position: MapEntityPosition? = null, val surfaceIndex: SurfaceIndex? = null, val tag: String? = null, - val diedCause: EntityIdentifiers? = null, + val diedCause: EntityIdentifiersData? = null, val bannedReason: String? = null, val kickedReason: String? = null, @@ -154,7 +154,7 @@ data class EntityUpdate( val localisedDescription: String? = null, val localisedName: String? = null, val prototype: PrototypeName? = null, -) : FactorioEventUpdate(), EntityIdentifiers, EntityUpdateKey { +) : FactorioEventUpdate(), EntityUpdateKey { @EncodeDefault override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.ENTITY } @@ -177,7 +177,7 @@ data class MapChunkUpdate( override val eventCounts: Map? = null, val player: PlayerIndex? = null, - val robot: EntityIdentifiers? = null, + val robot: EntityIdentifiersData? = null, val force: ForceIndex? = null, /** updated tiles - might be partial and not all tiles in the chunk */ val tileDictionary: MapTileDictionary? = null, diff --git a/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/UpdatePacketsTest.kt b/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/UpdatePacketsTest.kt index 02c1760..638ceea 100644 --- a/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/UpdatePacketsTest.kt +++ b/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/UpdatePacketsTest.kt @@ -62,7 +62,7 @@ class UpdatePacketsTest : FunSpec({ bannedReason = null, characterUnitNumber = null, chatColour = Colour(red = 1f, green = 0.4f, blue = 0.6f, alpha = 1f), - colour = Colour(red = 0.8f, green = 0.5f, blue = 0.2f, alpha = 0.5f), + colour = Colour(red = 0.8f, green = 0.5f, blue = 0.2f, alpha = 0.5f), diedCause = null, disconnectReason = null, eventCounts = mapOf("on_player_changed_position" to 7u, "on_player_joined_game" to 1u), @@ -141,9 +141,70 @@ class UpdatePacketsTest : FunSpec({ } } + context("player died") { + // language=JSON + val json = """ + { + "tick": 227934, + "modVersion": "0.3.2", + "packetType": "UPDATE", + "update": { + "index": 1, + "updateType": "PLAYER", + "lastOnline": 227932, + "onlineTime": 227888, + "afkTime": 324, + "isConnected": true, + "diedCause": { + "unitNumber": 66, + "name": "small-biter", + "type": "unit" + }, + "eventCounts": { + "on_player_died": 2 + } + } + } + """.trimIndent() + + test("decode") { + val packet: FactorioEventUpdatePacket = jsonMapperKafkatorio.decodeFromString(json) + + packet shouldBe FactorioEventUpdatePacket( + modVersion = "0.3.2", + tick = Tick(227934u), + update = PlayerUpdate( + afkTime = Tick(324u), + bannedReason = null, + characterUnitNumber = null, + chatColour = null, + colour = null, + diedCause = EntityIdentifiersData( + unitNumber = UnitNumber(66u), + name = "small-biter", + type = "unit", + ), + disconnectReason = null, + eventCounts = mapOf("on_player_died" to 2u), + forceIndex = null, + index = PlayerIndex(1u), + isAdmin = null, + isConnected = true, + isRemoved = null, + isShowOnMap = null, + isSpectator = null, + kickedReason = null, + lastOnline = Tick(227932u), + name = null, + onlineTime = Tick(227888u), + position = null, + surfaceIndex = null, + tag = null, + ticksToRespawn = null, + ) + ) + } + + } }) - -/* -{"tick":66,"modVersion":"0.3.2","packetType":"UPDATE","update":{"index":1,"updateType":"PLAYER","position":{"y":13.5234375,"x":-58.9375},"eventCounts":{"on_player_changed_position":7,"on_player_joined_game":1},"isAdmin":true,"chatColour":{"red":1,"green":0.62999999523162841796875,"blue":0.259000003337860107421875,"alpha":1},"colour":{"red":0.869000017642974853515625,"green":0.5,"blue":0.12999999523162841796875,"alpha":0.5},"forceIndex":1,"name":"fredthedeadhead","isShowOnMap":true,"isSpectator":false,"tag":"","lastOnline":0,"onlineTime":0,"afkTime":0,"isConnected":true}} - */