Private
Public Access
1
0

Update version to 0.1.20 and enhance train name retrieval in statistics

This commit is contained in:
Jan Grießhaber
2026-01-03 19:18:00 +01:00
parent 0cb1b40f72
commit d7394ac3ea
2 changed files with 21 additions and 11 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "factorio-metrics-exporter",
"version": "0.1.19",
"version": "0.1.20",
"title": "Prometheus Metrics Exporter",
"author": "Jan Grießhaber",
"contact": "jan@griesshaber.systems",

View File

@@ -5,13 +5,25 @@ function ScanTrains()
end
end
---comment Get the first locomotive in the front, if non, in the back, if none empty string
---@param train LuaTrain
function GetTrainName(train)
if train.locomotives.front_movers[1] then
return train.locomotives.front_movers[1].backer_name
else if train.locomotives.back_movers[1] then
return train.locomotives.back_movers[1].backer_name
end
end
return ""
end
function GetTrainPlayerKills()
local trainKills = {}
trainKills[#trainKills+1] = "---train-player-kills---"
---@type LuaTrain
for _, train in pairs(storage.trains ) do
for killedPlayerID,killedPlayerCount in pairs(train.killed_players) do
trainKills[#trainKills+1] = ("%d%s%s%d"):format(train.id,killedPlayerID,game.players[killedPlayerID].name,killedPlayerCount)
trainKills[#trainKills+1] = ("%s:%s:%s:%s:%d"):format(train.id,GetTrainName(train),killedPlayerID,game.players[killedPlayerID].name,killedPlayerCount)
end
end
return table.concat(trainKills,"\n")
@@ -23,7 +35,7 @@ function GetTrainTotalKills()
trainKills[#trainKills+1] = "---train-total-kills---"
---@type LuaTrain
for _, train in pairs(storage.trains) do
trainKills[#trainKills+1] = ("%d:%d"):format(train.id,train.kill_count)
trainKills[#trainKills+1] = ("%s:%s:%d"):format(train.id,GetTrainName(train),train.kill_count)
end
return table.concat(trainKills,"\n")
end
@@ -35,13 +47,13 @@ function GetTrainStates()
local trainsManual = 0
for _, train in pairs(storage.trains) do
if train.state == defines.train_state.wait_station
or train.state ==defines.train_state.destination_full
or train.state == defines.train_state.destination_full
or train.state == defines.train_state.no_schedule
then trainsWaiting = trainsWaiting + 1 end
if train.state == defines.train_state.on_the_path
or train.state ==defines.train_state.arrive_signal
or train.state ==defines.train_state.wait_signal
or train.state == defines.train_state.arrive_signal
or train.state == defines.train_state.wait_signal
then trainsDriving = trainsDriving + 1 end
if train.state == defines.train_state.manual_control
@@ -140,15 +152,13 @@ function onTrainStateChange(event)
if stat.currentInventory and stat.lastInventory then
--Get Total Cargo
for key, value in pairs(inventoryDiff(stat.lastInventory,stat.currentInventory)) do
stat.totalCargoCount = (stat.totalCargoCount or 0) + value.delta
end
end
storage.trainStats[trainID] = stat
end
end
--log("inEvent")
end
@@ -157,7 +167,7 @@ function GetTrainStatistics()
local trainParts = {}
trainParts[#trainParts+1] = "---train-total-statistics---\n"
for trainID, stat in pairs(storage.trainStats) do
trainParts[#trainParts+1] = ("%d:%d"):format(trainID,stat.totalCargoCount)
trainParts[#trainParts+1] = ("%d:%s:%d"):format(trainID,GetTrainName(storage.trains[trainID]),stat.totalCargoCount)
end
return table.concat(trainParts,"\n")
end