Guest User

ServerStartup.lua - A.1.2.11

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