so close to working performantly.... just the tiles are transparent???

- bump some versions
- MapTile no longer has objectName field
- fix nulls/optional fields in FactorioConfigurationUpdate
- use bytes to encode rgba colours - waaaayyyy smaller
- increase Kafka record size to ~10mb
This commit is contained in:
Adam 2022-02-08 01:35:52 +01:00
parent ea0bfb350b
commit bdb2d9577f
24 changed files with 619 additions and 404 deletions

View file

@ -23,7 +23,12 @@ fun Colour.toHexadecimal() = if (isPercentage()) {
(alpha * 255).coerceIn(0f..255f),
)
} else {
this
Colour(
red.coerceIn(0f..255f),
green.coerceIn(0f..255f),
blue.coerceIn(0f..255f),
alpha.coerceIn(0f..255f),
)
}
fun Colour.toPercentage() = if (isPercentage()) {

View file

@ -1,5 +1,6 @@
package dev.adamko.kafkatorio.events.schema
import dev.adamko.kafkatorio.events.schema.converters.toHexadecimal
import kotlinx.serialization.EncodeDefault
import kotlinx.serialization.Serializable
@ -56,7 +57,7 @@ data class MapBoundingBox(
* Red, green, blue and alpha values, all in range `[0, 1]` or all in range `[0, 255]` if any
* value is > 1.
*
* All values here are optional. Colour channels default to 0, the alpha channel defaults to 1.
* All values here are optional. Colour channels default to `0`, the alpha channel defaults to `1`.
*/
@Serializable
data class Colour(
@ -68,4 +69,39 @@ data class Colour(
val blue: Float = 0f,
@EncodeDefault
val alpha: Float = 1f,
) {
fun toHex() = toHexadecimal().let {
ColourHex(
it.red.toUInt().toUByte(),
it.green.toUInt().toUByte(),
it.blue.toUInt().toUByte(),
it.alpha.toUInt().toUByte(),
)
}
}
/** Size-efficient version of [Colour] */
@Serializable
data class ColourHex(
@EncodeDefault
val red: UByte = UByte.MIN_VALUE,
@EncodeDefault
val green: UByte = UByte.MIN_VALUE,
@EncodeDefault
val blue: UByte = UByte.MIN_VALUE,
@EncodeDefault
val alpha: UByte = UByte.MAX_VALUE,
) {
companion object {
val TRANSPARENT = ColourHex(0u, 0u, 0u, 0u)
}
}
@Serializable
data class MapTile(
val prototypeName: String,
val position: MapTilePosition,
)

View file

@ -16,13 +16,13 @@ data class FactorioConfigurationUpdate(
@Serializable
data class FactorioGameDataUpdate(
val oldVersion: String?,
val newVersion: String?,
val oldVersion: String? = null,
val newVersion: String? = null,
)
@Serializable
data class FactorioModInfo(
val modName: String,
val currentVersion: String?,
val previousVersion: String?,
val currentVersion: String? = null,
val previousVersion: String? = null,
)

View file

@ -137,17 +137,6 @@ data class MapTiles(
override val objectName = ObjectName.LuaTiles
}
@Serializable
data class MapTile(
val prototypeName: String,
val position: MapTilePosition,
)
// : FactorioObjectData()
{
// @EncodeDefault
val objectName: String = ""
}
@Serializable
data class ConsoleChatMessage(
val authorPlayerIndex: UInt?,

View file

@ -203,7 +203,6 @@ class FactorioEventTest : FunSpec({
"surfaceIndex": 1,
"tiles": [
{
"objectName": "LuaTile",
"position": {
"y": 10,
"x": -65
@ -211,23 +210,20 @@ class FactorioEventTest : FunSpec({
"prototypeName": "refined-concrete"
},
{
"objectName": "LuaTile",
"position": {
"y": 11,
"x": -65
},
"prototypeName": "refined-concrete"
"prototypeName": "red-earth"
},
{
"objectName": "LuaTile",
"position": {
"y": 10,
"x": -64
},
"prototypeName": "refined-concrete"
"prototypeName": "blue-cheese"
},
{
"objectName": "LuaTile",
"position": {
"y": 11,
"x": -64
@ -256,11 +252,11 @@ class FactorioEventTest : FunSpec({
position = MapTilePosition(-65, 10)
),
MapTile(
prototypeName = "refined-concrete",
prototypeName = "red-earth",
position = MapTilePosition(-65, 11)
),
MapTile(
prototypeName = "refined-concrete",
prototypeName = "blue-cheese",
position = MapTilePosition(-64, 10)
),
MapTile(
@ -279,6 +275,60 @@ class FactorioEventTest : FunSpec({
expected.data.objectName shouldBe FactorioObjectData.ObjectName.LuaTiles
}
test("Then: expect encode equals json") {
val encoded = jsonMapperKafkatorio.encodeToString(actual)
encoded.shouldEqualJson(json)
}
}
}
context("Given: packet FactorioConfigurationUpdate") {
// language=JSON
val json = """
{
"modVersion": "0.2.8",
"packetType": "CONFIG",
"allMods": [
{
"modName": "kafkatorio-events",
"currentVersion": "0.2.8",
"previousVersion": "0.2.7"
},
{
"modName": "base",
"currentVersion": "1.1.53"
}
],
"factorioData": {}
}
""".trimIndent()
context("When: decoded") {
val actual: FactorioConfigurationUpdate = jsonMapperKafkatorio.decodeFromString(json)
test("parse") {
val expected = FactorioConfigurationUpdate(
modVersion = "0.2.8",
factorioData = FactorioGameDataUpdate(null, null),
allMods = listOf(
FactorioModInfo(
modName = "kafkatorio-events",
currentVersion = "0.2.8",
previousVersion = "0.2.7"
),
FactorioModInfo(
modName = "base",
currentVersion = "1.1.53",
previousVersion = null
),
)
)
actual shouldBe expected
expected.packetType shouldBe KafkatorioPacket.PacketType.CONFIG
}
test("Then: expect encode equals json") {
val encoded = jsonMapperKafkatorio.encodeToString(actual)
encoded.shouldEqualJson(json)