fix init of global vars, make MapTile DTO more concise, and version bump

This commit is contained in:
Adam 2022-02-21 15:36:12 +01:00
parent 8dff124a71
commit 494913d3b9
22 changed files with 137 additions and 126 deletions

View file

@ -8,7 +8,7 @@ plugins {
} }
group = "dev.adamko.kafkatorio" group = "dev.adamko.kafkatorio"
version = "0.3.0" version = "0.3.2"
val licenseFile: RegularFile by extra(layout.projectDirectory.file("LICENSE")) val licenseFile: RegularFile by extra(layout.projectDirectory.file("LICENSE"))

View file

@ -1,15 +1,44 @@
import {handleConfigurationUpdate} from "./main/config-update/handleConfigurationUpdate"; import {emitConfigurationUpdate} from "./main/config-update/handleConfigurationUpdate";
import {emitPrototypes} from "./main/config-update/prototypes"; import {emitPrototypes} from "./main/config-update/prototypes";
import {Queue} from "./main/queue/queue"; import {Queue} from "./main/queue/queue";
import {EventDataCache} from "./main/cache/EventDataCache"; import {EventDataCache} from "./main/cache/EventDataCache";
require("./main/events/registerHandlers") require("./main/events/registerHandlers")
require("./main/commands/kafkatorioCommands") require("./main/commands/kafkatorioCommands")
require("./main/cache/cacheChecker")
script.on_configuration_changed((data: ConfigurationChangedData) => {
// runs whenever the version of the base game, or a mod, changes
reInitGlobal()
emitConfigurationUpdate(data)
emitPrototypes()
})
script.on_configuration_changed(handleConfigurationUpdate)
script.on_init(() => { script.on_init(() => {
Queue.reset() // runs only once (when the mod is first added to the save)
emitPrototypes() initGlobal()
EventDataCache.init()
}) })
function initGlobal() {
log("Initialising Kafkatorio Global variables...")
Queue.init()
EventDataCache.init()
log("Finished initialising Kafkatorio Global variables")
}
function reInitGlobal() {
log("Re-initialising Kafkatorio Global variables...")
global.MOD_VERSION = script.active_mods[script.mod_name]
global.FACTORIO_VERSION = script.active_mods["base"]
Queue.reset()
EventDataCache.reset()
log("Finished re-initialising Kafkatorio Global variables")
}

View file

