Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- #region Setup
- local basalt_path = "bilzOS/basalt.lua"
- if not(fs.exists(basalt_path))then
- shell.run("wget run https://basalt.madefor.cc/install.lua packed bilzOS/basalt.lua")
- end
- local destinations_path = "destinations.txt"
- settings.load()
- local basalt = require("basalt")
- local monitors = {}
- local tracks = {}
- local current_station_id = settings.get("fahrkarten.bahnhofskennung")
- for i, v in ipairs(settings.get("fahrkarten.monitors")) do
- monitors[i] = {
- peripheral = nil,
- destination_select = nil,
- train_select = nil,
- person_select = nil
- }
- monitors[i].peripheral = peripheral.wrap(v)
- monitors[i].peripheral.setTextScale(0.5)
- monitors[i].peripheral.clear()
- end
- local track_count = settings.get("fahrkarten.gleisanzahl")
- for i = 1, track_count do
- tracks[i] = {
- station_a = peripheral.wrap(settings.get("fahrkarten.gleis" .. i .. ".station_a")),
- station_b = peripheral.wrap(settings.get("fahrkarten.gleis" .. i .. ".station_b"))
- }
- end
- -- #endregion
- -- #region Destinations
- local function download_destinations()
- print('downloading destinations_file')
- if fs.exists(destinations_path) then
- fs.delete(destinations_path)
- end
- shell.run("pastebin", "get", "YuViVj0h", destinations_path)
- if not fs.exists(destinations_path) then
- print('ERROR - Download failed.')
- print("Retrying in 5 seconds")
- sleep(5)
- os.reboot()
- end
- end
- local function set_selectable_destinations()
- local selectable_destinations = {}
- local file = fs.open(destinations_path, "r")
- local table_string = file.readAll()
- file.close()
- local all_destinations = textutils.unserialize(table_string)
- table.sort(all_destinations, function(a, b) return a.station_name < b.station_name end)
- for i, v in ipairs(all_destinations) do
- if v.station_id ~= current_station_id and v.is_passenger_station then
- table.insert(selectable_destinations, { v.station_name, colors.black, colors.gray, { destination = v.station_id} })
- end
- end
- for i, m in ipairs(monitors) do
- if m.destination_select ~= nil then
- print("Updating destination selection on monitor " .. i)
- m.destination_select:clear()
- m.destination_select:setOptions(selectable_destinations)
- local w, _ = m.peripheral.getSize()
- m.destination_select:setDropdownSize(w, #selectable_destinations)
- end
- end
- end
- local function set_schedule(destination, train, persons)
- print("Creating schedule for train " .. train.args.train .. " to " .. destination.text .. " for " .. persons.args.persons .. " persons")
- local schedule = {
- cyclic = false,
- entries = {
- {
- instruction = {
- id = "create:rename",
- data = { text = "^".. destination.text .."$"}
- },
- },
- {
- instruction = {
- id = "create:destination",
- data = { text = current_station_id .." P*"}
- },
- conditions = {
- {
- {
- id = "create:player_count",
- data = { count = persons.args.persons, exact = 1 }
- }
- }
- }
- },
- {
- instruction = {
- id = "create:rename",
- data = { text = "^FINAL_DEST$"}
- },
- },
- {
- instruction = {
- id = "create:destination",
- data = { text = destination.args.destination .." P*"}
- },
- conditions = {
- {
- {
- id = "create:player_count",
- data = { count = 11, exact = 1 }
- }
- }
- }
- },
- }
- }
- 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
- print("Setting schedule for station A on track " .. train.args.track)
- tracks[train.args.track].station_a.setSchedule(schedule)
- return
- 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
- print("Setting schedule for station B on track " .. train.args.track)
- tracks[train.args.track].station_b.setSchedule(schedule)
- return
- else
- print("Train " .. train.args.train .. " not found on track " .. train.args.track)
- print("No schedule set")
- end
- end
- -- #endregion
- -- #region Train Station
- local function is_train_allowed(train_name)
- if train_name == nil then
- return false
- elseif string.sub(train_name, 1, 4) == "BB-P" and string.sub(train_name, 4, 5) ~= "P0" then
- return true
- end
- print("Zugname " .. train_name .. " ist nicht auswaehlbar")
- return false
- end
- local function equal_trains(t1, t2)
- local equal_tables = true
- if #t1 ~= #t2 then
- return false
- end
- for i=1, #t1 do
- if t1[i][1] ~= t2[i][1] then
- return false
- end
- end
- return true
- end
- local function set_selectable_trains()
- local trains = {}
- while true do
- sleep(3)
- local previous_trains = trains
- trains = {}
- for i=1, track_count do
- local train_name = nil
- if tracks[i].station_a.isTrainPresent()
- and tracks[i].station_a.hasSchedule() then
- train_name = tracks[i].station_a.getTrainName()
- elseif tracks[i].station_b.isTrainPresent()
- and tracks[i].station_b.hasSchedule() then
- train_name = tracks[i].station_b.getTrainName()
- end
- if train_name ~= nil and is_train_allowed(train_name) then
- local display_text = string.sub(train_name, 4, #train_name) .. " (" .. i .. ")"
- table.insert(trains, { display_text, colors.black, colors.gray, { train = train_name, track = i } })
- end
- end
- if not equal_trains(trains, previous_trains) then
- print("Updating train selection")
- for i, m in ipairs(monitors) do
- if m.train_select ~= nil then
- m.train_select:clear()
- m.train_select:setOptions(trains)
- local w, _ = m.peripheral.getSize()
- m.train_select:setDropdownSize(w, #trains)
- end
- end
- end
- end
- end
- -- #endregion
- -- #region UI
- local function init_monitors()
- for i, m in ipairs(monitors) do
- local first_row = 8
- local w, h = m.peripheral.getSize()
- local ui = basalt.addMonitor()
- ui:setMonitor(m.peripheral)
- ui:addPane()
- :setPosition(1, 1)
- :setSize(w, 3)
- :setBackground(colors.yellow)
- ui:addLabel()
- :setText("Fahrkarten")
- :setPosition(1, 2)
- :setSize(w, 1)
- :setFontSize(1)
- :setTextAlign("center")
- :setForeground(colors.black)
- ui:addLabel()
- :setText("Ziel:")
- :setPosition(1, first_row)
- m.destination_select = ui:addDropdown()
- :setPosition(1, first_row + 1)
- :setSize(w, 1)
- ui:addLabel()
- :setText("Zug:")
- :setPosition(1, first_row + 3)
- m.train_select = ui:addDropdown()
- :setPosition(1, first_row + 4)
- :setSize(w, 1)
- ui:addLabel()
- :setText("Reisende:")
- :setPosition(1, first_row + 6)
- m.person_select = ui:addDropdown()
- :setPosition(1, first_row + 7)
- :setSize(w, 1)
- :setOptions({{"1 Person", colors.black, colors.gray, { persons = 1}}, {"2 Personen", colors.black, colors.gray, { persons = 2}}, {"Leerfahrt", colors.black, colors.gray, { persons = 0}}})
- :setDropdownSize(w, 3)
- local buy_button = ui:addButton()
- buy_button:setText("Kaufen")
- :setSize(8, 1)
- :setPosition(4, h - 1)
- :onClick(function()
- local selected_destination = m.destination_select:getValue()
- local selected_train = m.train_select:getValue()
- local selected_persons = m.person_select:getValue()
- if i == 1 then
- if selected_destination == nil or selected_train == nil or selected_persons == nil then
- print("MIndestens ein Feld ist nicht ausgefuellt")
- print("Keine Fahrplanerstellung moeglich")
- else
- set_schedule(selected_destination, selected_train, selected_persons)
- end
- end
- if m.destination_select ~= nil and m.destination_select:getItemCount() > 0 then
- m.destination_select:selectItem(1)
- end
- if m.train_select ~= nil and m.train_select:getItemCount() > 0 then
- m.train_select:selectItem(1)
- end
- if m.person_select ~= nil and m.person_select:getItemCount() > 0 then
- m.person_select:selectItem(1)
- end
- if m.success_label ~= nil then
- m.success_label:hide()
- end
- end)
- if i == 1 then
- ui:addThread()
- :start(set_selectable_trains)
- end
- end
- end
- -- #endregion
- init_monitors()
- download_destinations()
- set_selectable_destinations()
- basalt.autoUpdate()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement