imposiblaa

floorSelector

Oct 21st, 2023 (edited)
1,586
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. local surface = dofile("/surface")
  2. globalPort = 5296
  3. localId = "cf3357aaeb3ccdd8b4ab54b9f8ccdf3a"
  4.  
  5. modem = peripheral.find("modem")
  6. modem.open(globalPort)
  7. database = {
  8.     {name="test", id=1,}
  9. }
  10.  
  11. console = {}
  12. buttons = {}
  13. selected = nil
  14.  
  15.    
  16. local surf = surface.create(51,19,colors.black)
  17.  
  18. local bg = surface.create(102,57,colors.black)
  19.  
  20. commands = {
  21.     alert = function(input)
  22.         assert(type(input) == "string", "helloWorld expects a string for its input")
  23.         print("ALERT: " .. input)
  24.         return true
  25.     end,
  26.  
  27.     updateDb = function(db)
  28.     assert(type(db) == "table", "updateDB expects a table for its input")
  29.     database = db
  30.     end,
  31.  
  32.     updateStatus = function(id, status)
  33.     assert(type(id) == "number", "updateStatus expects an int for its input \"id\"")
  34.     assert(type(status) == "table", "updateStatus expects a table for its input \"id\"")
  35.     database[id]=status
  36.     end,
  37.  
  38.     report = function(id)
  39.         assert(type(id) == "number", "report expects an int for its input")
  40.         commandRunner("pong", id, {localId})
  41.     end
  42. }
  43.  
  44.  
  45. function commandParser(event, side, channel, replyChannel, message, distance)
  46.     if message.cmd ~= nil and message.args ~= nil then
  47.     newLine = (message.cmd .. " " .. message.tgt)
  48.     for i,arg in pairs(message.args) do
  49.         newLine = (newLine .. " " .. tostring(arg))
  50.     end
  51.     addLnCon(newLine)
  52.     end
  53.     if commands[message.cmd] ~= nil and message.args ~= nil then
  54.         ran, err = pcall(commands[message.cmd], table.unpack(message.args))
  55.         if not ran then
  56.             print(("Error in \"%s\": %s"):format(message.cmd, err))
  57.         end
  58.     else
  59.         print(("No command named \"%s\" found"):format(message.cmd))
  60.     end
  61. end
  62.  
  63. function commandRunner(command, target, arguments)
  64.     assert(type(command) == "string", "commandRunner expects a string for its input \"command\"")
  65.     assert(type(target) == "number", "commandRunner expects a number for its input \"target\"")
  66.     assert(type(arguments) == "table", "commandRunner expects a table for its input \"arguments\"")
  67.     modem.transmit(5296,5296, {cmd=command, tgt=target, args=arguments})
  68. end
  69.  
  70. function checkButtons(x,y)
  71.     for i,button in pairs(buttons) do
  72.         if x>=button.tl[1] and y>=button.tl[2] and x<=button.br[1] and y<=button.br[2] then
  73.             button.fun(i)
  74.         end
  75.     end
  76. end
  77.  
  78. function checkEvents()
  79.     while true do
  80.         event = {os.pullEvent()}
  81.        
  82.         if event[1] == "modem_message" then
  83.             commandParser(table.unpack(event))
  84.             return
  85.         elseif event[1] == "mouse_click" then
  86.             checkButtons(event[3], event[4])
  87.             return
  88.         end
  89.     end
  90. end
  91.  
  92. function drawBg()
  93.     bg:drawLine(26,0,26,55, colors.gray)
  94.     bg:drawLine(26,0,100,0, colors.gray)
  95.     bg:drawLine(100,0,100,55, colors.gray)
  96.     bg:drawLine(100,55,26,55, colors.gray)
  97.  
  98.     bg:drawLine(0,0,0,55, colors.gray)
  99.     bg:drawLine(0,0,26,0, colors.gray)
  100.     bg:drawLine(0,55,26,55, colors.gray)
  101. end
  102.  
  103. function drawCon()
  104.     for line, value in pairs(console) do
  105.         surf:drawString(value, 14, 16-line, colors.black, colors.gray) 
  106.     end
  107.  
  108.     surf:drawString("CMD > ", 14,17, colors.black, colors.purple)
  109.     surf:drawString("Network Feed", 25,0, colors.black, colors.purple)
  110. end
  111.  
  112. function addLnCon(input)
  113.     table.move(console, 1, 14, 2, console)
  114.     console[1]=string.sub(input, 1, 36)
  115. end
  116.  
  117. function consoleInput()
  118.     input = read()
  119.  
  120.     addLnCon(input)
  121.     input = input .. " "
  122.     cmd = ""
  123.     args = {}
  124.     i = 1
  125.     for w in input:gmatch("(.-) ") do
  126.         if tonumber(w) ~= nil then
  127.             w = tonumber(w)
  128.         end
  129.         if i==1 then
  130.             cmd = w
  131.         else
  132.             table.insert(args, w)
  133.         end
  134.         i=i+1
  135.     end
  136.  
  137.     commandRunner(cmd, 0, args)
  138. end
  139.  
  140. function drawFloors()
  141.     surf:drawString("Floors:", 1, 0, colors.black, colors.purple)
  142.     i=1
  143.     lFloor=nil
  144.     for i,floor in pairs(database) do
  145.         if floor.cur == localId then
  146.             lFloor = i
  147.         end
  148.     end
  149.     for id,floor in pairs(database) do
  150.         if id == lFloor then
  151.             surf:drawLine(1,2,11,2,colors.purple)
  152.             surf:drawString(floor.name,2,2,colors.purple,colors.black)
  153.         else
  154.             surf:drawLine(1,i*2+2,11,i*2+2,colors.gray)
  155.             buttons[floor.name] = {tl={2,i*2+3}, br={12,i*2+3}, fun=function(name) commandRunner("getFloors", -1, {}) selected=name end}
  156.             surf:drawString(floor.name, 2,i*2+2, colors.gray, colors.lightGray)
  157.             i=i+1
  158.         end
  159.     end
  160. end
  161.  
  162. function go()
  163.     dep=nil
  164.     dest=nil
  165.     for i,floor in pairs(database) do
  166.         if floor.cur == localId then
  167.             dep = i
  168.         end
  169.         if floor.name == selected then
  170.             dest = i
  171.         end
  172.  
  173.     end
  174.     if dep==nil then addLnCon("ERROR: Could not locate you!") return end
  175.     if dest==nil then addLnCon("ERROR: Could not locate destination!") return end
  176.     if dep==dest then
  177.         addLnCon("ERROR: Cannot go to current floor!")
  178.         return
  179.     end
  180.     addLnCon("requestTransport 0 " .. dep .. " " .. dest)
  181.     commandRunner("requestTransport", 0, {dep, dest})
  182.     commandRunner("getFloors", 0, {})
  183. end
  184.  
  185. function confirmation()
  186.     confirm = surface.create(21, 6, colors.gray)
  187.     confirm:drawString(("  Confirm travel to"), 0,1,colors.gray,colors.lightGray)
  188.     confirm:drawString("\""..selected.."\"", 2,2,colors.gray,colors.lightGray)
  189.     confirm:drawLine(1,4,8,4,colors.lightGray)
  190.     confirm:drawLine(11,4,19,4,colors.purple)
  191.     confirm:drawString("cancel", 2,4,colors.lightGray,colors.black)
  192.     confirm:drawString("confirm",12,4,colors.purple,colors.black)
  193.     surf.overwrite=true
  194.     surf:drawSurface(confirm, 15, 6)
  195.     buttons["cancel"] = {tl={17,11},br={24,11}, fun=(function(name) selected=nil end)}
  196.     buttons["confirm"] =  {tl={27,11},br={34,11}, fun=(function(name) go() selected=nil end)}
  197. end
  198.  
  199. function draw()
  200.     buttons = {}
  201.     surf:drawSurfaceSmall(bg, 0, 0)
  202.  
  203.     drawCon()
  204.     drawFloors()
  205.  
  206.  
  207.     if selected ~= nil then
  208.         confirmation() 
  209.         surf:output()
  210.         sleep(1)
  211.     else
  212.         surf:output()
  213.         term.setTextColor(colors.lightGray)
  214.         term.setBackgroundColor(colors.black)
  215.         term.setCursorPos(21,18)
  216.         term.setCursorBlink(true)
  217.         consoleInput()
  218.     end
  219.  
  220. end
  221.    
  222. drawBg()
  223. commandRunner("getFloors", 0, {})
  224. while true do
  225.     parallel.waitForAny(draw,checkEvents)            
  226. end
  227.  
Advertisement
Add Comment
Please, Sign In to add comment