shiskebab

Door Password Server

Jul 27th, 2015
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.67 KB | None | 0 0
  1. -- Other Authors
  2. -- https://sites.google.com/site/kleebcominecraft/kleebco---computer-craft----lets-play
  3. -- http://pastebin.com/s5aXm3Gs
  4.  
  5.  
  6. local serverAdminPassword = ""
  7. local Doors = {}
  8. local Passwords = {}
  9. local IsValid = 0
  10.  
  11. local function count() -- counts the amount of users in the 'Users' table and returns them
  12.  local count = 0
  13.  for _ in pairs(Doors) do count = count + 1 end
  14.  return count
  15. end
  16.  
  17. local function ServiceMenu() -- rednet state / server state
  18.  while true do
  19.   local mon = peripheral.wrap("back")
  20.   mon.restoreTo = term.current()
  21.   term.redirect(mon)
  22.   DrawMenu()
  23.   print ("idling...")
  24.   rednet.open("right")
  25.   id, message = rednet.receive()
  26.  
  27.    IsValid = AuthDoor(id, message)
  28.    if IsValid == -1 then
  29.     rednet.send(id, "InValid")
  30.     print ("Invalid Request From ID:"..id)
  31.  
  32.    elseif IsValid == 1 then
  33.     rednet.send(id, "Valid")
  34.     print ("Serviced Valid request from ID:"..id)
  35.    end
  36.    if message == "OPEN" then
  37.       term.redirect(mon.restoreTo)
  38.       SetupDoors()
  39.     break
  40.    end
  41. end
  42. end
  43.  
  44. local function clear() -- clears the screen
  45. term.clear()
  46. term.setCursorPos (1,1)
  47. end
  48.  
  49. function AuthDoor(id, password)
  50. for i=0, count() do
  51.    if Passwords[i] == password then
  52.       return 1
  53.    else
  54.       return -1
  55.    end
  56. end
  57.  
  58. end
  59.  
  60. function SetupDoors() --
  61. while true do
  62.   clear()
  63.   print ("wanna setup a new door?")
  64.   print ("Warning! once you move on the server goes into")
  65.   print ("rednet mopde, it will not be able to iterface with you")
  66.   print ("to open up this menu again send a 'OPEN' command to this")
  67.   print ("ID:", os.getComputerID())
  68.   print ("type Y/N , or type DELETE to enter delete mode")
  69.   write ("here: ")
  70.   input = read()
  71.   if input == "Y" then
  72.    clear()
  73.    print ("   this Computer's id: ", os.getComputerID())
  74.    print ("   Input the id of the door")
  75.    print ("   +--------------------------+   ")
  76.    print ("                              |   ")
  77.    print ("   +--------------------------+   ")
  78.    print ("    Input the password of the door")
  79.    print ("   +--------------------------+   ")
  80.    print ("                              |   ")
  81.    print ("   +--------------------------+   ")
  82.    term.setCursorPos(5,4)
  83.    local NewDoorID = read()
  84.    term.setCursorPos(5,4)
  85.    print ("ID Set")
  86.    term.setCursorPos(5,8)
  87.    local NewDoorPassword = read()
  88.    term.setCursorPos(5,8)
  89.    print ("Password set for door with ID: "..NewDoorID)
  90.    print ("creating new door hold on ...")
  91.    sleep(3)
  92.     if AddDoor(NewDoorID, NewDoorPassword) == true then
  93.       print ("SUCCESS New Door Created!")
  94.       sleep(1)
  95.     else
  96.      print ("Door already exists!")
  97.      sleep(1)
  98.     end
  99.  
  100.   elseif input == "DELETE" then
  101.    DeleteMenu()
  102.    break
  103.  
  104.   elseif input == "N" then
  105.    ServiceMenu()
  106.    break
  107.  end
  108. end
  109. end
  110.  
  111. local function SetupID() -- sets up the Server labvel and Admin password **THIS IS THE STARTUP FUNCTION**
  112.   clear()
  113.   print ("   this Computer's id: ", os.getComputerID())
  114.   print ("   Set this Computer's label:")
  115.   print ("   +--------------------------+   ")
  116.   print ("                              |   ")
  117.   print ("   +--------------------------+   ")
  118.   print ("        SET *ADMIN PASSWORD*      ")
  119.   print ("   +--------------------------+   ")
  120.   print ("                              |   ")
  121.   print ("   +--------------------------+   ")
  122.   term.setCursorPos(5,4)
  123.   local IgnLabel = read()
  124.   if IgnLabel == "no" then
  125.     term.setCursorPos(5,4)
  126.     print ("no label set !")
  127.     term.setCursorPos(5,8)
  128.    else
  129.     os.setComputerLabel(IgnLabel)
  130.     term.setCursorPos(5,4)
  131.     print ("Label successful")
  132.     term.setCursorPos(5,8)
  133.   end
  134.   serverAdminPassword = read()
  135.   term.setCursorPos(5,8)
  136.   print ("Admin password set!")
  137.   sleep(1)
  138.   clear()
  139.   print ("Setting up Door Menu!")
  140.   SetupDoors()
  141. end
  142.  
  143. function AddDoor(doorID, password)
  144.  if DoorExists(Doorid) == true then
  145.   return false
  146.  
  147.   else
  148.    table.insert(Doors, doorID)
  149.    table.insert(Passwords, password)
  150.    print ("Door"..doorID.."created")
  151.    sleep(3)
  152.    return true
  153.  end
  154. end
  155.  
  156. function DoorExists(DoorID)
  157.  for i = 0, count() do
  158.    if doorID == Doors[i] then
  159.      return true
  160.    end
  161.    if i == count then
  162.       return false
  163.     end
  164.    end
  165. end
  166.  
  167. function DeleteDoor(Door)
  168.   if DoorExists(Door) == true then
  169.     for i = 0, count() do
  170.           if Door == Doors[i] then
  171.            table.remove(Doors, i)
  172.            table.remove(Passwords, i)
  173.            print ("Door"..Door.."deleted")
  174.            sleep(1)
  175.           end
  176.         return true
  177.     end
  178.     elseif DoorExists(Door) == false then
  179.      print("Door does not exist!")
  180.      return false
  181.    end
  182. end
  183.  
  184. function DrawMenu() -- Creates a makeshift menu whenever called
  185.   print ("   +--------------------------+   ")
  186.   print ("   |   WELCOME TO THE SERVER  |   ")
  187.   print ("   +--------------------------+   ")
  188.   print ("   this Computer's id: ", os.getComputerID())
  189.   print ("   +--------------------------+   ")
  190.   print ("   |  THE SERVER IS AUTOMATIC |   ")
  191.   print ("   |   *MESSAGES DISPLAYING*  |   ")
  192.   print ("   +--------------------------+   ")
  193. end
  194.  
  195. function DeleteMenu()
  196.  while true do
  197.   clear()
  198.    print ("THIS IS THE DELETE MENU")
  199.    print ("to go back input 'N' instead of id")
  200.    print ("   Input the id of the door")
  201.    print ("   +--------------------------+   ")
  202.    print ("                              |   ")
  203.    print ("   +--------------------------+   ")
  204.    term.setCursorPos(5,5)
  205.    input = read()
  206.     if input == "N" then
  207.      SetupDoors()
  208.      break
  209.    
  210.     else
  211.      if DeleteDoor(input) == true then
  212.       print ("deleted Door with ID:"..input)
  213.       sleep(2)
  214.      else
  215.       print ("Unable to delete given door:"..input)
  216.       sleep(2)
  217.      end
  218.     end
  219.  end
  220. end
  221.  
  222. SetupID()
Advertisement
Add Comment
Please, Sign In to add comment