From d7394ac3eac4ea4f49dc2a3ab55fb13c1d5faf47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Grie=C3=9Fhaber?= Date: Sat, 3 Jan 2026 19:18:00 +0100 Subject: [PATCH] Update version to 0.1.20 and enhance train name retrieval in statistics --- info.json | 2 +- train-stats.lua | 30 ++++++++++++++++++++---------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/info.json b/info.json index 82cbbc8..61661b1 100644 --- a/info.json +++ b/info.json @@ -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", diff --git a/train-stats.lua b/train-stats.lua index 8b3b192..f5a1d08 100644 --- a/train-stats.lua +++ b/train-stats.lua @@ -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 + 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