Private
Public Access
1
0

Added more functions

Streamlined the process
This commit is contained in:
Jan Grießhaber
2025-12-29 16:32:47 +01:00
parent 149b085cb7
commit 5accb091b0
6 changed files with 133 additions and 112 deletions

View File

@@ -1,48 +1,42 @@
require("production-stats") require("production-stats")
require("pollution-stats")
-- control.lua for factorio-prometheus-exporter tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300
-- Adds a handler for player movement (on_player_changed_position) udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555
---@param event EventData.on_tick
local function SendSurfaceStats(event)
if event.tick % 300 ~= 0 then return end
helpers.send_udp(52555, game.tick,1) serverIndex = 1
for _,surface in pairs(game.surfaces) do if game.is_multiplayer() then
local surface_name = surface.name serverIndex = 0
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 end
end
-- Register the handler for the player movement event -- 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_changed_position, on_player_moved)
script.on_event(defines.events.on_player_died, function(event) script.on_event(defines.events.on_player_died, function(event)
local player = game.get_player(event.player_index) local player = game.get_player(event.player_index)
if not player then return end storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
local index = event.player_index
helpers.send_udp(52555, ("player-death %s %d"):format(player.name, player.index),index)
end) end)
script.on_event(defines.events.on_player_joined_game, function(event) script.on_event(defines.events.on_player_joined_game, function(event)
local player = game.get_player(event.player_index) local player = game.get_player(event.player_index)
if not player then return end if not player then return end
local index = event.player_index 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) end)
function GetAllPlayers()
for _,player in pairs(game.players) do
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
end end)
--script.on_event(defines.events.on_player_joined_game, on_player_joined) --script.on_event(defines.events.on_player_joined_game, on_player_joined)
--script.on_nth_tick(300, SendSurfaceStats) --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)

View File

