diff --git a/control.lua b/control.lua index ce43072..b1fd6d0 100644 --- a/control.lua +++ b/control.lua @@ -10,6 +10,19 @@ udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address isInitialized = false sendIndex = 0 +options = { + enableMod = false, + enablePlayers = false, + enableProduction = false, + enableFluid = false, + enableKills = false, + enablePollution = false, + enableRobots = false, + enableResearch = false, + enablePower = false, + enableTrains = false +} + script.on_init(function () storage.electricGrids = {} @@ -18,38 +31,101 @@ script.on_init(function () storage.totalLabCount = 0 storage.totalResearchSpeed = 0 storage.totalReseachProductivity = 0 + storage.labs = {} + sendIndex = 0 + + options.enableMod = settings.global["factorio-prometheus-exporter-exporter_enable"].value + options.enableProduction = settings.global["factorio-prometheus-exporter-export_production_stats"].value + options.enablePollution = settings.global["factorio-prometheus-exporter-export_pollution_stats"].value + options.enableFluid = settings.global["factorio-prometheus-exporter-export_fluid_stats"].value + options.enablePlayers = settings.global["factorio-prometheus-exporter-export_player_stats"].value + options.enableKills = settings.global["factorio-prometheus-exporter-export_kill_stats"].value + options.enablePower = settings.global["factorio-prometheus-exporter-export_power_stats"].value + options.enableResearch = settings.global["factorio-prometheus-exporter-export_research_stats"].value + options.enableRobots = settings.global["factorio-prometheus-exporter_export_logistic_stats"].value + + end) script.on_load(function () log("factorio-prometheus-exporter: on_load") log("tickInterval: "..tickInterval) log("udpAddress: "..udpAddress) + + options.enableMod = settings.global["factorio-prometheus-exporter-exporter_enable"].value + options.enableProduction = settings.global["factorio-prometheus-exporter-export_production_stats"].value + options.enablePollution = settings.global["factorio-prometheus-exporter-export_pollution_stats"].value + options.enableFluid = settings.global["factorio-prometheus-exporter-export_fluid_stats"].value + options.enablePlayers = settings.global["factorio-prometheus-exporter-export_player_stats"].value + options.enableKills = settings.global["factorio-prometheus-exporter-export_kill_stats"].value + options.enablePower = settings.global["factorio-prometheus-exporter-export_power_stats"].value + options.enableResearch = settings.global["factorio-prometheus-exporter-export_research_stats"].value + options.enableRobots = settings.global["factorio-prometheus-exporter_export_logistic_stats"].value end) +script.on_configuration_changed(function() + storage.electricGrids = storage.electricGrids or {} + storage.labs = storage.labs or {} + storage.playerDeathCount = storage.playerDeathCount or {} + storage.researchedTechnologies = storage.researchedTechnologies or {} + storage.totalLabCount = storage.totalLabCount or 0 + storage.totalReseachProductivity = storage.totalReseachProductivity or 0 + storage.totalResearchSpeed = storage.totalResearchSpeed or 0 +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 + if event.setting == "factorio-prometheus-exporter-enable" then + options.enableMod = settings.global["factorio-prometheus-exporter-enable"].value + end + if event.setting == "factorio-prometheus-exporter-export_production_stats" then + options.enableProduction = settings.global["factorio-prometheus-exporter-export_production_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_fluid_stats" then + options.enableFluid = settings.global["factorio-prometheus-exporter-export_fluid_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_pollution_stats" then + options.enablePollution = settings.global["factorio-prometheus-exporter-export_pollution_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_power_stats" then + options.enablePower = settings.global["factorio-prometheus-exporter-export_power_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_logistic_stats" then + options.enableRobots = settings.global["factorio-prometheus-exporter-export_logistic_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_player_stats" then + options.enablePlayers = settings.global["factorio-prometheus-exporter-export_player_stats"].value + end + + + if event.setting == "factorio-prometheus-exporter-export_kill_stats" then + options.enableKills = settings.global["factorio-prometheus-exporter-export_kill_stats"].value + end + + if event.setting == "factorio-prometheus-exporter-export_research_stats" then + options.enableResearch = settings.global["factorio-prometheus-exporter-export_research_stats"].value + end + end) -function SendStats(event) - SendProductionStats() - SendFluidProductionStats() - SendPollutionStats() - SendKillStats() -end +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) function SendGameStats(event) local returnParts = {} @@ -62,6 +138,7 @@ function SendGameStats(event) end function SendAll(event) + if(isInitialized == false) then if game.is_multiplayer() then serverIndex = 0 @@ -71,21 +148,42 @@ function SendAll(event) end isInitialized = true + if PowerGridScanned == false then + GenerateNetworks() + end - if (event.tick % math.floor(tickInterval/6) ~= 0) then return end + if LabsScanned == false then + GenerateLabInfo() +end - sendIndex = (sendIndex % 6) + 1 + + if options.enableMod==true then + + if (event.tick % math.floor(tickInterval/7) ~= 0) then return end + + sendIndex = (sendIndex % 7) + 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 sendIndex == 6 then SendResearchStats() end + if sendIndex == 7 then SendLogisticStats() end - if(event.tick % tickInterval*2 == 0) then SendGameStats(event) end + if(event.tick % tickInterval*2 == 0) then + if options.enablePlayers then + SendGameStats(event) end + end + end end script.on_event(defines.events.on_tick, SendAll) +--Script hooks for power stats +script.on_event(defines.events.on_built_entity,UpdateGrids,{{filter = "type", type = "electric-pole"}}) +script.on_event(defines.events.on_player_mined_entity, PurgeNetworks,{{filter = "type", type = "electric-pole"}}) +script.on_event(defines.events.on_robot_built_entity,UpdateGrids,{{filter = "type", type = "electric-pole"}}) +script.on_event(defines.events.on_robot_mined_entity,PurgeNetworks,{{filter = "type", type = "electric-pole"}}) + diff --git a/logistic-network-stats.lua b/logistic-network-stats.lua index 036cf5a..0801581 100644 --- a/logistic-network-stats.lua +++ b/logistic-network-stats.lua @@ -15,4 +15,10 @@ function GetAllLogisticGrids() end end return table.concat(resultParts, "\n") -end \ No newline at end of file +end + +function SendLogisticStats() + if options.enableRobots then + helpers.send_udp(udpAddress,GetAllLogisticGrids(),serverIndex) + end +end diff --git a/pollution-stats.lua b/pollution-stats.lua index 90c5849..119da8f 100644 --- a/pollution-stats.lua +++ b/pollution-stats.lua @@ -1,7 +1,5 @@ function SendPollutionStats() - if not (settings.global["factorio-prometheus-exporter-export_pollution_stats"].value) then - return - end + if options.enablePollution then local pollutionParts = {} pollutionParts[#pollutionParts+1] = "---pollution-stats---\n" for _,surface in pairs(game.surfaces) do @@ -25,11 +23,10 @@ function SendPollutionStats() 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 + if options.enableKills then local killParts = {} killParts[#killParts+1] = "---kill-stats---\n" for _,surface in pairs(game.surfaces) do @@ -52,4 +49,5 @@ function SendKillStats() end helpers.send_udp(udpAddress, table.concat(killParts,"\n"),serverIndex) +end end \ No newline at end of file diff --git a/power-stats.lua b/power-stats.lua index 6b6fda1..67971fb 100644 --- a/power-stats.lua +++ b/power-stats.lua @@ -1,6 +1,42 @@ -function GetGlobalGrids() +PowerGridScanned = false +function GetGlobalGridSurfaces() + local globalGrids = {} for _,surface in pairs(game.surfaces) do - local grids = surface.has_global_electric_network + if(surface.has_global_electric_network) then + globalGrids[#globalGrids+1] = surface + end --log("Surface: "..surface.name.." has global electric network: "..tostring(grids)) end end + +function UpdateGrids(event) + local entity = event.entity + local gridID = entity.electric_network_id + if gridID == nil then return end + storage.electricGrids[gridID] = (storage.electricGrids[gridID] or 0) + 1 + log(("Network %d has counter of %d"):format(gridID,storage.electricGrids[gridID])) +end + +function PurgeNetworks() + for ID, Counter in pairs(storage.electricGrids) do + if Counter <= 0 then + table.remove(storage.electricGrids, ID) + log(("Removed electric network with id %d from list"):format(ID)) + end + end +end + + +function GenerateNetworks() + if PowerGridScanned==false then + for _,surface in pairs(game.surfaces) do + for _, powerPole in pairs(surface.find_entities_filtered({filter = "type",type = "electric-pole"})) do + storage.electricGrids[powerPole.electric_network_id] = (storage.electricGrids[powerPole.electric_network_id] or 0) + 1 + end + + end +end + PowerGridScanned = true +end + + diff --git a/production-stats.lua b/production-stats.lua index b763bcb..5867ce5 100644 --- a/production-stats.lua +++ b/production-stats.lua @@ -1,4 +1,5 @@ function SendProductionStats() + if options.enableProduction then local productionParts = {} productionParts[#productionParts+1] = "---production-stats---" for _,surface in pairs(game.surfaces) do @@ -22,10 +23,12 @@ function SendProductionStats() end helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) end +end function SendFluidProductionStats() + if options.enableFluid then local productionParts = {} productionParts[#productionParts+1] = "---fluid-production-stats---" for _,surface in pairs(game.surfaces) do @@ -49,9 +52,11 @@ function SendFluidProductionStats() end helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex) end +end function SendBuildStats() + if options.enableProduction then local buildParts = {} buildParts[#buildParts+1] = "---build-stats---" for _,surface in pairs(game.surfaces) do @@ -74,4 +79,5 @@ function SendBuildStats() end end helpers.send_udp(udpAddress, table.concat(buildParts, "\n"), serverIndex) +end end \ No newline at end of file diff --git a/research-stats.lua b/research-stats.lua index f21c0b9..a987fc3 100644 --- a/research-stats.lua +++ b/research-stats.lua @@ -2,6 +2,21 @@ storage.totalLabCount = 0 local labBaseSpeed = 1 local biolabBaseSpeed = 2 +LabsScanned = false + + +function GenerateLabInfo() + for _, surface in pairs(game.surfaces) do + local labs = surface.find_entities_filtered({type="lab"}) + for index, lab in pairs(labs) do + if lab.valid then + storage.labs[index] = lab + end + end + end +end + + function GetEstimatedResearchTime() -- Base time in seconds for research @@ -29,11 +44,8 @@ end function UpdateLabInfos() local totalLabs = 0 local totalSpeed = 0 - local totalProd = 0 - for _, surface in pairs(game.surfaces) do - local labs = surface.find_entities_filtered{type="lab"} - totalLabs = totalLabs + #labs - for _, lab in pairs(labs) do + totalLabs = #storage.labs + for _, lab in pairs(storage.labs) do if lab.valid then if lab.status == defines.entity_status.working then local labBase @@ -48,16 +60,16 @@ function UpdateLabInfos() end end end - end storage.totalLabCount = totalLabs storage.totalResearchSpeed = totalSpeed - storage.totalReseachProductivity = totalProd end function SendResearchStats() + if options.enableResearch == true then UpdateLabInfos() local researchTimeInfo = GetEstimatedResearchTime() if researchTimeInfo then helpers.send_udp(udpAddress, researchTimeInfo, serverIndex) + end end end \ No newline at end of file diff --git a/settings.lua b/settings.lua index 9e04d28..5b3a094 100644 --- a/settings.lua +++ b/settings.lua @@ -63,5 +63,19 @@ data:extend({ setting_type = "runtime-global", default_value = true, order = "h" + }, + { + type = "bool-setting", + name = "factorio-prometheus-exporter-export_research_stats", + setting_type = "runtime-global", + default_value = true, + order = "i" + }, + { + type = "bool-setting", + name = "factorio-prometheus-exporter-export_logistic_stats", + setting_type = "runtime-global", + default_value = true, + order = "i" } }) \ No newline at end of file