Advertisement
CryptekCoding

accessServer

Mar 30th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.84 KB | None | 0 0
  1.  --[[ Server side: accessServer ]]--
  2.  
  3.  local myid = os.computerID()
  4. local doorList = { 2 }
  5. local passwordForDoor = { "ACS" }
  6.  
  7. mon=peripheral.wrap("bottom")
  8. print("Access Terminal")
  9. rednet.open("back")
  10.  
  11.  
  12. print("Computer id for Access Terminal is "..tostring(myid))
  13.  
  14. function findIndexForId(id)
  15.    for i,v in ipairs(doorList) do
  16.       if id == v then
  17.          return i
  18.       end
  19.    end
  20.    return 0
  21. end
  22.  
  23.  
  24. --[[ for future implementation ]]--
  25. function setPasswordForLock(id,password)
  26.    local i = findIndexForId(id)
  27.    
  28.    if i == 0 then
  29.       return false
  30.    end
  31.    
  32.    passwordForDoor[i] = password
  33.    print("in setPasswordForLock"..id..password)
  34.    return true
  35. end
  36.  
  37. function checkPasswordForLock(id,password)
  38.    local i = findIndexForId(id)
  39.    if i == 0 then
  40.       return -1
  41.    end
  42.    if passwordForDoor[i] == password then
  43.       return 1
  44.    else
  45.       return 0
  46.    end
  47. end
  48.  
  49. --[[ Not needed yet, for later when we allow remove password changes ]]
  50. function saveData()
  51.   local myfile = io.open("/doorpw.dat","w")
  52.   print(tostring(myfile))
  53.   print("1")
  54.   for i,pw in ipairs(passwordForDoor) do
  55.      myfile:write(pw)
  56.   end
  57.   print("4")
  58.   myfile:close()
  59. end
  60.  
  61.  
  62. local isValid = 0
  63.  
  64. while true do
  65.    local timeString = textutils.formatTime(os.time(),false)
  66.  
  67.    senderId, message, distance = rednet.receive()
  68.      
  69.    isValid = checkPasswordForLock(senderId, message)
  70.  
  71.    if isValid == -1 then
  72.       print("server "..senderId.." sent us a request but is not in our list")
  73.    elseif isValid == 1 then
  74.       rednet.send(senderId, "Valid")
  75.       mon.scroll(1)
  76.       mon.setCursorPos(1,4)
  77.       mon.write("Access from "..senderId.." at "..timeString)
  78.    else
  79.       rednet.send(senderId, "Not Valid")
  80.       mon.scroll(1)
  81.       mon.setCursorPos(1,4)
  82.       mon.write("Failure from "..senderId.." at "..timeString)
  83.   end
  84. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement