Guest User

PhoneStartup.lua - A.1.2.2

a guest
Oct 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.35 KB | None | 0 0
  1. function printMessage(mode, message)
  2.     if aY >= 15 then
  3.         term.clear()
  4.         term.setCursorPos(1, 1)
  5.     end
  6.  
  7.     if aX == termXsize then
  8.         term.setCursorPos(1, aY+1)
  9.     end
  10.    
  11.     if not mode then mode = "S:"
  12.     elseif mode == 1 then mode = "E:"
  13.     elseif mode == 2 then mode = "Sec:"
  14.     elseif mode == 3 then mode = "MS:"
  15.     elseif mode == 0 then
  16.         term.clear()
  17.         term.setCursorPos(1, 1)
  18.         mode = "S:"
  19.         message = "------------------------------------"
  20.     elseif mode == "-" then mode = ""
  21.     else mode = "nil:"
  22.     end
  23.     aX, aY = term.getCursorPos()
  24.     term.write(tostring(mode)..tostring(message))
  25.     term.setCursorPos(1, aY+1)
  26. end
  27.  
  28. function errorHandle()
  29.  
  30.     if setHost_fail then
  31.         printMessage(1, "Unable to host")
  32.         errorCount = errorCount + 1
  33.     end
  34.  
  35.  
  36.     if userCheck_fail then
  37.         printMessage(1, "userCheck() failed.")
  38.         errorCount = errorCount + 1
  39.     end
  40.  
  41.     if houseServer_fail then
  42.         printMessage(1, "Cannot locate Main Server.")
  43.         errorCount = errorCount + 1
  44.     end
  45.  
  46.     if requestCommands_retry >= 4 then
  47.         printMessage(1, "Cannot fetch commands. No more attempts.")
  48.         errorCount = errorCount + 1
  49.     end
  50.  
  51.     if not loggedIn then
  52.         printMessage(1, "Not logged in.")
  53.         errorCount = errorCount + 1
  54.     end
  55.  
  56.     if stayOn_fail then
  57.         printMessage(1, "Stay On failed.")
  58.         errorCount = errorCount + 1
  59.     end
  60.  
  61.     if identify_fail then
  62.         printMessage(1, "Identification failed.")
  63.         errorCount = errorCount + 1
  64.     end
  65.  
  66. end
  67.  
  68. --[[Action Functions]]--
  69. function act()
  70.     printMessage(nil, "Input a command")
  71.     local command = read()
  72.     rednet.send(houseServer, command, "Command")
  73.     printMessage(nil,"'"..command.."' issued.")
  74.     local id, message, protocol = rednet.receive(10)
  75.     if id and message == "stayOn" then
  76.         stayOn(id)
  77.         local success = true
  78.         printMessage(nil, "Staying On.")
  79.  
  80.     elseif id then
  81.         printMessage(nil, "Succesful")
  82.         printMessage(nil, message)
  83.  
  84.     else
  85.         printMessage(1, "Unsuccesful")
  86.         printMessage(nil, message)
  87.     end
  88. end
  89.  
  90. function stayOn(serverID)
  91.     rednet.send(serverID, true, "Success")
  92.     stayOn_act = true
  93.     while stayOn_act and not stayOn_fail do
  94.         printMessage(nil, "Waiting 15 seconds for reply")
  95.         id, message, protocol = rednet.receive(15)
  96.  
  97.         if message == "actionEnd" then
  98.             printMessage(nil, "actionEnd received. stopping.")
  99.             stayOn_act = false
  100.  
  101.         elseif message == "actionREAD" then
  102.             printMessage(nil, "actionREAD received. reading input")
  103.             stayOn_inputCommand = read()
  104.             printMessage(nil, stayOn_inputCommand.." issued to "..serverID)
  105.             rednet.send(serverID, stayOn_inputCommand, "Success")
  106.            
  107.         elseif id then
  108.             printMessage(3, message)
  109.  
  110.         elseif not id then
  111.             printMessage(1, "No message received, stopping...")
  112.             stayOn_fail = true
  113.         end
  114.     end
  115. end
  116.  
  117. --[[Action Functions end]]--
  118.  
  119. function startup()
  120.     -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
  121.         protocolLv1 = "alpha"
  122.         protocolLv2 = "beta"
  123.         protocolLv3 = "gamma"
  124.         protocolLv4 = "delta"
  125.     -- end
  126.    
  127.     -- Checks for username that will be used as hostname and identification
  128.     local userData = fs.open("/user/localuser", "w")
  129.     local username = userData.readLine()
  130.     userData.close()
  131.     if not username then
  132.         printMessage(nil, "Input Username")
  133.         local newUsername = read()
  134.         userData.write(newUsername)
  135.         userData.close()
  136.         newUsername = username
  137.     end
  138.     -- end
  139.  
  140.     -- local host settings Goes after username is set
  141.         server1_protocol = protocolLv2
  142.         server1_host = tostring(username)
  143.     -- end
  144.  
  145.     -- global variables Must be right after protocol hierarchy list
  146.         errorCount = 0
  147.         checkForCommand_attempts = 0
  148.         logIn_attempts = 0
  149.         retryTime = 2
  150.         modemPos = "back"
  151.         requestCommands_retry = 0
  152.         termXsize, termYsize = term.getSize()
  153.         aX = 1
  154.         aY = 1
  155.         identify_retryCount = 0
  156.         RCD_VERSION = "A.1.2.2"
  157.         getCommands_retry = 0
  158.         identify_tries = 0
  159.     -- end
  160.  
  161.     --peripherals MUST BE FOURTH ROW IN EVER startup() FUNCTION
  162.         term.clear()
  163.         term.setCursorPos(1,1)
  164.         rednet.open(modemPos)
  165.     -- end
  166.  
  167.     --Set host
  168.     modemState = rednet.isOpen(modemPos)
  169.     if not modemState then
  170.         setHost_fail = true
  171.         printMessage("-", tostring(setHost_fail))
  172.     else
  173.         rednet.host(server1_protocol, server1_host)
  174.         setHost_fail = false
  175.         printMessage(nil, "Hosted "..server1_protocol..":"..server1_host)
  176.     end
  177.     -- end
  178.    
  179.     -- Prints current running version
  180.     printMessage(nil, "Running RCD v.'"..RCD_VERSION.."'")
  181.     printMessage(nil, "with '".._VERSION.."'")
  182.     printMessage(nil, "--DiegoG")
  183.     -- end
  184.    
  185.     -- Password login
  186.     local passwordRead = fs.open("/user/localpassword", "r")
  187.     local password = passwordRead.readLine()
  188.     passwordRead.close()
  189.     if password then
  190.         while inputPassword ~= password and passwordRetry ~= 4 do
  191.             printMessage(nil, "Input password, tries: "..passwordRetry.."/3")
  192.             inputPassword = read('*')
  193.             passwordRetry = passwordRetry + 1
  194.         end
  195.         if inputPassword == password then
  196.             loggedIn = true
  197.         else
  198.             return true
  199.         end
  200.     else
  201.         printMessage(nil, "Input a new password")
  202.         local newPassword = read('*')
  203.         local passwordWrite = fs.open("/user/localpassword", "w")
  204.         passwordWrite.write(newPassword)
  205.     end
  206.     -- end
  207.  
  208.     -- static hosts settings MUST BE AFTER THE HOST IS SET IN EVERY startup() FUNCTION
  209.         printMessage(nil, "Looking for Main Server...")
  210.         houseServer = rednet.lookup(protocolLv1, "houseServer")
  211.             if not houseServer then
  212.                 printMessage(1, "Could not find Main Server")
  213.                 houseServer_fail = true
  214.                 return true
  215.             else
  216.                 printMessage(nil, "Main Server's ID: "..tostring(houseServer))
  217.             end
  218.     -- end
  219.  
  220.     -- Identify MUST BE RIGHT BEFORE fetchCommands() CALL
  221.     while not identify_success and identify_tries ~= 3 do
  222.         printMessage(nil, "Registering in Server")
  223.         rednet.send(houseServer, "identity", "Command")
  224.         printMessage(nil, "Waiting for reply")
  225.         local id_identify, message_identify, protocol_identify = rednet.receive(5)
  226.         if id then
  227.             printMessage(nil, "Sending identity data")
  228.             rednet.send(houseServer, username, "Success")
  229.             printMessage(nil, "Successfully registered in Server")
  230.             identify_success = true
  231.             os.sleep(2)
  232.         else
  233.             printMessage(nil, "Unable to register")
  234.             printMessage(nil, "Retrying...")
  235.             identify_tries = identify_tries + 1
  236.             os.sleep(1)
  237.         end
  238.     end
  239.  
  240.     -- commands list MUST BE RIGHT BEFORE errorHandle() CALL
  241.     while not receivedCommands and getCommands_retry ~= 4 do
  242.         rednet.send(houseServer, "request_commands", "Request")
  243.         local id_receivedCommands, message_receivedCommands, protocol_receivedCommands = rednet.receive(5)
  244.         if id then
  245.             receivedCommands = message
  246.         else
  247.             printMessage(1, "Failed to retrieve, retrying...")
  248.             printMessage(1, "Tries: "..getCommands_retry.."/3")
  249.             os.sleep(2)
  250.             getCommands_retry = getCommands_retry + 1
  251.         end
  252.     end
  253.     if getCommands_retry >= 4 then
  254.         printMessage(1, "Unable to retrieve commands.")
  255.         receivedCommands = "No commands available"
  256.     end
  257.     -- end
  258.  
  259.     errorHandle()
  260.         if errorCount == 1 then
  261.         printMessage(nil, "Started with "..errorCount.." error.")
  262.     elseif errorCount == 0 then
  263.         printMessage(nil, "Started with no errors")
  264.     elseif errorCount >= 2 then
  265.         printMessage(nil, "Started with "..errorCount.." errors.")
  266.         end
  267.  
  268. printMessage(nil, "Available commands are: ")
  269. printMessage("-", receivedCommands)
  270.  
  271. end
  272.  
  273. startup_fail = startup()
  274. if startup_fail then
  275.     printMessage(1, "Startup unsuccesful")
  276.     errorHandle()
  277.     os.sleep(4)
  278.     os.shutdown()
  279. end
  280.  
  281. while true do
  282.     act()
  283. end
  284.  
  285. --DiegoG
  286. --Last edit 24/10/2016
  287. --Created on 11/10/2016
Advertisement
Add Comment
Please, Sign In to add comment