Private
Public Access
1
0

Rewritten sending helper and logic

This commit is contained in:
Jan Grießhaber
2026-01-10 18:24:52 +01:00
parent 8fc41c2e5a
commit 2e3ea7dd4d
14 changed files with 317 additions and 138 deletions

View File

@@ -6,12 +6,14 @@ 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
udpAddress = 52555
--tickInterval = tonumber(settings.global["factorio-metrics-exporter-tick-interval"].value) or 300
--options.udpPort = 52555
isInitialized = false
sendIndex = 0
serverIndex = 0
--options.sendIndex = 0
--serverIndex = 0
scannedGrids = false
scannedLabs = false
scannedGenerators = false
@@ -20,6 +22,12 @@ autotrainDepotName = ""
options = {
---@type integer
udpPort = 52555,
---@type integer
senderIndex = 0,
---@type integer
tickInterval = 300,
enableMod = false,
enablePlayers = false,
enableProduction = false,
@@ -35,12 +43,8 @@ options = {
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 = {}
@@ -58,16 +62,18 @@ script.on_init(function()
storage.cargoStats = {}
storage.metrics = {}
storage.cliffsDestroyed = 0
storage.nuclearReactorDeaths = 0
storage.playersOnline = 0
storage.scannedGrids = false
storage.scannedLabs = false
sendIndex = 0
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
@@ -84,10 +90,12 @@ end)
script.on_load(function()
log("factorio-metrics-exporter: on_load")
log("tickInterval: " .. tickInterval)
log("udpAddress: " .. udpAddress)
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
@@ -103,13 +111,9 @@ script.on_load(function()
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
@@ -127,7 +131,7 @@ script.on_configuration_changed(function()
storage.metrics = storage.metrics or {}
storage.cargoStats = storage.cargoStats or {}
storage.nuclearReactorDeaths = storage.nuclearReactorDeaths or 0
storage.cliffsDestroyed = storage.cliffsDestroyed or 0
storage.playersOnline = storage.playersOnline or 0
ScanNetworks()
ScanLabs()
ScanTrains()
@@ -139,12 +143,24 @@ 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
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
@@ -176,15 +192,19 @@ script.on_event(defines.events.on_runtime_mod_setting_changed, function(event)
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
@@ -192,41 +212,11 @@ end)
script.on_event(defines.events.on_player_died, function(event)
--Log player cause by player
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
local killerName = killer.name
local victimName = game.players[victim_index].name
log(("Player ID %d:%s killed player ID %d:%s"):format(killer_index, killerName, victim_index, victimName))
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
--Log cause of player death
if event.cause and event.cause.name then
storage.playerDeathCause[event.player_index] =
storage.playerDeathCause[event.player_index] or {}
storage.playerDeathCause[event.player_index][event.cause.name] =
(storage.playerDeathCause[event.player_index][event.cause.name] or 0) + 1
log(("Player %s died from type %s"):format(game.players[event.player_index].name, event.cause.name))
end
--Log player death count
storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
onPlayerKilledPlayer(event)
onPlayerDiedDeathCause(event)
onPlayerDeath(event)
end)
function SendGameStats()
if options.enablePlayers then
local returnParts = {}
@@ -240,7 +230,7 @@ function SendGameStats()
returnParts[#returnParts + 1] = GetPlayerDeaths()
returnParts[#returnParts + 1] = GetPlayerDeathCauses()
returnParts[#returnParts + 1] = GetPlayerKills()
helpers.send_udp(udpAddress, table.concat(returnParts, "\n"), serverIndex)
SendChunked(table.concat(returnParts, "\n"))
end
end
@@ -274,25 +264,25 @@ function SendAll(event)
end
if options.enableMod == true then
local interval = math.max(1, math.floor(tickInterval / 10))
local interval = math.max(1, math.floor(options.tickInterval / 10))
if event.tick % interval ~= 0 then return end
sendIndex = (sendIndex % 10) + 1
if sendIndex == 1 then SendProductionStats() end
if sendIndex == 2 then SendPollutionStats() end
if sendIndex == 3 then
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 sendIndex == 4 then SendFluidProductionStats() end
if sendIndex == 5 then SendBuildStats() end
if sendIndex == 6 then
if options.senderIndex == 4 then SendFluidProductionStats() end
if options.senderIndex == 5 then SendBuildStats() end
if options.senderIndex == 6 then
SendResearchStats()
SendCombinatorMetrics()
end
if sendIndex == 7 then SendLogisticStats() end
if sendIndex == 8 then SendPowerStats() end
if sendIndex == 9 then SendGameStats() end
if sendIndex == 10 then SendTrainStats() 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
@@ -311,6 +301,7 @@ function UpdateStorage(event)
end
end
function RemoveStorage(event)
if not event then return end
if event.entity.type == "lab" then
@@ -325,11 +316,8 @@ function RemoveStorage(event)
RemoveGenerator(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!!!")
game.kick_player(event.player_index, "")
@@ -343,10 +331,11 @@ function RemoveStorage(event)
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"
@@ -358,7 +347,6 @@ function CreateEntity(event)
end
end
--Event is RobotPlaced
if event.name == defines.events.on_robot_built_entity then
if event.entity.name ~= "entity-ghost"
@@ -373,7 +361,6 @@ function CreateEntity(event)
end
end
--Event is spaceplatform build
if event.name == defines.events.on_space_platform_built_entity then
if event.entity.name ~= "entity-ghost"
@@ -395,7 +382,7 @@ 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
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
@@ -405,7 +392,7 @@ function RemoveEntity(event)
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
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 {}
@@ -418,7 +405,7 @@ function RemoveEntity(event)
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
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 {}
@@ -429,8 +416,9 @@ function RemoveEntity(event)
end
if event.name == defines.events.on_entity_died then
CheckReactor(event)
end
CheckReactor(event)
RemoveStorage(event)
onMetricsCombinatorDied(event)
onMetricsCombinatorMined(event)
@@ -438,13 +426,6 @@ 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"}})
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)
@@ -459,6 +440,3 @@ 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)
script.on_event(defines.events.on_cargo_pod_delivered_cargo, onCargoDelivered)
script.on_event(defines.events.on_rocket_launched, onCargoDelivered)