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 66d53db..a9598fc 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 @@ -7,7 +7,7 @@ interface FactorioEventUpdateKey { } interface FactorioEventUpdate extends FactorioEventUpdateKey { - events: string[] | null; + eventCounts: { [key: string]: int } | null; updateType: FactorioEventUpdateType; } @@ -30,7 +30,7 @@ interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey { colour: Colour | null; diedCause: EntityIdentifiers | null; disconnectReason: string | null; - events: string[] | null; + eventCounts: { [key: string]: int } | null; forceIndex: uint | null; index: uint; isAdmin: boolean | null; @@ -57,7 +57,7 @@ interface EntityUpdateKey extends EntityIdentifiers, FactorioEventUpdateKey { interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey { chunkPosition: MapChunkPosition | null; - events: string[] | null; + eventCounts: { [key: string]: int } | null; graphicsVariation: number | null; health: float | null; isActive: boolean | null; @@ -79,7 +79,7 @@ interface MapChunkUpdateKey extends FactorioEventUpdateKey { interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey { chunkPosition: MapChunkPosition; - events: string[] | null; + eventCounts: { [key: string]: int } | null; force: uint | null; isDeleted: boolean | null; player: uint | null; diff --git a/modules/events-mod/src/main/typescript/main/events/onMapChunkEventListeners.ts b/modules/events-mod/src/main/typescript/main/events/onMapChunkEventListeners.ts index 8f5a4ae..f2e6665 100644 --- a/modules/events-mod/src/main/typescript/main/events/onMapChunkEventListeners.ts +++ b/modules/events-mod/src/main/typescript/main/events/onMapChunkEventListeners.ts @@ -41,12 +41,10 @@ function mapTilesUpdateDebounce( ) } - if (data.events == null) { - data.events = [] - } - if (eventName ! in data.events) { - data.events.push(eventName) + if (data.eventCounts == undefined) { + data.eventCounts = {} } + data.eventCounts[eventName] = ((data.eventCounts ?? {}) [eventName] ?? 0) + 1 if (updater != undefined) { updater(data) @@ -111,7 +109,7 @@ script.on_event( const groupedTiles = groupTiles(e.tiles) for (const [chunkPos, tiles] of groupedTiles) { - mapTilesUpdateDebounce(surface, chunkPos, tiles, Converters.eventNameString(e.name)) + mapTilesUpdateDebounce(surface, chunkPos, tiles, Converters.eventNameString(e.name)) } } ) diff --git a/modules/events-mod/src/main/typescript/main/events/onPlayerEventListeners.ts b/modules/events-mod/src/main/typescript/main/events/onPlayerEventListeners.ts index 1463b9e..f844ec3 100644 --- a/modules/events-mod/src/main/typescript/main/events/onPlayerEventListeners.ts +++ b/modules/events-mod/src/main/typescript/main/events/onPlayerEventListeners.ts @@ -17,10 +17,10 @@ function playerUpdateThrottle( mutate(player, data) } - if (data.events == null) { - data.events = [] + if (data.eventCounts == undefined) { + data.eventCounts = {} } - data.events.push(eventName) + data.eventCounts[eventName] = ((data.eventCounts ?? {}) [eventName] ?? 0) + 1 }) ) } @@ -57,6 +57,7 @@ script.on_event( data.name = player.name data.isShowOnMap = player.show_on_map data.isSpectator = player.spectator + data.surfaceIndex = player.surface.index data.tag = player.tag playerOnlineInfo(player, data) } 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 67d45ff..fbcd3a1 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 @@ -13,7 +13,7 @@ import kotlinx.serialization.json.jsonPrimitive @Serializable -class FactorioEventUpdatePacket( +data class FactorioEventUpdatePacket( override val modVersion: String, /** game time */ val tick: Tick, @@ -37,8 +37,8 @@ sealed class FactorioEventUpdate : FactorioEventUpdateKey { @EncodeDefault abstract override val updateType: FactorioEventUpdateType - @Contextual - abstract val events: List? // TODO change this to Map + + abstract val eventCounts: Map? @Serializable enum class FactorioEventUpdateType { @@ -95,8 +95,7 @@ sealed interface PlayerUpdateKey : FactorioEventUpdateKey { data class PlayerUpdate( override val index: PlayerIndex, - @Contextual - override val events: List? = null, + override val eventCounts: Map? = null, val characterUnitNumber: UnitNumber? = null, val chatColour: Colour? = null, @@ -145,8 +144,7 @@ data class EntityUpdate( override val name: String, override val type: String, - @Contextual - override val events: List? = null, + override val eventCounts: Map? = null, val chunkPosition: MapChunkPosition? = null, val graphicsVariation: UShort? = null, @@ -177,8 +175,7 @@ data class MapChunkUpdate( override val chunkPosition: MapChunkPosition, override val surfaceIndex: SurfaceIndex, - @Contextual - override val events: List? = null, + override val eventCounts: Map? = null, val player: PlayerIndex? = null, val robot: EntityIdentifiers? = 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 new file mode 100644 index 0000000..02c1760 --- /dev/null +++ b/modules/events-schema/src/commonTest/kotlin/dev/adamko/kafkatorio/events/schema/UpdatePacketsTest.kt @@ -0,0 +1,149 @@ +package dev.adamko.kafkatorio.events.schema + +import io.kotest.core.spec.style.FunSpec +import io.kotest.matchers.shouldBe +import kotlinx.serialization.decodeFromString +import kotlinx.serialization.encodeToString + +class UpdatePacketsTest : FunSpec({ + + context("player update packet") { + // language=JSON + val json = """ + { + "tick": 66, + "modVersion": "0.3.2", + "packetType": "UPDATE", + "update": { + "afkTime": 0, + "chatColour": { + "red": 1, + "green": 0.4, + "blue": 0.6, + "alpha": 1 + }, + "colour": { + "red": 0.8, + "green": 0.5, + "blue": 0.2, + "alpha": 0.5 + }, + "eventCounts": { + "on_player_changed_position": 7, + "on_player_joined_game": 1 + }, + "forceIndex": 1, + "index": 1, + "isAdmin": true, + "isConnected": true, + "isShowOnMap": true, + "isSpectator": false, + "lastOnline": 0, + "name": "fredthedeadhead", + "onlineTime": 0, + "position": { + "y": 13, + "x": -58 + }, + "tag": "", + "updateType": "PLAYER" + } + } + """.trimIndent() + + test("decode") { + val packet: FactorioEventUpdatePacket = jsonMapperKafkatorio.decodeFromString(json) + + packet shouldBe FactorioEventUpdatePacket( + modVersion = "0.3.2", + tick = Tick(66u), + update = PlayerUpdate( + afkTime = Tick(0u), + 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), + diedCause = null, + disconnectReason = null, + eventCounts = mapOf("on_player_changed_position" to 7u, "on_player_joined_game" to 1u), + forceIndex = ForceIndex(1u), + index = PlayerIndex(1u), + isAdmin = true, + isConnected = true, + isRemoved = null, + isShowOnMap = true, + isSpectator = false, + kickedReason = null, + lastOnline = Tick(0u), + name = "fredthedeadhead", + onlineTime = Tick(0u), + position = MapEntityPosition(-58.0, 13.0), + surfaceIndex = null, + tag = "", + ticksToRespawn = null, + ) + ) + } + + test("encode") { + val v = FactorioEventUpdatePacket( + modVersion = "0.3.2", + tick = Tick(66u), + update = PlayerUpdate( + index = PlayerIndex(1u), + characterUnitNumber = null, + chatColour = null, + colour = null, + name = "fredthedeadhead", + afkTime = Tick(0u), + ticksToRespawn = null, + forceIndex = null, + isAdmin = null, + isConnected = null, + isShowOnMap = true, + isSpectator = false, + lastOnline = Tick(0u), + onlineTime = Tick(0u), + position = MapEntityPosition(-58.0, 13.0), + surfaceIndex = null, + tag = null, + diedCause = null, + bannedReason = null, + kickedReason = null, + disconnectReason = null, + isRemoved = null, + ) + ) + + val string = jsonMapperKafkatorio.encodeToString(v) + // language=JSON + string shouldBe """ + { + "modVersion": "0.3.2", + "tick": 66, + "update": { + "index": 1, + "name": "fredthedeadhead", + "afkTime": 0, + "isShowOnMap": true, + "isSpectator": false, + "lastOnline": 0, + "onlineTime": 0, + "position": { + "x": -58.0, + "y": 13.0 + }, + "updateType": "PLAYER" + }, + "packetType": "UPDATE" + } + """.trimIndent() + } + } + + +}) + +/* +{"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}} + */