fix init of global vars, make MapTile DTO more concise, and version bump
This commit is contained in:
parent
8dff124a71
commit
494913d3b9
22 changed files with 137 additions and 126 deletions
|
|
@ -8,7 +8,7 @@ plugins {
|
|||
}
|
||||
|
||||
group = "dev.adamko.kafkatorio"
|
||||
version = "0.3.0"
|
||||
version = "0.3.2"
|
||||
|
||||
val licenseFile: RegularFile by extra(layout.projectDirectory.file("LICENSE"))
|
||||
|
||||
|
|
|
|||
|
|
@ -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 {Queue} from "./main/queue/queue";
|
||||
import {EventDataCache} from "./main/cache/EventDataCache";
|
||||
|
||||
require("./main/events/registerHandlers")
|
||||
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(() => {
|
||||
Queue.reset()
|
||||
emitPrototypes()
|
||||
EventDataCache.init()
|
||||
// runs only once (when the mod is first added to the save)
|
||||
initGlobal()
|
||||
})
|
||||
|
||||
|
||||
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")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -89,8 +89,9 @@ interface MapChunkPosition {
|
|||
}
|
||||
|
||||
interface MapTile {
|
||||
position: MapTilePosition;
|
||||
prototypeName: string;
|
||||
proto: string;
|
||||
x: int;
|
||||
y: int;
|
||||
}
|
||||
|
||||
interface EntityIdentifiers {
|
||||
|
|
|
|||
|
|
@ -5,20 +5,20 @@ export namespace EventDataCache {
|
|||
|
||||
declare const global: {
|
||||
cache: LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>,
|
||||
DEFAULT_CACHE_DURATION_TICKS: LuaTable<FactorioEventUpdateType, 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() {
|
||||
if (typeof global.cache == undefined) {
|
||||
global.cache = new LuaTable<CacheKey<FactorioEventUpdateType>, CacheEntry<FactorioEventUpdateType>>()
|
||||
|
||||
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
|
||||
log("Initialising EventDataCache")
|
||||
reset()
|
||||
|
||||
global.DEFAULT_CACHE_DURATION_TICKS2 = {
|
||||
"PLAYER": 30, // 0.5 seconds
|
||||
|
|
|
|||
|
|
@ -6,16 +6,20 @@ script.on_nth_tick(6, (nthTick: NthTickEventData) => {
|
|||
|
||||
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 = {
|
||||
tick: nthTick.tick,
|
||||
modVersion: global.MOD_VERSION,
|
||||
packetType: "UPDATE",
|
||||
update: event,
|
||||
for (const event of cachedEvents) {
|
||||
|
||||
const packet: FactorioEventUpdatePacket = {
|
||||
tick: nthTick.tick,
|
||||
modVersion: global.MOD_VERSION,
|
||||
packetType: "UPDATE",
|
||||
update: event,
|
||||
}
|
||||
|
||||
emitPacket(packet)
|
||||
}
|
||||
|
||||
emitPacket(packet)
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
import {emitPacket} from "../emitKafkatorioPacket";
|
||||
import {updateGlobal} from "./updateGlobal";
|
||||
import {emitPrototypes} from "./prototypes";
|
||||
import {Queue} from "../queue/queue";
|
||||
|
||||
export function handleConfigurationUpdate(changeData: ConfigurationChangedData) {
|
||||
updateGlobal()
|
||||
export function emitConfigurationUpdate(changeData: ConfigurationChangedData) {
|
||||
|
||||
emitPacket<FactorioConfigurationUpdate>({
|
||||
modVersion: global.MOD_VERSION,
|
||||
|
|
@ -13,8 +9,6 @@ export function handleConfigurationUpdate(changeData: ConfigurationChangedData)
|
|||
factorioData: factorioData(changeData)
|
||||
})
|
||||
|
||||
Queue.reset()
|
||||
emitPrototypes()
|
||||
}
|
||||
|
||||
function factorioData(changeData: ConfigurationChangedData): FactorioGameDataUpdate {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
import {Converters} from "../events/converters";
|
||||
import {emitPacket} from "../emitKafkatorioPacket";
|
||||
// import { global } from "../global";
|
||||
|
||||
|
||||
export function emitPrototypes() {
|
||||
emitPacket<FactorioPrototypes>({
|
||||
|
|
@ -20,7 +20,7 @@ function prototypes(): FactorioPrototype[] {
|
|||
|
||||
function getMapTilePrototypes(): MapTilePrototype[] {
|
||||
let tiles: MapTilePrototype[] = []
|
||||
for (let [, tile] of pairs(game.tile_prototypes)) {
|
||||
for (let [, tile] of game.tile_prototypes) {
|
||||
tiles.push(
|
||||
<MapTilePrototype>{
|
||||
prototypeObjectName: tile.object_name,
|
||||
|
|
|
|||
|
|
@ -1,4 +0,0 @@
|
|||
export function updateGlobal() {
|
||||
global.MOD_VERSION = script.active_mods[script.mod_name]
|
||||
global.FACTORIO_VERSION = script.active_mods["base"]
|
||||
}
|
||||
|
|
@ -99,8 +99,9 @@ export namespace Converters {
|
|||
|
||||
export function convertTile(tile: LuaTile): MapTile {
|
||||
return {
|
||||
position: mapTilePosition(tile.position),
|
||||
prototypeName: tile.prototype.name,
|
||||
x: tile.position.x,
|
||||
y: tile.position.y,
|
||||
proto: tile.prototype.name,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -92,8 +92,9 @@ export function handleTilesUpdate(
|
|||
|
||||
let convertedTiles: MapTile[] = updatedTiles.map((it) => {
|
||||
return {
|
||||
position: it.position, // TODO use converters for LuaTile
|
||||
prototypeName: newTileType.name,
|
||||
x: it.position.x,
|
||||
y: it.position.y,
|
||||
proto: newTileType.name
|
||||
}
|
||||
})
|
||||
|
||||
|
|
|
|||
|
|
@ -2,9 +2,8 @@ import {
|
|||
handleChunkUpdate,
|
||||
handleConsoleChat,
|
||||
handleEntityUpdate,
|
||||
// handlePlayerUpdate,
|
||||
handleSurfaceUpdate,
|
||||
handleTilesUpdate
|
||||
// handleTilesUpdate
|
||||
} from "./handlers";
|
||||
import {Queue} from "../queue/queue";
|
||||
import {EventName} from "../types";
|
||||
|
|
@ -15,18 +14,6 @@ for (const [eventName, eventId] of pairs(defines.events)) {
|
|||
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(
|
||||
defines.events.on_player_mined_entity,
|
||||
|
|
@ -91,25 +78,25 @@ script.on_event(
|
|||
}
|
||||
)
|
||||
|
||||
script.on_event(
|
||||
defines.events.on_chunk_generated,
|
||||
(e: OnChunkGeneratedEvent) => {
|
||||
handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area)
|
||||
}
|
||||
)
|
||||
|
||||
script.on_event(
|
||||
[
|
||||
defines.events.on_player_built_tile,
|
||||
defines.events.on_robot_built_tile,
|
||||
],
|
||||
(builtTilesEvent) => {
|
||||
handleTilesUpdate(
|
||||
builtTilesEvent.tick,
|
||||
mapEventIdToName.get(builtTilesEvent.name),
|
||||
builtTilesEvent.surface_index,
|
||||
builtTilesEvent.tile,
|
||||
builtTilesEvent.tiles,
|
||||
)
|
||||
}
|
||||
)
|
||||
// script.on_event(
|
||||
// defines.events.on_chunk_generated,
|
||||
// (e: OnChunkGeneratedEvent) => {
|
||||
// handleChunkUpdate(e.tick, mapEventIdToName.get(e.name), e.surface.index, e.position, e.area)
|
||||
// }
|
||||
// )
|
||||
//
|
||||
// script.on_event(
|
||||
// [
|
||||
// defines.events.on_player_built_tile,
|
||||
// defines.events.on_robot_built_tile,
|
||||
// ],
|
||||
// (builtTilesEvent) => {
|
||||
// handleTilesUpdate(
|
||||
// builtTilesEvent.tick,
|
||||
// mapEventIdToName.get(builtTilesEvent.name),
|
||||
// builtTilesEvent.surface_index,
|
||||
// builtTilesEvent.tile,
|
||||
// builtTilesEvent.tiles,
|
||||
// )
|
||||
// }
|
||||
// )
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "kafkatorio-events-mod",
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.2",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "kafkatorio-events-mod",
|
||||
"version": "0.3.0",
|
||||
"version": "0.3.2",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"build": "tstl",
|
||||
|
|
@ -9,7 +9,8 @@
|
|||
"engines": {
|
||||
"node": ">=10.13.0"
|
||||
},
|
||||
"devDependencies": {},
|
||||
"devDependencies": {
|
||||
},
|
||||
"dependencies": {
|
||||
"lua-types": "^2.11.0",
|
||||
"@types/node": "^12.0.8",
|
||||
|
|
@ -18,7 +19,10 @@
|
|||
"typescript": "4.5.5"
|
||||
},
|
||||
"license": "UNLICENSED",
|
||||
"peerDependencies": {},
|
||||
"optionalDependencies": {},
|
||||
"bundledDependencies": []
|
||||
"peerDependencies": {
|
||||
},
|
||||
"optionalDependencies": {
|
||||
},
|
||||
"bundledDependencies": [
|
||||
]
|
||||
}
|
||||
|
|
|
|||
|
|
@ -34,6 +34,6 @@
|
|||
"tstlVerbose": true,
|
||||
"noHeader": true,
|
||||
"noImplicitSelf": true,
|
||||
"luaLibImport": "inline"
|
||||
"luaLibImport": "require"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -98,8 +98,9 @@ data class ColourHex(
|
|||
|
||||
@Serializable
|
||||
data class MapTile(
|
||||
val prototypeName: PrototypeName,
|
||||
val position: MapTilePosition,
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
val proto: PrototypeName,
|
||||
)
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ object FactorioPrototypeJsonSerializer : JsonContentPolymorphicSerializer<Factor
|
|||
|
||||
@Serializable
|
||||
data class MapTilePrototype(
|
||||
val name: String,
|
||||
val name: PrototypeName,
|
||||
val layer: UInt,
|
||||
val mapColour: Colour,
|
||||
// @Serializable(with = FactorioJsonListSerializer::class)
|
||||
|
|
|
|||
|
|
@ -209,32 +209,24 @@ class FactorioEventTest : FunSpec({
|
|||
"surfaceIndex": 1,
|
||||
"tiles": [
|
||||
{
|
||||
"position": {
|
||||
"y": 10,
|
||||
"x": -65
|
||||
},
|
||||
"prototypeName": "refined-concrete"
|
||||
"y": 10,
|
||||
"x": -65,
|
||||
"proto": "refined-concrete"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"y": 11,
|
||||
"x": -65
|
||||
},
|
||||
"prototypeName": "red-earth"
|
||||
"y": 11,
|
||||
"x": -65,
|
||||
"proto": "red-earth"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"y": 10,
|
||||
"x": -64
|
||||
},
|
||||
"prototypeName": "blue-cheese"
|
||||
"y": 10,
|
||||
"x": -64,
|
||||
"proto": "blue-cheese"
|
||||
},
|
||||
{
|
||||
"position": {
|
||||
"y": 11,
|
||||
"x": -64
|
||||
},
|
||||
"prototypeName": "refined-concrete"
|
||||
"y": 11,
|
||||
"x": -64,
|
||||
"proto": "refined-concrete"
|
||||
}
|
||||
]
|
||||
},
|
||||
|
|
@ -254,20 +246,24 @@ class FactorioEventTest : FunSpec({
|
|||
surfaceIndex = SurfaceIndex(1u),
|
||||
tiles = listOf(
|
||||
MapTile(
|
||||
prototypeName = PrototypeName("refined-concrete"),
|
||||
position = MapTilePosition(-65, 10)
|
||||
proto = PrototypeName("refined-concrete"),
|
||||
x = -65,
|
||||
y = 10,
|
||||
),
|
||||
MapTile(
|
||||
prototypeName = PrototypeName("red-earth"),
|
||||
position = MapTilePosition(-65, 11)
|
||||
proto = PrototypeName("red-earth"),
|
||||
x = -65,
|
||||
y = 11,
|
||||
),
|
||||
MapTile(
|
||||
prototypeName = PrototypeName("blue-cheese"),
|
||||
position = MapTilePosition(-64, 10)
|
||||
proto = PrototypeName("blue-cheese"),
|
||||
x = -64,
|
||||
y = 10,
|
||||
),
|
||||
MapTile(
|
||||
prototypeName = PrototypeName("refined-concrete"),
|
||||
position = MapTilePosition(-64, 11)
|
||||
proto = PrototypeName("refined-concrete"),
|
||||
x = -64,
|
||||
y = 11,
|
||||
),
|
||||
)
|
||||
),
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import dev.adamko.kafkatorio.events.schema.ColourHex
|
|||
import dev.adamko.kafkatorio.events.schema.FactorioEvent
|
||||
import dev.adamko.kafkatorio.events.schema.MapChunk
|
||||
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.MapTiles
|
||||
import dev.adamko.kafkatorio.events.schema.SurfaceIndex
|
||||
|
|
@ -141,3 +142,6 @@ fun groupTilesIntoChunksWithColours(
|
|||
return colourisedChunkTable
|
||||
|
||||
}
|
||||
|
||||
val MapTile.position
|
||||
get() = MapTilePosition(x, y)
|
||||
|
|
|
|||
|
|
@ -17,19 +17,12 @@ import org.apache.kafka.streams.kstream.KStream
|
|||
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 */
|
||||
@JvmInline
|
||||
@Serializable
|
||||
value class TileProtoHashCode private constructor(val code: Int) {
|
||||
constructor(prototype: MapTilePrototype) : this(prototype.name.hashCode())
|
||||
constructor(tile: MapTile) : this(tile.prototypeName.hashCode())
|
||||
constructor(tile: MapTile) : this(tile.proto.hashCode())
|
||||
}
|
||||
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
# Do not edit manually. This file was created with task 'dockerEnv'
|
||||
COMPOSE_PROJECT_NAME=kafkatorio
|
||||
FACTORIO_VERSION=1.1.53
|
||||
KAFKATORIO_VERSION=0.3.0
|
||||
KAFKATORIO_VERSION=0.3.2
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
# Do not edit manually. This file was created with task 'dockerEnv'
|
||||
COMPOSE_PROJECT_NAME=kafkatorio
|
||||
KAFKATORIO_VERSION=0.3.0
|
||||
KAFKATORIO_VERSION=0.3.2
|
||||
|
|
|
|||
|
|
@ -1,3 +1,3 @@
|
|||
# Do not edit manually. This file was created with task 'dockerEnv'
|
||||
COMPOSE_PROJECT_NAME=kafkatorio
|
||||
KAFKATORIO_VERSION=0.3.0
|
||||
KAFKATORIO_VERSION=0.3.2
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue