From 5accb091b04dfadb2618515bc7dae6a7e5b3b3fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grie=C3=9Fhaber?= Date: Mon, 29 Dec 2025 16:32:47 +0100 Subject: [PATCH] Added more functions Streamlined the process --- control.lua | 46 ++++++++++------------ game-stats.lua | 23 +++++++++-- pollution-stats.lua | 26 ++++++++----- production-stats.lua | 93 +++++++++++++++++++++----------------------- settings.lua | 56 ++++++++++++++------------ storage.lua | 1 + 6 files changed, 133 insertions(+), 112 deletions(-) create mode 100644 storage.lua diff --git a/control.lua b/control.lua index 65e15d0..9827f18 100644 --- a/control.lua +++ b/control.lua @@ -1,48 +1,42 @@ require("production-stats") +require("pollution-stats") --- control.lua for factorio-prometheus-exporter --- Adds a handler for player movement (on_player_changed_position) ----@param event EventData.on_tick -local function SendSurfaceStats(event) - if event.tick % 300 ~= 0 then return end +tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300 +udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555 - helpers.send_udp(52555, game.tick,1) - for _,surface in pairs(game.surfaces) do - local surface_name = surface.name - local productionStat = CreateItemStatisticsString(game.forces["player"].get_item_production_statistics(surface_name), surface) - local fluidStat = CreateFluidStatisticsString(game.forces["player"].get_fluid_production_statistics(surface_name), surface) - local deathStat = CreateDeathStatisticsString(game.forces["player"].get_kill_count_statistics(surface_name), surface) - helpers.send_udp(52555, productionStat..fluidStat..deathStat,1) - end +serverIndex = 1 +if game.is_multiplayer() then + serverIndex = 0 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) - if not player then return end - local index = event.player_index - helpers.send_udp(52555, ("player-death %s %d"):format(player.name, player.index),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(52555, ("player-join %s %d"):format(player.name, player.index),index) + helpers.send_udp(udpAddress, ("player-join %s %d"):format(player.name, player.index),index) end) -function GetAllPlayers() - for _,player in pairs(game.players) do - end -end +script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) + if event.setting == "factorio-prometheus-exporter-tick-interval" then + tickInterval = settings.global["factorio-prometheus-exporter-tick-interval"].value + 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, SendSurfaceStats) +script.on_nth_tick(tickInterval, SendProductionStats) +script.on_nth_tick(tickInterval, SendFluidProductionStats) +script.on_nth_tick(tickInterval, GetPollutionStats) + + diff --git a/game-stats.lua b/game-stats.lua index a9b1ffa..edd8030 100644 --- a/game-stats.lua +++ b/game-stats.lua @@ -3,10 +3,13 @@ function GetMods() local mods = script.active_mods local modstring = "---mod-info---\n" for k,v in pairs(mods) do - modstring = modstring .. ("mod-info:%s:%s\n"):format(k,v) - + modstring = modstring .. ("%s:%s\n"):format(k,v) end - helpers.send_udp(52555, modstring,1) + helpers.send_udp(udpAddress, modstring,serverIndex) +end + +function GetMapSeed() + helpers.send_udp(udpAddress, ("---map-seed---\n%d"):format(game.surfaces["nauvis"].map_gen_settings.seed),serverIndex) end ---Concats all player online times into a single string @@ -16,7 +19,19 @@ function GetPlayerTime() local timeParts = {} timeParts[#timeParts+1] = "---player-times---\n" for _,player in pairs(game.players) do - timeParts[#timeParts+1] = ("player-time:%s:%d:%d"):format(player.name, player.index, player.online_time) + timeParts[#timeParts+1] = ("%s:%d:%d"):format(player.name, player.index, player.online_time) end return table.concat(timeParts, "\n") +end + + +---comment +---@return string +function GetPlayerDeaths() + local deathParts = {} + deathParts[#deathParts+1] = "---player-deaths---\n" + for _,player in pairs(game.players) do + deathParts[#deathParts+1] = ("%s:%d:%d"):format(player.name, player.index, storage.playerDeathCount[player.index]or 0) + end + return table.concat(deathParts, "\n") end \ No newline at end of file diff --git a/pollution-stats.lua b/pollution-stats.lua index 2db252f..be9ddf0 100644 --- a/pollution-stats.lua +++ b/pollution-stats.lua @@ -1,18 +1,26 @@ function GetPollutionStats() + if not (settings.global["factorio-prometheus-exporter-export_pollution_stats"].value) then + return + end + local pollutionParts = {} + pollutionParts[#pollutionParts+1] = "---pollution-stats---\n" for _,surface in pairs(game.surfaces) do local surface_name = surface.name - local pollutionParts = {} - local pollution_stats = game.surfaces[surface_name].pollution_statistics - local pollution_input = pollution_stats.input_counts - local pollution_output = pollution_stats.output_counts - pollutionParts[#pollutionParts+1] = ("---pollution-input---%s\n"):format(surface_name) + + local pollution_input = game.surfaces[surface_name].pollution_statistics.input_counts + local pollution_output = game.surfaces[surface_name].pollution_statistics.output_counts + + if surface.platform ~= nil then + surface_name = surface.platform.name + end + for name, stat in pairs(pollution_input) do - pollutionParts[#pollutionParts+1] = ("%s:%d"):format(name, stat) + pollutionParts[#pollutionParts+1] = ("%s:in:%s:%d"):format(name,surface_name, stat) end - pollutionParts[#pollutionParts+1] = ("---pollution-output---%s\n"):format(surface_name) + for name, stat in pairs(pollution_output) do - pollutionParts[#pollutionParts+1] = ("%s:%d"):format(name, stat) + pollutionParts[#pollutionParts+1] = ("%s:out:%s:%d"):format(name,surface_name, stat) end - helpers.send_udp(52555, table.concat(pollutionParts,"\n"),1) + helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex) end end \ No newline at end of file diff --git a/production-stats.lua b/production-stats.lua index 7288a20..5cd4ca9 100644 --- a/production-stats.lua +++ b/production-stats.lua @@ -1,56 +1,51 @@ ----@param productionStatsTable LuaFlowStatistics ----@param surface LuaSurface ----@return string -function CreateItemStatisticsString(productionStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]]) - local parts = {} - parts[#parts+1] = surface.name - parts[#parts+1] = "---input---" - for itemName, itemCount in pairs(productionStatsTable.input_counts) do - parts[#parts+1] = itemName .. ":" .. itemCount +function SendProductionStats() + local productionParts = {} + productionParts[#productionParts+1] = "---production-stats---" + for _,surface in pairs(game.surfaces) do + local surfaceName = surface.name + + local inputStats = game.forces["player"].get_item_production_statistics(surfaceName).input_counts + local outputStats = game.forces["player"].get_item_production_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 + productionParts[#productionParts+1] = ("%s:in:%s:%d"):format(surfaceName, itemName, itemCount) + end + + for itemName, itemCount in pairs(outputStats) do + productionParts[#productionParts+1] = ("%s:out:%s:%d"):format(surfaceName, itemName, itemCount) + end end - parts[#parts+1] = "---output---" - for itemName, itemCount in pairs(productionStatsTable.output_counts) do - parts[#parts+1] = itemName .. ":" .. itemCount - end - parts[#parts+1] = surface.name - return table.concat(parts, "\n") + helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) end ----comment ----@param fluidStatsTable LuaFlowStatistics ----@param surface LuaSurface ----@return string -function CreateFluidStatisticsString(fluidStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]]) - local parts = {} - parts[#parts+1] = surface.name - parts[#parts+1] = "---input---" - for fluidName, fluidCount in pairs(fluidStatsTable.input_counts) do - parts[#parts+1] = fluidName .. ":" .. fluidCount - end - parts[#parts+1] = "---output---" - for fluidName, fluidCount in pairs(fluidStatsTable.output_counts) do - parts[#parts+1] = fluidName .. ":" .. fluidCount - end - parts[#parts+1] = surface.name - return table.concat(parts, "\n") -end ----comment ----@param deathStatsTable LuaFlowStatistics ----@param surface LuaSurface ----@return string -function CreateDeathStatisticsString(deathStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]]) - local parts = {} - parts[#parts+1] = surface.name.."{" - parts[#parts+1] = "---input---" - for name, count in pairs(deathStatsTable.input_counts) do - parts[#parts+1] = name .. ":" .. count +function SendFluidProductionStats() + local productionParts = {} + productionParts[#productionParts+1] = "---fluid-production-stats---" + for _,surface in pairs(game.surfaces) do + local surfaceName = surface.name + + local inputStats = game.forces["player"].get_fluid_production_statistics(surfaceName).input_counts + local outputStats = game.forces["player"].get_fluid_production_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 + productionParts[#productionParts+1] = ("%s:in:%s:%d"):format(surfaceName, itemName, itemCount) + end + + for itemName, itemCount in pairs(outputStats) do + productionParts[#productionParts+1] = ("%s:out:%s:%d"):format(surfaceName, itemName, itemCount) + end end - parts[#parts+1] = "---output---" - for name, count in pairs(deathStatsTable.output_counts) do - parts[#parts+1] = name .. ":" .. count - end - parts[#parts+1] = surface.name - return table.concat(parts, "\n") + helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) end \ No newline at end of file diff --git a/settings.lua b/settings.lua index 56cd86a..71f48c3 100644 --- a/settings.lua +++ b/settings.lua @@ -1,52 +1,60 @@ data:extend({ { - type = "bool-setting", - name = "factorio-prometheus-exporter-enable", - setting_type = "startup", - default_value = true, - order = "a" - }, - { - type = "string-setting", - name = "factorio-prometheus-exporter-udp-address", - setting_type = "startup", - allow_blank = true, - default_value = "", + type = "int-setting", + name = "factorio-prometheus-exporter-tick-interval", + setting_type = "runtime-global", + minimum_value = 60, + default_value = 300, order = "b" }, { type = "bool-setting", - name = "factorio-prometheus-exporter-export_production_stats", - setting_type = "startup", + name = "factorio-prometheus-exporter-enable", + setting_type = "runtime-global", default_value = true, + order = "a" + }, + { + type = "int-setting", + name = "factorio-prometheus-exporter-udp-address", + setting_type = "startup", + allow_blank = false, + default_value = 52555, order = "c" }, { type = "bool-setting", - name = "factorio-prometheus-exporter-export_player_deaths", - setting_type = "startup", + name = "factorio-prometheus-exporter-export_production_stats", + setting_type = "runtime-global", default_value = true, order = "d" }, { type = "bool-setting", - name = "factorio-prometheus-exporter-export_fluid_stats", - setting_type = "startup", + name = "factorio-prometheus-exporter-export_player_stats", + setting_type = "runtime-global", default_value = true, order = "e" }, { type = "bool-setting", - name = "factorio-prometheus-exporter-export_power_stats", - setting_type = "startup", - default_value = false, + name = "factorio-prometheus-exporter-export_fluid_stats", + setting_type = "runtime-global", + default_value = true, order = "f" }, { type = "bool-setting", - name = "factorio-prometheus-exporter-export_pollution_stats", - setting_type = "startup", - default_value = true, + name = "factorio-prometheus-exporter-export_power_stats", + setting_type = "runtime-global", + default_value = false, order = "g" + }, + { + type = "bool-setting", + name = "factorio-prometheus-exporter-export_pollution_stats", + setting_type = "runtime-global", + default_value = true, + order = "h" } }) \ No newline at end of file diff --git a/storage.lua b/storage.lua new file mode 100644 index 0000000..bb20628 --- /dev/null +++ b/storage.lua @@ -0,0 +1 @@ +storage.playerDeathCount = {} \ No newline at end of file