Private
Public Access
1
0
Files
lua-prometheus-exporter/control.lua

255 lines
9.2 KiB
Lua

require("game-stats")
require("production-stats")
require("pollution-stats")
require("research-stats")
require("power-stats")
require("logistic-network-stats")
tickInterval = tonumber(settings.global["factorio-metrics-exporter-tick-interval"].value) or 300
udpAddress = 52555
isInitialized = false
sendIndex = 0
serverIndex = 0
scannedGrids = false
scannedLabs = false
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 = {}
storage.researchedTechnologies = {}
storage.playerDeathCount = {}
storage.totalLabCount = 0
storage.totalResearchSpeed = 0
storage.totalReseachProductivity = 0
storage.labs = {}
storage.playerKillCount = {}
storage.representativePoles = {}
storage.scannedGrids = false
storage.scannedLabs = false
sendIndex = 0
options.enableMod = settings.global["factorio-metrics-exporter-enable"].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
end)
script.on_load(function ()
log("factorio-metrics-exporter: on_load")
log("tickInterval: "..tickInterval)
log("udpAddress: "..udpAddress)
options.enableMod = settings.global["factorio-metrics-exporter-enable"].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
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
storage.playerKillCount = storage.playerKillCount or {}
storage.representativePoles = storage.representativePoles or {}
storage.scannedGrids = storage.scannedGrids or false
storage.scannedLabs = storage.scannedLabs or false
ScanNetworks()
ScanLabs()
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
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-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
end)
script.on_event(defines.events.on_player_died, function(event)
if event.cause and event.cause.type == "character" then
local killer = event.cause.player
if killer then
local killer_index = killer.index
local victim_index = event.player_index
log(("Player ID %d killed player ID %d"):format(killer_index,victim_index))
storage.playerKillCount[killer_index] =
storage.playerKillCount[killer_index] or {}
storage.playerKillCount[killer_index][victim_index] =
(storage.playerKillCount[killer_index][victim_index] or 0) + 1
end
end
storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
end)
function SendGameStats()
if options.enablePlayers then
local returnParts = {}
GetMods()
returnParts[#returnParts+1] = GetMapSeed()
returnParts[#returnParts+1] = GetRocketsLaunched()
returnParts[#returnParts+1] = GetPlayerTime()
returnParts[#returnParts+1] = GetPlayerDeaths()
returnParts[#returnParts+1] = GetPlayerKills()
helpers.send_udp(udpAddress, table.concat(returnParts, "\n"), serverIndex)
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
ScanNetworks()
scannedGrids = true
end
if scannedLabs == false then
ScanLabs()
scannedLabs = true
end
if options.enableMod==true then
local interval = math.max(1, math.floor(tickInterval / 9))
if event.tick % interval ~= 0 then return end
sendIndex = (sendIndex % 9) + 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 sendIndex == 8 then SendPowerStats() end
if sendIndex == 9 then SendGameStats()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
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
--log(event.entity.name)
if event.entity.name == "crash-site-spaceship" then
--log(event.name)
if event.name == defines.events.on_player_mined_entity then
--log("in ban call")
if settings.global["factorio-metrics-exporter-enable_denkmalschutz"].value == true then
game.ban_player(event.player_index,"You violated the rules of DENKMALSCHUTZ!!!")
end
end
end
end
script.on_event(defines.events.on_tick, SendAll)
--Script hooks for power and lab stats
script.on_event(defines.events.on_built_entity,UpdateStorage,{{filter = "type", type = "electric-pole"},{filter ="type", type="lab"}})
script.on_event(defines.events.on_player_mined_entity, RemoveStorage,{{filter = "type", type = "electric-pole"},{filter ="type", type="lab"},{filter = "type", type="container"}})
script.on_event(defines.events.on_robot_built_entity,UpdateStorage,{{filter = "type", type = "electric-pole"},{filter ="type", type="lab"}})
script.on_event(defines.events.on_robot_mined_entity,RemoveStorage,{{filter = "type", type = "electric-pole"},{filter ="type", type="lab"},{filter = "type", type="container"}})
script.on_event(defines.events.on_entity_died,RemoveStorage,{{filter = "type", type = "electric-pole"},{filter ="type", type="lab"},{filter = "type", type="container"}})