Private
Public Access
1
0

Added logistics, small fixes and cleanup

This commit is contained in:
Jan Grießhaber
2025-12-30 00:15:38 +01:00
parent dc3aea982e
commit 0facba95d6
6 changed files with 73 additions and 44 deletions

View File

@@ -16,5 +16,6 @@
"SendPollutionStats",
"SendKillStats",
"SendFluidProductionStats"
]
],
"editor.fontSize": 13
}

View File

@@ -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)

View File

@@ -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

View File

@@ -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
end

View File

@@ -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

View File

@@ -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