mod: entity prototype improvements and refactoring

- refactor and move queue & emitter classes for consistency
- don't emit all prototypes at once
- add simple queue for 'instant' packets so the printer doesn't get overwhelmed and mix up the output
This commit is contained in:
Adam 2022-07-09 14:03:57 +02:00
parent ae0eb98f12
commit 90aa086dd4
33 changed files with 742 additions and 652 deletions

View file

@ -1,144 +0,0 @@
local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__New = ____lualib.__TS__New
local ____exports = {}
local ____KafkatorioSettings = require("main.settings.KafkatorioSettings")
local KafkatorioSettings = ____KafkatorioSettings.default
local ____PacketEmitter = require("main.PacketEmitter")
local packetEmitter = ____PacketEmitter.default
____exports.EventUpdates = {}
local EventUpdates = ____exports.EventUpdates
do
local CacheEntry
EventUpdates.Manager = __TS__Class()
local Manager = EventUpdates.Manager
Manager.name = "Manager"
function Manager.prototype.____constructor(self)
end
function Manager.prototype.init(self, force)
local isAnythingUndefined = global.cache == nil
log(((("Initialising EventDataCache (force=" .. tostring(force)) .. ", isAnythingUndefined=") .. tostring(isAnythingUndefined)) .. ")...")
if force == true or isAnythingUndefined then
log("Initialising global.cache")
global.cache = {}
end
log("Finished initialising EventDataCache")
end
function Manager.prototype.throttle(self, key, ____type, mutate, expirationDurationTicks)
self:update(
key,
____type,
mutate,
false,
expirationDurationTicks
)
end
function Manager.prototype.debounce(self, key, ____type, mutate, expirationDurationTicks)
self:update(
key,
____type,
mutate,
true,
expirationDurationTicks
)
end
function Manager.prototype.update(self, key, ____type, mutate, resetLastUpdated, expirationDurationTicks)
local entry = self:getCacheEntry(key, ____type) or EventUpdates.Manager:createCacheEntry(key, ____type)
mutate(entry.packet)
if resetLastUpdated then
entry.lastUpdatedTick = game.tick
end
if expirationDurationTicks ~= nil then
entry.expirationDurationTicks = expirationDurationTicks
end
if entry.expirationDurationTicks ~= nil and entry.expirationDurationTicks <= 0 then
self:extractAndEmitExpiredPackets()
end
end
function Manager.prototype.setExpiration(self, key, ____type, expirationDurationTicks)
local entry = self:getCacheEntry(key, ____type)
if entry ~= nil then
entry.expirationDurationTicks = expirationDurationTicks
end
end
function Manager.prototype.extractExpired(self)
local countExpired = 0
local countTotal = 0
local expiredData = {}
for key, entry in pairs(global.cache) do
countTotal = countTotal + 1
if self:isExpired(entry) then
countExpired = countExpired + 1
expiredData[#expiredData + 1] = entry.packet
global.cache[key] = nil
end
end
local hasExpiredItems = countExpired > 0
local hasAnyItem = countTotal > 0
if hasExpiredItems or game.tick % 30 == 0 and hasAnyItem then
log((("[extractExpired] expired items: " .. tostring(countExpired)) .. ", total: ") .. tostring(countTotal))
end
return expiredData
end
function Manager.prototype.extractAndEmitExpiredPackets(self)
local cachedEvents = self:extractExpired()
if #cachedEvents > 0 then
log((("[EventDataCache] emitting " .. tostring(#cachedEvents)) .. " events on tick ") .. tostring(game.tick))
for ____, event in ipairs(cachedEvents) do
packetEmitter:emitKeyedPacket(event)
end
end
return #cachedEvents
end
function Manager.prototype.getCacheEntry(self, key, ____type)
local hash = EventUpdates.Manager:hashKey(key)
if not (global.cache[hash] ~= nil) then
return nil
end
local value = global.cache[hash]
if self:isEntryInstanceOf(value, ____type) then
return value
else
return nil
end
end
function Manager.createCacheEntry(self, key, ____type)
local data = {type = ____type, key = key}
local entry = __TS__New(CacheEntry, data)
local hash = EventUpdates.Manager:hashKey(key)
global.cache[hash] = entry
return entry
end
function Manager.prototype.isExpired(self, entry)
local expiryDuration = entry.expirationDurationTicks or KafkatorioSettings:getEventCacheExpirationTicks(entry.packet)
if expiryDuration == nil then
return true
else
return game.tick - entry.lastUpdatedTick > expiryDuration
end
end
function Manager.prototype.isEntryInstanceOf(self, entry, ____type)
local ____self_isDataInstanceOf_2 = self.isDataInstanceOf
local ____entry_packet_0 = entry
if ____entry_packet_0 ~= nil then
____entry_packet_0 = ____entry_packet_0.packet
end
return ____self_isDataInstanceOf_2(self, ____entry_packet_0, ____type)
end
function Manager.prototype.isDataInstanceOf(self, packet, ____type)
return packet ~= nil and packet.type == ____type
end
function Manager.hashKey(self, key)
return game.encode_string(game.table_to_json(key))
end
CacheEntry = __TS__Class()
CacheEntry.name = "CacheEntry"
function CacheEntry.prototype.____constructor(self, packet, expirationDurationTicks)
self.lastUpdatedTick = game.tick
self.packet = packet
self.expirationDurationTicks = expirationDurationTicks
end
end
local EventUpdatesManager = __TS__New(____exports.EventUpdates.Manager)
____exports.default = EventUpdatesManager
return ____exports

View file

@ -5,10 +5,10 @@ local __TS__ParseInt = ____lualib.__TS__ParseInt
local ____exports = {}
local ____global_2Dinit = require("main.global-init")
local initGlobal = ____global_2Dinit.initGlobal
local ____queue = require("main.queue.queue")
local Queue = ____queue.Queue
local ____prototypeUpdates = require("main.events.prototypeUpdates")
local emitPrototypes = ____prototypeUpdates.emitPrototypes
local ____queue = require("main.queue.queue")
local EventDataQueue = ____queue.default
commands.add_command(
"kafkatorio",
"kafkatorio innit bruv",
@ -56,7 +56,7 @@ commands.add_command(
surface = surface,
tick = e.tick
}
Queue.enqueue(
EventDataQueue:enqueue(
(tostring(surface.index) .. tostring(chunk.x)) .. tostring(chunk.y),
data,
50

View file

@ -0,0 +1,141 @@
local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__New = ____lualib.__TS__New
local ____exports = {}
local CacheEntry
local ____KafkatorioSettings = require("main.settings.KafkatorioSettings")
local KafkatorioSettings = ____KafkatorioSettings.default
local ____PacketEmitter = require("main.emitting.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
____exports.EventUpdatesManager = __TS__Class()
local EventUpdatesManager = ____exports.EventUpdatesManager
EventUpdatesManager.name = "EventUpdatesManager"
function EventUpdatesManager.prototype.____constructor(self)
self:init()
end
function EventUpdatesManager.prototype.init(self, force)
local isAnythingUndefined = global.eventUpdatesManagerCache == nil
log(((("Initialising EventDataCache (force=" .. tostring(force)) .. ", isAnythingUndefined=") .. tostring(isAnythingUndefined)) .. ")...")
if force == true or isAnythingUndefined then
log("Initialising global.cache")
global.eventUpdatesManagerCache = {}
end
log("Finished initialising EventDataCache")
end
function EventUpdatesManager.prototype.throttle(self, key, ____type, mutate, expirationDurationTicks)
self:update(
key,
____type,
mutate,
false,
expirationDurationTicks
)
end
function EventUpdatesManager.prototype.debounce(self, key, ____type, mutate, expirationDurationTicks)
self:update(
key,
____type,
mutate,
true,
expirationDurationTicks
)
end
function EventUpdatesManager.prototype.update(self, key, ____type, mutate, resetLastUpdated, expirationDurationTicks)
local entry = self:getCacheEntry(key, ____type) or ____exports.EventUpdatesManager:createCacheEntry(key, ____type)
mutate(entry.packet)
if resetLastUpdated then
entry.lastUpdatedTick = game.tick
end
if expirationDurationTicks ~= nil then
entry.expirationDurationTicks = expirationDurationTicks
end
if entry.expirationDurationTicks ~= nil and entry.expirationDurationTicks <= 0 then
self:extractAndEmitExpiredPackets()
end
end
function EventUpdatesManager.prototype.setExpiration(self, key, ____type, expirationDurationTicks)
local entry = self:getCacheEntry(key, ____type)
if entry ~= nil then
entry.expirationDurationTicks = expirationDurationTicks
end
end
function EventUpdatesManager.prototype.extractExpired(self)
local countExpired = 0
local countTotal = 0
local expiredData = {}
for key, entry in pairs(global.eventUpdatesManagerCache) do
countTotal = countTotal + 1
if self:isExpired(entry) then
countExpired = countExpired + 1
expiredData[#expiredData + 1] = entry.packet
global.eventUpdatesManagerCache[key] = nil
end
end
local hasExpiredItems = countExpired > 0
local hasAnyItem = countTotal > 0
if hasExpiredItems or game.tick % 30 == 0 and hasAnyItem then
log((("[extractExpired] expired items: " .. tostring(countExpired)) .. ", total: ") .. tostring(countTotal))
end
return expiredData
end
function EventUpdatesManager.prototype.extractAndEmitExpiredPackets(self)
local cachedEvents = self:extractExpired()
if #cachedEvents > 0 then
log((("[EventDataCache] emitting " .. tostring(#cachedEvents)) .. " events on tick ") .. tostring(game.tick))
for ____, event in ipairs(cachedEvents) do
PacketEmitter:emitKeyedPacket(event)
end
end
return #cachedEvents
end
function EventUpdatesManager.prototype.getCacheEntry(self, key, ____type)
local hash = ____exports.EventUpdatesManager:hashKey(key)
if not (global.eventUpdatesManagerCache[hash] ~= nil) then
return nil
end
local value = global.eventUpdatesManagerCache[hash]
if self:isEntryInstanceOf(value, ____type) then
return value
else
return nil
end
end
function EventUpdatesManager.createCacheEntry(self, key, ____type)
local data = {type = ____type, key = key}
local entry = __TS__New(CacheEntry, data)
local hash = ____exports.EventUpdatesManager:hashKey(key)
global.eventUpdatesManagerCache[hash] = entry
return entry
end
function EventUpdatesManager.prototype.isExpired(self, entry)
local expiryDuration = entry.expirationDurationTicks or KafkatorioSettings:getEventCacheExpirationTicks(entry.packet)
if expiryDuration == nil then
return true
else
return game.tick - entry.lastUpdatedTick > expiryDuration
end
end
function EventUpdatesManager.prototype.isEntryInstanceOf(self, entry, ____type)
local ____self_isDataInstanceOf_2 = self.isDataInstanceOf
local ____entry_packet_0 = entry
if ____entry_packet_0 ~= nil then
____entry_packet_0 = ____entry_packet_0.packet
end
return ____self_isDataInstanceOf_2(self, ____entry_packet_0, ____type)
end
function EventUpdatesManager.prototype.isDataInstanceOf(self, packet, ____type)
return packet ~= nil and packet.type == ____type
end
function EventUpdatesManager.hashKey(self, key)
return game.encode_string(game.table_to_json(key))
end
local EventUpdates = __TS__New(____exports.EventUpdatesManager)
____exports.default = EventUpdates
CacheEntry = __TS__Class()
CacheEntry.name = "CacheEntry"
function CacheEntry.prototype.____constructor(self, packet, expirationDurationTicks)
self.lastUpdatedTick = game.tick
self.packet = packet
self.expirationDurationTicks = expirationDurationTicks
end
return ____exports

View file

@ -0,0 +1,44 @@
local ____lualib = require("lualib_bundle")
local __TS__Class = ____lualib.__TS__Class
local __TS__SetDescriptor = ____lualib.__TS__SetDescriptor
local __TS__New = ____lualib.__TS__New
local ____exports = {}
____exports.KafkatorioPacketQueueManager = __TS__Class()
local KafkatorioPacketQueueManager = ____exports.KafkatorioPacketQueueManager
KafkatorioPacketQueueManager.name = "KafkatorioPacketQueueManager"
function KafkatorioPacketQueueManager.prototype.____constructor(self)
self.size = 0
self.first = nil
self.last = nil
end
__TS__SetDescriptor(
KafkatorioPacketQueueManager.prototype,
"length",
{get = function(self)
return self.size
end},
true
)
function KafkatorioPacketQueueManager.prototype.enqueue(self, packet)
local last = self.last
self.last = {value = packet}
if last ~= nil then
last.next = self.last
end
if self.first == nil then
self.first = self.last
end
self.size = self.size + 1
end
function KafkatorioPacketQueueManager.prototype.dequeue(self)
local first = self.first
if first == nil then
return nil
end
self.first = first.next or nil
self.size = self.size - 1
return first.value
end
local KafkatorioPacketQueue = __TS__New(____exports.KafkatorioPacketQueueManager)
____exports.default = KafkatorioPacketQueue
return ____exports

View file

@ -1,21 +1,20 @@
local ____exports = {}
local ____PacketEmitter = require("main.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local Type = KafkatorioPacketData.Type
local ____KafkatorioPacketQueue = require("main.emitting.KafkatorioPacketQueue")
local KafkatorioPacketQueue = ____KafkatorioPacketQueue.default
script.on_event(
defines.events.on_console_chat,
function(event)
local update = {type = Type.ConsoleChatUpdate, authorPlayerIndex = event.player_index or nil, content = event.message}
PacketEmitter:emitInstantPacket(update)
local update = {type = KafkatorioPacketData.Type.ConsoleChatUpdate, authorPlayerIndex = event.player_index or nil, content = event.message}
KafkatorioPacketQueue:enqueue(update)
end
)
script.on_event(
defines.events.on_console_command,
function(event)
local update = {type = Type.ConsoleCommandUpdate, authorPlayerIndex = event.player_index or nil, command = event.command, parameters = event.parameters}
PacketEmitter:emitInstantPacket(update)
local update = {type = KafkatorioPacketData.Type.ConsoleCommandUpdate, authorPlayerIndex = event.player_index or nil, command = event.command, parameters = event.parameters}
KafkatorioPacketQueue:enqueue(update)
end
)
return ____exports

View file

@ -9,8 +9,8 @@ local __TS__ArrayMap = ____lualib.__TS__ArrayMap
local ____exports = {}
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____PacketEmitter = require("main.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
local ____KafkatorioPacketQueue = require("main.emitting.KafkatorioPacketQueue")
local KafkatorioPacketQueue = ____KafkatorioPacketQueue.default
____exports.ConfigurationUpdateHandler = __TS__Class()
local ConfigurationUpdateHandler = ____exports.ConfigurationUpdateHandler
ConfigurationUpdateHandler.name = "ConfigurationUpdateHandler"
@ -24,7 +24,7 @@ function ConfigurationUpdateHandler.prototype.emitConfigurationUpdate(self, chan
allMods = ____exports.ConfigurationUpdateHandler:allMods(changeData),
factorioData = ____exports.ConfigurationUpdateHandler:factorioData(changeData)
}
PacketEmitter:emitInstantPacket(configUpdateData)
KafkatorioPacketQueue:enqueue(configUpdateData)
end
function ConfigurationUpdateHandler.factorioData(self, changeData)
return {oldVersion = changeData.old_version or nil, newVersion = changeData.new_version or nil}

View file

@ -7,7 +7,7 @@ local FactorioEntityData = ____kafkatorio_2Dschema.FactorioEntityData
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____converters = require("main.events.converters")
local Converters = ____converters.Converters
local ____EventDataCache = require("main.cache.EventDataCache")
local ____EventDataCache = require("main.emitting.EventDataCache")
local EventDataCache = ____EventDataCache.default
____exports.EntityUpdatesHandler = __TS__Class()
local EntityUpdatesHandler = ____exports.EntityUpdatesHandler

View file

@ -10,8 +10,8 @@ local ____eventTypeCheck = require("main.events.eventTypeCheck")
local isEventType = ____eventTypeCheck.isEventType
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____EventDataCache = require("main.cache.EventDataCache")
local EventUpdatesManager = ____EventDataCache.default
local ____EventDataCache = require("main.emitting.EventDataCache")
local EventUpdates = ____EventDataCache.default
local MapChunkUpdateHandler = __TS__Class()
MapChunkUpdateHandler.name = "MapChunkUpdateHandler"
function MapChunkUpdateHandler.prototype.____constructor(self)
@ -124,7 +124,7 @@ function MapChunkUpdateHandler.prototype.handlePreChunkDeleted(self, event)
end
for ____, position in ipairs(event.positions) do
local key = {surfaceIndex = surface.index, chunkPosition = {position.x, position.y}}
EventUpdatesManager:debounce(
EventUpdates:debounce(
key,
KafkatorioPacketData.Type.MapChunkTileUpdate,
function(data)
@ -141,7 +141,7 @@ function MapChunkUpdateHandler.mapTilesUpdateDebounce(self, surface, chunkPositi
end
local eventName = Converters.eventNameString(event.name)
local key = {surfaceIndex = surface.index, chunkPosition = chunkPosition}
EventUpdatesManager:debounce(
EventUpdates:debounce(
key,
KafkatorioPacketData.Type.MapChunkTileUpdate,
function(data)

View file

@ -6,8 +6,8 @@ local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____converters = require("main.events.converters")
local Converters = ____converters.Converters
local ____EventDataCache = require("main.cache.EventDataCache")
local EventUpdatesManager = ____EventDataCache.default
local ____EventDataCache = require("main.emitting.EventDataCache")
local EventUpdates = ____EventDataCache.default
____exports.PlayerUpdateHandler = __TS__Class()
local PlayerUpdateHandler = ____exports.PlayerUpdateHandler
PlayerUpdateHandler.name = "PlayerUpdateHandler"
@ -31,7 +31,7 @@ function PlayerUpdateHandler.prototype.playerUpdateThrottle(self, event, mutate,
return
end
local eventName = Converters.eventNameString(event.name)
EventUpdatesManager:throttle(
EventUpdates:throttle(
{index = playerIndex},
KafkatorioPacketData.Type.PlayerUpdate,
function(data)

View file

@ -4,7 +4,7 @@ local ____converters = require("main.events.converters")
local Converters = ____converters.Converters
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____PacketEmitter = require("main.PacketEmitter")
local ____PacketEmitter = require("main.emitting.PacketEmitter")
local packetEmitter = ____PacketEmitter.default
local ____PlayerUpdateHandler = require("main.events.handlers.PlayerUpdateHandler")
local PlayerUpdates = ____PlayerUpdateHandler.default

View file

@ -1,25 +1,21 @@
local ____exports = {}
local emit, getMapTilePrototypes, getEntityPrototypes, convertItemGroup, convertItemSubgroup
local ____PacketEmitter = require("main.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local FactorioPrototype = ____kafkatorio_2Dschema.FactorioPrototype
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData
local ____converters = require("main.events.converters")
local Converters = ____converters.Converters
local ____KafkatorioPacketQueue = require("main.emitting.KafkatorioPacketQueue")
local KafkatorioPacketQueue = ____KafkatorioPacketQueue.default
function emit(protosByType)
for ____, protos in pairs(protosByType) do
PacketEmitter:emitInstantPacket({type = KafkatorioPacketData.Type.PrototypesUpdate, prototypes = protos})
KafkatorioPacketQueue:enqueue({type = KafkatorioPacketData.Type.PrototypesUpdate, prototypes = protos})
end
end
function getMapTilePrototypes()
local tiles = {}
if not (tiles.tile ~= nil) then
tiles.tile = {}
end
for ____, tile in pairs(game.tile_prototypes) do
local ____tiles_tile_0 = tiles.tile
____tiles_tile_0[#____tiles_tile_0 + 1] = {
tiles[#tiles + 1] = {
type = FactorioPrototype.Type.MapTile,
protoId = Converters.prototypeId("tile", tile.name),
order = tile.order,
@ -29,13 +25,13 @@ function getMapTilePrototypes()
canBeMined = tile.mineable_properties.minable
}
end
return tiles
return {tiles = tiles}
end
function getEntityPrototypes()
local protos = {}
for ____, entity in pairs(game.entity_prototypes) do
local key = (entity.group.name .. "/") .. entity.subgroup.name
if not (protos[key] ~= nil) then
if protos[key] == nil then
protos[key] = {}
end
local entityProto = {
@ -58,8 +54,8 @@ function getEntityPrototypes()
entityProto.mapColourEnemy = Converters.mapColour(entity.enemy_map_color)
end
entityProto.miningProperties = Converters.miningProperties(entity.mineable_properties)
local ____protos_key_1 = protos[key]
____protos_key_1[#____protos_key_1 + 1] = entityProto
local ____protos_key_0 = protos[key]
____protos_key_0[#____protos_key_0 + 1] = entityProto
end
return protos
end
@ -67,19 +63,17 @@ function convertItemGroup(itemGroup)
return {name = itemGroup.name, type = itemGroup.type, parentName = nil}
end
function convertItemSubgroup(itemGroup)
local ____itemGroup_name_4 = itemGroup.name
local ____itemGroup_type_5 = itemGroup.type
local ____itemGroup_group_name_2 = itemGroup.group
if ____itemGroup_group_name_2 ~= nil then
____itemGroup_group_name_2 = ____itemGroup_group_name_2.name
local ____itemGroup_name_3 = itemGroup.name
local ____itemGroup_type_4 = itemGroup.type
local ____itemGroup_group_name_1 = itemGroup.group
if ____itemGroup_group_name_1 ~= nil then
____itemGroup_group_name_1 = ____itemGroup_group_name_1.name
end
return {name = ____itemGroup_name_4, type = ____itemGroup_type_5, parentName = ____itemGroup_group_name_2}
return {name = ____itemGroup_name_3, type = ____itemGroup_type_4, parentName = ____itemGroup_group_name_1}
end
function ____exports.emitPrototypes()
local mapTileProtos = getMapTilePrototypes()
emit(mapTileProtos)
local ____ = game.item_group_prototypes
local ____ = game.item_subgroup_prototypes
local entityProtos = getEntityPrototypes()
emit(entityProtos)
end

View file

@ -1,16 +1,20 @@
local ____exports = {}
local ____queue = require("main.queue.queue")
local Queue = ____queue.Queue
local ____eventTypeCheck = require("main.events.eventTypeCheck")
local isEventType = ____eventTypeCheck.isEventType
local ____converters = require("main.events.converters")
local Converters = ____converters.Converters
local ____surfaceUpdates = require("main.events.surfaceUpdates")
local handleSurfaceUpdate = ____surfaceUpdates.handleSurfaceUpdate
local ____EventDataCache = require("main.cache.EventDataCache")
local ____EventDataCache = require("main.emitting.EventDataCache")
local EventCacheService = ____EventDataCache.default
local ____MapChunkUpdateHandler = require("main.events.handlers.MapChunkUpdateHandler")
local MapChunkUpdate = ____MapChunkUpdateHandler.default
local ____queue = require("main.queue.queue")
local EventDataQueue = ____queue.default
local ____KafkatorioPacketQueue = require("main.emitting.KafkatorioPacketQueue")
local KafkatorioPacketQueue = ____KafkatorioPacketQueue.default
local ____PacketEmitter = require("main.emitting.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
script.on_event(
defines.events.on_tick,
function(event)
@ -23,6 +27,10 @@ script.on_event(
)
end
end
local kPacket = KafkatorioPacketQueue:dequeue()
if kPacket ~= nil then
PacketEmitter:emitInstantPacket(kPacket)
end
if event.tick % 6 == 0 then
local emittedCount = EventCacheService:extractAndEmitExpiredPackets()
if emittedCount > 0 then
@ -30,9 +38,9 @@ script.on_event(
end
end
if event.tick % 7 == 0 then
local events = Queue.dequeueValues(1)
local events = EventDataQueue:dequeueValues(1)
if #events > 0 then
log((((("[on_tick:" .. tostring(event.tick)) .. "] dequeued ") .. tostring(#events)) .. " events, current size: ") .. tostring(Queue.size()))
log((((("[on_tick:" .. tostring(event.tick)) .. "] dequeued ") .. tostring(#events)) .. " events, current size: ") .. tostring(EventDataQueue:size()))
local i = 1
for ____, event in ipairs(events) do
if isEventType(event, defines.events.on_chunk_generated) then

View file

@ -1,5 +1,5 @@
local ____exports = {}
local ____PacketEmitter = require("main.PacketEmitter")
local ____PacketEmitter = require("main.emitting.PacketEmitter")
local PacketEmitter = ____PacketEmitter.default
local ____kafkatorio_2Dschema = require("generated.kafkatorio-schema")
local KafkatorioPacketData = ____kafkatorio_2Dschema.KafkatorioPacketData

View file

@ -1,8 +1,8 @@
local ____exports = {}
local ____EventDataCache = require("main.emitting.EventDataCache")
local EventUpdates = ____EventDataCache.default
local ____queue = require("main.queue.queue")
local Queue = ____queue.Queue
local ____EventDataCache = require("main.cache.EventDataCache")
local EventUpdatesManager = ____EventDataCache.default
local EventDataQueueManager = ____queue.EventDataQueueManager
function ____exports.initGlobal(force)
if force == nil then
force = false
@ -10,8 +10,8 @@ function ____exports.initGlobal(force)
log(("Initialising Kafkatorio Global variables (force=" .. tostring(force)) .. ")...")
global.MOD_VERSION = script.active_mods[script.mod_name]
global.FACTORIO_VERSION = script.active_mods.base
Queue.init(true)
EventUpdatesManager:init(true)
EventDataQueueManager:init(true)
EventUpdates:init(true)
log("Finished initialising Kafkatorio Global variables")
end
return ____exports

View file

@ -1,84 +1,87 @@
local ____lualib = require("lualib_bundle")
local __TS__New = ____lualib.__TS__New
local __TS__Class = ____lualib.__TS__Class
local __TS__New = ____lualib.__TS__New
local ____exports = {}
____exports.Queue = {}
local Queue = ____exports.Queue
do
local Node
function Queue.init(force)
local isAnythingUndefined = global.store == nil or global.size == nil or global.head == nil or global.tail == nil
log(((("Initialising EventQueue globals (force=" .. tostring(force)) .. ", isAnythingUndefined=") .. tostring(isAnythingUndefined)) .. ")")
if force == true or isAnythingUndefined then
global.store = {}
global.head = nil
global.tail = nil
global.size = 0
end
log("Finished initialising EventQueue")
local Node
____exports.EventDataQueueManager = __TS__Class()
local EventDataQueueManager = ____exports.EventDataQueueManager
EventDataQueueManager.name = "EventDataQueueManager"
function EventDataQueueManager.prototype.____constructor(self)
end
function EventDataQueueManager.init(self, force)
local isAnythingUndefined = global.store == nil or global.size == nil or global.head == nil or global.tail == nil
log(((("Initialising EventDataQueueManager globals (force=" .. tostring(force)) .. ", isAnythingUndefined=") .. tostring(isAnythingUndefined)) .. ")")
if force == true or isAnythingUndefined then
global.store = {}
global.head = nil
global.tail = nil
global.size = 0
end
function Queue.size()
return global.size
log("Finished initialising EventDataQueueManager")
end
function EventDataQueueManager.prototype.size(self)
return global.size
end
function EventDataQueueManager.prototype.enqueue(self, key, event, weight)
if weight == nil then
weight = 1
end
function Queue.enqueue(key, event, weight)
if weight == nil then
weight = 1
end
if global.store[key] ~= nil then
local node = __TS__New(Node, key, weight)
if global.head == nil then
global.head = node
end
if global.tail ~= nil then
global.tail.next = node
end
global.tail = node
global.size = global.size + 1
end
global.store[key] = event
end
function Queue.dequeue()
if global.store[key] ~= nil then
local node = __TS__New(Node, key, weight)
if global.head == nil then
global.head = node
end
if global.tail ~= nil then
global.tail.next = node
end
global.tail = node
global.size = global.size + 1
end
global.store[key] = event
end
function EventDataQueueManager.prototype.dequeue(self)
if global.head == nil then
return nil
else
local weight = global.head.weight
local storeKey = global.head.storeKey
global.head = global.head.next
global.size = global.size - 1
local storedPacket = global.store[storeKey]
global.store[storeKey] = nil
if storedPacket == nil then
return nil
else
local weight = global.head.weight
local storeKey = global.head.storeKey
global.head = global.head.next
global.size = global.size - 1
local storedPacket = global.store[storeKey]
global.store[storeKey] = nil
if storedPacket == nil then
return nil
else
return {value = storedPacket, weight = weight}
end
return {value = storedPacket, weight = weight}
end
end
function Queue.dequeueValues(targetWeight)
if targetWeight == nil then
targetWeight = 50
end
local values = {}
local weight = 0
repeat
do
local packet = Queue.dequeue()
if packet == nil then
break
else
weight = weight + packet.weight
values[#values + 1] = packet.value
end
end
until not (weight <= targetWeight)
return values
end
Node = __TS__Class()
Node.name = "Node"
function Node.prototype.____constructor(self, storeKey, weight)
self.next = nil
self.storeKey = storeKey
self.weight = weight
end
end
function EventDataQueueManager.prototype.dequeueValues(self, targetWeight)
if targetWeight == nil then
targetWeight = 50
end
local values = {}
local weight = 0
repeat
do
local packet = self:dequeue()
if packet == nil then
break
else
weight = weight + packet.weight
values[#values + 1] = packet.value
end
end
until not (weight <= targetWeight)
return values
end
local EventDataQueue = __TS__New(____exports.EventDataQueueManager)
____exports.default = EventDataQueue
Node = __TS__Class()
Node.name = "Node"
function Node.prototype.____constructor(self, storeKey, weight)
self.next = nil
self.storeKey = storeKey
self.weight = weight
end
return ____exports