Private
Public Access
1
0

Fixes and performance improvements

This commit is contained in:
Jan Grießhaber
2025-12-29 18:55:32 +01:00
parent 5accb091b0
commit dc3aea982e
8 changed files with 109 additions and 16 deletions

View File

@@ -10,6 +10,11 @@
}
],
"Lua.diagnostics.globals": [
"create_stats_string"
"serverIndex",
"udpAddress",
"SendProductionStats",
"SendPollutionStats",
"SendKillStats",
"SendFluidProductionStats"
]
}

View File

@@ -1,13 +1,27 @@
require("game-stats")
require("production-stats")
require("pollution-stats")
require("research-stats")
require("power-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
if game.is_multiplayer() then
serverIndex = 0
end
script.on_init(function ()
storage.researchedTechnologies = {}
storage.playerDeathCount = {}
end)
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)
@@ -30,13 +44,40 @@ script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
end
end)
function SendStats(event)
if(event.tick % (tickInterval) ~= 0) then
return
end
SendProductionStats()
SendFluidProductionStats()
SendPollutionStats()
SendKillStats()
end
function SendGameStats(event)
if(event.tick % (tickInterval*2) ~= 0) then
return
end
local returnParts = {}
returnParts[#returnParts+1] = GetMods()
returnParts[#returnParts+1] = GetMapSeed()
returnParts[#returnParts+1] = GetRocketsLaunched()
returnParts[#returnParts+1] = GetPlayerTime()
returnParts[#returnParts+1] = GetPlayerDeaths()
helpers.send_udp(udpAddress, table.concat(returnParts, "\n"), serverIndex)
end
function SendAll(event)
SendStats(event)
SendGameStats(event)
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_nth_tick(tickInterval, SendProductionStats)
script.on_nth_tick(tickInterval, SendFluidProductionStats)
script.on_nth_tick(tickInterval, GetPollutionStats)
script.on_event(defines.events.on_tick, SendAll)
--script.on_nth_tick(tickInterval*2, SendGameStats)

View File

@@ -9,9 +9,13 @@ function GetMods()
end
function GetMapSeed()
helpers.send_udp(udpAddress, ("---map-seed---\n%d"):format(game.surfaces["nauvis"].map_gen_settings.seed),serverIndex)
return("---map-seed---\n%d"):format(game.surfaces["nauvis"].map_gen_settings.seed)
end
function GetRocketsLaunched()
return("---rocket-launches---\n%d"):format(game.forces["player"].rockets_launched)
end
---Concats all player online times into a single string
---Takes all players that ever visited the server into account
---@return string

View File

@@ -1,4 +1,4 @@
function GetPollutionStats()
function SendPollutionStats()
if not (settings.global["factorio-prometheus-exporter-export_pollution_stats"].value) then
return
end
@@ -15,12 +15,41 @@ function GetPollutionStats()
end
for name, stat in pairs(pollution_input) do
pollutionParts[#pollutionParts+1] = ("%s:in:%s:%d"):format(name,surface_name, stat)
pollutionParts[#pollutionParts+1] = ("%s:in:%s:%d"):format(surface_name,name, stat)
end
for name, stat in pairs(pollution_output) do
pollutionParts[#pollutionParts+1] = ("%s:out:%s:%d"):format(name,surface_name, stat)
pollutionParts[#pollutionParts+1] = ("%s:out:%s:%d"):format(surface_name,name, stat)
end
end
helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex)
end
end
function SendKillStats()
if not (settings.global["factorio-prometheus-exporter-export_kill_stats"].value) then
return
end
local killParts = {}
killParts[#killParts+1] = "---kill-stats---\n"
for _,surface in pairs(game.surfaces) do
local surface_name = surface.name
local kill_input = game.forces["player"].get_kill_count_statistics(surface_name).input_counts
local kill_output = game.forces["player"].get_kill_count_statistics(surface_name).output_counts
if surface.platform ~= nil then
surface_name = surface.platform.name
end
for name, stat in pairs(kill_input) do
killParts[#killParts+1] = ("%s:in:%s:%d"):format(surface_name,name, stat)
end
for name, stat in pairs(kill_output) do
killParts[#killParts+1] = ("%s:out:%s:%d"):format(surface_name,name, stat)
end
end
helpers.send_udp(udpAddress, table.concat(killParts,"\n"),serverIndex)
end

6
power-stats.lua Normal file
View File

@@ -0,0 +1,6 @@
function GetGlobalGrids()
for _,surface in pairs(game.surfaces) do
local grids = surface.has_global_electric_network
--log("Surface: "..surface.name.." has global electric network: "..tostring(grids))
end
end

View File

@@ -1,11 +1,11 @@
local researches = {}
---comment
---@param event EventData.on_research_finished
function onResearchFinished(event)
for id, tech in pairs(game.forces["player"].technologies) do
researches[id] = tech
storage.researched[id] = tech
end
end

View File

@@ -56,5 +56,12 @@ data:extend({
setting_type = "runtime-global",
default_value = true,
order = "h"
},
{
type = "bool-setting",
name = "factorio-prometheus-exporter-export_kill_stats",
setting_type = "runtime-global",
default_value = true,
order = "h"
}
})

View File

@@ -1 +1,2 @@
storage.playerDeathCount = {}
storage.researchedTech = {}