Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local serverAdminPassword = ""
- local Doors = {}
- local IsValid = 0
- local function save()
- local file = fs.open("Doors","w")
- file.write(textutils.serialize(Doors))
- file.close()
- end
- local function LoadTable()
- if fs.exists("Door") then
- local file = fs.open("Doors","r")
- local data = file.readAll()
- file.close()
- textutils.unserialize(data)
- end
- end
- local function count() -- counts the amount of users in the 'Users' table and returns them
- local count = 0
- for _ in pairs(Doors) do count = count + 1 end
- return count
- end
- local function clear() -- clears the screen
- term.clear()
- term.setCursorPos (1,1)
- end
- function checkDoorExists( doorID ) -- +++
- for key, value in pairs(Doors) do
- if key == doorID then
- return true
- end
- end
- return false
- end
- local function AuthDoor(id, password) -- value function +++
- for key,value in pairs(Doors) do
- if id == key then
- if password == Doors[key] then
- return 1
- else
- return -1
- end
- end
- end
- end
- local function AddDoor(doorID, password) -- table function +++
- if checkDoorExists(Doorid) == true then
- print ("Door already exists")
- sleep(2)
- return false
- else
- Doors[ doorID ] = password
- print ("Door "..doorID.." with password: "..tostring(password))
- sleep(3)
- return true
- end
- end
- local function DeleteDoor(Door) -- value function +++
- if checkDoorExists(Door) == true then
- Doors[ Door ] = nil
- print ("Door "..Door.." deleted")
- sleep(0.5)
- return true
- else
- print ("could not delete door "..Door.." It does not exist")
- return false
- end
- end
- -- CONTROLL FUNCTIONS ABOVE *********************************
- -- CONTROLL FUNCTIONS ABOVE *********************************
- local function SetupID() -- sets up the Server labvel and Admin password **THIS IS THE STARTUP FUNCTION**
- clear()
- LoadTable()
- print (" this Computer's id: ", os.getComputerID())
- print (" Set this Computer's label:")
- print (" +--------------------------+ ")
- print (" | ")
- print (" +--------------------------+ ")
- print (" SET *ADMIN PASSWORD* ")
- print (" +--------------------------+ ")
- print (" | ")
- print (" +--------------------------+ ")
- term.setCursorPos(5,4)
- local IgnLabel = read()
- if IgnLabel == "no" then
- term.setCursorPos(5,4)
- print ("no label set !")
- term.setCursorPos(5,8)
- else
- os.setComputerLabel(IgnLabel)
- term.setCursorPos(5,4)
- print ("Label successful")
- term.setCursorPos(5,8)
- end
- serverAdminPassword = read()
- term.setCursorPos(5,8)
- print ("Admin password set!")
- sleep(1)
- clear()
- print ("Setting up Door Menu!")
- SetupDoors()
- end
- function SetupDoors() -- Governs the doors; Will Create/delete
- while true do
- clear()
- print ("wanna setup a new door?")
- print ("Warning! once you move on the server goes into")
- print ("rednet mopde, it will not be able to iterface with you")
- print ("to open up this menu again send a 'OPEN' command to this")
- print ("ID:", os.getComputerID())
- print ("type Y/N , or type DELETE to enter delete mode")
- write ("here: ")
- input = read()
- if input == "Y" then
- clear()
- print (" this Computer's id: ", os.getComputerID())
- print (" Input the id of the door")
- print (" +--------------------------+ ")
- print (" | ")
- print (" +--------------------------+ ")
- print (" Input the password of the door")
- print (" +--------------------------+ ")
- print (" | ")
- print (" +--------------------------+ ")
- term.setCursorPos(5,4)
- local NewDoorID = read()
- term.setCursorPos(5,4)
- print ("ID Set")
- term.setCursorPos(5,8)
- local NewDoorPassword = read()
- term.setCursorPos(5,8)
- print ("Password set for door with ID: "..NewDoorID)
- print ("creating new door hold on ...")
- sleep(3)
- if AddDoor(NewDoorID, NewDoorPassword) == true then
- save()
- LoadTable()
- print ("SUCCESS New Door Created!")
- sleep(1)
- else
- print ("Door already exists!")
- sleep(1)
- end
- elseif input == "DELETE" then
- DeleteMenu()
- break
- elseif input == "N" then
- ServiceMenu()
- break
- end
- end
- end
- function DeleteMenu()
- while true do
- clear()
- print ("THIS IS THE DELETE MENU")
- print ("to go back input 'N' instead of id")
- print (" Input the id of the door")
- print (" +--------------------------+ ")
- print (" | ")
- print (" +--------------------------+ ")
- term.setCursorPos(5,5)
- input = read()
- if input == "N" then
- SetupDoors()
- break
- else
- if DeleteDoor(tostring(input)) == true then
- save()
- LoadTable()
- print ("deleted Door with ID:"..input)
- sleep(2)
- else
- print ("Unable to delete given door:"..input)
- sleep(2)
- end
- end
- end
- end
- function ServiceMenu() -- rednet state / server state
- while true do
- local mon = peripheral.wrap("back")
- term.redirect(mon)
- print ("idling...")
- rednet.open("right")
- id, message = rednet.receive()
- IsValid = AuthDoor(tostring(id), tostring(message))
- if IsValid == -1 then
- sleep(4)
- rednet.send(tonumber(id), "InValid")
- print ("Invalid Request From ID:"..id)
- elseif IsValid == 1 then
- sleep(4)
- rednet.send(tonumber(id), "Valid")
- print ("Serviced Valid request from ID:"..id)
- end
- if message == "OPEN" then
- term.redirect(term.native())
- SetupDoors()
- break
- end
- end
- end
- SetupID()
Advertisement
Add Comment
Please, Sign In to add comment