Private
Public Access
1
0

Enhance player death tracking, add train statistics, and update version to 0.1.11

This commit is contained in:
Jan Grießhaber
2026-01-03 16:01:45 +01:00
parent 1e736c89ab
commit 0739dac907
6 changed files with 303 additions and 29 deletions

View File

@@ -4,6 +4,7 @@ require("pollution-stats")
require("research-stats")
require("power-stats")
require("logistic-network-stats")
require("train-stats")
tickInterval = tonumber(settings.global["factorio-metrics-exporter-tick-interval"].value) or 300
udpAddress = 52555
@@ -37,6 +38,13 @@ script.on_init(function ()
storage.labs = {}
storage.playerKillCount = {}
storage.representativePoles = {}
storage.playerDeathCause = {}
storage.constructedEntites = {}
storage.deconstructedEntities = {}
storage.trainStats = {}
---@type LuaTrain[]
storage.trains = {}
storage.scannedGrids = false
storage.scannedLabs = false
@@ -83,8 +91,15 @@ script.on_configuration_changed(function()
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.trains = storage.trains or {}
---@type table<uint, trainStat>
storage.trainStats = storage.trainStats or {}
ScanNetworks()
ScanLabs()
ScanTrains()
end
)
@@ -134,12 +149,15 @@ 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
log(("Player ID %d killed player ID %d"):format(killer_index,victim_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 {}
@@ -148,12 +166,22 @@ script.on_event(defines.events.on_player_died, function(event)
(storage.playerKillCount[killer_index][victim_index] or 0) + 1
end
end
--Log cause of player death
if event.cause and event.cause.type then
storage.playerDeathCause[event.player_index] =
storage.playerDeathCause[event.player_index] or {}
storage.playerDeathCause[event.player_index][event.cause.type] =
(storage.playerDeathCause[event.player_index][event.cause.type] or 0) + 1
log(("Player %s died from type %s"):format(game.players[event.player_index].name,event.cause.type))
end
--Log player death count
storage.playerDeathCount[event.player_index] = (storage.playerDeathCount[event.player_index] or 0) + 1
end)
function SendGameStats()
if options.enablePlayers then
local returnParts = {}
@@ -163,6 +191,7 @@ function SendGameStats()
returnParts[#returnParts+1] = GetPlayerTime()
returnParts[#returnParts+1] = GetPlayerDeaths()
returnParts[#returnParts+1] = GetPlayerKills()
returnParts[#returnParts+1] = GetPlayerEntityStats()
helpers.send_udp(udpAddress, table.concat(returnParts, "\n"), serverIndex)
end
end
@@ -190,24 +219,22 @@ function SendAll(event)
scannedLabs = true
end
if options.enableMod==true then
local interval = math.max(1, math.floor(tickInterval / 9))
local interval = math.max(1, math.floor(tickInterval / 10))
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
sendIndex = (sendIndex % 10) + 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
if sendIndex == 10 then SendTrainStats() end
end
end
function UpdateStorage(event)
@@ -240,15 +267,82 @@ 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" 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" 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
--Event is spaceplatform build
if event.name == defines.events.on_space_platform_built_entity then
if event.entity.name ~= "entity-ghost" 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
UpdateStorage(event)
end
function RemoveEntity(event)
if event.name == defines.events.on_player_mined_entity 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
if event.name == defines.events.on_robot_mined_entity then
if event.entity.name ~= "entity-ghost" 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
if event.name == defines.events.on_space_platform_mined_entity then
if event.entity.name ~= "entity-ghost" 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
if event.name == defines.events.on_entity_died then
end
RemoveStorage(event)
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,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)
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)