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

@ -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")
}

View file

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

View file

@ -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

View file

@ -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)
}
})

View file

@ -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 {

View file

@ -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,

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 {
return {
position: mapTilePosition(tile.position),
prototypeName: tile.prototype.name,
x: tile.position.x,
y: tile.position.y,
proto: tile.prototype.name,
}
}

View file

@ -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
}
})

View file

@ -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,
// )
// }
// )

View file

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

View file

@ -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": [
]
}

View file

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