Advertisement
cc-editor

Fahrkarten

Oct 7th, 2024 (edited)
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.06 KB | None | 0 0
  1. -- #region Setup
  2. local basalt_path = "bilzOS/basalt.lua"
  3. if not(fs.exists(basalt_path))then
  4.     shell.run("wget run https://basalt.madefor.cc/install.lua packed bilzOS/basalt.lua")
  5. end
  6. local destinations_path = "destinations.txt"
  7.  
  8. settings.load()
  9. local basalt = require("basalt")
  10. local monitors = {}
  11. local tracks = {}
  12.  
  13. local current_station_id = settings.get("fahrkarten.bahnhofskennung")
  14.  
  15. for i, v in ipairs(settings.get("fahrkarten.monitors")) do
  16.     monitors[i] = {
  17.         peripheral = nil,
  18.         destination_select = nil,
  19.         train_select = nil,
  20.         person_select = nil
  21.     }
  22.     monitors[i].peripheral = peripheral.wrap(v)
  23.     monitors[i].peripheral.setTextScale(0.5)
  24.     monitors[i].peripheral.clear()
  25. end
  26.  
  27. local track_count = settings.get("fahrkarten.gleisanzahl")
  28. for i = 1, track_count do
  29.     tracks[i] = {
  30.         station_a = peripheral.wrap(settings.get("fahrkarten.gleis" .. i .. ".station_a")),
  31.         station_b = peripheral.wrap(settings.get("fahrkarten.gleis" .. i .. ".station_b"))
  32.     }
  33. end
  34. -- #endregion
  35.  
  36. -- #region Destinations
  37. local function download_destinations()
  38.     print('downloading destinations_file')
  39.  
  40.     if fs.exists(destinations_path) then
  41.         fs.delete(destinations_path)
  42.     end
  43.  
  44.     shell.run("pastebin", "get", "YuViVj0h", destinations_path)
  45.     if not fs.exists(destinations_path) then
  46.         print('ERROR - Download failed.')
  47.         print("Retrying in 5 seconds")
  48.         sleep(5)
  49.         os.reboot()
  50.     end
  51. end
  52.  
  53. local function set_selectable_destinations()
  54.     local selectable_destinations = {}
  55.     local file = fs.open(destinations_path, "r")
  56.     local table_string = file.readAll()
  57.     file.close()
  58.  
  59.     local all_destinations = textutils.unserialize(table_string)
  60.     table.sort(all_destinations, function(a, b) return a.station_name < b.station_name end)
  61.  
  62.     for i, v in ipairs(all_destinations) do
  63.         if v.station_id ~= current_station_id and v.is_passenger_station then
  64.             table.insert(selectable_destinations, { v.station_name, colors.black, colors.gray, { destination = v.station_id} })
  65.         end
  66.     end
  67.  
  68.     for i, m in ipairs(monitors) do
  69.         if m.destination_select ~= nil then
  70.             print("Updating destination selection on monitor " .. i)
  71.             m.destination_select:clear()
  72.             m.destination_select:setOptions(selectable_destinations)
  73.             local w, _ = m.peripheral.getSize()
  74.             m.destination_select:setDropdownSize(w, #selectable_destinations)
  75.         end
  76.     end
  77. end
  78.  
  79. local function set_schedule(destination, train, persons)
  80.     print("Creating schedule for train " .. train.args.train .. " to " .. destination.text .. " for " .. persons.args.persons .. " persons")
  81.  
  82.     local schedule = {
  83.         cyclic = false,
  84.         entries = {
  85.             {
  86.                 instruction = {
  87.                     id = "create:rename",
  88.                     data = { text = "^".. destination.text .."$"}
  89.                 },
  90.             },
  91.             {
  92.                 instruction = {
  93.                     id = "create:destination",
  94.                     data = { text = current_station_id .." P*"}
  95.                 },
  96.                 conditions = {
  97.                     {
  98.                         {
  99.                             id = "create:player_count",
  100.                             data = { count = persons.args.persons, exact = 1 }
  101.                         }
  102.  
  103.                     }
  104.                 }
  105.             },
  106.             {
  107.                 instruction = {
  108.                     id = "create:rename",
  109.                     data = { text = "^FINAL_DEST$"}
  110.                 },
  111.             },
  112.             {
  113.                 instruction = {
  114.                     id = "create:destination",
  115.                     data = { text = destination.args.destination .." P*"}
  116.                 },
  117.                 conditions = {
  118.                     {
  119.                         {
  120.                             id = "create:player_count",
  121.                             data = { count = 11, exact = 1 }
  122.                         }
  123.  
  124.                     }
  125.                 }
  126.             },
  127.  
  128.         }
  129.     }
  130.  
  131.     if tracks[train.args.track].station_a.isTrainPresent() and tracks[train.args.track].station_a.hasSchedule() and tracks[train.args.track].station_a.getTrainName() == train.args.train then
  132.         print("Setting schedule for station A on track " .. train.args.track)
  133.         tracks[train.args.track].station_a.setSchedule(schedule)
  134.         return
  135.     elseif tracks[train.args.track].station_b.isTrainPresent() and tracks[train.args.track].station_b.hasSchedule() and tracks[train.args.track].station_b.getTrainName() == train.args.train then
  136.         print("Setting schedule for station B on track " .. train.args.track)
  137.         tracks[train.args.track].station_b.setSchedule(schedule)
  138.         return
  139.     else
  140.         print("Train " .. train.args.train .. " not found on track " .. train.args.track)
  141.         print("No schedule set")
  142.     end
  143.  
  144.  
  145. end
  146. -- #endregion
  147.  
  148. -- #region Train Station
  149. local function is_train_allowed(train_name)
  150.     if train_name == nil then
  151.         return false
  152.     elseif string.sub(train_name, 1, 4) == "BB-P" and string.sub(train_name, 4, 5) ~= "P0" then
  153.         return true
  154.     end
  155.     print("Zugname " .. train_name .. " ist nicht auswaehlbar")
  156.     return false
  157. end
  158.  
  159. local function equal_trains(t1, t2)
  160.     local equal_tables = true
  161.     if #t1 ~= #t2 then
  162.         return false
  163.     end
  164.  
  165.     for i=1, #t1 do
  166.         if t1[i][1] ~= t2[i][1] then
  167.             return false
  168.         end
  169.     end
  170.     return true
  171. end
  172.  
  173. local function set_selectable_trains()
  174.     local trains = {}
  175.     while true do
  176.         sleep(3)
  177.  
  178.         local previous_trains = trains
  179.         trains = {}
  180.         for i=1, track_count do
  181.             local train_name = nil
  182.             if tracks[i].station_a.isTrainPresent()
  183.             and tracks[i].station_a.hasSchedule() then
  184.                 train_name = tracks[i].station_a.getTrainName()
  185.             elseif tracks[i].station_b.isTrainPresent()
  186.             and tracks[i].station_b.hasSchedule() then
  187.                 train_name = tracks[i].station_b.getTrainName()
  188.             end
  189.  
  190.             if train_name ~= nil and is_train_allowed(train_name) then
  191.                 local display_text = string.sub(train_name, 4, #train_name) .. " (" .. i .. ")"
  192.                 table.insert(trains, { display_text, colors.black, colors.gray, { train = train_name, track = i } })
  193.             end
  194.         end
  195.  
  196.         if not equal_trains(trains, previous_trains) then
  197.             print("Updating train selection")
  198.             for i, m in ipairs(monitors) do
  199.                 if m.train_select ~= nil then
  200.                     m.train_select:clear()
  201.                     m.train_select:setOptions(trains)
  202.                     local w, _ = m.peripheral.getSize()
  203.                     m.train_select:setDropdownSize(w, #trains)
  204.                 end
  205.             end
  206.         end
  207.     end
  208. end
  209. -- #endregion
  210.  
  211. -- #region UI
  212. local function init_monitors()
  213.     for i, m in ipairs(monitors) do
  214.         local first_row = 8
  215.         local w, h = m.peripheral.getSize()
  216.         local ui = basalt.addMonitor()
  217.         ui:setMonitor(m.peripheral)
  218.  
  219.         ui:addPane()
  220.             :setPosition(1, 1)
  221.             :setSize(w, 3)
  222.             :setBackground(colors.yellow)
  223.  
  224.         ui:addLabel()
  225.             :setText("Fahrkarten")
  226.             :setPosition(1, 2)
  227.             :setSize(w, 1)
  228.             :setFontSize(1)
  229.             :setTextAlign("center")
  230.             :setForeground(colors.black)
  231.  
  232.         ui:addLabel()
  233.             :setText("Ziel:")
  234.             :setPosition(1, first_row)
  235.  
  236.         m.destination_select = ui:addDropdown()
  237.             :setPosition(1, first_row + 1)
  238.             :setSize(w, 1)
  239.  
  240.         ui:addLabel()
  241.             :setText("Zug:")
  242.             :setPosition(1, first_row + 3)
  243.  
  244.         m.train_select = ui:addDropdown()
  245.             :setPosition(1, first_row + 4)
  246.             :setSize(w, 1)
  247.  
  248.         ui:addLabel()
  249.             :setText("Reisende:")
  250.             :setPosition(1, first_row + 6)
  251.  
  252.         m.person_select = ui:addDropdown()
  253.             :setPosition(1, first_row + 7)
  254.             :setSize(w, 1)
  255.             :setOptions({{"1 Person", colors.black, colors.gray, { persons = 1}}, {"2 Personen", colors.black, colors.gray, { persons = 2}}, {"Leerfahrt", colors.black, colors.gray, { persons = 0}}})
  256.             :setDropdownSize(w, 3)
  257.  
  258.         local buy_button = ui:addButton()
  259.         buy_button:setText("Kaufen")
  260.             :setSize(8, 1)
  261.             :setPosition(4, h - 1)
  262.             :onClick(function()
  263.                 local selected_destination = m.destination_select:getValue()
  264.                 local selected_train = m.train_select:getValue()
  265.                 local selected_persons = m.person_select:getValue()
  266.  
  267.                 if i == 1 then
  268.                     if selected_destination == nil or selected_train == nil or selected_persons == nil then
  269.                         print("MIndestens ein Feld ist nicht ausgefuellt")
  270.                         print("Keine Fahrplanerstellung moeglich")
  271.                     else
  272.                         set_schedule(selected_destination, selected_train, selected_persons)
  273.                     end
  274.                 end
  275.  
  276.                 if m.destination_select ~= nil and m.destination_select:getItemCount() > 0 then
  277.                     m.destination_select:selectItem(1)
  278.                 end
  279.                 if m.train_select ~= nil and m.train_select:getItemCount() > 0 then
  280.                     m.train_select:selectItem(1)
  281.                 end
  282.                 if m.person_select ~= nil and m.person_select:getItemCount() > 0 then
  283.                     m.person_select:selectItem(1)
  284.                 end
  285.                 if m.success_label ~= nil then
  286.                     m.success_label:hide()
  287.                 end
  288.             end)
  289.  
  290.         if i == 1 then
  291.             ui:addThread()
  292.                 :start(set_selectable_trains)
  293.         end
  294.     end
  295. end
  296. -- #endregion
  297.  
  298. init_monitors()
  299. download_destinations()
  300. set_selectable_destinations()
  301.  
  302. basalt.autoUpdate()
  303.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement