- make each event update have a key/data interface

- separate .d.ts file for event updates
-  fix initialisation of global items
This commit is contained in:
Adam 2022-02-23 11:51:15 +01:00
parent 494913d3b9
commit 5208d9affd
14 changed files with 451 additions and 266 deletions

View file

@ -1,5 +1,6 @@
package dev.adamko.kafkatorio.events.schema
import dev.adamko.kafkatorio.events.schema.FactorioEventUpdate.FactorioEventUpdateType
import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.Serializable
@ -22,11 +23,24 @@ class FactorioEventUpdatePacket(
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
sealed interface FactorioEventUpdateKey {
val updateType: FactorioEventUpdateType
}
sealed interface FactorioEventUpdateData {
val updateType: FactorioEventUpdateType
}
@Serializable(with = FactorioEventUpdate.Companion.JsonSerializer::class)
sealed class FactorioEventUpdate {
sealed class FactorioEventUpdate : FactorioEventUpdateKey, FactorioEventUpdateData {
@EncodeDefault
abstract val updateType: FactorioEventUpdateType
abstract override val updateType: FactorioEventUpdateType
@Serializable
enum class FactorioEventUpdateType {
@ -61,7 +75,7 @@ sealed class FactorioEventUpdate {
return when (type) {
FactorioEventUpdateType.PLAYER -> PlayerUpdate.serializer()
FactorioEventUpdateType.MAP_CHUNK -> TODO()
FactorioEventUpdateType.MAP_CHUNK -> MapChunkUpdate.serializer()
FactorioEventUpdateType.ENTITY -> EntityUpdate.serializer()
}
}
@ -71,75 +85,154 @@ sealed class FactorioEventUpdate {
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
sealed interface PlayerUpdateKey : FactorioEventUpdateKey {
val index: PlayerIndex
}
sealed interface PlayerUpdateData : FactorioEventUpdateData {
val characterUnitNumber: UnitNumber?
val chatColour: Colour?
val colour: Colour?
val name: String?
val afkTime: Tick?
val ticksToRespawn: Tick?
val forceIndex: ForceIndex?
val isAdmin: Boolean?
val isConnected: Boolean?
val isShowOnMap: Boolean?
val isSpectator: Boolean?
val lastOnline: Tick?
val onlineTime: Tick?
val position: MapEntityPosition?
val surfaceIndex: SurfaceIndex?
val tag: String?
val diedCause: EntityIdentifiers?
val bannedReason: String?
val kickedReason: String?
val disconnectReason: String?
/** `true` when a player is removed (deleted) from the game */
val isRemoved: Boolean?
}
@Serializable
data class PlayerUpdate(
val index: PlayerIndex,
override val index: PlayerIndex,
val characterUnitNumber: UnitNumber? = null,
val chatColour: Colour? = null,
val colour: Colour? = null,
val name: String? = null,
override val characterUnitNumber: UnitNumber? = null,
override val chatColour: Colour? = null,
override val colour: Colour? = null,
override val name: String? = null,
val afkTime: Tick? = null,
val ticksToRespawn: Tick? = null,
val forceIndex: ForceIndex? = null,
val isAdmin: Boolean? = null,
val isConnected: Boolean? = null,
val isShowOnMap: Boolean? = null,
val isSpectator: Boolean? = null,
val lastOnline: Tick? = null,
val onlineTime: Tick? = null,
val position: MapEntityPosition? = null,
val surfaceIndex: SurfaceIndex? = null,
val tag: String? = null,
val diedCause: EntityIdentifiers? = null,
override val afkTime: Tick? = null,
override val ticksToRespawn: Tick? = null,
override val forceIndex: ForceIndex? = null,
override val isAdmin: Boolean? = null,
override val isConnected: Boolean? = null,
override val isShowOnMap: Boolean? = null,
override val isSpectator: Boolean? = null,
override val lastOnline: Tick? = null,
override val onlineTime: Tick? = null,
override val position: MapEntityPosition? = null,
override val surfaceIndex: SurfaceIndex? = null,
override val tag: String? = null,
override val diedCause: EntityIdentifiers? = null,
val bannedReason: String? = null,
val kickedReason: String? = null,
val disconnectReason: String? = null,
override val bannedReason: String? = null,
override val kickedReason: String? = null,
override val disconnectReason: String? = null,
/** `true` when a player is removed (deleted) from the game */
val isRemoved: Boolean? = null,
) : FactorioEventUpdate() {
override val isRemoved: Boolean? = null,
) : FactorioEventUpdate(), PlayerUpdateKey, PlayerUpdateData {
@EncodeDefault
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.PLAYER
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
sealed interface EntityUpdateKey : EntityIdentifiers, FactorioEventUpdateKey {
/** A [unitNumber] is required for caching */
override val unitNumber: UnitNumber
override val name: String
override val type: String
}
sealed interface EntityUpdateData : FactorioEventUpdateData {
val chunkPosition: MapChunkPosition?
val graphicsVariation: UShort?
val health: Float?
val isActive: Boolean?
val isRotatable: Boolean?
val lastUser: UInt?
val localisedDescription: String?
val localisedName: String?
val prototype: PrototypeName?
}
@Serializable
data class EntityUpdate(
/** A [unitNumber] is required for caching */
override val unitNumber: UnitNumber,
override val name: String,
override val type: String,
val chunkPosition: MapChunkPosition? = null,
val graphicsVariation: UShort? = null,
val health: Float? = null,
val isActive: Boolean? = null,
val isRotatable: Boolean? = null,
val lastUser: UInt? = null,
val localisedDescription: String? = null,
val localisedName: String? = null,
val prototype: PrototypeName? = null,
) : FactorioEventUpdate(), EntityIdentifiers {
override val chunkPosition: MapChunkPosition? = null,
override val graphicsVariation: UShort? = null,
override val health: Float? = null,
override val isActive: Boolean? = null,
override val isRotatable: Boolean? = null,
override val lastUser: UInt? = null,
override val localisedDescription: String? = null,
override val localisedName: String? = null,
override val prototype: PrototypeName? = null,
) : FactorioEventUpdate(), EntityIdentifiers, EntityUpdateKey, EntityUpdateData {
@EncodeDefault
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.ENTITY
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
sealed interface MapChunkUpdateKey : FactorioEventUpdateKey {
val chunkPosition: MapChunkPosition
val surfaceIndex: SurfaceIndex
}
sealed interface MapChunkUpdateData : FactorioEventUpdateData {
val player: PlayerIndex?
val robot: EntityIdentifiers?
val force: ForceIndex?
/** updated tiles - might be partial */
val tiles: List<MapTile>?
val isDeleted: Boolean?
}
@Serializable
data class MapChunkUpdate(
val chunkPosition: MapChunkPosition,
val surfaceIndex: SurfaceIndex,
override val chunkPosition: MapChunkPosition,
override 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: Map<MapTilePosition, PrototypeName>? = null,
val isDeleted: Boolean? = null,
) : FactorioEventUpdate() {
override val player: PlayerIndex? = null,
override val robot: EntityIdentifiers? = null,
override val force: ForceIndex? = null,
// note: this must be a List, not a Map<Location, ProtoName>
// because JSON can't have non-string keys.
override val tiles: List<MapTile>? = null,
override val isDeleted: Boolean? = null,
) : FactorioEventUpdate(), MapChunkUpdateKey, MapChunkUpdateData {
@EncodeDefault
override val updateType: FactorioEventUpdateType = FactorioEventUpdateType.MAP_CHUNK
}
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

View file

@ -14,12 +14,19 @@ fun main(args: Array<String>) {
TypeScriptGenerator(
rootClasses = buildSet {
addAll(KafkatorioPacket::class.sealedSubclasses)
addAll(FactorioObjectData::class.sealedSubclasses)
addAll(FactorioPrototype::class.sealedSubclasses)
add(FactorioPrototypes::class)
add(FactorioConfigurationUpdate::class)
add(FactorioEventUpdatePacket::class)
addAll(FactorioEventUpdate::class.sealedSubclasses)
addAll(FactorioEventUpdateKey::class.sealedSubclasses)
addAll(FactorioEventUpdateData::class.sealedSubclasses)
},
mappings = mapOf(
// builtin Factorio numeric types > `typed-factorio/generated/builtin-types.d.ts`
@ -66,18 +73,38 @@ private fun splitDefinitions(definitions: String): Map<String, String> {
return definitions
.split("\n\n")
.groupBy { def ->
when {
"extends FactorioObjectData " in def ||
"interface FactorioObjectData " in def ||
"type ObjectName " in def -> "object-data.d.ts"
"interface FactorioObjectData " in def ||
"type ObjectName " in def ||
def.contains(Regex("interface.+extends.+(FactorioObjectData).*"))
-> "object-data.d.ts"
"extends FactorioPrototype " in def ||
"interface FactorioPrototypes " in def ||
"interface FactorioPrototype " in def ||
"type PrototypeObjectName " in def -> "prototype.d.ts"
"extends FactorioConfigurationUpdate " in def ||
"interface FactorioConfigurationUpdate " in def ||
"FactorioGameDataUpdate " in def ||
"FactorioModInfo " in def -> "config-update.d.ts"
"interface FactorioEventUpdatePacket" in def ||
"type FactorioEventUpdateType =" in def ||
"interface FactorioEventUpdateKey {" in def ||
"interface FactorioEventUpdateData {" in def ||
def.contains(
Regex(
"""
interface.+extends.+(FactorioEventUpdate|PlayerUpdateKey|PlayerUpdateData).*
""".trimIndent()
)
)
-> "update.d.ts"
else -> "schema.d.ts"
}
}