Advertisement
faubiguy

Password Lock

Mar 12th, 2013
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.07 KB | None | 0 0
  1. term.clear()
  2. term.setCursorPos(1, 1)
  3. termX, termY = term.getSize()
  4.  
  5. if not fs.isDir('.PasswordFiles') then
  6.  if fs.exists('.PasswordFiles') then
  7.   fs.delete('.PasswordFiles')
  8.  end
  9.  fs.makeDir('.PasswordFiles')
  10. end
  11.  
  12. if not fs.exists(".PasswordFiles/Password") then
  13.  print("Password file not found. Access granted")
  14. else
  15.  passFile = fs.open(".PasswordFiles/Password", "r")
  16.  pass = passFile.readLine()
  17.  passFile.close()
  18.  
  19.  term.write("You have _ attempts to enter correct password")
  20.  
  21.  function accessCodeMatch(diskACfile)
  22.   equal = true
  23.   if not fs.exists(".PasswordFiles/AccessCode") then return false
  24.   else
  25.    pcACfile = fs.open(".PasswordFiles/AccessCode", "rb")
  26.    for n = 1, 32 do
  27.     pcByte = pcACfile.read()
  28.     diskByte = diskACfile.read()
  29.     if pcByte ~= diskByte then
  30.      equal = false
  31.      break
  32.     end
  33.    end
  34.    pcACfile.close()
  35.    return equal
  36.   end
  37.  end  
  38.  
  39.  function checkDrive(sSide)
  40.   if sSide == "all" then
  41.    return checkDrive("bottom") or checkDrive("top") or checkDrive("left") or checkDrive("right") or checkDrive("front") or checkDrive("back")
  42.   else if (sSide == "bottom") or (sSide == "top") or (sSide == "left") or (sSide == "right") or (sSide == "front") or (sSide == "back") then
  43.    if disk.isPresent(sSide) then
  44.     if disk.hasData(sSide) then
  45.      if fs.exists(disk.getMountPath(sSide) .. "/AccessCode") then
  46.       accessCodeFile = fs.open(disk.getMountPath(sSide) .. "/AccessCode", "rb")
  47.       if accessCodeMatch(accessCodeFile) then
  48.        accessCodeFile.close()
  49.        return true
  50.       else return false
  51.       end
  52.      end
  53.     end
  54.    end  
  55.   end
  56.   end
  57.  end
  58.    
  59.  function updateFailed(nFailed)
  60.   failedFile = fs.open(".PasswordFiles/Failed", "w")
  61.   failedFile.write(tostring(nFailed))
  62.   failedFile.close()
  63.  end
  64.  
  65.  function getFailed()
  66.   if not fs.exists(".PasswordFiles/Failed") then return 0
  67.   else
  68.    failedFile = fs.open(".PasswordFiles/Failed", "r")
  69.    if not failedFile then return 0 end
  70.    nFailed = failedFile.readLine()
  71.    nFailed = tonumber(nFailed)
  72.    failedFile.close()
  73.    if nFailed then return nFailed
  74.    else return 0 end
  75.   end
  76.  end
  77.  
  78.  function checkFailed()
  79.   if failed >= 3 then
  80.    term.setCursorPos(1, 5)
  81.    print("Too many failed attempts. Access Denied")
  82.    print("Insert Access Disk to unlock")
  83.    if checkDrive("all") then return true end
  84.    while true do
  85.     event, side = os.pullEvent("disk")
  86.     if checkDrive(side) then return true end
  87.    end
  88.   end
  89.  end
  90.  
  91.  failed = getFailed()  
  92.  
  93.  while true do
  94.   term.setCursorPos(10, 1)
  95.   term.write(tostring(3 - failed))
  96.   term.setCursorPos(1, termY)
  97.   term.write("Failed Attempts:")
  98.   for n = 1, 3 do
  99.    if failed > n - 1 then
  100.     term.write(" [X]")
  101.    else
  102.     term.write(" [ ]")
  103.    end
  104.   end
  105.   term.setCursorPos(1, 3)
  106.   term.clearLine()
  107.   term.write("Password: ")
  108.   updateFailed(failed)
  109.   if checkFailed() then break end
  110.  
  111.   if read() == pass then
  112.    print("Correct Password! Access Granted")
  113.    sleep(1)
  114.    break
  115.   else
  116.    failed = failed + 1
  117.   end
  118.  end
  119.  updateFailed(0)
  120.  term.clear()
  121.  term.setCursorPos(1, 1)
  122. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement