Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function printMessage(mode, message)
- if aY >= 15 then
- term.clear()
- term.setCursorPos(1, 1)
- end
- if aX == termXsize then
- term.setCursorPos(1, aY+1)
- end
- if not mode then mode = "S:"
- elseif mode == 1 then mode = "E:"
- elseif mode == 2 then mode = "Sec:"
- elseif mode == 3 then mode = "MS:"
- elseif mode == 0 then
- term.clear()
- term.setCursorPos(1, 1)
- mode = "S:"
- message = "------------------------------------"
- elseif mode == "-" then mode = ""
- else mode = "nil:"
- end
- aX, aY = term.getCursorPos()
- term.write(tostring(mode)..tostring(message))
- term.setCursorPos(1, aY+1)
- end
- function errorHandle()
- if setHost_fail then
- printMessage(1, "Unable to host")
- errorCount = errorCount + 1
- end
- if userCheck_fail then
- printMessage(1, "userCheck() failed.")
- errorCount = errorCount + 1
- end
- if houseServer_fail then
- printMessage(1, "Cannot locate Main Server.")
- errorCount = errorCount + 1
- end
- if requestCommands_retry >= 4 then
- printMessage(1, "Cannot fetch commands. No more attempts.")
- errorCount = errorCount + 1
- end
- if not loggedIn then
- printMessage(1, "Not logged in.")
- errorCount = errorCount + 1
- end
- if stayOn_fail then
- printMessage(1, "Stay On failed.")
- errorCount = errorCount + 1
- end
- if identify_fail then
- printMessage(1, "Identification failed.")
- errorCount = errorCount + 1
- end
- end
- --[[Action Functions]]--
- function act()
- printMessage(nil, "Input a command")
- local command = read()
- rednet.send(houseServer, command, "Command")
- printMessage(nil,"'"..command.."' issued.")
- local id, message, protocol = rednet.receive(10)
- if id and message == "stayOn" then
- stayOn(id)
- local success = true
- printMessage(nil, "Staying On.")
- elseif id then
- printMessage(nil, "Succesful")
- printMessage(nil, message)
- else
- printMessage(1, "Unsuccesful")
- printMessage(nil, message)
- end
- end
- function stayOn(serverID)
- rednet.send(serverID, true, "Success")
- stayOn_act = true
- while stayOn_act and not stayOn_fail do
- printMessage(nil, "Waiting 15 seconds for reply")
- id, message, protocol = rednet.receive(15)
- if message == "actionEnd" then
- printMessage(nil, "actionEnd received. stopping.")
- stayOn_act = false
- elseif message == "actionREAD" then
- printMessage(nil, "actionREAD received. reading input")
- stayOn_inputCommand = read()
- printMessage(nil, stayOn_inputCommand.." issued to "..serverID)
- rednet.send(serverID, stayOn_inputCommand, "Success")
- elseif id then
- printMessage(3, message)
- elseif not id then
- printMessage(1, "No message received, stopping...")
- stayOn_fail = true
- end
- end
- end
- --[[Action Functions end]]--
- function startup()
- -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
- protocolLv1 = "alpha"
- protocolLv2 = "beta"
- protocolLv3 = "gamma"
- protocolLv4 = "delta"
- -- end
- -- Checks for username that will be used as hostname and identification
- local userData = fs.open("/user/localuser", "w")
- local username = userData.readLine()
- userData.close()
- if not username then
- printMessage(nil, "Input Username")
- local newUsername = read()
- userData.write(newUsername)
- userData.close()
- newUsername = username
- end
- -- end
- -- local host settings Goes after username is set
- server1_protocol = protocolLv2
- server1_host = tostring(username)
- -- end
- -- global variables Must be right after protocol hierarchy list
- errorCount = 0
- checkForCommand_attempts = 0
- logIn_attempts = 0
- retryTime = 2
- modemPos = "back"
- requestCommands_retry = 0
- termXsize, termYsize = term.getSize()
- aX = 1
- aY = 1
- identify_retryCount = 0
- RCD_VERSION = "A.1.2.2"
- getCommands_retry = 0
- identify_tries = 0
- -- end
- --peripherals MUST BE FOURTH ROW IN EVER startup() FUNCTION
- term.clear()
- term.setCursorPos(1,1)
- rednet.open(modemPos)
- -- end
- --Set host
- modemState = rednet.isOpen(modemPos)
- if not modemState then
- setHost_fail = true
- printMessage("-", tostring(setHost_fail))
- else
- rednet.host(server1_protocol, server1_host)
- setHost_fail = false
- printMessage(nil, "Hosted "..server1_protocol..":"..server1_host)
- end
- -- end
- -- Prints current running version
- printMessage(nil, "Running RCD v.'"..RCD_VERSION.."'")
- printMessage(nil, "with '".._VERSION.."'")
- printMessage(nil, "--DiegoG")
- -- end
- -- Password login
- local passwordRead = fs.open("/user/localpassword", "r")
- local password = passwordRead.readLine()
- passwordRead.close()
- if password then
- while inputPassword ~= password and passwordRetry ~= 4 do
- printMessage(nil, "Input password, tries: "..passwordRetry.."/3")
- inputPassword = read('*')
- passwordRetry = passwordRetry + 1
- end
- if inputPassword == password then
- loggedIn = true
- else
- return true
- end
- else
- printMessage(nil, "Input a new password")
- local newPassword = read('*')
- local passwordWrite = fs.open("/user/localpassword", "w")
- passwordWrite.write(newPassword)
- end
- -- end
- -- static hosts settings MUST BE AFTER THE HOST IS SET IN EVERY startup() FUNCTION
- printMessage(nil, "Looking for Main Server...")
- houseServer = rednet.lookup(protocolLv1, "houseServer")
- if not houseServer then
- printMessage(1, "Could not find Main Server")
- houseServer_fail = true
- return true
- else
- printMessage(nil, "Main Server's ID: "..tostring(houseServer))
- end
- -- end
- -- Identify MUST BE RIGHT BEFORE fetchCommands() CALL
- while not identify_success and identify_tries ~= 3 do
- printMessage(nil, "Registering in Server")
- rednet.send(houseServer, "identity", "Command")
- printMessage(nil, "Waiting for reply")
- local id_identify, message_identify, protocol_identify = rednet.receive(5)
- if id then
- printMessage(nil, "Sending identity data")
- rednet.send(houseServer, username, "Success")
- printMessage(nil, "Successfully registered in Server")
- identify_success = true
- os.sleep(2)
- else
- printMessage(nil, "Unable to register")
- printMessage(nil, "Retrying...")
- identify_tries = identify_tries + 1
- os.sleep(1)
- end
- end
- -- commands list MUST BE RIGHT BEFORE errorHandle() CALL
- while not receivedCommands and getCommands_retry ~= 4 do
- rednet.send(houseServer, "request_commands", "Request")
- local id_receivedCommands, message_receivedCommands, protocol_receivedCommands = rednet.receive(5)
- if id then
- receivedCommands = message
- else
- printMessage(1, "Failed to retrieve, retrying...")
- printMessage(1, "Tries: "..getCommands_retry.."/3")
- os.sleep(2)
- getCommands_retry = getCommands_retry + 1
- end
- end
- if getCommands_retry >= 4 then
- printMessage(1, "Unable to retrieve commands.")
- receivedCommands = "No commands available"
- end
- -- end
- errorHandle()
- if errorCount == 1 then
- printMessage(nil, "Started with "..errorCount.." error.")
- elseif errorCount == 0 then
- printMessage(nil, "Started with no errors")
- elseif errorCount >= 2 then
- printMessage(nil, "Started with "..errorCount.." errors.")
- end
- printMessage(nil, "Available commands are: ")
- printMessage("-", receivedCommands)
- end
- startup_fail = startup()
- if startup_fail then
- printMessage(1, "Startup unsuccesful")
- errorHandle()
- os.sleep(4)
- os.shutdown()
- end
- while true do
- act()
- end
- --DiegoG
- --Last edit 24/10/2016
- --Created on 11/10/2016
Advertisement
Add Comment
Please, Sign In to add comment