diff --git a/station_broadcast.lua b/station_broadcast.lua index 788b0df..9791e47 100644 --- a/station_broadcast.lua +++ b/station_broadcast.lua @@ -1,6 +1,6 @@ -- 1. Configuration & Constants -local APP_VERSION_TEXT = "v1.0.75" +local APP_VERSION_TEXT = "v1.0.76" local CONFIG = { CLOUD_URL = "ws://stabadmin.fse-media.group", @@ -3141,6 +3141,23 @@ local function isStationDestination(destName) return true end +local function unwrapNbtObject(obj) + if type(obj) ~= "table" then return obj end + if obj[1] == nil then return obj end + local inner = obj[1] + local isInnerUseful = false + if type(inner) == "table" then + for _ in pairs(inner) do isInnerUseful = true break end + elseif inner ~= nil then + isInnerUseful = true + end + if not isInnerUseful then return obj end + local outerKeyCount = 0 + for _ in pairs(obj) do outerKeyCount = outerKeyCount + 1 end + if outerKeyCount == 1 then return inner end + return obj +end + local function extractAlphaNumCodes(text) if not text then return {} end local s = tostring(text) @@ -3431,18 +3448,29 @@ function Main.loop() for i, stop in ipairs(data.TrainStops) do local okStop, err = pcall(function() - local st = type(stop.Station) == "table" and stop.Station[1] or stop.Station + local st = unwrapNbtObject(stop.Station) if st then local trainName = st.TrainName or "Unknown" - local rtStation = type(st.RealTimeStation) == "table" and st.RealTimeStation[1] or nil - local schStation = type(st.ScheduledStation) == "table" and st.ScheduledStation[1] or nil - local rtStationName = rtStation and rtStation.StationName or nil - local schStationName = schStation and schStation.StationName or nil + local rtStation = unwrapNbtObject(st.RealTimeStation) + local schStation = unwrapNbtObject(st.ScheduledStation) + local rtStationName = type(rtStation) == "table" and rtStation.StationName or nil + local schStationName = type(schStation) == "table" and schStation.StationName or nil local isLast = (stop.IsLast == 1 or stop.IsLast == true) local destination = st.Destination or "Unknown" if type(stop.Stopovers) == "table" and #stop.Stopovers > 0 then - local lastStopover = stop.Stopovers[#stop.Stopovers] + local lastStopover_raw = stop.Stopovers[#stop.Stopovers] + local lastStopover + if type(lastStopover_raw) == "table" then + local unwrapped = unwrapNbtObject(lastStopover_raw) + if type(unwrapped) == "table" then + lastStopover = unwrapped.StationName or unwrapped.Name or unwrapped[1] or lastStopover_raw + else + lastStopover = unwrapped or lastStopover_raw + end + else + lastStopover = lastStopover_raw + end if looksLikeValidDestinationText(lastStopover) then destination = lastStopover elseif not looksLikeValidDestinationText(destination) then @@ -3466,11 +3494,11 @@ function Main.loop() end local platform = "1" - local rtStationInfo = rtStation and type(rtStation.StationInfo) == "table" and rtStation.StationInfo[1] or nil - local schStationInfo = schStation and type(schStation.StationInfo) == "table" and schStation.StationInfo[1] or nil - if schStationInfo and schStationInfo.Platform then + local rtStationInfo = type(rtStation) == "table" and unwrapNbtObject(rtStation.StationInfo) or nil + local schStationInfo = type(schStation) == "table" and unwrapNbtObject(schStation.StationInfo) or nil + if type(schStationInfo) == "table" and schStationInfo.Platform then platform = schStationInfo.Platform - elseif rtStationInfo and rtStationInfo.Platform then + elseif type(rtStationInfo) == "table" and rtStationInfo.Platform then platform = rtStationInfo.Platform else platform = State.stationPlatform or "1" @@ -3479,13 +3507,16 @@ function Main.loop() if isStationDestination(destination) then local cancelled = false local reason = nil - local tr = type(stop.Train) == "table" and stop.Train[1] or stop.Train - if tr then + local tr = unwrapNbtObject(stop.Train) + if type(tr) == "table" then cancelled = tr.Cancelled == 1 or tr.Cancelled == true local status = tr.Status if type(status) == "table" then for _, s in ipairs(status) do - if type(s) == "string" then + local unwrapped_s = unwrapNbtObject(s) + if type(unwrapped_s) == "string" then + reason = mapTrainStatusReason(unwrapped_s) or reason + elseif type(s) == "string" then reason = mapTrainStatusReason(s) or reason end end