Private
Public Access
1
0

Update version to 0.2.1, add player color retrieval, and enhance train trip statistics

Performance improvments for power stats
This commit is contained in:
Jan Grießhaber
2026-01-04 01:37:25 +01:00
parent 9b608fc412
commit f40e219a2e
8 changed files with 108 additions and 15 deletions

View File

@@ -79,6 +79,8 @@ end
---@class trainStat
trainStat = {
trainID = 0,
trainName = "",
lastInventory={},
currentInventory={},
lastState = 0,
@@ -87,8 +89,16 @@ trainStat = {
totalCargoCount = 0,
totalCargo = {},
lastArrivalTime = 0,
currentArrivalTime = 0
currentArrivalTime = 0,
---@type trip
trips = {}
}
---@class trip
trip = {
startStation= {},
endStation = {},
timeTaken = 0
}
---@param inv table[]
---@return table<string, table>
@@ -150,9 +160,13 @@ function onTrainStateChange(event)
local trainID = train.id
---@type trainStat
local stat = storage.trainStats[trainID] or {}
stat.trips = stat.trips or {}
stat.trainID = trainID
stat.trainName = GetTrainName(train)
if event.train.state == defines.train_state.wait_station then
if train.station.unit_number == stat.lastStationUnitNumber then return end
stat.lastStationUnitNumber = stat.currentStationUnitNumber
stat.lastInventory = stat.currentInventory
stat.lastArrivalTime = stat.currentArrivalTime
@@ -160,12 +174,20 @@ function onTrainStateChange(event)
stat.currentStationUnitNumber = train.station.unit_number
stat.currentInventory = train.get_contents()
stat.currentArrivalTime = game.tick
if stat.lastStationUnitNumber
and stat.currentStationUnitNumber then
local tripIdentifier = tostring(stat.lastStationUnitNumber) .. tostring(stat.currentStationUnitNumber)
stat.trips[tripIdentifier] = {
startStation = game.get_entity_by_unit_number(stat.lastStationUnitNumber),
endStation = game.get_entity_by_unit_number(stat.currentStationUnitNumber),
timeTaken = stat.currentArrivalTime-stat.lastArrivalTime}
end
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
stat.totalCargoCount = (stat.totalCargoCount or 0) + math.abs(value.delta)
end
end
@@ -174,25 +196,64 @@ function onTrainStateChange(event)
--log("inEvent")
end
function GetTrainTripStats()
local tripParts = {}
tripParts[#tripParts+1] = "---train-trips---\n"
for trainID,stats in pairs(storage.trainStats) do
for _,trip in pairs(stats.trips or {}) do
tripParts[#tripParts+1] =
("%d:%s:%s:%s:%d"):format(
trainID,
stats.trainName,
trip.startStation.backer_name,
trip.endStation.backer_name,
trip.timeTaken)
end
end
return table.concat(tripParts,"\n")
end
function GetTrainStatistics()
local trainParts = {}
trainParts[#trainParts+1] = "---train-total-statistics---\n"
for trainID, stat in pairs(storage.trainStats) do
trainParts[#trainParts+1] = ("%d:%s:%d"):format(trainID,GetTrainName(storage.trains[trainID]),stat.totalCargoCount or 0)
trainParts[#trainParts+1] = ("%d:%s:%d"):format(trainID,stat.trainName,stat.totalCargoCount or 0)
end
return table.concat(trainParts,"\n")
end
---Purges stats for trains that no longer exist
---@param trainID integer|nil If provided, purges only that train. If nil, purges all dead trains.
function PurgeDeadTrainStats(trainID)
if trainID then
-- Purge a specific train if it doesn't exist
if not storage.trains[trainID] then
storage.trainStats[trainID] = nil
return true
end
else
-- Purge all trains not in storage.trains
for statTrainID in pairs(storage.trainStats) do
if not storage.trains[statTrainID] then
storage.trainStats[statTrainID] = nil
end
end
end
return false
end
function SendTrainStats()
if options.enableTrains then
ScanTrains()
PurgeDeadTrainStats()
local returnParts = {}
returnParts[#returnParts+1] = GetTrainPlayerKills()
returnParts[#returnParts+1] = GetTrainTotalKills()
returnParts[#returnParts+1] = GetTrainStates()
returnParts[#returnParts+1] = GetTrainStatistics()
returnParts[#returnParts+1] = GetTrainsInDepot()
returnParts[#returnParts+1] = GetTrainTripStats()
helpers.send_udp(udpAddress,table.concat(returnParts,"\n"),serverIndex)
end
end