From 0facba95d691989eada658858264a26fd038ef4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grie=C3=9Fhaber?= Date: Tue, 30 Dec 2025 00:15:38 +0100 Subject: [PATCH] Added logistics, small fixes and cleanup --- .vscode/settings.json | 3 ++- control.lua | 48 +++++++++++++++++++++----------------- logistic-network-stats.lua | 18 ++++++++++++++ power-stats.lua | 2 +- production-stats.lua | 26 +++++++++++++++++++++ research-stats.lua | 20 ---------------- 6 files changed, 73 insertions(+), 44 deletions(-) create mode 100644 logistic-network-stats.lua diff --git a/.vscode/settings.json b/.vscode/settings.json index c359285..735b124 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -16,5 +16,6 @@ "SendPollutionStats", "SendKillStats", "SendFluidProductionStats" - ] + ], + "editor.fontSize": 13 } \ No newline at end of file diff --git a/control.lua b/control.lua index c7ca5ad..0e591b6 100644 --- a/control.lua +++ b/control.lua @@ -3,13 +3,17 @@ require("production-stats") require("pollution-stats") require("research-stats") require("power-stats") +require("logistic-network-stats") require("storage") tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300 udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555 -serverIndex = 1 +isInitialized = false +sendIndex = 0 + script.on_init(function () + storage.electricGrids = {} storage.researchedTechnologies = {} storage.playerDeathCount = {} end) @@ -18,36 +22,25 @@ script.on_load(function () log("factorio-prometheus-exporter: on_load") log("tickInterval: "..tickInterval) log("udpAddress: "..udpAddress) - end) -- Register the handler for the player movement event ---script.on_event(defines.events.on_player_changed_position, on_player_moved) script.on_event(defines.events.on_player_died, function(event) local player = game.get_player(event.player_index) storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1 end) -script.on_event(defines.events.on_player_joined_game, function(event) - local player = game.get_player(event.player_index) - if not player then return end - local index = event.player_index - helpers.send_udp(udpAddress, ("player-join %s %d"):format(player.name, player.index),index) -end) - script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) + log("Mod setting changed: "..event.setting) if event.setting == "factorio-prometheus-exporter-tick-interval" then tickInterval = settings.global["factorio-prometheus-exporter-tick-interval"].value end end) function SendStats(event) - if(event.tick % (tickInterval) ~= 0) then - return - end SendProductionStats() SendFluidProductionStats() SendPollutionStats() @@ -56,9 +49,6 @@ end function SendGameStats(event) - if(event.tick % (tickInterval*2) ~= 0) then - return - end local returnParts = {} returnParts[#returnParts+1] = GetMods() returnParts[#returnParts+1] = GetMapSeed() @@ -69,15 +59,29 @@ function SendGameStats(event) end function SendAll(event) - SendStats(event) - SendGameStats(event) + if(isInitialized == false) then + if game.is_multiplayer() then + serverIndex = 0 + end + else + serverIndex = 1 + end + isInitialized = true + + + if (event.tick % math.floor(tickInterval/4) ~= 0) then return end + + sendIndex = (sendIndex % 5) + 1 + if sendIndex == 1 then SendProductionStats() end + if sendIndex == 2 then SendPollutionStats() end + if sendIndex == 3 then SendKillStats() end + if sendIndex == 4 then SendFluidProductionStats() end + if sendIndex == 5 then SendBuildStats() end + + if(event.tick % tickInterval*2 == 0) then SendGameStats(event) end end ---script.on_event(defines.events.on_player_joined_game, on_player_joined) ---script.on_nth_tick(300, SendSurfaceStats) ---script.on_event(defines.events.on_tick, SendSurfaceStats) script.on_event(defines.events.on_tick, SendAll) ---script.on_nth_tick(tickInterval*2, SendGameStats) diff --git a/logistic-network-stats.lua b/logistic-network-stats.lua new file mode 100644 index 0000000..036cf5a --- /dev/null +++ b/logistic-network-stats.lua @@ -0,0 +1,18 @@ +function GetAllLogisticGrids() + local resultParts = {} + resultParts[#resultParts+1] = "---logistic-grids---\n" + for _,surface in pairs(game.surfaces) do + local grids = game.forces["player"].logistic_networks[surface.name] + for _,grid in pairs(grids) do + resultParts[#resultParts+1] = ("%s:%d:%d:%d:%d:%d:%d"):format( + surface.name, + grid.network_id, + grid.all_construction_robots, + grid.all_logistic_robots, + grid.available_construction_robots, + grid.available_logistic_robots, + grid.robot_limit) + end + end + return table.concat(resultParts, "\n") +end \ No newline at end of file diff --git a/power-stats.lua b/power-stats.lua index 4e8c4ac..6b6fda1 100644 --- a/power-stats.lua +++ b/power-stats.lua @@ -3,4 +3,4 @@ function GetGlobalGrids() local grids = surface.has_global_electric_network --log("Surface: "..surface.name.." has global electric network: "..tostring(grids)) end -end \ No newline at end of file +end diff --git a/production-stats.lua b/production-stats.lua index 5cd4ca9..b763bcb 100644 --- a/production-stats.lua +++ b/production-stats.lua @@ -48,4 +48,30 @@ function SendFluidProductionStats() end end helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) +end + + +function SendBuildStats() + local buildParts = {} + buildParts[#buildParts+1] = "---build-stats---" + for _,surface in pairs(game.surfaces) do + local surfaceName = surface.name + + local inputStats = game.forces["player"].get_entity_build_count_statistics(surfaceName).input_counts + local outputStats = game.forces["player"].get_entity_build_count_statistics(surfaceName).output_counts + + if(surface.platform ~= nil) then + --surface is a space platform and has a seperate name we can use + surfaceName = surface.platform.name + end + + for itemName, itemCount in pairs(inputStats) do + buildParts[#buildParts+1] = ("%s:in:%s:%d"):format(surfaceName, itemName, itemCount) + end + + for itemName, itemCount in pairs(outputStats) do + buildParts[#buildParts+1] = ("%s:out:%s:%d"):format(surfaceName, itemName, itemCount) + end + end + helpers.send_udp(udpAddress, table.concat(buildParts, "\n"), serverIndex) end \ No newline at end of file diff --git a/research-stats.lua b/research-stats.lua index da27aad..e69de29 100644 --- a/research-stats.lua +++ b/research-stats.lua @@ -1,20 +0,0 @@ - - - ----comment ----@param event EventData.on_research_finished -function onResearchFinished(event) - for id, tech in pairs(game.forces["player"].technologies) do - storage.researched[id] = tech - end - end - ----comment ----@param event EventData.on_research_started -function onResearchStarted(event) -end - ----comment ----@param event EventData.on_research_queued -function onResearchQueued(event) -end