@ -89,8 +89,9 @@ interface MapChunkPosition {
} }
interface MapTile { interface MapTile {
position: MapTilePosition; proto: string;
prototypeName: string; x: int;
y: int;
} }
interface EntityIdentifiers { interface EntityIdentifiers {

View file

@ -5,20 +5,20 @@ export namespace EventDataCache {
declare const global: { declare const global: {
cache: LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>, cache: LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>,
DEFAULT_CACHE_DURATION_TICKS: LuaTable<FactorioEventUpdateType, uint>,
DEFAULT_CACHE_DURATION_TICKS2: Record<FactorioEventUpdateType | "default", uint>, DEFAULT_CACHE_DURATION_TICKS2: Record<FactorioEventUpdateType | "default", uint>,
MAP_CHUNK_CACHE_DURATION_TICKS: uint, }
export function reset() {
log("Resetting EventDataCache")
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
} }
export function init() { export function init() {
if (typeof global.cache == undefined) { if (typeof global.cache == undefined) {
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>() log("Initialising EventDataCache")
reset()
global.DEFAULT_CACHE_DURATION_TICKS = new LuaTable<FactorioEventUpdateType, uint>()
global.DEFAULT_CACHE_DURATION_TICKS.set("PLAYER", 30) // 0.5 seconds
global.DEFAULT_CACHE_DURATION_TICKS.set("MAP_CHUNK", 60 * 15) // ~ 15 seconds
global.DEFAULT_CACHE_DURATION_TICKS.set("ENTITY", 60 * 2) // ~2 seconds
global.DEFAULT_CACHE_DURATION_TICKS2 = { global.DEFAULT_CACHE_DURATION_TICKS2 = {
"PLAYER": 30, // 0.5 seconds "PLAYER": 30, // 0.5 seconds

View file

@ -6,16 +6,20 @@ script.on_nth_tick(6, (nthTick: NthTickEventData) => {
const cachedEvents: Array<EventDataCache.CacheData<any>> = EventDataCache.extractExpired() const cachedEvents: Array<EventDataCache.CacheData<any>> = EventDataCache.extractExpired()
for (const event of cachedEvents) { if (cachedEvents.length > 0) {
log(`nth tick ${nthTick.tick} has ${cachedEvents.length} events`)
const packet: FactorioEventUpdatePacket = { for (const event of cachedEvents) {
tick: nthTick.tick,
modVersion: global.MOD_VERSION, const packet: FactorioEventUpdatePacket = {
packetType: "UPDATE", tick: nthTick.tick,
update: event, modVersion: global.MOD_VERSION,
packetType: "UPDATE",
update: event,
}
emitPacket(packet)
} }
emitPacket(packet)
} }
}) })

View file

@ -1,10 +1,6 @@
import {emitPacket} from "../emitKafkatorioPacket"; import {emitPacket} from "../emitKafkatorioPacket";
import {updateGlobal} from "./updateGlobal";
import {emitPrototypes} from "./prototypes";
import {Queue} from "../queue/queue";
export function handleConfigurationUpdate(changeData: ConfigurationChangedData) { export function emitConfigurationUpdate(changeData: ConfigurationChangedData) {
updateGlobal()
emitPacket<FactorioConfigurationUpdate>({ emitPacket<FactorioConfigurationUpdate>({
modVersion: global.MOD_VERSION, modVersion: global.MOD_VERSION,
@ -13,8 +9,6 @@ export function handleConfigurationUpdate(changeData: ConfigurationChangedData)
factorioData: factorioData(changeData) factorioData: factorioData(changeData)
}) })
Queue.reset()
emitPrototypes()
} }
function factorioData(changeData: ConfigurationChangedData): FactorioGameDataUpdate { function factorioData(changeData: ConfigurationChangedData): FactorioGameDataUpdate {

View file

@ -1,6 +1,6 @@
import {Converters} from "../events/converters"; import {Converters} from "../events/converters";
import {emitPacket} from "../emitKafkatorioPacket"; import {emitPacket} from "../emitKafkatorioPacket";
// import { global } from "../global";
export function emitPrototypes() { export function emitPrototypes() {
emitPacket<FactorioPrototypes>({ emitPacket<FactorioPrototypes>({
@ -20,7 +20,7 @@ function prototypes(): FactorioPrototype[] {
function getMapTilePrototypes(): MapTilePrototype[] { function getMapTilePrototypes(): MapTilePrototype[] {
let tiles: MapTilePrototype[] = [] let tiles: MapTilePrototype[] = []
for (let [, tile] of pairs(game.tile_prototypes)) { for (let [, tile] of game.tile_prototypes) {
tiles.push( tiles.push(
<MapTilePrototype>{ <MapTilePrototype>{
prototypeObjectName: tile.object_name, prototypeObjectName: tile.object_name,

View file

@ -1,4 +0,0 @@
export function updateGlobal() {
global.MOD_VERSION = script.active_mods[script.mod_name]
global.FACTORIO_VERSION = script.active_mods["base"]
}

View file

@ -99,8 +99,9 @@ export namespace Converters {
export function convertTile(tile: LuaTile): MapTile { export function convertTile(tile: LuaTile): MapTile {
return { return {
position: mapTilePosition(tile.position), x: tile.position.x,
prototypeName: tile.prototype.name, y: tile.position.y,
proto: tile.prototype.name,
} }
} }

View file

@ -92,8 +92,9 @@ export function handleTilesUpdate(
let convertedTiles: MapTile[] = updatedTiles.map((it) => { let convertedTiles: MapTile[] = updatedTiles.map((it) => {
return { return {
position: it.position, // TODO use converters for LuaTile x: it.position.x,
prototypeName: newTileType.name, y: it.position.y,
proto: newTileType.name
} }
}) })

View file

@ -2,9 +2,8 @@ import {
handleChunkUpdate, handleChunkUpdate,
handleConsoleChat, handleConsoleChat,
handleEntityUpdate, handleEntityUpdate,
// handlePlayerUpdate,
handleSurfaceUpdate, handleSurfaceUpdate,
handleTilesUpdate // handleTilesUpdate
} from "./handlers"; } from "./handlers";
import {Queue} from "../queue/queue"; import {Queue} from "../queue/queue";
import {EventName} from "../types"; import {EventName} from "../types";
@ -15,18 +14,6 @@ for (const [eventName, eventId] of pairs(defines.events)) {
mapEventIdToName.set(eventId, eventName) mapEventIdToName.set(eventId, eventName)
} }
// const mapEventIdToDefinedType = new LuaTable<EventId<EventData>, EventId<EventData>>()
// for (const [eventName, eventId] of pairs(defines.events)) {
// let definedType = defines.events[eventName]
// mapEventIdToDefinedType.set(eventId, definedType)
// }
// script.on_event(
// [defines.events.on_pre_build, defines.events.on_player_dropped_item],
// (e: OnPreBuildEvent | OnPlayerDroppedItemEvent) => {
// game.print("player " + e.player_index + " dropped item, tick:" + e.tick)
// }
// );
script.on_event( script.on_event(
defines.events.on_player_mined_entity, defines.events.on_player_mined_entity,
@ -91,25 +78,25 @@ script.on_event(
} }
) )
script.on_event( // script.on_event(
defines.events.on_chunk_generated, // defines.events.on_chunk_generated,
(e: OnChunkGeneratedEvent) => { // (e: OnChunkGeneratedEvent) => {
handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area) // handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area)
} // }
) // )
//
script.on_event( // script.on_event(
[ // [
defines.events.on_player_built_tile, // defines.events.on_player_built_tile,
defines.events.on_robot_built_tile, // defines.events.on_robot_built_tile,
], // ],
(builtTilesEvent) => { // (builtTilesEvent) => {
handleTilesUpdate( // handleTilesUpdate(
builtTilesEvent.tick, // builtTilesEvent.tick,
mapEventIdToName.get(builtTilesEvent.name), // mapEventIdToName.get(builtTilesEvent.name),
builtTilesEvent.surface_index, // builtTilesEvent.surface_index,
builtTilesEvent.tile, // builtTilesEvent.tile,
builtTilesEvent.tiles, // builtTilesEvent.tiles,
) // )
} // }
) // )

View file

@ -1,6 +1,6 @@
{ {
"name": "kafkatorio-events-mod", "name": "kafkatorio-events-mod",
"version": "0.3.0", "version": "0.3.2",
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {

View file

@ -1,6 +1,6 @@
{ {
"name": "kafkatorio-events-mod", "name": "kafkatorio-events-mod",
"version": "0.3.0", "version": "0.3.2",
"private": true, "private": true,
"scripts": { "scripts": {
"build": "tstl", "build": "tstl",
@ -9,7 +9,8 @@
"engines": { "engines": {
"node": ">=10.13.0" "node": ">=10.13.0"
}, },
"devDependencies": {}, "devDependencies": {
},
"dependencies": { "dependencies": {
"lua-types": "^2.11.0", "lua-types": "^2.11.0",
"@types/node": "^12.0.8", "@types/node": "^12.0.8",
@ -18,7 +19,10 @@
"typescript": "4.5.5" "typescript": "4.5.5"
}, },
"license": "UNLICENSED", "license": "UNLICENSED",
"peerDependencies": {}, "peerDependencies": {
"optionalDependencies": {}, },
"bundledDependencies": [] "optionalDependencies": {
},
"bundledDependencies": [
]
} }

View file

@ -34,6 +34,6 @@
"tstlVerbose": true, "tstlVerbose": true,
"noHeader": true, "noHeader": true,
"noImplicitSelf": true, "noImplicitSelf": true,
"luaLibImport": "inline" "luaLibImport": "require"
} }
} }

View file

@ -98,8 +98,9 @@ data class ColourHex(
@Serializable @Serializable
data class MapTile( data class MapTile(
val prototypeName: PrototypeName, val x: Int,
val position: MapTilePosition, val y: Int,
val proto: PrototypeName,
) )

View file

@ -64,7 +64,7 @@ object FactorioPrototypeJsonSerializer : JsonContentPolymorphicSerializer<Factor
@Serializable @Serializable
data class MapTilePrototype( data class MapTilePrototype(
val name: String, val name: PrototypeName,
val layer: UInt, val layer: UInt,
val mapColour: Colour, val mapColour: Colour,
// @Serializable(with = FactorioJsonListSerializer::class) // @Serializable(with = FactorioJsonListSerializer::class)

View file

@ -209,32 +209,24 @@ class FactorioEventTest : FunSpec({
"surfaceIndex": 1, "surfaceIndex": 1,
"tiles": [ "tiles": [
{ {
"position": { "y": 10,
"y": 10, "x": -65,
"x": -65 "proto": "refined-concrete"
},
"prototypeName": "refined-concrete"
}, },
{ {
"position": { "y": 11,
"y": 11, "x": -65,
"x": -65 "proto": "red-earth"
},
"prototypeName": "red-earth"
}, },
{ {
"position": { "y": 10,
"y": 10, "x": -64,
"x": -64 "proto": "blue-cheese"
},
"prototypeName": "blue-cheese"
}, },
{ {
"position": { "y": 11,
"y": 11, "x": -64,
"x": -64 "proto": "refined-concrete"
},
"prototypeName": "refined-concrete"
} }
] ]
}, },
@ -254,20 +246,24 @@ class FactorioEventTest : FunSpec({
surfaceIndex = SurfaceIndex(1u), surfaceIndex = SurfaceIndex(1u),
tiles = listOf( tiles = listOf(
MapTile( MapTile(
prototypeName = PrototypeName("refined-concrete"), proto = PrototypeName("refined-concrete"),
position = MapTilePosition(-65, 10) x = -65,
y = 10,
), ),
MapTile( MapTile(
prototypeName = PrototypeName("red-earth"), proto = PrototypeName("red-earth"),
position = MapTilePosition(-65, 11) x = -65,
y = 11,
), ),
MapTile( MapTile(
prototypeName = PrototypeName("blue-cheese"), proto = PrototypeName("blue-cheese"),
position = MapTilePosition(-64, 10) x = -64,
y = 10,
), ),
MapTile( MapTile(
prototypeName = PrototypeName("refined-concrete"), proto = PrototypeName("refined-concrete"),
position = MapTilePosition(-64, 11) x = -64,
y = 11,
), ),
) )
), ),

View file

@ -4,6 +4,7 @@ import dev.adamko.kafkatorio.events.schema.ColourHex
import dev.adamko.kafkatorio.events.schema.FactorioEvent import dev.adamko.kafkatorio.events.schema.FactorioEvent
import dev.adamko.kafkatorio.events.schema.MapChunk import dev.adamko.kafkatorio.events.schema.MapChunk
import dev.adamko.kafkatorio.events.schema.MapChunkPosition import dev.adamko.kafkatorio.events.schema.MapChunkPosition
import dev.adamko.kafkatorio.events.schema.MapTile
import dev.adamko.kafkatorio.events.schema.MapTilePosition import dev.adamko.kafkatorio.events.schema.MapTilePosition
import dev.adamko.kafkatorio.events.schema.MapTiles import dev.adamko.kafkatorio.events.schema.MapTiles
import dev.adamko.kafkatorio.events.schema.SurfaceIndex import dev.adamko.kafkatorio.events.schema.SurfaceIndex
@ -141,3 +142,6 @@ fun groupTilesIntoChunksWithColours(
return colourisedChunkTable return colourisedChunkTable
} }
val MapTile.position
get() = MapTilePosition(x, y)

View file

@ -17,19 +17,12 @@ import org.apache.kafka.streams.kstream.KStream
import org.apache.kafka.streams.kstream.KTable import org.apache.kafka.streams.kstream.KTable
@Serializable
@JvmInline
value class PrototypeName(val name: String) {
override fun toString() = name
}
/** Use the hashcode of Prototypes names as keys - more efficient storage */ /** Use the hashcode of Prototypes names as keys - more efficient storage */
@JvmInline @JvmInline
@Serializable @Serializable
value class TileProtoHashCode private constructor(val code: Int) { value class TileProtoHashCode private constructor(val code: Int) {
constructor(prototype: MapTilePrototype) : this(prototype.name.hashCode()) constructor(prototype: MapTilePrototype) : this(prototype.name.hashCode())
constructor(tile: MapTile) : this(tile.prototypeName.hashCode()) constructor(tile: MapTile) : this(tile.proto.hashCode())
} }

View file

@ -1,4 +1,4 @@
# Do not edit manually. This file was created with task 'dockerEnv' # Do not edit manually. This file was created with task 'dockerEnv'
COMPOSE_PROJECT_NAME=kafkatorio COMPOSE_PROJECT_NAME=kafkatorio
FACTORIO_VERSION=1.1.53 FACTORIO_VERSION=1.1.53
KAFKATORIO_VERSION=0.3.0 KAFKATORIO_VERSION=0.3.2

View file

@ -1,3 +1,3 @@
# Do not edit manually. This file was created with task 'dockerEnv' # Do not edit manually. This file was created with task 'dockerEnv'
COMPOSE_PROJECT_NAME=kafkatorio COMPOSE_PROJECT_NAME=kafkatorio
KAFKATORIO_VERSION=0.3.0 KAFKATORIO_VERSION=0.3.2

View file

@ -1,3 +1,3 @@
# Do not edit manually. This file was created with task 'dockerEnv' # Do not edit manually. This file was created with task 'dockerEnv'
COMPOSE_PROJECT_NAME=kafkatorio COMPOSE_PROJECT_NAME=kafkatorio
KAFKATORIO_VERSION=0.3.0 KAFKATORIO_VERSION=0.3.2