Guest User

ServerStartup.lua

a guest
Oct 23rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.57 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, "Succesfully hosted "..server1_protocol..":"..server1_host..".")
  10. end
  11. end
  12.  
  13. function checkForModem()
  14.     checkForModem_fail = false
  15.     checkForModem_modem = modemPos
  16. end
  17.  
  18. function checkForCommand()
  19. printMessage(nil, "Waiting for Command, waiting "..commandTimer.." seconds.")
  20. local id, message, protocol = rednet.receive(commandTimer)
  21. checkForCommand_attempts = checkForCommand_attempts + 1 --Waits a bit to check for a command again, prints number of attempts and the clock
  22. os.sleep(sleepTimer)
  23. cleanTerm()
  24. printMessage(nil, "Command check Attempts: "..checkForCommand_attempts)
  25. return message, id
  26. end
  27.  
  28. function printMessage(mode, message) --For fancy messages
  29. if aY == termYsize - 1 then
  30. term.clear()
  31. term.setCursorPos(1, 1)
  32. end
  33. if not mode then mode = "[Server]: "
  34. elseif mode == 0 then mode = "[Server]:" message = "-------------------------------------------------------------------------------------------"
  35. elseif mode == 1 then mode = "[Error]: "
  36. elseif mode == 2 then mode = "[Door]: "
  37. elseif mode == 3 then mode = "[Lights]: "
  38. elseif mode == "-" then mode = ""
  39. else mode = "nil:"
  40. end
  41. aX, aY = term.getCursorPos()
  42. term.write(tostring(mode)..tostring(message))
  43. term.setCursorPos(1, aY+1)
  44. end
  45.  
  46. function cleanTerm()
  47. local bX, bY = term.getCursorPos()
  48. local runningtime = os.clock()
  49. term.setCursorPos(1, termYsize - 1)
  50. printMessage(nil, "Current computer runtime: "..runningtime.."                                                      ")
  51. term.setCursorPos(bX, bY)
  52. end
  53.  
  54. function errorHandle() --Just to ennumarate the errors
  55.  
  56. if setHost_fail then
  57. printMessage(1, "The function setHost() failed.")
  58. errorCount = errorCount + 1
  59. end
  60.  
  61. if checkForModem_fail then
  62. printMessage(1, "The function checkForModem() failed.")
  63. errorCount = errorCount + 1
  64. end
  65.  
  66. end
  67.  
  68. --[[Action Functions]]--
  69.     function action_ping()
  70.         rednet.send(senderID, "pong!", "Success")
  71.     end
  72.  
  73.     function action_time()
  74.         local gameTime = os.time()
  75.         local gameTime_floor = math.floor(gameTime)
  76.         rednet.send(senderID, gameTime_floor, "Success")
  77.     end
  78.  
  79.     function action_door(timer)
  80.         redstone.setOutput("front", true)
  81.         rednet.send(senderID, "Door is open", "stayOn")
  82.         printMessage(2, "Leaving Door open for "..timer.." seconds")
  83.         rednet.send(senderID, "Door open for "..timer.." seconds", "stayOn")
  84.         os.sleep(tonumber(timer))
  85.         printMessage(2, "Timer depleted, closing door...")
  86.         rednet.send(senderID, "Closing door...", "stayOn")
  87.         redstone.setOutput("front", false)
  88.         printMessage(2, "Door closed.")
  89.         rednet.send(senderID, "Door closed.", "stayOn")
  90.         os.sleep(1)
  91.         rednet.send(senderID, "actionEnd", "stayOn")
  92.     end
  93.    
  94. function action_lights(lights)
  95.     if lights == 1 then
  96.         local isOn = redstone.getOutput("back")
  97.         if isOn then
  98.             redstone.setOutput("back", false)
  99.             printMessage(3, "Light EnchantTop turned off")
  100.         else
  101.             redstone.setOutput("back", true)
  102.             printMessage(3, "Light EnchantTop turned on")
  103.         end
  104.     end
  105. end
  106.  
  107.     function action_unknown()
  108.         rednet.send(senderID, "Unknown Command", "Failure")
  109.     end
  110. --[[Action Functions end]]--
  111.  
  112. if setHost_fail or checkForModem_fail then
  113. startup_fail = true
  114. end
  115.  
  116. function senderID_username(mode, id, username)
  117.  
  118.     if mode == "register" then
  119.         if not senderID_A or id == senderID_A then
  120.             local senderID_A = id
  121.             local senderUSER_A = username
  122.         elseif not senderID_B or id == senderID_B then
  123.             local senderID_B = id
  124.             local senderUSER_B = username
  125.         elseif not senderID_C or id == senderID_C then
  126.             local senderID_C = id
  127.             local senderUSER_C = username
  128.         elseif not senderID_D or id == senderID_D then
  129.             local senderID_D = id
  130.             local senderUSER_D = username
  131.         elseif not senderID_E or id == senderID_E then
  132.             local senderID_E = id
  133.             local senderUSER_E = username
  134.         else
  135.             printMessage(1, "Cannot store anymore IDs")
  136.             return false
  137.         end
  138.  
  139.     elseif not mode then
  140.         if id == senderID_A then
  141.             return senderUSER_A
  142.  
  143.         elseif id == senderID_B then
  144.             return senderUSER_B
  145.  
  146.         elseif id == senderID_C then
  147.             return senderUSER_C
  148.  
  149.         elseif id == senderID_D then
  150.             return senderUSER_D
  151.  
  152.         elseif id == senderID_E then
  153.             return senderUSER_E
  154.  
  155.         else
  156.             printMessage(1, "Unknown ID, returning normal ID")
  157.             return id
  158.         end
  159.     end
  160. end
  161.  
  162. function checkPerms(id)
  163.     if id == 1 then
  164.         printMessage(nil, senderID.."'s privileges has been checked and command was accepted.")
  165.         return true
  166.     else
  167.         printMessage(nil, senderID.."'s privileges has been checked and command was denied.")
  168.         return false
  169.     end
  170. end
  171.  
  172. function startup()
  173.     -- protocol hierarchy list MUST BE TOP ROW IN EVERY startup() FUNCTION
  174.         protocolLv1 = "alpha"
  175.         protocolLv2 = "beta"
  176.         protocolLv3 = "gamma"
  177.         protocolLv4 = "delta"
  178.     -- end
  179.  
  180.     -- local host settings
  181.         server1_protocol = protocolLv1
  182.         server1_host = "houseServer"
  183.     -- end
  184.  
  185.     -- static hosts settings
  186.         --nil
  187.     -- end
  188.  
  189.     -- global variables
  190.         errorCount = 0
  191.         checkForCommand_attempts = 0
  192.         sleepTimer = 1
  193.         commandTimer = 20
  194.         modemPos = "top"
  195.         termXsize = 71
  196.         termYsize = 40
  197.         aX, aY = term.getCursorPos()
  198.         SERVER_VERSION = "A.1.2.9"
  199.     -- end
  200.  
  201.     --peripherals
  202.         monitor = peripheral.wrap("left")
  203.         term.redirect(monitor)
  204.         term.clear()
  205.         term.setCursorPos(1,1)
  206.         checkForModem()
  207.         rednet.open(modemPos)
  208.     -- end
  209.  
  210.     errorHandle()
  211.         if errorCount == 1 then
  212.         printMessage(nil, "Succesfully started with "..errorCount.." error.")
  213.     elseif errorCount == 0 then
  214.         printMessage(nil, "Succesfully started with no errors")
  215.     elseif errorCount >= 2 then
  216.         printMessage(nil, "Succesfully started with "..errorCount.." errors.")
  217.         end
  218.  
  219.     setHost()
  220. printMessage(nil, "Running Server version '"..SERVER_VERSION.."' with '".._VERSION.."'")
  221. serverStarted = true
  222. end
  223.  
  224. if not startup_fail then
  225.     startup()
  226. end
  227.  
  228. while true do --Main loop
  229.     received_command, senderID = checkForCommand()
  230.  
  231.         if received_command == "ping" then
  232.             printMessage(nil, "Received command: 'ping'")
  233.             printMessage(nil, "Executing function: 'action_ping()' for "..senderID_username(nil, senderID, nil))
  234.             action_ping()
  235.             printMessage(0, nil)
  236.  
  237.         elseif received_command == "request_commands" then
  238.             printMessage(nil, "Received command: 'request_commands'")
  239.             printMessage(nil, "Sending command list to: "..senderID_username(nil, senderID))
  240.             rednet.send(senderID, "ping, time, door \n shutdown, reboot", "CommandList") --Up to three commands per line
  241.             printMessage(0, nil)
  242.  
  243.         elseif received_command == "time" then
  244.             printMessage(nil, "Received command: 'time'")
  245.             printMessage(nil, "Executing function: 'action_time()' for "..senderID_username(nil, senderID, nil))
  246.             action_time()
  247.             printMessage(0, nil)
  248.  
  249.         elseif received_command == "identity" then
  250.             printMessage(nil, "Received command: 'identity'")
  251.             rednet.send(senderID, true, "Success")
  252.             printMessage(nil, "Waiting 5 seconds for identity from: "..senderID)
  253.             local user_id, user_message, user_protocol = rednet.receive(5)
  254.             if user_id then
  255.                 printMessage(nil, "Storing identity data for: "..senderID)
  256.                 senderID_username("register", senderID, user_message)
  257.             else
  258.                 printMessage(1, "Half Command")
  259.             end
  260.  
  261.         elseif received_command == "door" then
  262.             printMessage(nil, "Received command: 'door'")
  263.             rednet.send(senderID, "stayOn", "stayOn")
  264.             printMessage(nil, "stayOn request issued. Waiting 10 seconds for response")
  265.             idDOOR, messageDOOR, protocolDOOR = rednet.receive(10)
  266.             if idDOOR then
  267.                 rednet.send(senderID, "Please input a timer", "stayOn")
  268.                 os.sleep(1)
  269.                 rednet.send(senderID, "actionREAD", "stayOn")
  270.                 printMessage(nil, "Waiting for input")
  271.                 local idDOOR1, messageDOOR1, protocolDOOR1 = rednet.receive(10)
  272.                 if idDOOR1 then
  273.                     printMessage(nil, "Executing function: 'action_door()' for "..senderID_username(nil, senderID, nil).." with "..messageDOOR1.." seconds timer")
  274.                     action_door(messageDOOR1)
  275.                     printMessage(0, nil)
  276.                 end
  277.             else
  278.                 printMessage(1, "Half Command")
  279.             end
  280.  
  281.         elseif received_command == "shutdown" then
  282.             printMessage(nil, "Received command: 'shutdown'")
  283.             local canDo = checkPerms(senderID)
  284.             if canDo then
  285.                 printMessage(nil, "Shutting down server...")
  286.                 rednet.send(senderID, 1, "Success")
  287.                 os.sleep(2)
  288.                 term.clear()
  289.                 os.shutdown()
  290.             else
  291.                 printMessage(1, "Could not verify "..senderID_username(nil, senderID, nil))
  292.                 rednet.send(senderID, 0, "Failure")
  293.             end
  294.            
  295.         elseif received_command == "reboot" then
  296.             printMessage(nil, "Received command: 'reboot'")
  297.             local canDo = checkPerms(senderID)
  298.             if canDo then
  299.                 printMessage(nil, "Rebooting server...")
  300.                 rednet.send(senderID, "Rebooting server...", "Success")
  301.                 os.sleep(2)
  302.                 term.clear()
  303.                 os.reboot()
  304.             else
  305.                 printMessage(1, "Could not verify "..senderID_username(nil, senderID, nil))
  306.                 rednet.send(senderID, "Could not verify perms", "Failure")
  307.             end
  308.  
  309.         elseif received_command == "lights" then
  310.             printMessage(nil, "Received command: 'lights'")
  311.             rednet.send(senderID, "stayOn", "stayOn")
  312.             printMessage(nil, "stayOn request issued. Waiting 10 seconds for response")
  313.             local idLights1, messageLights1, protocolLights1 = rednet.receive(10)
  314.             if idLights1 then
  315.                 rednet.send(senderID, "Please input which light", "stayOn")
  316.                 os.sleep(1)
  317.                 rednet.send(senderID, "EnchantTop")
  318.                 os.sleep(1)
  319.                 rednet.send(senderID, "actionREAD", "stayOn")
  320.                 printMessage(nil, "Waiting for input")
  321.                 local idLights2, messageLights2, protocolLights2 = rednet.receive(10)
  322.                 if idLights2 then
  323.                     printMessage(nil, "Executing function: 'action_lights()' for "..senderID_username(nil, senderID, nil).." to "..messageLights2.." light ")
  324.                     local lights_success = action_lights(messageLights2)
  325.                     if lights_success then
  326.                         printMessage(nil, "Function 'action_lights()' returned successful")
  327.                         rednet.send(senderID, "Success", "Success")
  328.                     else
  329.                         printMessage(nil, "Function 'action_lights()' returned failure")
  330.                         rednet.send(senderID, "Fail", "Failure")
  331.                     printMessage(0, nil)
  332.                     end
  333.                 else
  334.                     printMessage(1, "Half Command")
  335.                 end
  336.             else
  337.                 printMessage(1, "Half Command")
  338.             end
  339.            
  340.         elseif received_command or senderID then
  341.             printMessage(1, "A command received, but it was invalid. Command: "..tostring(received_command))
  342.             action_unknown()
  343.         else
  344.             printMessage(nil, "No command was received, resting for "..sleepTimer.." seconds")
  345.             printMessage(0, nil)
  346.         end
  347. end
  348.  
  349. --DiegoG
  350. --Last edit 23/10/2016
  351. --Created on 11/10/2016
Advertisement
Add Comment
Please, Sign In to add comment