Private
Public Access
1
0
Files
lua-prometheus-exporter/control.lua
2025-12-30 00:15:38 +01:00

88 lines
2.4 KiB
Lua

require("game-stats")
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
isInitialized = false
sendIndex = 0
script.on_init(function ()
storage.electricGrids = {}
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_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_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)
SendProductionStats()
SendFluidProductionStats()
SendPollutionStats()
SendKillStats()
end
function SendGameStats(event)
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)
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_tick, SendAll)