Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function setHost()
- modemState = rednet.isOpen(checkForModem_modem)
- if not modemState then
- setHost_fail = true
- printMessage("-", tostring(setHost_fail))
- else
- rednet.host(server1_protocol, server1_host)
- setHost_fail = false
- printMessage(nil, "Succesfully hosted "..server1_protocol..":"..server1_host..".")
- end
- end
- function checkForModem()
- checkForModem_fail = false
- checkForModem_modem = modemPos
- end
- function checkForCommand()
- printMessage(nil, "Waiting for Command, waiting "..commandTimer.." seconds.")
- local id, message, protocol = rednet.receive(commandTimer)
- checkForCommand_attempts = checkForCommand_attempts + 1 --Waits a bit to check for a command again, prints number of attempts and the clock
- os.sleep(sleepTimer)
- cleanTerm()
- printMessage(nil, "Command check Attempts: "..checkForCommand_attempts)
- return message, id
- end
- function printMessage(mode, message) --For fancy messages
- if aY == termYsize - 1 then
- term.clear()
- term.setCursorPos(1, 1)
- end
- if not mode then mode = "[Server]: "
- elseif mode == 0 then mode = "[Server]:" message = "-------------------------------------------------------------------------------------------"
- elseif mode == 1 then mode = "[Error]: "
- elseif mode == 2 then mode = "[Door]: "
- elseif mode == 3 then mode = "[Lights]: "
- 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 cleanTerm()
- local bX, bY = term.getCursorPos()
- local runningtime = os.clock()
- term.setCursorPos(1, termYsize - 1)
- printMessage(nil, "Current computer runtime: "..runningtime.." ")
- term.setCursorPos(bX, bY)
- end
- function errorHandle() --Just to ennumarate the errors
- if setHost_fail then
- printMessage(1, "The function setHost() failed.")
- errorCount = errorCount + 1
- end
- if checkForModem_fail then
- printMessage(1, "The function checkForModem() failed.")
- errorCount = errorCount + 1
- end
- end
- --[[Action Functions]]--
- function action_ping()
- rednet.send(senderID, "pong!", "Success")
- end
- function action_time()
- local gameTime = os.time()
- local gameTime_floor = math.floor(gameTime)
- rednet.send(senderID, gameTime_floor, "Success")
- end
- function action_door(timer)
- redstone.setOutput("front", true)
- rednet.send(senderID, "Door is open", "stayOn")
- printMessage(2, "Leaving Door open for "..timer.." seconds")
- rednet.send(senderID, "Door open for "..timer.." seconds", "stayOn")
- os.sleep(tonumber(timer))
- printMessage(2, "Timer depleted, closing door...")
- rednet.send(senderID, "Closing door...", "stayOn")
- redstone.setOutput("front", false)
- printMessage(2, "Door closed.")
- rednet.send(senderID, "Door closed.", "stayOn")
- os.sleep(1)
- rednet.send(senderID, "actionEnd", "stayOn")
- end
- function action_lights(lights)
- if lights == 1 then
- local isOn = redstone.getOutput("back")
- if isOn then
- redstone.setOutput("back", false)
- printMessage(3, "Light EnchantTop turned off")
- else
- redstone.setOutput("back", true)
- printMessage(3, "Light EnchantTop turned on")
- end
- end
- end
- function action_unknown()
- rednet.send(senderID, "Unknown Command", "Failure")
- end
- --[[Action Functions end]]--
- if setHost_fail or checkForModem_fail then
- startup_fail = true
- end
- function senderID_username(mode, id, username)
- if mode == "register" then
- if not senderID_A or id == senderID_A then
- local senderID_A = id
- local senderUSER_A = username
- elseif not senderID_B or id == senderID_B then
- local senderID_B = id
- local senderUSER_B = username
- elseif not senderID_C or id == senderID_C then
- local senderID_C = id
- local senderUSER_C = username
- elseif not senderID_D or id == senderID_D then
- local senderID_D = id
- local senderUSER_D = username
- elseif not senderID_E or id == senderID_E then
- local senderID_E = id
- local senderUSER_E = username
- else
- printMessage(1, "Cannot store anymore IDs")
- return false
- end
- elseif not mode then
- if id == senderID_A then
- return senderUSER_A
- elseif id == senderID_B then
- return senderUSER_B
- elseif id == senderID_C then
- return senderUSER_C
- elseif id == senderID_D then
- return senderUSER_D
- elseif id == senderID_E then
- return senderUSER_E
- else
- printMessage(1, "Unknown ID, returning normal ID")
- return id
- end
- end
- end
- function checkPerms(id)
- if id == 1 then
- printMessage(nil, senderID.."'s privileges has been checked and command was accepted.")
- return true
- else
- printMessage(nil, senderID.."'s privileges has been checked and command was denied.")
- return false
- end
- end
- function startup()
- -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
- protocolLv1 = "alpha"
- protocolLv2 = "beta"
- protocolLv3 = "gamma"
- protocolLv4 = "delta"
- -- end
- -- local host settings
- server1_protocol = protocolLv1
- server1_host = "houseServer"
- -- end
- -- static hosts settings
- --nil
- -- end
- -- global variables
- errorCount = 0
- checkForCommand_attempts = 0
- sleepTimer = 1
- commandTimer = 20
- modemPos = "top"
- termXsize = 71
- termYsize = 40
- aX, aY = term.getCursorPos()
- SERVER_VERSION = "A.1.2.9"
- -- end
- --peripherals
- monitor = peripheral.wrap("left")
- term.redirect(monitor)
- term.clear()
- term.setCursorPos(1,1)
- checkForModem()
- rednet.open(modemPos)
- -- end
- errorHandle()
- if errorCount == 1 then
- printMessage(nil, "Succesfully started with "..errorCount.." error.")
- elseif errorCount == 0 then
- printMessage(nil, "Succesfully started with no errors")
- elseif errorCount >= 2 then
- printMessage(nil, "Succesfully started with "..errorCount.." errors.")
- end
- setHost()
- printMessage(nil, "Running Server version '"..SERVER_VERSION.."' with '".._VERSION.."'")
- serverStarted = true
- end
- if not startup_fail then
- startup()
- end
- while true do --Main loop
- received_command, senderID = checkForCommand()
- if received_command == "ping" then
- printMessage(nil, "Received command: 'ping'")
- printMessage(nil, "Executing function: 'action_ping()' for "..senderID_username(nil, senderID, nil))
- action_ping()
- printMessage(0, nil)
- elseif received_command == "request_commands" then
- printMessage(nil, "Received command: 'request_commands'")
- printMessage(nil, "Sending command list to: "..senderID_username(nil, senderID))
- rednet.send(senderID, "ping, time, door \n shutdown, reboot", "CommandList") --Up to three commands per line
- printMessage(0, nil)
- elseif received_command == "time" then
- printMessage(nil, "Received command: 'time'")
- printMessage(nil, "Executing function: 'action_time()' for "..senderID_username(nil, senderID, nil))
- action_time()
- printMessage(0, nil)
- elseif received_command == "identity" then
- printMessage(nil, "Received command: 'identity'")
- rednet.send(senderID, true, "Success")
- printMessage(nil, "Waiting 5 seconds for identity from: "..senderID)
- local user_id, user_message, user_protocol = rednet.receive(5)
- if user_id then
- printMessage(nil, "Storing identity data for: "..senderID)
- senderID_username("register", senderID, user_message)
- else
- printMessage(1, "Half Command")
- end
- elseif received_command == "door" then
- printMessage(nil, "Received command: 'door'")
- rednet.send(senderID, "stayOn", "stayOn")
- printMessage(nil, "stayOn request issued. Waiting 10 seconds for response")
- idDOOR, messageDOOR, protocolDOOR = rednet.receive(10)
- if idDOOR then
- rednet.send(senderID, "Please input a timer", "stayOn")
- os.sleep(1)
- rednet.send(senderID, "actionREAD", "stayOn")
- printMessage(nil, "Waiting for input")
- local idDOOR1, messageDOOR1, protocolDOOR1 = rednet.receive(10)
- if idDOOR1 then
- printMessage(nil, "Executing function: 'action_door()' for "..senderID_username(nil, senderID, nil).." with "..messageDOOR1.." seconds timer")
- action_door(messageDOOR1)
- printMessage(0, nil)
- end
- else
- printMessage(1, "Half Command")
- end
- elseif received_command == "shutdown" then
- printMessage(nil, "Received command: 'shutdown'")
- local canDo = checkPerms(senderID)
- if canDo then
- printMessage(nil, "Shutting down server...")
- rednet.send(senderID, 1, "Success")
- os.sleep(2)
- term.clear()
- os.shutdown()
- else
- printMessage(1, "Could not verify "..senderID_username(nil, senderID, nil))
- rednet.send(senderID, 0, "Failure")
- end
- elseif received_command == "reboot" then
- printMessage(nil, "Received command: 'reboot'")
- local canDo = checkPerms(senderID)
- if canDo then
- printMessage(nil, "Rebooting server...")
- rednet.send(senderID, "Rebooting server...", "Success")
- os.sleep(2)
- term.clear()
- os.reboot()
- else
- printMessage(1, "Could not verify "..senderID_username(nil, senderID, nil))
- rednet.send(senderID, "Could not verify perms", "Failure")
- end
- elseif received_command == "lights" then
- printMessage(nil, "Received command: 'lights'")
- rednet.send(senderID, "stayOn", "stayOn")
- printMessage(nil, "stayOn request issued. Waiting 10 seconds for response")
- local idLights1, messageLights1, protocolLights1 = rednet.receive(10)
- if idLights1 then
- rednet.send(senderID, "Please input which light", "stayOn")
- os.sleep(1)
- rednet.send(senderID, "EnchantTop")
- os.sleep(1)
- rednet.send(senderID, "actionREAD", "stayOn")
- printMessage(nil, "Waiting for input")
- local idLights2, messageLights2, protocolLights2 = rednet.receive(10)
- if idLights2 then
- printMessage(nil, "Executing function: 'action_lights()' for "..senderID_username(nil, senderID, nil).." to "..messageLights2.." light ")
- local lights_success = action_lights(messageLights2)
- if lights_success then
- printMessage(nil, "Function 'action_lights()' returned successful")
- rednet.send(senderID, "Success", "Success")
- else
- printMessage(nil, "Function 'action_lights()' returned failure")
- rednet.send(senderID, "Fail", "Failure")
- printMessage(0, nil)
- end
- else
- printMessage(1, "Half Command")
- end
- else
- printMessage(1, "Half Command")
- end
- elseif received_command or senderID then
- printMessage(1, "A command received, but it was invalid. Command: "..tostring(received_command))
- action_unknown()
- else
- printMessage(nil, "No command was received, resting for "..sleepTimer.." seconds")
- printMessage(0, nil)
- end
- end
- --DiegoG
- --Last edit 23/10/2016
- --Created on 11/10/2016
Advertisement
Add Comment
Please, Sign In to add comment