diff --git a/data.json b/data.json new file mode 100644 index 0000000..e69de29 diff --git a/test-db.js b/test-db.js new file mode 100644 index 0000000..8992f00 --- /dev/null +++ b/test-db.js @@ -0,0 +1,16 @@ +const DataService = require('./server/services/data'); +async function run() { + try { + await DataService.init(); + const lines = DataService.getLines(); + console.log("Line 0 stops:", lines[0]?.stops, lines[0]?.['站序'], lines[0]?.['站点列表']); + console.log("Line 0 name:", lines[0]?.name, lines[0]?.['线路名称']); + const fares = DataService.getFares(); + console.log("Fare 0:", fares[0]); + } catch(e) { + console.error(e); + } finally { + process.exit(0); + } +} +run(); \ No newline at end of file diff --git a/ticketmachine.lua b/ticketmachine.lua index 0694c3c..aa1924e 100644 --- a/ticketmachine.lua +++ b/ticketmachine.lua @@ -963,6 +963,7 @@ local function waitButtons() end end elseif ev == 'config_updated' then + singleSegments = nil -- If config was updated in background, return to force UI re-render return end @@ -1175,13 +1176,20 @@ local function buildSingleSegments() local segments = {} local fares = CFG.fares or {} for _, line in ipairs(CFG.lines or {}) do - local stops = line.stops or line['站点列表'] or {} + local stops = {} + if type(line.stops) == 'table' then stops = line.stops + elseif type(line['站点列表']) == 'table' then stops = line['站点列表'] + elseif type(line.stations) == 'table' then stops = line.stations + elseif type(line['站点']) == 'table' then stops = line['站点'] + end local lineFares = {} for i = 1, #stops - 1 do - local a, b = stops[i], stops[i+1] + local a, b = normalizeCode(stops[i]), normalizeCode(stops[i+1]) local f = nil for _, fx in ipairs(fares) do - if (fx.from == a and fx.to == b) or (fx.from == b and fx.to == a) then + local from = normalizeCode(fx.from) + local to = normalizeCode(fx.to) + if (from == a and to == b) or (from == b and to == a) then f = fx break end @@ -1203,10 +1211,10 @@ local function buildSingleSegments() end local function showSingleSegmentSelection() - if not singleSegments then - singleSegments = buildSingleSegments() - end while state.page == 'single_segment' do + if not singleSegments or #singleSegments == 0 then + singleSegments = buildSingleSegments() + end drawHeader('Select Single Segment', 'Select a line and fare') Buttons = {} local itemsPerPage = math.floor((h - 10) / 4) * 2 @@ -1566,7 +1574,15 @@ local function renderLinesSelection(title, selectedCode, backPage, nextPage, inf local y = startY for _, l in ipairs(CFG.lines) do y = y + 2; local x = 2 - for _, sc in ipairs((type(l) == 'table' and type(l.stations) == 'table' and l.stations) or {}) do + local stopsList = {} + if type(l) == 'table' then + if type(l.stations) == 'table' then stopsList = l.stations + elseif type(l.stops) == 'table' then stopsList = l.stops + elseif type(l['站点列表']) == 'table' then stopsList = l['站点列表'] + elseif type(l['站点']) == 'table' then stopsList = l['站点'] + end + end + for _, sc in ipairs(stopsList) do local c = normalizeCode(sc) local st = stationByCode[c] local label = (st and (st.en_name or st.name)) or c @@ -1598,7 +1614,15 @@ local function renderLinesSelection(title, selectedCode, backPage, nextPage, inf termDev.setBackgroundColor(colors.black) end y = y + 1; local x = 2 - for _, sc in ipairs((type(l) == 'table' and type(l.stations) == 'table' and l.stations) or {}) do + local stopsList = {} + if type(l) == 'table' then + if type(l.stations) == 'table' then stopsList = l.stations + elseif type(l.stops) == 'table' then stopsList = l.stops + elseif type(l['站点列表']) == 'table' then stopsList = l['站点列表'] + elseif type(l['站点']) == 'table' then stopsList = l['站点'] + end + end + for _, sc in ipairs(stopsList) do local c = normalizeCode(sc) local st = stationByCode[c] local label = (st and (st.en_name or st.name)) or c