shiskebab

Computercraft Client/Server interaction

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