Guest User

RCDStartup.lua

a guest
Oct 23rd, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.16 KB | None | 0 0
  1. function setHost()
  2. modemState = rednet.isOpen(checkForModem_modem)
  3. if not modemState then
  4.     setHost_fail = true
  5.     printMessage("-", tostring(setHost_fail))
  6. else
  7.     rednet.host(server1_protocol, server1_host)
  8.     setHost_fail = false
  9.     printMessage(nil, "Hosted "..server1_protocol..":"..server1_host)
  10. end
  11. end
  12.  
  13. function userCheck() --Logs in an user, then returns the username to be used as the hostname
  14. local isRegistered = fs.exists("/user/localuser")
  15.     if not isRegistered then
  16.         fs.makeDir("/user/")
  17.         local userData = fs.open("/user/localuser", "w")
  18.             if not userData then userCheck_fail = true
  19.             else
  20.                 printMessage(nil, "Input Username")
  21.                 local newUsername = read()
  22.                 userData.write(newUsername)
  23.                 userData.close()
  24.                 return newUsername
  25.             end
  26.     elseif isRegistered then
  27.         local userData = fs.open("/user/localuser", "r")
  28.         local username = userData.readLine()
  29.         userData.close()
  30.         return username
  31.     end
  32. end
  33.  
  34. function logIn() --Fancy security measures
  35.     local passwordExists = fs.exists("/user/localpassword")
  36.     if passwordExists then
  37.         local passwordRead = fs.open("/user/localpassword", "r")
  38.         local password = passwordRead.readLine()
  39.         passwordRead.close()
  40.         printMessage(nil, "Input password")
  41.         inputPassword = read('*')
  42.         if inputPassword == password then
  43.             return true
  44.         else
  45.             logIn_retry()
  46.         end
  47.     else
  48.         local passwordWrite = fs.open("/user/localpassword", "w")
  49.         printMessage(nil, "Input a new password")
  50.         newPassword = read()
  51.         passwordWrite.write(newPassword)
  52.         printMessage(nil, "Password saved. Rebooting...")
  53.         os.sleep(1)
  54.         os.reboot()
  55.     end
  56. end
  57.  
  58. function logIn_retry()
  59.     if logIn_attempts ~= 3  then
  60.         logIn_attempts = logIn_attempts + 1
  61.         printMessage(2, "Login failed, retry. "..logIn_attempts.."/3")
  62.         logIn()
  63.     else
  64.         if retryTime >= 51 then retryTime = 50 end
  65.         printMessage(2, "Login failed, tried too much. try again in "..retryTime.."s")
  66.         printMessage(2, "Waiting "..retryTime.." seconds to retry.")
  67.         local bX, bY = term.getCursorPos()
  68.         retryTime_count = retryTime
  69.             while retryTime_count ~= 0 do
  70.                 retryTime_count = retryTime_count - 1
  71.                 os.sleep(1)
  72.                 term.setCursorPos(bX, bY - 1)
  73.                 printMessage(2, "Waiting "..retryTime_count.." seconds to retry.    ")
  74.             end
  75.         retryTime = retryTime + retryTime
  76.         loggedIn = logIn()
  77.     end
  78.  
  79. end
  80.  
  81. function checkForModem()
  82.     checkForModem_fail = false
  83.     checkForModem_modem = modemPos
  84. end
  85.  
  86. function printMessage(mode, message)
  87. if aY >= 15 then
  88.     term.clear()
  89.     term.setCursorPos(1, 1)
  90. end
  91. if aX == termXsize then
  92.     term.setCursorPos(1, aY+1)
  93. end
  94.     if not mode then mode = "S:"
  95.     elseif mode == 1 then mode = "E:"
  96.     elseif mode == 2 then mode = "Sec:"
  97.     elseif mode == 3 then mode = "MS:"
  98.     elseif mode == 0 then
  99.         term.clear()
  100.         term.setCursorPos(1, 1)
  101.         mode = "S:"
  102.         message = "------------------------------------"
  103.     elseif mode == "-" then mode = ""
  104.     else mode = "nil:"
  105.     end
  106.     aX, aY = term.getCursorPos()
  107.     term.write(tostring(mode)..tostring(message))
  108.     term.setCursorPos(1, aY+1)
  109. end
  110.  
  111. function errorHandle()
  112.  
  113. if setHost_fail then
  114. printMessage(1, "setHost() failed.")
  115. errorCount = errorCount + 1
  116. end
  117.  
  118. if checkForModem_fail then
  119. printMessage(1, "checkForModem() failed.")
  120. errorCount = errorCount + 1
  121. end
  122.  
  123. if userCheck_fail then
  124. printMessage(1, "userCheck() failed.")
  125. errorCount = errorCount + 1
  126. end
  127.  
  128. if houseServer_fail then
  129. printMessage(1, "Cannot locate Main Server.")
  130. errorCount = errorCount + 1
  131. end
  132.  
  133. if not receivedCommands_success then
  134. printMessage(1, "Nothing received, Server busy or closed.")
  135. errorCount = errorCount + 1
  136. end
  137.  
  138. if requestCommands_retry >= 4 then
  139. printMessage(1, "Cannot fetch commands. No more attempts.")
  140. errorCount = errorCount + 1
  141. end
  142.  
  143. if not loggedIn then
  144. printMessage(1, "Not logged in.")
  145. errorCount = errorCount + 1
  146. end
  147.  
  148. if stayOn_fail then
  149. printMessage(1, "Stay On failed.")
  150. errorCount = errorCount + 1
  151. end
  152.  
  153. if identify_fail then
  154. printMessage(1, "Identification failed.")
  155. errorCount = errorCount + 1
  156. end
  157.  
  158. end
  159.  
  160. --[[Action Functions]]--
  161.  
  162. function requestCommands()
  163.     rednet.send(houseServer, request_commands, "Request")
  164.     local id, message, protocol = rednet.receive(5)
  165.         if id then
  166.             return message, true
  167.         else
  168.             return "No commands available", false
  169.         end
  170. end
  171.  
  172. function issueCommand(command)
  173.     rednet.send(houseServer, command, "Command")
  174.     local id, message, protocol = rednet.receive(20)
  175.         if id and message == "stayOn" then
  176.             stayOn(id)
  177.             return message, true
  178.         elseif id then
  179.             return message, true
  180.         else
  181.             return message, false
  182.         end
  183. end
  184.  
  185. function inputCommand()
  186.     printMessage(nil, "Input a command")
  187.     local command = read()
  188.     if command == shutdown then
  189.         printMessage(nil, "Command: 'shutdown' received.")
  190.         printMessage(nil, "Shutting down...")
  191.         os.sleep(2)
  192.         os.shutdown()
  193.     else
  194.         printMessage(nil,"'"..command.."' issued.")
  195.         return command
  196.     end
  197. end
  198.  
  199. function act()
  200.     local command = inputCommand()
  201.     local reply, success = issueCommand(command)
  202.         if success then
  203.             printMessage(3, message)
  204.         else
  205.             printMessage(1, "Unsuccesful")
  206.         end
  207. end
  208.  
  209. function stayOn(serverID)
  210.     rednet.send(serverID, true, "Success")
  211.     stayOn_act = true
  212.     while stayOn_act and not stayOn_fail do
  213.         printMessage(nil, "Waiting 15 seconds for reply")
  214.         id, message, protocol = rednet.receive(15)
  215.  
  216.         if message == "actionEnd" then
  217.             printMessage(nil, "actionEnd received. stopping.")
  218.             stayOn_act = false
  219.  
  220.         elseif message == "actionREAD" then
  221.             printMessage(nil, "actionREAD received. reading input")
  222.             stayOn_inputCommand = read()
  223.             printMessage(nil, stayOn_inputCommand.." issued to "..serverID)
  224.             rednet.send(serverID, stayOn_inputCommand, "Success")
  225.            
  226.         elseif id then
  227.             printMessage(3, message)
  228.  
  229.         elseif not id then
  230.             printMessage(1, "No message received, stopping...")
  231.             stayOn_fail = true
  232.         end
  233.     end
  234. end
  235.  
  236. --[[Action Functions end]]--
  237.  
  238. function fetchCommands()
  239.     printMessage(nil, "Fetching commands...")
  240.     receivedCommands, receivedCommands_success = requestCommands()
  241.     if receivedCommands_success then
  242.         return true
  243.     else
  244.         return false
  245.     end
  246. end
  247.  
  248. if not setHost_fail or checkForModem_fail then
  249. startup_fail = true
  250. end
  251.  
  252. function identify()
  253.     printMessage(nil, "Registering in Server")
  254.     rednet.send(houseServer, "identity", "Command")
  255.     printMessage(nil, "Waiting for reply")
  256.     local id, message, protocol = rednet.receive(10)
  257.     if id then
  258.         printMessage(nil, "Sending identity data")
  259.         rednet.send(houseServer, username, "Success")
  260.     else
  261.        
  262.         identify_retry()
  263.     end
  264. end
  265.  
  266. function identify_retry()
  267.     printMessage(1, "Did not receive reply in time")
  268.     identify_retryCount = identify_retryCount + 1
  269.     if identify_retryCount ~= 4 then
  270.         printMessage(nil, "Trying again...")
  271.         identify()
  272.     else
  273.         printMessage(1, "Tried too many times, gave up.")
  274.         identify_fail = true
  275.     end
  276. end
  277.  
  278.  
  279. function startup()
  280.     -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
  281.         protocolLv1 = "alpha"
  282.         protocolLv2 = "beta"
  283.         protocolLv3 = "gamma"
  284.         protocolLv4 = "delta"
  285.     -- end
  286.  
  287.     -- local host settings MUST BE SECOND ROW IN EVERY startup() FUNCTION
  288.         server1_protocol = protocolLv2
  289.         server1_host = tostring(userCheck())
  290.     -- end
  291.  
  292.     -- global variables MUST BE THIRD ROW IN EVERY startup() FUNCTION
  293.         errorCount = 0
  294.         checkForCommand_attempts = 0
  295.         logIn_attempts = 0
  296.         retryTime = 2
  297.         modemPos = "back"
  298.         requestCommands_retry = 0
  299.         receivedCommands = nil
  300.         termXsize, termYsize = term.getSize()
  301.         aX = 1
  302.         aY = 1
  303.         identify_retryCount = 0
  304.         RCD_VERSION = "A.1.1.7"
  305.         request_commands = "request_commands"
  306.     -- end
  307.  
  308.     --peripherals MUST BE FOURTH ROW IN EVER startup() FUNCTION
  309.         term.clear()
  310.         term.setCursorPos(1,1)
  311.         checkForModem()
  312.         rednet.open(modemPos)
  313.     -- end
  314.  
  315. setHost()
  316. printMessage(nil, "Running RCD v.'"..RCD_VERSION.."'")
  317. printMessage(nil, "with '".._VERSION.."'")
  318. loggedIn = logIn()
  319.  
  320.     -- static hosts settings MUST BE AFTER setHost() CALL IN EVERY startup() FUNCTION
  321.         printMessage(nil, "Looking for Main Server...")
  322.         houseServer = rednet.lookup(protocolLv1, "houseServer")
  323.             if not houseServer then
  324.                 printMessage(1, "Could not find Main Server")
  325.                 houseServer_fail = true
  326.                 return true
  327.             else
  328.                 printMessage(nil, "Main Server's ID: "..tostring(houseServer))
  329.             end
  330.     -- end
  331.  
  332.     -- Identify MUST BE RIGHT BEFORE fetchCommands() CALL
  333.         identify()
  334.     -- commands list MUST BE RIGHT BEFORE errorHandle() CALL
  335.     fetchCommands()
  336.     while not receivedCommands_success and requestCommands_retry ~= 3 do
  337.             requestCommands_retry = requestCommands_retry + 1
  338.             printMessage(1, "Failed. Retrying. "..requestCommands_retry.. "/3")
  339.             fetchCommands()
  340.     end
  341.     -- end
  342.  
  343.     errorHandle()
  344.         if errorCount == 1 then
  345.         printMessage(nil, "Started with "..errorCount.." error.")
  346.     elseif errorCount == 0 then
  347.         printMessage(nil, "Started with no errors")
  348.     elseif errorCount >= 2 then
  349.         printMessage(nil, "Started with "..errorCount.." errors.")
  350.         end
  351.  
  352. printMessage(nil, "Available commands are: ")
  353. printMessage("-", receivedCommands)
  354.  
  355. end
  356.  
  357. startup_fail = startup()
  358. if startup_fail then
  359.     printMessage(1, "Startup unsuccesful")
  360.     os.sleep(2)
  361.     os.shutdown()
  362. end
  363.  
  364. while true do
  365.     act()
  366.     errorHandle()
  367. end
  368.    
  369. --DiegoG
  370. --Base RCD OS A.1.1.7
  371. --23/10/2016
Advertisement
Add Comment
Please, Sign In to add comment