Elrol

client

Apr 10th, 2020 (edited)
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.52 KB | None | 0 0
  1. local modem = nil
  2. local serverName = ""
  3. local serverID = 0
  4. local clientID = 0
  5. local side = "top"
  6. local selected = 1
  7.  
  8. local w, h = term.getSize()
  9. local pos = 0
  10. local color = colors.white
  11. local infoText = ""
  12. local index = 1
  13. local cmds = {}
  14. local originID = ""
  15. local currentID = ""
  16. local isText = false
  17. local toOpen = ""
  18. local cancel = false
  19. local name = ""
  20.  
  21. os.loadAPI("menu")
  22.  
  23. function ask(request)
  24.     modem.transmit(serverID, clientID, request)
  25.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  26.     return message
  27. end
  28.  
  29. function info(text, isCenter)
  30.     if text == nil then
  31.         term.setCursorPos(1, y+2)
  32.     elseif isCenter then
  33.         x, y = term.getCursorPos()
  34.         x = (w - #text) / 2
  35.         term.setCursorPos(x, y)
  36.         write(text)
  37.         term.setCursorPos(1, y+1)
  38.     else
  39.         print(text)
  40.     end
  41. end
  42.  
  43. function longInfo(text, isCenter)
  44.     line = ""
  45.     for k,s in pairs(split(text, " ")) do
  46.         length = string.len(line) + string.len(s) + 1
  47.         if w >= length then
  48.             a = line .. " " .. s
  49.             line = a
  50.         else
  51.             info(line, isCenter)
  52.             line = " "..s
  53.         end
  54.     end
  55.     info(line, isCenter)
  56. end
  57.  
  58. function hr()
  59.     x, y = term.getCursorPos()
  60.     for i = 1, w, 1 do
  61.         write("-")
  62.     end
  63.     term.setCursorPos(1, y+1)
  64. end
  65.  
  66. function cmdLine()
  67.     flag = true
  68.     count = 0
  69.     while not cancel do
  70.         term.setCursorPos(1, h)
  71.         term.setBackgroundColor(colors.black)
  72.         term.setTextColor(color)
  73.         term.clearLine()
  74.         textLine = "> "..infoText
  75.         term.write(textLine)
  76.         term.setCursorPos(#textLine + 1, h)        
  77.         if flag then
  78.             term.setBackgroundColor(color)
  79.             term.write(" ")
  80.             term.setBackgroundColor(colors.black)
  81.             flag = false
  82.         else
  83.             term.setBackgroundColor(colors.black)
  84.             term.write(" ")
  85.             term.setBackgroundColor(color)
  86.             flag = true
  87.         end
  88.         sleep(0.5)
  89.         count = count + 1
  90.         if count >= 10 then
  91.             infoText = ""
  92.             count = 0
  93.         end
  94.     end
  95. end
  96.  
  97. function buttons()
  98.     while true do
  99.         local event, key, isHeld = os.pullEvent("key")
  100.         if not isHeld then
  101.             if isText then
  102.                 if not isEmpty(originID) then
  103.                     toOpen = originID
  104.                     isText = false
  105.                     currentID = ""
  106.                     originID = ""
  107.                     return
  108.                 end
  109.             end
  110.             if key == keys.up then
  111.                 if selected > 1 then
  112.                     selected = selected - 1
  113.                 else
  114.                     if index > 1 then
  115.                         index = index - 1
  116.                     end
  117.                 end
  118.             elseif key == keys.down then
  119.                 if selected < table.getn(cmds) then
  120.                     if selected + pos < h - 1  then
  121.                         selected = selected + 1
  122.                     else
  123.                         if index < (h - pos) then
  124.                             index = index + 1
  125.                         end
  126.                     end
  127.                 end
  128.             elseif key == keys.enter then
  129.                 cmd = cmds[(selected + index - 1)]
  130.                 if string.starts(cmd[1], "toggle") then
  131.                     numb = tonumber(split(cmd[1], " ")[2])
  132.                     flag = ask("check "..numb)
  133.                     if cmd[3] then flag = not flag end
  134.                     if flag then
  135.                         infoText = "Disabling "..cmd[2]
  136.                     else
  137.                         infoText = "Enabling "..cmd[2]
  138.                     end
  139.                     ask("toggle "..numb)
  140.                 elseif string.starts(cmd[1], "trigger") then
  141.                     numb = tonumber(split(cmd[1], " ")[2])
  142.                     infoText = "Activating "..cmd[2]
  143.                     ask("pulse "..numb)
  144.                 elseif string.starts(cmd[1], "open") then
  145.                     id = split(cmd[1], " ")[2]
  146.                     toOpen = id
  147.                     isText = false
  148.                     return
  149.                 elseif string.starts(cmd[1], "read") then
  150.                     id = split(cmd[1], " ")[2]
  151.                     toOpen = id
  152.                     isText = true
  153.                     return
  154.                 end
  155.             elseif key == keys.backspace then
  156.                 if not isEmpty(originID) then
  157.                     toOpen = originID
  158.                     currentID = ""
  159.                     originID = ""
  160.                     return
  161.                 end
  162.             end
  163.             options(index)
  164.         end
  165.     end
  166. end
  167.  
  168. 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.                 numb = tonumber(split(cmd[1], " ")[2])
  188.                 info("> "..cmd[2].." "..hasColor(numb, cmd[3]))
  189.             elseif string.starts(cmd[1], "trigger") then
  190.                 info("> "..cmd[2])
  191.             elseif string.starts(cmd[1], "open") then
  192.                 info("> "..cmd[2])
  193.             elseif string.starts(cmd[1], "read") then
  194.                 info("> "..cmd[2])
  195.             end
  196.         end
  197.         ind = ind + 1
  198.     end
  199. end
  200.  
  201. function renderMenu(m)
  202.     isText = false
  203.     if isEmpty(currentID) then
  204.         currentID = m.id
  205.     else
  206.         originID = currentID
  207.         currentID = m.id
  208.     end
  209.     term.setTextColor(color)
  210.     term.setBackgroundColor(colors.black)
  211.     term.clear()
  212.     term.setCursorPos(1,1)
  213.     info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
  214.     info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
  215.     info("-Server "..serverID.."-", true)
  216.     info(nil, false)
  217.     info(serverName, false)
  218.     info("======================", false)
  219.     info(m.title, false)
  220.     hr()
  221.     x,y = term.getCursorPos()
  222.     pos = (h - y)
  223.     selected = 1
  224.     cmds = m.cmds
  225.     options(index)
  226.     parallel.waitForAny(buttons, cmdLine)
  227.     if not isEmpty(toOpen) then
  228.         if isText then
  229.             renderText(menu.getText(toOpen))
  230.         else
  231.             renderMenu(menu.getMenu(toOpen))
  232.         end
  233.     end
  234. end
  235.  
  236. function renderText(text)
  237.     isText = true
  238.     if isEmpty(currentID) then
  239.         currentID = text.id
  240.     else
  241.         originID = currentID
  242.         currentID = text.id
  243.     end
  244.     term.setTextColor(color)
  245.     term.setBackgroundColor(colors.black)
  246.     term.clear()
  247.     term.setCursorPos(1,1)
  248.     info("ROBCO INDUSTRIES UNIFIED OPERATING SYSTEM", true)
  249.     info("COPYRIGHT 2019-2020 ROBCO INDUSTRIES", true)
  250.     info("-Server 1-", true)
  251.     info(nil, false)
  252.     info(serverName, false)
  253.     info("======================", false)
  254.     info(text.title, false)
  255.     hr()
  256.     x,y = term.getCursorPos()
  257.     pos = (h - y)
  258.     selected = 1
  259.     cmds = {}
  260.     longInfo(text.txt, false)
  261.     parallel.waitForAny(buttons, cmdLine)
  262.     if not isEmpty(toOpen) then
  263.         if isText then
  264.             renderText(menu.getText(toOpen))
  265.         else
  266.             renderMenu(menu.getMenu(toOpen))
  267.         end
  268.     end
  269. end
  270.  
  271.  
  272. function split(inputstr, sep)
  273.     if sep == nil then
  274.         sep = "%s"
  275.     end
  276.     local t={}
  277.     for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
  278.         table.insert(t, str)
  279.     end
  280.     return t
  281. end
  282.  
  283. function string.starts(String,Start)
  284.    return string.sub(String,1,string.len(Start))==Start
  285. end
  286.  
  287. function isEmpty(s)
  288.     return s == nil or s == ''
  289. end
  290.  
  291. function hasColor(c, flag)
  292.     f = ask("check "..c)
  293.     if flag then f = not f end
  294.     if f then
  295.         return "[On]"
  296.     else
  297.         return "[Off]"
  298.     end
  299. end
  300.  
  301. function init(c, port, id, n, s)
  302.     side = s
  303.     color = c
  304.     modem = peripheral.wrap(side)
  305.     serverID = port
  306.     clientID = id
  307.     name = n
  308.     modem.open(clientID)
  309.     term.setTextColor(color)
  310.     term.setBackgroundColor(colors.black)
  311.     term.clear()
  312.     term.setCursorPos(1,1)
  313.     serverName = ask("name:"..name)
  314.     renderMenu(menu.getMenu("start"))
  315. end
Add Comment
Please, Sign In to add comment