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", "SendPollutionStats",
"SendKillStats", "SendKillStats",
"SendFluidProductionStats" "SendFluidProductionStats"
] ],
"editor.fontSize": 13
} }

View File

@@ -3,13 +3,17 @@ require("production-stats")
require("pollution-stats") require("pollution-stats")
require("research-stats") require("research-stats")
require("power-stats") require("power-stats")
require("logistic-network-stats")
require("storage") require("storage")
tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300 tickInterval = tonumber(settings.global["factorio-prometheus-exporter-tick-interval"].value) or 300
udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555 udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address"].value) or 52555
serverIndex = 1 isInitialized = false
sendIndex = 0
script.on_init(function () script.on_init(function ()
storage.electricGrids = {}
storage.researchedTechnologies = {} storage.researchedTechnologies = {}
storage.playerDeathCount = {} storage.playerDeathCount = {}
end) end)
@@ -18,36 +22,25 @@ script.on_load(function ()
log("factorio-prometheus-exporter: on_load") log("factorio-prometheus-exporter: on_load")
log("tickInterval: "..tickInterval) log("tickInterval: "..tickInterval)
log("udpAddress: "..udpAddress) log("udpAddress: "..udpAddress)
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_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)
storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1 storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
end) 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) 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 if event.setting == "factorio-prometheus-exporter-tick-interval" then
tickInterval = settings.global["factorio-prometheus-exporter-tick-interval"].value tickInterval = settings.global["factorio-prometheus-exporter-tick-interval"].value
end end
end) end)
function SendStats(event) function SendStats(event)
if(event.tick % (tickInterval) ~= 0) then
return
end
SendProductionStats() SendProductionStats()
SendFluidProductionStats() SendFluidProductionStats()
SendPollutionStats() SendPollutionStats()
@@ -56,9 +49,6 @@ end
function SendGameStats(event) function SendGameStats(event)
if(event.tick % (tickInterval*2) ~= 0) then
return
end
local returnParts = {} local returnParts = {}
returnParts[#returnParts+1] = GetMods() returnParts[#returnParts+1] = GetMods()
returnParts[#returnParts+1] = GetMapSeed() returnParts[#returnParts+1] = GetMapSeed()
@@ -69,15 +59,29 @@ function SendGameStats(event)
end end
function SendAll(event) function SendAll(event)
SendStats(event) if(isInitialized == false) then
SendGameStats(event) 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 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_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

@@ -49,3 +49,29 @@ function SendFluidProductionStats()
end end
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
end 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