Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Reactor Controller 2.5 RAT
- local settings, version = {}, 2.5
- local x,y = term.getSize()
- shell.run("/m1")
- function redraw(title) --Redraws frame and title, does not clear screen
- local title = title or "Reactor Controller "..tostring(version).." RAT"
- --Draw the border
- for i = 1, y do
- printex(i, "|", 1, "noclr")
- printex(i, "|", x, "noclr")
- end
- for i = 1, x do
- printex(1, "-", i, "noclr")
- printex(y, "-", i, "noclr")
- end
- printex(1, title, "center", "noclr")
- end
- function connect() --Allow user to select a reactor controller
- term.clear()
- redraw()
- os.startTimer(1)
- --Get all reactor controllers in range
- local servers = {}
- printex(2, "Scanning...", "center", "noclr")
- rednet.broadcast("r3act0r1d")
- repeat
- local event, p1, p2 = os.pullEvent()
- if event == "rednet_message" then servers[p1] = p2 end
- until event == "timer"
- local sel, go = 1, false
- --Print a list of all the controllers
- repeat
- term.clear()
- redraw()
- printex(2, "Select a Reactor:", "center", "noclr")
- printex(3, " ID - Authorised?", 2, "noclr")
- local pos, count = 3, 0
- for i,data in pairs(servers) do
- pos, count = pos+1, count+1
- local data2;
- --State the login method
- if tonumber(data) then data2 = "M1cr0S Server "..data
- elseif data == "no" then data2 = "Controller Password"
- else data2 = data end
- --Return selected controller
- if sel == count and data ~= "Unauthorised" and go then
- settings[1] = i
- settings[4] = data
- return i
- elseif sel == count then printex(pos, "["..tostring(i).."] - "..data2, 2, "noclr") else printex(pos, " "..tostring(i).." - "..data2, 2, "noclr") end
- end
- --Manage input
- local event, key = os.pullEvent("key")
- if key == 28 then go = true
- elseif key == 208 and sel < count then sel = sel+1
- elseif key == 200 and sel > 1 then sel = sel-1 end
- until key == 28 and settings[4]
- end
- function login() --Check credentials with controller
- --Get username and password
- term.clear()
- redraw()
- printex(2, "Enter Username: ", 2, "noclr")
- settings[2], settings[3] = readex(3, nil, nil, 2, "noclr"), {}
- printex(4, "Enter Password: ", 2, "noclr")
- repeat
- local evt, key = os.pullEvent()
- if key == 14 then table.remove(settings[3])
- elseif evt == "key" then table.insert(settings[3], key) end
- local stars = ""
- for i = 1,#settings[3] do stars = stars.."*" end
- printex(5, stars, 2)
- redraw()
- until key == 28
- --Check credentials
- rednet.send(settings[1], serialize({[1] = "pwdchk", [2] = settings[2], [3] = settings[3]}))
- local id, res = rednet.receive(1)
- --Make sure to display an error if there is one
- local txt = "No connection to server/Unauthorised PC"
- if res == "false" then txt = "Incorrect name or password" elseif res and res ~= "true" then txt = res end
- if res ~= "true" then
- printex(7, txt, 2, "noclr")
- os.sleep(2)
- return false
- end
- --If credentials are correct return true
- return true
- end
- function showHistory() --Retrieve and print history
- --Refresh every 2 seconds
- local timer = os.startTimer(2)
- local id, res;
- repeat
- local evt, p1 = os.pullEvent()
- if p1 == timer then
- tablesend({[1] = "r3act0rctr1r", [2] = 7}, settings[1])
- repeat id, res = rednet.receive(1) until not id or id == settings[1]
- if not id then
- printex(12, "No connection to server/Unauthorised", 2, "noclr")
- return true
- elseif not deserialize(res) then
- printex(12, "Got wrong message from controller", 2, "noclr")
- return true
- end
- term.clear()
- printex(2, table.concat(deserialize(res), "\n"), 2, "noclr")
- redraw("Reactor History - Enter to leave")
- timer = os.startTimer(2)
- end
- until p1 == 28 or p1 == 1
- end
- function sendCmd(cmd) --Send commands to the controller
- local tbl = {[1] = "r3act0rctr1r"}
- if cmd == 2 then
- printex(10, "How many EUs (x1m)?", 2, "noclr")
- local eus;
- repeat eus = readex(11, nil, nil, 2, "noclr") until tonumber(eus)
- tbl[2] = "eu"..tostring(eus)
- --Get EUs after this
- else tbl[2] = cmd end
- --Send and get result
- tablesend(tbl, settings[1])
- local id, res;
- --Fail safe to ensure the right message is received
- repeat id, res = rednet.receive(1) until not id or id == settings[1]
- --Print errors
- if not id and cmd ~= 8 then printex(12, "No connection to server", "center", "noclr")
- elseif res == "Unauthorised" then printex(12, "Controller is Unauthorised", "center", "noclr")
- --Print info if it worked
- elseif res == "true" then
- if cmd == 1 then printex(12, "Reactor Shutdown", "center", "noclr")
- elseif cmd == 3 then printex(12, "Detectors Reset", "center", "noclr")
- elseif cmd == 6 then printex(12, "Reactor Started", "center", "noclr") end
- --Extra info for specific commands
- elseif cmd == 2 and res then
- printex(12, "Reactor shutting down in "..res.." seconds", "center", "noclr")
- elseif cmd == 4 and res then
- printex(12, "Door "..res, "center", "noclr")
- elseif cmd == 5 and res then
- printex(12, "ESU Control "..res, "center", "noclr")
- end
- os.sleep(2)
- end
- function menu() --Allow user to select commands
- local id, res, timer;
- local selection = " "
- local function refreshInfo() --Show info at bottom of UI
- local status = "Reactor:"..res[2]
- if res[1] then status = status.." - Door:"..res[1] end
- if res[3] then status = status.." - Heat:"..res[3] end
- if res[4] then status, timer = status.." - Time:"..tostring(res[4]), os.startTimer(1) end
- redraw("Select an Action")
- printex(y, status, "center", "noclr")
- end
- local function getInfo() --Get info for bottom of UI
- tablesend({[1] = "r3act0rctr1r", [2] = 9}, settings[1])
- repeat id, res = rednet.receive(1) until not id or id == settings[1]
- if not id then
- printex(2, "No Connection to Controller/Unauthorised", 2, "noclr")
- os.sleep(2)
- return "restart"
- end
- res = deserialize(res)
- end
- --Print menu
- if getInfo() == "restart" then return "restart" end
- term.clear()
- refreshInfo()
- printex(2, "[1] Shutdown Reactor\n[2] Generate Xm EUs\n[3] Reset Detectors", 2, "noclr")
- if res[1] then printex(nil, "[4] Toggle Door", 2, "noclr") end
- if res[5] then printex(nil, "[5] Toggle ESU Control", 2, "noclr") end
- printex(nil, "[6] Start Reactor Permanently (unless fault)", 2, "noclr")
- printex(nil, "[7] Show Controller History", 2, "noclr")
- printex(nil, "[8] Cancel", 2, "noclr")
- --Get input and update downtime
- repeat
- local evt, key = os.pullEvent()
- if evt == "key" and key == 14 then selection = " "
- elseif evt == "key" and key == 1 then return "restart"
- elseif evt == "key" and key ~= 28 and key-1 > -1 and key-1 < 9 then selection = key-1 end
- printex(10, tostring(selection), 2, "noclr")
- --Update downtime
- if evt == "timer" and key == timer and res[4] and res[4] > 0 then
- res[4] = res[4]-1
- refreshInfo()
- elseif evt == "timer" and key == timer and res[4] and res[4] <= 0 then
- --Update status if countdown is done
- if getInfo() == "restart" then return "restart" end
- refreshInfo()
- end
- until key == 28 and tonumber(selection)
- --Set 0 to 8 so that program quits when esc is pressed
- if selection == 0 then selection = 8 end
- if selection == 8 then printex(12, "Exiting...", "center", "noclr") end
- if selection == 7 then
- printex(12, "Loading History", "center", "noclr")
- if showHistory()then return "restart" end
- elseif selection and selection ~= " " then sendCmd(selection) end
- if selection == 8 then
- rednet.close(getperipheral("modem"))
- return "restart"
- end
- end
- function run() --Runs the other functions
- rednet.open(getperipheral("modem"))
- --Establish a connection
- repeat settings = {[1] = "pwdchk"} until connect()
- --Get Correct login details
- repeat settings[2], settings[3] = nil, nil until login()
- --Show menu and run commands
- repeat until menu() == "restart"
- end
- repeat run() until false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement