update packet, add 'eventCounts'

This commit is contained in:
Adam 2022-02-26 22:23:08 +01:00
parent b42eaaa67e
commit 1ba4371b14
5 changed files with 167 additions and 22 deletions

View file

@ -7,7 +7,7 @@ interface FactorioEventUpdateKey {
} }
interface FactorioEventUpdate extends FactorioEventUpdateKey { interface FactorioEventUpdate extends FactorioEventUpdateKey {
events: string[] | null; eventCounts: { [key: string]: int } | null;
updateType: FactorioEventUpdateType; updateType: FactorioEventUpdateType;
} }
@ -30,7 +30,7 @@ interface PlayerUpdate extends FactorioEventUpdate, PlayerUpdateKey {
colour: Colour | null; colour: Colour | null;
diedCause: EntityIdentifiers | null; diedCause: EntityIdentifiers | null;
disconnectReason: string | null; disconnectReason: string | null;
events: string[] | null; eventCounts: { [key: string]: int } | null;
forceIndex: uint | null; forceIndex: uint | null;
index: uint; index: uint;
isAdmin: boolean | null; isAdmin: boolean | null;
@ -57,7 +57,7 @@ interface EntityUpdateKey extends EntityIdentifiers, FactorioEventUpdateKey {
interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey { interface EntityUpdate extends FactorioEventUpdate, EntityIdentifiers, EntityUpdateKey {
chunkPosition: MapChunkPosition | null; chunkPosition: MapChunkPosition | null;
events: string[] | null; eventCounts: { [key: string]: int } | null;
graphicsVariation: number | null; graphicsVariation: number | null;
health: float | null; health: float | null;
isActive: boolean | null; isActive: boolean | null;
@ -79,7 +79,7 @@ interface MapChunkUpdateKey extends FactorioEventUpdateKey {
interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey { interface MapChunkUpdate extends FactorioEventUpdate, MapChunkUpdateKey {
chunkPosition: MapChunkPosition; chunkPosition: MapChunkPosition;
events: string[] | null; eventCounts: { [key: string]: int } | null;
force: uint | null; force: uint | null;
isDeleted: boolean | null; isDeleted: boolean | null;
player: uint | null; player: uint | null;

View file

@ -41,12 +41,10 @@ function mapTilesUpdateDebounce(
) )
} }
if (data.events == null) { if (data.eventCounts == undefined) {
data.events = [] data.eventCounts = {}
}
if (eventName ! in data.events) {
data.events.push(eventName)
} }
data.eventCounts[eventName] = ((data.eventCounts ?? {}) [eventName] ?? 0) + 1
if (updater != undefined) { if (updater != undefined) {
updater(data) updater(data)
@ -111,7 +109,7 @@ script.on_event(
const groupedTiles = groupTiles(e.tiles) const groupedTiles = groupTiles(e.tiles)
for (const [chunkPos, tiles] of groupedTiles) { for (const [chunkPos, tiles] of groupedTiles) {
mapTilesUpdateDebounce(surface, chunkPos, tiles, Converters.eventNameString(e.name)) mapTilesUpdateDebounce(surface, chunkPos, tiles, Converters.eventNameString(e.name))
} }
} }
) )

View file

@ -17,10 +17,10 @@ function playerUpdateThrottle(
mutate(player, data) mutate(player, data)
} }
if (data.events == null) { if (data.eventCounts == undefined) {
data.events = [] 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.name = player.name
data.isShowOnMap = player.show_on_map data.isShowOnMap = player.show_on_map
data.isSpectator = player.spectator data.isSpectator = player.spectator
data.surfaceIndex = player.surface.index
data.tag = player.tag data.tag = player.tag
playerOnlineInfo(player, data) playerOnlineInfo(player, data)
} }

View file

@ -13,7 +13,7 @@ import kotlinx.serialization.json.jsonPrimitive
@Serializable @Serializable
class FactorioEventUpdatePacket( data class FactorioEventUpdatePacket(
override val modVersion: String, override val modVersion: String,
/** game time */ /** game time */
val tick: Tick, val tick: Tick,
@ -37,8 +37,8 @@ sealed class FactorioEventUpdate : FactorioEventUpdateKey {
@EncodeDefault @EncodeDefault
abstract override val updateType: FactorioEventUpdateType abstract override val updateType: FactorioEventUpdateType
@Contextual
abstract val events: List<String>? // TODO change this to Map<String, UInt> abstract val eventCounts: Map<String, UInt>?
@Serializable @Serializable
enum class FactorioEventUpdateType { enum class FactorioEventUpdateType {
@ -95,8 +95,7 @@ sealed interface PlayerUpdateKey : FactorioEventUpdateKey {
data class PlayerUpdate( data class PlayerUpdate(
override val index: PlayerIndex, override val index: PlayerIndex,
@Contextual override val eventCounts: Map<String, UInt>? = null,
override val events: List<String>? = null,
val characterUnitNumber: UnitNumber? = null, val characterUnitNumber: UnitNumber? = null,
val chatColour: Colour? = null, val chatColour: Colour? = null,
@ -145,8 +144,7 @@ data class EntityUpdate(
override val name: String, override val name: String,
override val type: String, override val type: String,
@Contextual override val eventCounts: Map<String, UInt>? = null,
override val events: List<String>? = null,
val chunkPosition: MapChunkPosition? = null, val chunkPosition: MapChunkPosition? = null,
val graphicsVariation: UShort? = null, val graphicsVariation: UShort? = null,
@ -177,8 +175,7 @@ data class MapChunkUpdate(
override val chunkPosition: MapChunkPosition, override val chunkPosition: MapChunkPosition,
override val surfaceIndex: SurfaceIndex, override val surfaceIndex: SurfaceIndex,
@Contextual override val eventCounts: Map<String, UInt>? = null,
override val events: List<String>? = null,
val player: PlayerIndex? = null, val player: PlayerIndex? = null,
val robot: EntityIdentifiers? = null, val robot: EntityIdentifiers? = null,

View file

@ -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}}
*/