Skiret

nano

Nov 29th, 2020
459
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local event = require("event")
  2. local component = require("component")
  3. local term = require("term")
  4.  
  5. term.clear()
  6. print ("Переводом на RUS занимался: Skiret")
  7. os.sleep(1)
  8.  
  9. local modem = component.modem
  10. modem.open(1)
  11. modem.broadcast(1, "nanomachines", "setResponsePort", 1)
  12.  
  13. local lastResponse = ""
  14. local function printResponse()
  15.   local w, h = component.gpu.getResolution()
  16.   component.gpu.fill(1, h, w, h, " ")
  17.   component.gpu.set(1, h, lastResponse)
  18. end
  19. local function handleModemMessage(_, _, _, _, _, header, command, ...)
  20.   if header ~= "nanomachines" then return end
  21.   lastResponse = "Last response: " .. command
  22.   for _, v in ipairs({...}) do
  23.     lastResponse = lastResponse .. ", " .. tostring(v)
  24.   end
  25.   printResponse()
  26. end
  27.  
  28. event.listen("modem_message", handleModemMessage)
  29.  
  30. local function send(command, ...)
  31.   component.modem.broadcast(1, "nanomachines", command, ...)
  32. end
  33.  
  34. local function readNumber(name, validator)
  35.   local index
  36.   while not index do
  37.     io.write(name..": ")
  38.     index = tonumber(io.read())
  39.     if not index or validator and not validator(index) then
  40.       index = nil
  41.       io.write("invalid input\n")
  42.     end
  43.   end
  44.   return index
  45. end
  46.  
  47. local running = true
  48. local commands = {
  49.   { "Get power state",
  50.     function()
  51.       send("getPowerState")
  52.     end
  53.   },
  54.  
  55.   { "Get active effects",
  56.     function()
  57.       send("getActiveEffects")
  58.     end
  59.   },
  60.   { "Get input",
  61.     function()
  62.       local index = readNumber("index")
  63.       send("getInput", index)
  64.     end
  65.   },
  66.   { "Set input",
  67.     function()
  68.       local index = readNumber("index")
  69.       io.write("1. On\n")
  70.       io.write("2. Off\n")
  71.       local value = readNumber("state", function(x) return x == 1 or x == 2 end)
  72.       send("setInput", index, value == 1)
  73.     end
  74.   },
  75.   { "Get total input count",
  76.     function()
  77.       send("getTotalInputCount")
  78.     end
  79.   },
  80.   { "Get safe active input count",
  81.     function()
  82.       send("getSafeActiveInputs")
  83.     end
  84.   },
  85.   { "Get max active input count",
  86.     function()
  87.       send("getMaxActiveInputs")
  88.     end
  89.   },
  90.  
  91.   { "Save Configuration",
  92.     function()
  93.       send("saveConfiguration")
  94.     end
  95.   },
  96.  
  97.   { "Get health",
  98.     function()
  99.       send("getHealth")
  100.     end
  101.   },
  102.   { "Get hunger",
  103.     function()
  104.       send("getHunger")
  105.     end
  106.   },
  107.   { "Get age",
  108.     function()
  109.       send("getAge")
  110.     end
  111.   },
  112.   { "Get name",
  113.     function()
  114.       send("getName")
  115.     end
  116.   },
  117.   { "Get experience",
  118.     function()
  119.       send("getExperience")
  120.     end
  121.   },
  122.  
  123.   { "Exit",
  124.     function()
  125.       running = false
  126.     end
  127.   }
  128. }
  129.  
  130. function main()
  131.   while running do
  132.     term.clear()
  133.     for i = 1, #commands do
  134.       local command = commands[i]
  135.       io.write(i,". ",command[1],"\n")
  136.     end
  137.     printResponse()
  138.  
  139.     local command = readNumber("command", function(x) return x > 0 and x <= #commands end)
  140.     commands[command][2]()
  141.   end
  142. end
  143.  
  144. local result, reason = pcall(main)
  145. if not result then
  146.   io.stderr:write(reason, "\n")
  147. end
  148.  
  149. event.ignore("modem_message", handleModemMessage)
  150.  
  151. term.clear()
RAW Paste Data