Fixed options
This commit is contained in:
128
control.lua
128
control.lua
@@ -10,6 +10,19 @@ udpAddress = tonumber(settings.startup["factorio-prometheus-exporter-udp-address
|
|||||||
isInitialized = false
|
isInitialized = false
|
||||||
sendIndex = 0
|
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 ()
|
script.on_init(function ()
|
||||||
storage.electricGrids = {}
|
storage.electricGrids = {}
|
||||||
@@ -18,38 +31,101 @@ script.on_init(function ()
|
|||||||
storage.totalLabCount = 0
|
storage.totalLabCount = 0
|
||||||
storage.totalResearchSpeed = 0
|
storage.totalResearchSpeed = 0
|
||||||
storage.totalReseachProductivity = 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)
|
end)
|
||||||
|
|
||||||
script.on_load(function ()
|
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)
|
||||||
|
|
||||||
|
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)
|
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)
|
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
|
||||||
log("Mod setting changed: "..event.setting)
|
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
|
||||||
|
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)
|
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)
|
function SendGameStats(event)
|
||||||
local returnParts = {}
|
local returnParts = {}
|
||||||
@@ -62,6 +138,7 @@ function SendGameStats(event)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function SendAll(event)
|
function SendAll(event)
|
||||||
|
|
||||||
if(isInitialized == false) then
|
if(isInitialized == false) then
|
||||||
if game.is_multiplayer() then
|
if game.is_multiplayer() then
|
||||||
serverIndex = 0
|
serverIndex = 0
|
||||||
@@ -71,21 +148,42 @@ function SendAll(event)
|
|||||||
end
|
end
|
||||||
isInitialized = true
|
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 == 1 then SendProductionStats() end
|
||||||
if sendIndex == 2 then SendPollutionStats() end
|
if sendIndex == 2 then SendPollutionStats() end
|
||||||
if sendIndex == 3 then SendKillStats() end
|
if sendIndex == 3 then SendKillStats() end
|
||||||
if sendIndex == 4 then SendFluidProductionStats() end
|
if sendIndex == 4 then SendFluidProductionStats() end
|
||||||
if sendIndex == 5 then SendBuildStats() end
|
if sendIndex == 5 then SendBuildStats() end
|
||||||
if sendIndex == 6 then SendResearchStats() 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
|
end
|
||||||
|
|
||||||
script.on_event(defines.events.on_tick, SendAll)
|
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"}})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -15,4 +15,10 @@ function GetAllLogisticGrids()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
return table.concat(resultParts, "\n")
|
return table.concat(resultParts, "\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function SendLogisticStats()
|
||||||
|
if options.enableRobots then
|
||||||
|
helpers.send_udp(udpAddress,GetAllLogisticGrids(),serverIndex)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|||||||
@@ -1,7 +1,5 @@
|
|||||||
function SendPollutionStats()
|
function SendPollutionStats()
|
||||||
if not (settings.global["factorio-prometheus-exporter-export_pollution_stats"].value) then
|
if options.enablePollution then
|
||||||
return
|
|
||||||
end
|
|
||||||
local pollutionParts = {}
|
local pollutionParts = {}
|
||||||
pollutionParts[#pollutionParts+1] = "---pollution-stats---\n"
|
pollutionParts[#pollutionParts+1] = "---pollution-stats---\n"
|
||||||
for _,surface in pairs(game.surfaces) do
|
for _,surface in pairs(game.surfaces) do
|
||||||
@@ -25,11 +23,10 @@ function SendPollutionStats()
|
|||||||
end
|
end
|
||||||
helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex)
|
helpers.send_udp(udpAddress, table.concat(pollutionParts,"\n"),serverIndex)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function SendKillStats()
|
function SendKillStats()
|
||||||
if not (settings.global["factorio-prometheus-exporter-export_kill_stats"].value) then
|
if options.enableKills then
|
||||||
return
|
|
||||||
end
|
|
||||||
local killParts = {}
|
local killParts = {}
|
||||||
killParts[#killParts+1] = "---kill-stats---\n"
|
killParts[#killParts+1] = "---kill-stats---\n"
|
||||||
for _,surface in pairs(game.surfaces) do
|
for _,surface in pairs(game.surfaces) do
|
||||||
@@ -52,4 +49,5 @@ function SendKillStats()
|
|||||||
|
|
||||||
end
|
end
|
||||||
helpers.send_udp(udpAddress, table.concat(killParts,"\n"),serverIndex)
|
helpers.send_udp(udpAddress, table.concat(killParts,"\n"),serverIndex)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@@ -1,6 +1,42 @@
|
|||||||
function GetGlobalGrids()
|
PowerGridScanned = false
|
||||||
|
function GetGlobalGridSurfaces()
|
||||||
|
local globalGrids = {}
|
||||||
for _,surface in pairs(game.surfaces) do
|
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))
|
--log("Surface: "..surface.name.." has global electric network: "..tostring(grids))
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
function SendProductionStats()
|
function SendProductionStats()
|
||||||
|
if options.enableProduction then
|
||||||
local productionParts = {}
|
local productionParts = {}
|
||||||
productionParts[#productionParts+1] = "---production-stats---"
|
productionParts[#productionParts+1] = "---production-stats---"
|
||||||
for _,surface in pairs(game.surfaces) do
|
for _,surface in pairs(game.surfaces) do
|
||||||
@@ -22,10 +23,12 @@ function SendProductionStats()
|
|||||||
end
|
end
|
||||||
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
|
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function SendFluidProductionStats()
|
function SendFluidProductionStats()
|
||||||
|
if options.enableFluid then
|
||||||
local productionParts = {}
|
local productionParts = {}
|
||||||
productionParts[#productionParts+1] = "---fluid-production-stats---"
|
productionParts[#productionParts+1] = "---fluid-production-stats---"
|
||||||
for _,surface in pairs(game.surfaces) do
|
for _,surface in pairs(game.surfaces) do
|
||||||
@@ -49,9 +52,11 @@ function SendFluidProductionStats()
|
|||||||
end
|
end
|
||||||
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
|
helpers.send_udp(udpAddress, table.concat(productionParts, "\n"), serverIndex)
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
function SendBuildStats()
|
function SendBuildStats()
|
||||||
|
if options.enableProduction then
|
||||||
local buildParts = {}
|
local buildParts = {}
|
||||||
buildParts[#buildParts+1] = "---build-stats---"
|
buildParts[#buildParts+1] = "---build-stats---"
|
||||||
for _,surface in pairs(game.surfaces) do
|
for _,surface in pairs(game.surfaces) do
|
||||||
@@ -74,4 +79,5 @@ function SendBuildStats()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
helpers.send_udp(udpAddress, table.concat(buildParts, "\n"), serverIndex)
|
helpers.send_udp(udpAddress, table.concat(buildParts, "\n"), serverIndex)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@@ -2,6 +2,21 @@ storage.totalLabCount = 0
|
|||||||
local labBaseSpeed = 1
|
local labBaseSpeed = 1
|
||||||
local biolabBaseSpeed = 2
|
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()
|
function GetEstimatedResearchTime()
|
||||||
-- Base time in seconds for research
|
-- Base time in seconds for research
|
||||||
@@ -29,11 +44,8 @@ end
|
|||||||
function UpdateLabInfos()
|
function UpdateLabInfos()
|
||||||
local totalLabs = 0
|
local totalLabs = 0
|
||||||
local totalSpeed = 0
|
local totalSpeed = 0
|
||||||
local totalProd = 0
|
totalLabs = #storage.labs
|
||||||
for _, surface in pairs(game.surfaces) do
|
for _, lab in pairs(storage.labs) do
|
||||||
local labs = surface.find_entities_filtered{type="lab"}
|
|
||||||
totalLabs = totalLabs + #labs
|
|
||||||
for _, lab in pairs(labs) do
|
|
||||||
if lab.valid then
|
if lab.valid then
|
||||||
if lab.status == defines.entity_status.working then
|
if lab.status == defines.entity_status.working then
|
||||||
local labBase
|
local labBase
|
||||||
@@ -48,16 +60,16 @@ function UpdateLabInfos()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
storage.totalLabCount = totalLabs
|
storage.totalLabCount = totalLabs
|
||||||
storage.totalResearchSpeed = totalSpeed
|
storage.totalResearchSpeed = totalSpeed
|
||||||
storage.totalReseachProductivity = totalProd
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function SendResearchStats()
|
function SendResearchStats()
|
||||||
|
if options.enableResearch == true then
|
||||||
UpdateLabInfos()
|
UpdateLabInfos()
|
||||||
local researchTimeInfo = GetEstimatedResearchTime()
|
local researchTimeInfo = GetEstimatedResearchTime()
|
||||||
if researchTimeInfo then
|
if researchTimeInfo then
|
||||||
helpers.send_udp(udpAddress, researchTimeInfo, serverIndex)
|
helpers.send_udp(udpAddress, researchTimeInfo, serverIndex)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
14
settings.lua
14
settings.lua
@@ -63,5 +63,19 @@ data:extend({
|
|||||||
setting_type = "runtime-global",
|
setting_type = "runtime-global",
|
||||||
default_value = true,
|
default_value = true,
|
||||||
order = "h"
|
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"
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
Reference in New Issue
Block a user