@@ -3,10 +3,13 @@ function GetMods()
local mods = script.active_mods local mods = script.active_mods
local modstring = "---mod-info---\n" local modstring = "---mod-info---\n"
for k,v in pairs(mods) do 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 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 end
---Concats all player online times into a single string ---Concats all player online times into a single string
@@ -16,7 +19,19 @@ function GetPlayerTime()
local timeParts = {} local timeParts = {}
timeParts[#timeParts+1] = "---player-times---\n" timeParts[#timeParts+1] = "---player-times---\n"
for _,player in pairs(game.players) do 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 end
return table.concat(timeParts, "\n") return table.concat(timeParts, "\n")
end 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

View File

@@ -1,18 +1,26 @@
function GetPollutionStats() 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 for _,surface in pairs(game.surfaces) do
local surface_name = surface.name local surface_name = surface.name
local pollutionParts = {}
local pollution_stats = game.surfaces[surface_name].pollution_statistics local pollution_input = game.surfaces[surface_name].pollution_statistics.input_counts
local pollution_input = pollution_stats.input_counts local pollution_output = game.surfaces[surface_name].pollution_statistics.output_counts
local pollution_output = pollution_stats.output_counts
pollutionParts[#pollutionParts+1] = ("---pollution-input---%s\n"):format(surface_name) if surface.platform ~= nil then
surface_name = surface.platform.name
end
for name, stat in pairs(pollution_input) do 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 end
pollutionParts[#pollutionParts+1] = ("---pollution-output---%s\n"):format(surface_name)
for name, stat in pairs(pollution_output) do 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 end
helpers.send_udp(52555, table.concat(pollutionParts,"\n"),1) helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex)
end end
end end

View File

@@ -1,56 +1,51 @@
---@param productionStatsTable LuaFlowStatistics function SendProductionStats()
---@param surface LuaSurface local productionParts = {}
---@return string productionParts[#productionParts+1] = "---production-stats---"
function CreateItemStatisticsString(productionStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]]) for _,surface in pairs(game.surfaces) do
local parts = {} local surfaceName = surface.name
parts[#parts+1] = surface.name
parts[#parts+1] = "---input---" local inputStats = game.forces["player"].get_item_production_statistics(surfaceName).input_counts
for itemName, itemCount in pairs(productionStatsTable.input_counts) do local outputStats = game.forces["player"].get_item_production_statistics(surfaceName).output_counts
parts[#parts+1] = itemName .. ":" .. itemCount
if(surface.platform ~= nil) then
--surface is a space platform and has a seperate name we can use
surfaceName = surface.platform.name
end end
parts[#parts+1] = "---output---"
for itemName, itemCount in pairs(productionStatsTable.output_counts) do for itemName, itemCount in pairs(inputStats) do
parts[#parts+1] = itemName .. ":" .. itemCount productionParts[#productionParts+1] = ("%s:in:%s:%d"):format(surfaceName, itemName, itemCount)
end end
parts[#parts+1] = surface.name
return table.concat(parts, "\n") for itemName, itemCount in pairs(outputStats) do
productionParts[#productionParts+1] = ("%s:out:%s:%d"):format(surfaceName, itemName, itemCount)
end
end
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
end end
---comment
---@param fluidStatsTable LuaFlowStatistics function SendFluidProductionStats()
---@param surface LuaSurface local productionParts = {}
---@return string productionParts[#productionParts+1] = "---fluid-production-stats---"
function CreateFluidStatisticsString(fluidStatsTable --[[LuaFlowStatistics]], surface --[[LuaSurface]]) for _,surface in pairs(game.surfaces) do
local parts = {} local surfaceName = surface.name
parts[#parts+1] = surface.name
parts[#parts+1] = "---input---" local inputStats = game.forces["player"].get_fluid_production_statistics(surfaceName).input_counts
for fluidName, fluidCount in pairs(fluidStatsTable.input_counts) do local outputStats = game.forces["player"].get_fluid_production_statistics(surfaceName).output_counts
parts[#parts+1] = fluidName .. ":" .. fluidCount
end if(surface.platform ~= nil) then
parts[#parts+1] = "---output---" --surface is a space platform and has a seperate name we can use
for fluidName, fluidCount in pairs(fluidStatsTable.output_counts) do surfaceName = surface.platform.name
parts[#parts+1] = fluidName .. ":" .. fluidCount
end
parts[#parts+1] = surface.name
return table.concat(parts, "\n")
end end
---comment for itemName, itemCount in pairs(inputStats) do
---@param deathStatsTable LuaFlowStatistics productionParts[#productionParts+1] = ("%s:in:%s:%d"):format(surfaceName, itemName, itemCount)
---@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
end end
parts[#parts+1] = "---output---"
for name, count in pairs(deathStatsTable.output_counts) do for itemName, itemCount in pairs(outputStats) do
parts[#parts+1] = name .. ":" .. count productionParts[#productionParts+1] = ("%s:out:%s:%d"):format(surfaceName, itemName, itemCount)
end end
parts[#parts+1] = surface.name end
return table.concat(parts, "\n") helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
end end

View File

@@ -1,52 +1,60 @@
data:extend({ data:extend({
{ {
type = "bool-setting", type = "int-setting",
name = "factorio-prometheus-exporter-enable", name = "factorio-prometheus-exporter-tick-interval",
setting_type = "startup", setting_type = "runtime-global",
default_value = true, minimum_value = 60,
order = "a" default_value = 300,
},
{
type = "string-setting",
name = "factorio-prometheus-exporter-udp-address",
setting_type = "startup",
allow_blank = true,
default_value = "",
order = "b" order = "b"
}, },
{ {
type = "bool-setting", type = "bool-setting",
name = "factorio-prometheus-exporter-export_production_stats", name = "factorio-prometheus-exporter-enable",
setting_type = "startup", setting_type = "runtime-global",
default_value = true, 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" order = "c"
}, },
{ {
type = "bool-setting", type = "bool-setting",
name = "factorio-prometheus-exporter-export_player_deaths", name = "factorio-prometheus-exporter-export_production_stats",
setting_type = "startup", setting_type = "runtime-global",
default_value = true, default_value = true,
order = "d" order = "d"
}, },
{ {
type = "bool-setting", type = "bool-setting",
name = "factorio-prometheus-exporter-export_fluid_stats", name = "factorio-prometheus-exporter-export_player_stats",
setting_type = "startup", setting_type = "runtime-global",
default_value = true, default_value = true,
order = "e" order = "e"
}, },
{ {
type = "bool-setting", type = "bool-setting",
name = "factorio-prometheus-exporter-export_power_stats", name = "factorio-prometheus-exporter-export_fluid_stats",
setting_type = "startup", setting_type = "runtime-global",
default_value = false, default_value = true,
order = "f" order = "f"
}, },
{ {
type = "bool-setting", type = "bool-setting",
name = "factorio-prometheus-exporter-export_pollution_stats", name = "factorio-prometheus-exporter-export_power_stats",
setting_type = "startup", setting_type = "runtime-global",
default_value = true, default_value = false,
order = "g" order = "g"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_pollution_stats",
setting_type = "runtime-global",
default_value = true,
order = "h"
} }
}) })

1
storage.lua Normal file
View File

@@ -0,0 +1 @@
storage.playerDeathCount = {}