Advertisement
Guest User

Untitled

a guest
Mar 12th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. if type(record)~="table" then record={} end
  2. local function prompt(q,y,n) --a function we will be using to get a yes or no answer for anything, very usefull
  3. while true do
  4. term.clear()
  5. term.setCursorPos(1,1)
  6. print(q.." ["..y.." | "..n.."]")
  7. local prompt=read()
  8. if prompt==y then
  9. return y
  10. elseif prompt==n then
  11. return n
  12. end
  13. end
  14. end
  15. local function load(file) --a function to load the usernames from the file we saved last session
  16. if fs.exists(file) then
  17. record["users"]=textutils.unserialize(io.open(file):read) -- I will explain this later
  18. else
  19. print("user file not found")
  20. record["users"]={}
  21. end
  22. end
  23. local function createuser(user, file) --add users to the list and save them to the file
  24. if user==nil then
  25. print("Enter new username")
  26. local user=read()
  27. end
  28. term.clear()
  29. term.setCursorPos(1,1)
  30. print("enter password for "..user)
  31. local pass=read("*")
  32. record["users"][user]={}
  33. record["users"][user]["password"]=pass
  34. io.open(file,"w+"):write(textutils.serialize(record["users"]))
  35. print("Username created")
  36. end
  37. local function login() --the actual login function
  38. term.clear()
  39. term.setCursorPos(1,1)
  40. print("Enter your username")
  41. record["username"]=read()
  42. if record["users"][record["username"]]~=nil then
  43. term.clear()
  44. term.setCursorPos(1,1)
  45. print("Enter password for "..record["username"])
  46. local password=read("*")
  47. if record["users"][record["username"]]["password"]==password then
  48. print("LOGGED IN")
  49. return true
  50. else
  51. print("Incorrect password, restarting")
  52. sleep(2)
  53. reboot()
  54. end
  55. else --if record["users"][record["username"]] is nil (not already in the database)
  56. local answer=prompt("username: "..record["username"].." not found, would you like to add it?","y","n")
  57. if answer=="y" then
  58. createuser(record["username"], "userfile")
  59. sleep(2)
  60. reboot()
  61. elseif answer=="n" then
  62. reboot()
  63. end
  64. end
  65. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement