Private
Public Access
1
0
Files
lua-prometheus-exporter/control.lua
2026-01-10 18:24:52 +01:00

443 lines
17 KiB
Lua

require("game-stats")
require("production-stats")
require("pollution-stats")
require("research-stats")
require("power-stats")
require("logistic-network-stats")
require("train-stats")
require("metrics-combinator")
require("player-statistics")
require("send-utils")
--tickInterval = tonumber(settings.global["factorio-metrics-exporter-tick-interval"].value) or 300
--options.udpPort = 52555
isInitialized = false
--options.sendIndex = 0
--serverIndex = 0
scannedGrids = false
scannedLabs = false
scannedGenerators = false
autotrainGroupName = ""
autotrainDepotName = ""
options = {
---@type integer
udpPort = 52555,
---@type integer
senderIndex = 0,
---@type integer
tickInterval = 300,
enableMod = false,
enablePlayers = false,
enableProduction = false,
enableFluid = false,
enableKills = false,
enablePollution = false,
enableRobots = false,
enableResearch = false,
enablePower = false,
enableTrains = false,
enableTrainTrips = false
}
script.on_init(function()
storage.playerDeathCount = {}
storage.totalLabCount = 0
storage.labs = {}
storage.playerKillCount = {}
storage.representativePoles = {}
storage.playerDeathCause = {}
storage.constructedEntites = {}
storage.deconstructedEntities = {}
storage.trainStats = {}
storage.networkCache = {}
---@type LuaEntity[]
storage.powerGenerators = {}
---@type LuaTrain[]
storage.trains = {}
storage.powerStats = {}
storage.cargoStats = {}
storage.metrics = {}
storage.nuclearReactorDeaths = 0
storage.playersOnline = 0
storage.scannedGrids = false
storage.scannedLabs = false
options.senderIndex = 0
options.enableMod = settings.global["factorio-metrics-exporter-enable"].value
options.senderIndex = settings.global["factorio-metrics-exporter-sending-player-index"].value
options.udpPort = settings.global["factorio-metrics-exporter-udp-port"].value
options.enableProduction = settings.global["factorio-metrics-exporter-export_production_stats"].value
options.enablePollution = settings.global["factorio-metrics-exporter-export_pollution_stats"].value
options.enableFluid = settings.global["factorio-metrics-exporter-export_fluid_stats"].value
options.enablePlayers = settings.global["factorio-metrics-exporter-export_player_stats"].value
options.enableKills = settings.global["factorio-metrics-exporter-export_kill_stats"].value
options.enablePower = settings.global["factorio-metrics-exporter-export_power_stats"].value
options.enableResearch = settings.global["factorio-metrics-exporter-export_research_stats"].value
options.enableRobots = settings.global["factorio-metrics-exporter-export_logistic_stats"].value
options.enableTrains = settings.global["factorio-metrics-exporter-export_train_stats"].value
options.enableTrainTrips = settings.global["factorio-metrics-exporter-export_train_trips"].value
autotrainGroupName = settings.global["factorio-metrics-exporter-autotrain_group_name"].value
autotrainDepotName = settings.global["factorio-metrics-exporter-autotrain_depot_name"].value
end)
script.on_load(function()
log("factorio-metrics-exporter: on_load")
log("tickInterval: " .. options.tickInterval)
log("udpAddress: " .. options.udpPort)
options.enableMod = settings.global["factorio-metrics-exporter-enable"].value
options.senderIndex = settings.global["factorio-metrics-exporter-sending-player-index"].value
options.udpPort = settings.global["factorio-metrics-exporter-udp-port"].value
options.enableProduction = settings.global["factorio-metrics-exporter-export_production_stats"].value
options.enablePollution = settings.global["factorio-metrics-exporter-export_pollution_stats"].value
options.enableFluid = settings.global["factorio-metrics-exporter-export_fluid_stats"].value
options.enablePlayers = settings.global["factorio-metrics-exporter-export_player_stats"].value
options.enableKills = settings.global["factorio-metrics-exporter-export_kill_stats"].value
options.enablePower = settings.global["factorio-metrics-exporter-export_power_stats"].value
options.enableResearch = settings.global["factorio-metrics-exporter-export_research_stats"].value
options.enableRobots = settings.global["factorio-metrics-exporter-export_logistic_stats"].value
options.enableTrains = settings.global["factorio-metrics-exporter-export_train_stats"].value
options.enableTrainTrips = settings.global["factorio-metrics-exporter-export_train_trips"].value
autotrainGroupName = settings.global["factorio-metrics-exporter-autotrain_group_name"].value
autotrainDepotName = settings.global["factorio-metrics-exporter-autotrain_depot_name"].value
end)
script.on_configuration_changed(function()
storage.labs = storage.labs or {}
storage.playerDeathCount = storage.playerDeathCount or {}
storage.totalLabCount = storage.totalLabCount or 0
storage.playerKillCount = storage.playerKillCount or {}
storage.representativePoles = storage.representativePoles or {}
storage.scannedGrids = storage.scannedGrids or false
storage.scannedLabs = storage.scannedLabs or false
storage.playerDeathCause = storage.playerDeathCause or {}
storage.constructedEntites = storage.constructedEntites or {}
storage.deconstructedEntities = storage.deconstructedEntities or {}
storage.networkCache = storage.networkCache or {}
storage.trains = storage.trains or {}
---@type table<uint, trainStat>
storage.trainStats = storage.trainStats or {}
---@type LuaEntity[]
storage.powerGenerators = storage.powerGenerators or {}
storage.powerStats = storage.powerStats or {}
storage.metrics = storage.metrics or {}
storage.cargoStats = storage.cargoStats or {}
storage.nuclearReactorDeaths = storage.nuclearReactorDeaths or 0
storage.playersOnline = storage.playersOnline or 0
ScanNetworks()
ScanLabs()
ScanTrains()
ScanGenerators()
end
)
script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
log("Mod setting changed: " .. event.setting)
if event.setting == "factorio-metrics-exporter-tick-interval" then
options.tickInterval = settings.global["factorio-metrics-exporter-tick-interval"].value
end
if event.setting == "factorio-metrics-exporter-enable" then
options.enableMod = settings.global["factorio-metrics-exporter-enable"].value
end
if event.setting == "factorio-metrics-exporter-sending-player-index" then
options.senderIndex = settings.global["factorio-metrics-exporter-sending-player-index"].value
end
if event.setting == "factorio-metrics-exporter-udp-port" then
options.udpPort = settings.global["factorio-metrics-exporter-sending-player-index"].value
end
if event.setting == "factorio-metrics-exporter-export_production_stats" then
options.enableProduction = settings.global["factorio-metrics-exporter-export_production_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_fluid_stats" then
options.enableFluid = settings.global["factorio-metrics-exporter-export_fluid_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_pollution_stats" then
options.enablePollution = settings.global["factorio-metrics-exporter-export_pollution_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_power_stats" then
options.enablePower = settings.global["factorio-metrics-exporter-export_power_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_logistic_stats" then
options.enableRobots = settings.global["factorio-metrics-exporter-export_logistic_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_player_stats" then
options.enablePlayers = settings.global["factorio-metrics-exporter-export_player_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_kill_stats" then
options.enableKills = settings.global["factorio-metrics-exporter-export_kill_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_research_stats" then
options.enableResearch = settings.global["factorio-metrics-exporter-export_research_stats"].value
end
if event.setting == "factorio-metrics-exporter-export_train_stats" then
options.enableTrains = settings.global["factorio-metrics-exporter-export_train_stats"].value
end
if event.setting == "factorio-metrics-exporter-autotrain_depot_name" then
autotrainDepotName = settings.global["factorio-metrics-exporter-autotrain_depot_name"].value
end
if event.setting == "factorio-metrics-exporter-autotrain_group_name" then
autotrainGroupName = settings.global["factorio-metrics-exporter-autotrain_group_name"].value
end
if event.setting == "factorio-metrics-exporter-export_train_trips" then
options.enableTrainTrips = settings.global["factorio-metrics-exporter-export_train_trips"].value
end
end)
script.on_event(defines.events.on_player_died, function(event)
onPlayerKilledPlayer(event)
onPlayerDiedDeathCause(event)
onPlayerDeath(event)
end)
function SendGameStats()
if options.enablePlayers then
local returnParts = {}
GetMods()
returnParts[#returnParts + 1] = GetMapSeed()
returnParts[#returnParts + 1] = GetEvolution()
returnParts[#returnParts + 1] = GetRocketsLaunched()
returnParts[#returnParts + 1] = GetReactorExplosions()
returnParts[#returnParts + 1] = GetTotalPlayTime()
returnParts[#returnParts + 1] = GetPlayerTime()
returnParts[#returnParts + 1] = GetPlayerDeaths()
returnParts[#returnParts + 1] = GetPlayerDeathCauses()
returnParts[#returnParts + 1] = GetPlayerKills()
SendChunked(table.concat(returnParts, "\n"))
end
end
function SendAll(event)
if (isInitialized == false) then
if game.is_multiplayer() then
serverIndex = 0
log("Loaded game as mulitplayer")
else
log("Loaded game as singleplayer")
serverIndex = 1
end
end
isInitialized = true
if scannedGrids == false then
storage.networkCache = nil
ScanNetworks()
scannedGrids = true
end
if scannedGenerators == false then
storage.powerGenerators = {}
ScanGenerators()
scannedGenerators = true
end
if scannedLabs == false then
ScanLabs()
scannedLabs = true
end
if options.enableMod == true then
local interval = math.max(1, math.floor(options.tickInterval / 10))
if event.tick % interval ~= 0 then return end
options.senderIndex = (options.senderIndex % 10) + 1
if options.senderIndex == 1 then SendProductionStats() end
if options.senderIndex == 2 then SendPollutionStats() end
if options.senderIndex == 3 then
SendKillStats()
SendPlayerEntityStats()
end
if options.senderIndex == 4 then SendFluidProductionStats() end
if options.senderIndex == 5 then SendBuildStats() end
if options.senderIndex == 6 then
SendResearchStats()
SendCombinatorMetrics()
end
if options.senderIndex == 7 then SendLogisticStats() end
if options.senderIndex == 8 then SendPowerStats() end
if options.senderIndex == 9 then SendGameStats() end
if options.senderIndex == 10 then SendTrainStats() end
end
end
function UpdateStorage(event)
if not event then return end
if event.entity.type == "lab" then
UpdateLabs(event)
end
if event.entity.type == "electric-pole" then
AddPowerPole(event)
end
if event.entity.type == "generator"
or event.entity.type == "fusion-generator"
or event.entity.type == "solar-panel" then
AddGenerator(event)
end
end
function RemoveStorage(event)
if not event then return end
if event.entity.type == "lab" then
RemoveLab(event)
end
if event.entity.type == "electric-pole" then
RemovePowerPole(event)
end
if event.entity.type == "generator"
or event.entity.type == "fusion-generator"
or event.entity.type == "solar-panel" then
RemoveGenerator(event)
end
if event.entity.name == "crash-site-spaceship" then
if event.name == defines.events.on_player_mined_entity then
if settings.global["factorio-metrics-exporter-enable_denkmalschutz"].value == true then
game.ban_player(event.player_index, "You violated the rules of DENKMALSCHUTZ!!!")
game.kick_player(event.player_index, "")
end
end
if event.name == defines.events.on_robot_mined_entity then
if event.entity.last_user then
game.ban_player(event.entity.last_user.index, "You violated the rules of DENKMALSCHUTZ!!!")
end
end
end
end
function CreateEntity(event)
if not event then return end
--Event is PlayerPlaced
if event.name == defines.events.on_built_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible_tile_proxy" then
storage.constructedEntites[event.player_index] = storage.constructedEntites[event.player_index] or {}
storage.constructedEntites[event.player_index][event.entity.name] = (storage.constructedEntites[event.player_index][event.entity.name] or 0) +
1
end
end
--Event is RobotPlaced
if event.name == defines.events.on_robot_built_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible_tile_proxy" then
if event.entity.last_user then
local lastUser = event.entity.last_user.index
storage.constructedEntites[lastUser] = storage.constructedEntites[lastUser] or {}
storage.constructedEntites[lastUser][event.entity.name] = (storage.constructedEntites[lastUser][event.entity.name] or 0) +
1
end
end
end
--Event is spaceplatform build
if event.name == defines.events.on_space_platform_built_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible_tile_proxy" then
if event.entity.last_user then
local lastUser = event.entity.last_user.index
storage.constructedEntites[lastUser] = storage.constructedEntites[lastUser] or {}
storage.constructedEntites[lastUser][event.entity.name] = (storage.constructedEntites[lastUser][event.entity.name] or 0) +
1
end
end
end
UpdateStorage(event)
CreateMetricsEntry(event)
end
function RemoveEntity(event)
if event.name == defines.events.on_player_mined_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible-tile-proxy" then
storage.deconstructedEntities[event.player_index] = storage.deconstructedEntities[event.player_index] or {}
storage.deconstructedEntities[event.player_index][event.entity.name] = (storage.deconstructedEntities[event.player_index][event.entity.name] or 0) +
1
end
end
if event.name == defines.events.on_robot_mined_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible-tile-proxy" then
if event.entity.last_user then
local lastUser = event.entity.last_user.index
storage.deconstructedEntities[lastUser] = storage.deconstructedEntities[lastUser] or {}
storage.deconstructedEntities[lastUser][event.entity.name] = (storage.deconstructedEntities[lastUser][event.entity.name] or 0) +
1
end
end
end
if event.name == defines.events.on_space_platform_mined_entity then
if event.entity.name ~= "entity-ghost"
and event.entity.name ~= "tile-ghost"
and event.entity.name ~= "deconstructible-tile-proxy" then
if event.entity.last_user then
local lastUser = event.entity.last_user.index
storage.deconstructedEntities[lastUser] = storage.deconstructedEntities[lastUser] or {}
storage.deconstructedEntities[lastUser][event.entity.name] = (storage.deconstructedEntities[lastUser][event.entity.name] or 0) +
1
end
end
end
if event.name == defines.events.on_entity_died then
CheckReactor(event)
end
RemoveStorage(event)
onMetricsCombinatorDied(event)
onMetricsCombinatorMined(event)
end
script.on_event(defines.events.on_tick, SendAll)
script.on_event(defines.events.on_built_entity, CreateEntity)
script.on_event(defines.events.on_robot_built_entity, CreateEntity)
script.on_event(defines.events.on_space_platform_built_entity, CreateEntity)
script.on_event(defines.events.on_player_mined_entity, RemoveEntity)
script.on_event(defines.events.on_robot_mined_entity, RemoveEntity)
script.on_event(defines.events.on_space_platform_mined_entity, RemoveEntity)
script.on_event(defines.events.on_entity_died, RemoveEntity)
script.on_event(defines.events.on_train_changed_state, onTrainStateChange)
script.on_event(defines.events.on_gui_checked_state_changed, onGuiCheckedState)
script.on_event(defines.events.on_gui_opened, onGuiOpened)
script.on_event(defines.events.on_gui_click, onGuiClick)
script.on_event(defines.events.on_gui_closed, onClosedCombinatorGui)
script.on_event(defines.events.on_gui_text_changed, onGuiTextChanged)