Advertisement
m1cr0man

RC RAT 2.5

Jun 26th, 2012
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.74 KB | None | 0 0
  1. -- Reactor Controller 2.5 RAT
  2. local settings, version = {}, 2.5
  3. local x,y = term.getSize()
  4. shell.run("/m1")
  5.  
  6. function redraw(title)      --Redraws frame and title, does not clear screen
  7.     local title = title or "Reactor Controller "..tostring(version).." RAT"
  8. --Draw the border
  9.     for i = 1, y do
  10.         printex(i, "|", 1, "noclr")
  11.         printex(i, "|", x, "noclr")
  12.     end
  13.     for i = 1, x do
  14.         printex(1, "-", i, "noclr")
  15.         printex(y, "-", i, "noclr")
  16.     end
  17.     printex(1, title, "center", "noclr")
  18. end
  19.  
  20. function connect()          --Allow user to select a reactor controller
  21.     term.clear()
  22.     redraw()
  23.     os.startTimer(1)
  24. --Get all reactor controllers in range
  25.     local servers = {}
  26.     printex(2, "Scanning...", "center", "noclr")
  27.     rednet.broadcast("r3act0r1d")
  28.     repeat
  29.         local event, p1, p2 = os.pullEvent()
  30.         if event == "rednet_message" then servers[p1] = p2 end
  31.     until event == "timer"
  32.     local sel, go = 1, false
  33. --Print a list of all the controllers
  34.     repeat
  35.         term.clear()   
  36.         redraw()
  37.         printex(2, "Select a Reactor:", "center", "noclr")
  38.         printex(3, " ID - Authorised?", 2, "noclr")
  39.         local pos, count = 3, 0
  40.         for i,data in pairs(servers) do
  41.             pos, count = pos+1, count+1
  42.             local data2;
  43.         --State the login method
  44.             if tonumber(data) then data2 = "M1cr0S Server "..data
  45.             elseif data == "no" then data2 = "Controller Password"
  46.             else data2 = data end
  47.         --Return selected controller
  48.             if sel == count and data ~= "Unauthorised" and go then
  49.                 settings[1] = i
  50.                 settings[4] = data
  51.                 return i
  52.             elseif sel == count then printex(pos, "["..tostring(i).."] - "..data2, 2, "noclr") else printex(pos, " "..tostring(i).."  - "..data2, 2, "noclr") end
  53.         end
  54.     --Manage input
  55.         local event, key = os.pullEvent("key")
  56.         if key == 28 then go = true
  57.         elseif key == 208 and sel < count then sel = sel+1
  58.         elseif key == 200 and sel > 1 then sel = sel-1 end
  59.     until key == 28 and settings[4]
  60. end
  61.  
  62. function login()            --Check credentials with controller
  63. --Get username and password
  64.     term.clear()
  65.     redraw()
  66.     printex(2, "Enter Username: ", 2, "noclr")
  67.     settings[2], settings[3] = readex(3, nil, nil, 2, "noclr"), {}
  68.     printex(4, "Enter Password: ", 2, "noclr")
  69.     repeat
  70.         local evt, key = os.pullEvent()
  71.         if key == 14 then table.remove(settings[3])
  72.         elseif evt == "key" then table.insert(settings[3], key) end
  73.         local stars = ""
  74.         for i = 1,#settings[3] do stars = stars.."*" end
  75.         printex(5, stars, 2)
  76.         redraw()
  77.     until key == 28
  78. --Check credentials
  79.     rednet.send(settings[1], serialize({[1] = "pwdchk", [2] = settings[2], [3] = settings[3]}))
  80.     local id, res = rednet.receive(1)
  81. --Make sure to display an error if there is one
  82.     local txt = "No connection to server/Unauthorised PC"
  83.     if res == "false" then txt = "Incorrect name or password" elseif res and res ~= "true" then txt = res end
  84.     if res ~= "true" then
  85.         printex(7, txt, 2, "noclr")
  86.         os.sleep(2)
  87.         return false
  88.     end
  89. --If credentials are correct return true
  90.     return true
  91. end
  92.  
  93. function showHistory()      --Retrieve and print history
  94. --Refresh every 2 seconds
  95.     local timer = os.startTimer(2)
  96.     local id, res;
  97.     repeat
  98.         local evt, p1 = os.pullEvent()
  99.         if p1 == timer then
  100.             tablesend({[1] = "r3act0rctr1r", [2] = 7}, settings[1])
  101.             repeat id, res = rednet.receive(1) until not id or id == settings[1]
  102.             if not id then
  103.                 printex(12, "No connection to server/Unauthorised", 2, "noclr")
  104.                 return true
  105.             elseif not deserialize(res) then
  106.                 printex(12, "Got wrong message from controller", 2, "noclr")
  107.                 return true
  108.             end
  109.             term.clear()
  110.             printex(2, table.concat(deserialize(res), "\n"), 2, "noclr")
  111.             redraw("Reactor History - Enter to leave")
  112.             timer = os.startTimer(2)
  113.         end
  114.     until p1 == 28 or p1 == 1
  115. end
  116.  
  117. function sendCmd(cmd)       --Send commands to the controller
  118.     local tbl = {[1] = "r3act0rctr1r"}
  119.     if cmd == 2 then
  120.         printex(10, "How many EUs (x1m)?", 2, "noclr")
  121.         local eus;
  122.         repeat eus = readex(11, nil, nil, 2, "noclr") until tonumber(eus)
  123.         tbl[2] = "eu"..tostring(eus)
  124.     --Get EUs after this
  125.     else tbl[2] = cmd end
  126. --Send and get result
  127.     tablesend(tbl, settings[1])
  128.     local id, res;
  129. --Fail safe to ensure the right message is received
  130.     repeat id, res = rednet.receive(1) until not id or id == settings[1]
  131. --Print errors
  132.     if not id and cmd ~= 8 then printex(12, "No connection to server", "center", "noclr")
  133.     elseif res == "Unauthorised" then printex(12, "Controller is Unauthorised", "center", "noclr")
  134. --Print info if it worked
  135.     elseif res == "true" then
  136.         if cmd == 1 then printex(12, "Reactor Shutdown", "center", "noclr")
  137.         elseif cmd == 3 then printex(12, "Detectors Reset", "center", "noclr")
  138.         elseif cmd == 6 then printex(12, "Reactor Started", "center", "noclr") end
  139. --Extra info for specific commands
  140.     elseif cmd == 2 and res then
  141.         printex(12, "Reactor shutting down in "..res.." seconds", "center", "noclr")
  142.     elseif cmd == 4 and res then
  143.         printex(12, "Door "..res, "center", "noclr")
  144.     elseif cmd == 5 and res then
  145.         printex(12, "ESU Control "..res, "center", "noclr")
  146.     end
  147.     os.sleep(2)
  148. end
  149.  
  150. function menu()             --Allow user to select commands
  151.     local id, res, timer;
  152.     local selection = " "
  153.     local function refreshInfo()    --Show info at bottom of UI
  154.         local status = "Reactor:"..res[2]
  155.         if res[1] then status = status.." - Door:"..res[1] end
  156.         if res[3] then status = status.." - Heat:"..res[3] end
  157.         if res[4] then status, timer = status.." - Time:"..tostring(res[4]), os.startTimer(1) end
  158.         redraw("Select an Action")
  159.         printex(y, status, "center", "noclr")
  160.     end
  161.     local function getInfo()        --Get info for bottom of UI
  162.         tablesend({[1] = "r3act0rctr1r", [2] = 9}, settings[1])
  163.         repeat id, res = rednet.receive(1) until not id or id == settings[1]
  164.         if not id then
  165.             printex(2, "No Connection to Controller/Unauthorised", 2, "noclr")
  166.             os.sleep(2)
  167.             return "restart"
  168.         end
  169.         res = deserialize(res)
  170.     end
  171. --Print menu
  172.     if getInfo() == "restart" then return "restart" end
  173.     term.clear()
  174.     refreshInfo()
  175.     printex(2, "[1] Shutdown Reactor\n[2] Generate Xm EUs\n[3] Reset Detectors", 2, "noclr")
  176.     if res[1] then printex(nil, "[4] Toggle Door", 2, "noclr") end
  177.     if res[5] then printex(nil, "[5] Toggle ESU Control", 2, "noclr") end
  178.     printex(nil, "[6] Start Reactor Permanently (unless fault)", 2, "noclr")
  179.     printex(nil, "[7] Show Controller History", 2, "noclr")
  180.     printex(nil, "[8] Cancel", 2, "noclr")
  181. --Get input and update downtime
  182.     repeat
  183.         local evt, key = os.pullEvent()
  184.         if evt == "key" and key == 14 then selection = " "
  185.         elseif evt == "key" and key == 1 then return "restart"
  186.         elseif evt == "key" and key ~= 28 and key-1 > -1 and key-1 < 9 then selection = key-1 end
  187.         printex(10, tostring(selection), 2, "noclr")
  188.     --Update downtime
  189.         if evt == "timer" and key == timer and res[4] and res[4] > 0 then
  190.             res[4] = res[4]-1
  191.             refreshInfo()
  192.         elseif evt == "timer" and key == timer and res[4] and res[4] <= 0 then
  193.     --Update status if countdown is done
  194.             if getInfo() == "restart" then return "restart" end
  195.             refreshInfo()
  196.         end
  197.     until key == 28 and tonumber(selection)
  198.     --Set 0 to 8 so that program quits when esc is pressed
  199.     if selection == 0 then selection = 8 end
  200.     if selection == 8 then printex(12, "Exiting...", "center", "noclr") end
  201.     if selection == 7 then
  202.         printex(12, "Loading History", "center", "noclr")
  203.         if showHistory()then return "restart" end
  204.     elseif selection and selection ~= " " then sendCmd(selection) end
  205.     if selection == 8 then
  206.         rednet.close(getperipheral("modem"))
  207.         return "restart"
  208.     end
  209. end
  210.  
  211. function run()              --Runs the other functions
  212.     rednet.open(getperipheral("modem"))
  213. --Establish a connection
  214.     repeat settings = {[1] = "pwdchk"} until connect()
  215. --Get Correct login details
  216.     repeat settings[2], settings[3] = nil, nil until login()
  217. --Show menu and run commands
  218.     repeat until menu() == "restart"
  219. end
  220.  
  221. repeat run() until false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement