Elrol

client

Apr 10th, 2020 (edited)
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.09 KB | None | 0 0
  1. local modem = nil
  2. local serverName = ""
  3. local port = 0
  4. local serverPort = 0
  5. local selected = 1
  6.  
  7. local w, h = term.getSize()
  8. local pos = 0
  9. local color = colors.white
  10. local infoText = ""
  11. local index = 1
  12. local cmds = {}
  13. local originID = ""
  14. local currentID = ""
  15. local isText = false
  16. local toOpen = ""
  17. local cancel = false
  18. local name = ""
  19. local listenerID = -1
  20.  
  21. local menu = require("menu")
  22. menu.loadMenus()
  23.  
  24. local function ask(targetPort, request, timeout)
  25.     timeout = timeout or 1
  26.     modem.transmit(targetPort, port, request)
  27.    
  28.     local timerID = os.startTimer(timeout)
  29.    
  30.     local message = nil
  31.     local status = "Timeout"
  32.    
  33.     local loop = true
  34.    
  35.     while loop do
  36.    
  37.         local eventData = {os.pullEvent()}
  38.         local event = eventData[1]
  39.        
  40.         if event == "modem_message" then
  41.             local a, side, senderChannel, replyChannel, replyMessage, distance = unpack(eventData)
  42.            
  43.             if replyChannel == targetPort then
  44.                 message = replyMessage
  45.                 status = "Success"
  46.                 break
  47.             end
  48.            
  49.         elseif event == "timer" then
  50.             if eventData[2] == timerID then
  51.                 os.cancelTimer(timerID)
  52.                 return nil, "Timeout"
  53.             end
  54.         else
  55.             message = event
  56.             status = "Failed"
  57.         end
  58.    
  59.     end
  60.    
  61.     os.cancelTimer(timerID)
  62.     return message, status
  63. end
  64.  
  65. local function split(inputstr, sep)
  66.     if sep == nil then
  67.         sep = "%s"
  68.     end
  69.     local t={}
  70.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  71.         table.insert(t, str)
  72.     end
  73.     return t
  74. end
  75.  
  76. function string.starts(String,Start)
  77.    return string.sub(tostring(String), 1, string.len(Start))== tostring(Start)
  78. end
  79.  
  80. local function isEmpty(s)
  81.     return s == nil or s == ''
  82. end
  83.  
  84. local function hasColor(targetPort, c, flag)
  85.     local r, status = ask(targetPort, "check "..targetPort.." "..c)
  86.    
  87.     if status == "Success" then
  88.         local f = r == "true"
  89.         if flag then f = not f end
  90.         if f then
  91.             return "[On]"
  92.         else
  93.             return "[Off]"
  94.         end
  95.     else
  96.         return "[FAILED]"
  97.     end
  98. end
  99.  
  100. local function info(text, isCenter)
  101.     if text == nil then
  102.         term.setCursorPos(1, y+2)
  103.     elseif isCenter then
  104.         x, y = term.getCursorPos()
  105.         x = (w - #text) / 2
  106.         term.setCursorPos(x, y)
  107.         write(text)
  108.         term.setCursorPos(1, y+1)
  109.     else
  110.         print(text)
  111.     end
  112. end
  113.  
  114. local function longInfo(text, isCenter)
  115.     line = ""
  116.     for k,s in pairs(split(text, " ")) do
  117.         length = string.len(line) + string.len(s) + 1
  118.         if w >= length then
  119.             a = line .. " " .. s
  120.             line = a
  121.         else
  122.             info(line, isCenter)
  123.             line = " "..s
  124.         end
  125.     end
  126.     info(line, isCenter)
  127. end
  128.  
  129. local function hr()
  130.     x, y = term.getCursorPos()
  131.     for i = 1, w, 1 do
  132.         write("-")
  133.     end
  134.     term.setCursorPos(1, y+1)
  135. end
  136.  
  137. local function cmdLine()
  138.     flag = true
  139.     count = 0
  140.     while not cancel do
  141.         term.setCursorPos(1, h)
  142.         term.setBackgroundColor(colors.black)
  143.         term.setTextColor(color)
  144.         term.clearLine()
  145.         textLine = "> "..infoText
  146.         term.write(textLine)
  147.         term.setCursorPos(#textLine + 1, h)        
  148.         if flag then
  149.             term.setBackgroundColor(color)
  150.             term.write(" ")
  151.             term.setBackgroundColor(colors.black)
  152.             flag = false
  153.         else
  154.             term.setBackgroundColor(colors.black)
  155.             term.write(" ")
  156.             term.setBackgroundColor(color)
  157.             flag = true
  158.         end
  159.         sleep(0.5)
  160.         count = count + 1
  161.         if count >= 10 then
  162.             infoText = ""
  163.             count = 0
  164.         end
  165.     end
  166. end
  167.  
  168. local function options(index)
  169.     term.setCursorPos(x,pos)
  170.     local ind = index
  171.     if cmds == nil then
  172.         return
  173.     end
  174.     for a = pos+1, h-1, 1 do
  175.         cmd = cmds[ind]
  176.         if cmd == nil then
  177.         else
  178.             if ind == (selected + index - 1) then
  179.                 term.setTextColor(colors.black)
  180.                 term.setBackgroundColor(color)
  181.             else
  182.                 term.setTextColor(color)
  183.                 term.setBackgroundColor(colors.black)
  184.             end
  185.             term.clearLine()
  186.             if string.starts(cmd[1], "toggle") then
  187.                 local splitCMD = split(cmd[1], " ")
  188.                 local targetPort = tonumber(splitCMD[2])
  189.                 local numb = tonumber(splitCMD[3])
  190.                 info("> "..cmd[2].." "..hasColor(targetPort, numb, cmd[3]))
  191.             elseif string.starts(cmd[1], "trigger") then
  192.                 info("> "..cmd[2])
  193.             elseif string.starts(cmd[1], "open") then
  194.                 info("> "..cmd[2])
  195.             elseif string.starts(cmd[1], "read") then
  196.                 info("> "..cmd[2])
  197.             end
  198.         end
  199.         ind = ind + 1
  200.     end
  201. end
  202.  
  203. local function buttons()
  204.     while true do
  205.         local event, key, isHeld = os.pullEvent("key")
  206.         if not isHeld then
  207.             if isText then
  208.                 if not isEmpty(originID) then
  209.                     toOpen = originID
  210.                     isText = false
  211.                     currentID = ""
  212.                     originID = ""
  213.                     return
  214.                 end
  215.             end
  216.             if key == keys.up then
  217.                 if selected > 1 then
  218.                     selected = selected - 1
  219.                 else
  220.                     if index > 1 then
  221.                         index = index - 1
  222.                     end
  223.                 end
  224.             elseif key == keys.down then
  225.                 if selected < table.getn(cmds) then
  226.                     if selected + pos < h - 1  then
  227.                         selected = selected + 1
  228.                     else
  229.                         if index < (h - pos) then
  230.                             index = index + 1
  231.                         end
  232.                     end
  233.                 end
  234.             elseif key == keys.enter then
  235.                 local command = cmds[(selected + index - 1)]
  236.                 local splitCMD = split(command[1], " ")
  237.                
  238.                 local cmdString = splitCMD[1]
  239.                
  240.                 if type(cmdString) ~= "string" then
  241.                     print("Invalid command string: "..cmdString)
  242.                     return
  243.                 end
  244.                
  245.                 if cmdString == "toggle" then
  246.                     local targetPort = tonumber(splitCMD[2])
  247.                     local numb = tonumber(splitCMD[3])
  248.                     local f, status = ask(targetPort, "check "..numb) -- false when off, true when on
  249.                     local flag = f == "true"
  250.                    
  251.                     if status == "Success" then
  252.                         if command[3] then -- check the invert boolean to check if the flag should be inverted
  253.                             flag = not flag
  254.                         end
  255.                        
  256.                         if flag then
  257.                             infoText = "Disabling "..command[2]
  258.                         else
  259.                             infoText = "Enabling "..command[2]
  260.                         end
  261.                         ask(targetPort, "toggle "..targetPort.." "..numb)
  262.                     end
  263.                 elseif cmdString == "trigger" then
  264.                     local targetPort = tonumber(splitCMD[2])
  265.                     numb = tonumber(splitCMD[3])
  266.                     infoText = "Activating "..command[2]
  267.                     ask(targetPort, "pulse "..targetPort.." "..numb)
  268.                 elseif cmdString == "open" then
  269.                     id = splitCMD[2]
  270.                     toOpen = id
  271.                     isText = false
  272.                     return
  273.                 elseif cmdString == "read" then
  274.                     id = splitCMD[2]
  275.                     toOpen = id
  276.                     isText = true
  277.                     return
  278.                 end
  279.             elseif key == keys.backspace then
  280.                 if not isEmpty(originID) then
  281.                     toOpen = originID
  282.                     currentID = ""
  283.                     originID = ""
  284.                     return
  285.                 end
  286.             end
  287.             options(index)
  288.         end
  289.     end
  290. end
  291.  
  292. function renderText(text)
  293.     isText = true
  294.     if isEmpty(currentID) then
  295.         currentID = text.id
  296.     else
  297.         originID = currentID
  298.         currentID = text.id
  299.     end
  300.     term.setTextColor(color)
  301.     term.setBackgroundColor(colors.black)
  302.     term.clear()
  303.     term.setCursorPos(1,1)
  304.     info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
  305.     info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
  306.     info("-Server 1-", true)
  307.     info(nil, false)
  308.     info(serverName, false)
  309.     info("======================", false)
  310.     info(text.title, false)
  311.     hr()
  312.     x,y = term.getCursorPos()
  313.     pos = (h - y)
  314.     selected = 1
  315.     cmds = {}
  316.     longInfo(text.txt, false)
  317.     parallel.waitForAny(buttons, cmdLine)
  318.     if not isEmpty(toOpen) then
  319.         if isText then
  320.             renderText(menu.getText(toOpen))
  321.         else
  322.             renderMenu(menu.getMenu(toOpen))
  323.         end
  324.     end
  325. end
  326.  
  327. function renderMenu(m)
  328.     isText = false
  329.     if isEmpty(currentID) then
  330.         currentID = m.id
  331.     else
  332.         originID = currentID
  333.         currentID = m.id
  334.     end
  335.     term.setTextColor(color)
  336.     term.setBackgroundColor(colors.black)
  337.     term.clear()
  338.     term.setCursorPos(1,1)
  339.     info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
  340.     info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
  341.     info("-Server "..port.."-", true)
  342.     info(nil, false)
  343.     info(serverName, false)
  344.     info("======================", false)
  345.     info(m.title, false)
  346.     hr()
  347.     x,y = term.getCursorPos()
  348.     pos = (h - y)
  349.     selected = 1
  350.     cmds = m.cmds
  351.     options(index)
  352.     parallel.waitForAny(buttons, cmdLine)
  353.     if not isEmpty(toOpen) then
  354.         if isText then
  355.             renderText(menu.getText(toOpen))
  356.         else
  357.             renderMenu(menu.getMenu(toOpen))
  358.         end
  359.     end
  360. end
  361.  
  362. function init(n, p, sp, side, c)
  363.     name = n
  364.     port = p
  365.     serverPort = sp
  366.     modem = peripheral.wrap(side)
  367.     color = c
  368.    
  369.     if modem then
  370.         modem.open(port)
  371.        
  372.         local reply, status = ask(serverPort, "name:"..name)
  373.         if status == "Success" then
  374.             serverName = reply
  375.         else
  376.             serverName = status
  377.         end
  378.     else
  379.         print("Error: Modem was nil: " .. side .. ":" .. port)
  380.         return
  381.     end
  382.    
  383.    
  384.     term.setTextColor(color)
  385.     term.setBackgroundColor(colors.black)
  386.     term.clear()
  387.     term.setCursorPos(1,1)
  388.     renderMenu(menu.getMenu("start"))
  389. end
  390.  
  391. return {
  392.     init = init
  393. }
Advertisement
Add Comment
Please, Sign In to add comment