fatboychummy

Minimenu

Jun 23rd, 2020
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.03 KB | None | 0 0
  1. -- Paths to data files
  2. local sPasswordPath = "/.password"
  3. local sDataPath = "/.data"
  4.  
  5. -- functions to read/write to/from a file
  6. local function writeToFile(sFilename, sData)
  7.   local file = io.open(sFilename, 'w') -- open the file in write (w) mode
  8.   file:write(sData):close() -- write the data then close it
  9. end
  10. local function readFromFile(sFilename)
  11.   local file = io.open(sFilename, 'r') -- open the file in read (r) mode
  12.   local sData = file:read("*a") -- read all (*a)
  13.   file:close() -- close it
  14.   return sData -- return the data read
  15. end
  16.  
  17. -- check if the data files exist, and if not, write defaults.
  18. -- default password is 'admin', default redstone output side is 'top'
  19. if not fs.exists(sPasswordPath) then
  20.   writeToFile(sPasswordPath, "admin")
  21. end
  22. if not fs.exists(sDataPath) then
  23.   writeToFile(sDataPath, "top")
  24. end
  25.  
  26. -- get the data from the files
  27. local sPassword = readFromFile(sPasswordPath)
  28. local sSide = readFromFile(sDataPath)
  29.  
  30. --[[
  31.   key resolver table, keys in the 'keys' table are stored as a number
  32.   and the values differ from version to version.
  33.  
  34.   With these resolver tables, I can index the table via tKeyResolver[key] and
  35.   get a value from 1-9.
  36. ]]
  37. -- 1-9 on top
  38. local tKeyResolver = {
  39.   [keys.one] = 1,
  40.   [keys.two] = 2,
  41.   [keys.three] = 3,
  42.   [keys.four] = 4,
  43.   [keys.five] = 5,
  44.   [keys.six] = 6,
  45.   [keys.seven] = 7,
  46.   [keys.eight] = 8,
  47.   [keys.nine] = 9
  48. }
  49. -- numpad
  50. local tKeyResolver2 = {
  51.   [keys.numPad1] = 1,
  52.   [keys.numPad2] = 2,
  53.   [keys.numPad3] = 3,
  54.   [keys.numPad4] = 4,
  55.   [keys.numPad5] = 5,
  56.   [keys.numPad6] = 6,
  57.   [keys.numPad7] = 7,
  58.   [keys.numPad8] = 8,
  59.   [keys.numPad9] = 9
  60. }
  61.  
  62. -- display the table of information, and if sEvent is given, add that to the bottom.
  63. local function display(tInfo, sEvent)
  64.   -- clear the screen.
  65.   term.setBackgroundColor(colors.black)
  66.   term.setTextColor(colors.white)
  67.   term.clear()
  68.   term.setCursorPos(1, 1)
  69.  
  70.   -- tell the user what options there are
  71.   print("Select one of the following:")
  72.   for i = 1, #tInfo do -- for i = 1 to n where n is the length of tInfo
  73.     print(string.format("%d: %s", i, tInfo[i]))
  74.     -- print the number, and the string in the table at that value
  75.   end
  76.  
  77.   -- if an event occured and was passed to this function, print it.
  78.   if sEvent then
  79.     print("\n")
  80.     print(sEvent)
  81.   end
  82.  
  83.   -- Loop to listen for 1-9 on keyboard or numpad
  84.   while true do
  85.     -- pull event RAW to bypass terminate
  86.     local sEvent, iKey = os.pullEventRaw("key")
  87.     if tKeyResolver[iKey] and tKeyResolver[iKey] <= #tInfo then
  88.       -- if the key is part of 1-9 on the keyboard, and one of the options correspond to the value hit, return the value hit.
  89.       return tKeyResolver[iKey]
  90.     end
  91.     if tKeyResolver2[iKey] and tKeyResolver2[iKey] <= #tInfo then
  92.       -- if the key is part of 1-9 on the numpad, and one of the options correspond to the value hit, return the value hit.
  93.       return tKeyResolver2[iKey]
  94.     end
  95.   end
  96. end
  97.  
  98. -- sleeps using pullEventRaw instead of pullEvent to bypass termination
  99. local function safeSleep(fSeconds)
  100.   -- start a timer for the time inputted
  101.   -- (os.startTimer will queue a timer event after fSeconds seconds.)
  102.   local iTimer = os.startTimer(fSeconds)
  103.   while true do
  104.     -- wait for our timer event from the os
  105.     local sEvent, iT = os.pullEventRaw("timer")
  106.     if iTimer == iT then
  107.       -- once we recieve the timer event, return
  108.       return
  109.     end
  110.   end
  111. end
  112.  
  113. -- create a table of functions which correspond to an option in the display call
  114. -- for example, tFuncs[1]() opens the door, tFuncs[2]() closes the door, and so on...
  115. -- these functions used in the function "loggedIn"
  116. local tFuncs = {
  117.   function() -- Open door
  118.     redstone.setOutput(sSide, true)
  119.     last = "Door opened." -- on the next iteration, tell the user the door was opened
  120.   end,
  121.   function() -- Close door
  122.     redstone.setOutput(sSide, false)
  123.     last = "Door closed." -- on the next iteration, tell the user the door was opened
  124.   end,
  125.   function() -- change redstone output side
  126.     local tSides = redstone.getSides()
  127.     local iVal = display(tSides)  -- display all sides you can output redstone from, and let the user choose one.
  128.     sSide = tSides[iVal]          -- set the redstone output side locally (in RAM)
  129.     writeToFile(sDataPath, sSide) -- save the side so if the computer reboots it doesn't set itself back to default values
  130.     last = string.format("Redstone output side set to '%s'", sSide) -- tell the user the side was set.
  131.   end,
  132.   function() -- change password
  133.     -- clear the screen
  134.     term.clear()
  135.     term.setCursorPos(1, 1)
  136.     safeSleep(0.05) -- wait a short bit, for some reason events above here get passed to read so an extra character gets added
  137.  
  138.     -- get the user's password twice, to ensure they entered it correctly
  139.     term.write("Enter new password: ")
  140.     local sPass1 = read("*")
  141.     term.setCursorPos(1, 2)
  142.     safeSleep(0.05) -- wait a short bit, for some reason events above here get passed to read so an extra character gets added
  143.     term.write("Confirm password  : ")
  144.     local sPass2 = read("*")
  145.  
  146.     -- if the passwords match, save the password and set the new password in RAM
  147.     -- otherwise let the user know the password set failed
  148.     if sPass1 == sPass2 then
  149.       writeToFile(sPasswordPath, sPass1)
  150.       sPassword = sPass1
  151.       last = "Password has been changed."
  152.     else
  153.       last = "Password change failed: Passwords did not match."
  154.     end
  155.   end
  156. }
  157.  
  158. -- logged in menu page, does all the stuff once you're logged in
  159. local function loggedIn()
  160.  
  161.   local last -- declared here so it's persistant between loop iterations
  162.  
  163.   -- main loop to listen for user input and do things
  164.   while true do
  165.     -- display the options and get what the user chooses
  166.     local iVal = display({
  167.       "Open door",
  168.       "Close door",
  169.       "Change redstone output side",
  170.       "Change password",
  171.       "Log out"
  172.     }, last)
  173.  
  174.     -- if the function exists, then call the function
  175.     if tFuncs[iVal] then
  176.       tFuncs[iVal]()
  177.     elseif iVal == 5 then
  178.       -- if it does not exist, and we hit 5 (log out), then return to the last function (aka log out)
  179.       return
  180.     end
  181.   end
  182. end
  183.  
  184. local function main()
  185.   -- main loop
  186.   while true do
  187.     -- wait for user to login
  188.     display({"Login"})
  189.     safeSleep(0.05) -- wait a short bit, for some reason events above here get passed to read so an extra character gets added
  190.  
  191.     -- request password
  192.     term.clear()
  193.     term.setCursorPos(1, 1)
  194.     print("Enter your password.")
  195.     local sRead = read("*")
  196.  
  197.     -- if the password the user entered matches the password we have stored
  198.     if sRead == sPassword then
  199.       -- show the logged in screen
  200.       loggedIn()
  201.  
  202.       -- then clear the screen, and let the user know we logged them out.
  203.       term.clear()
  204.       term.setCursorPos(1, 1)
  205.       print("Logged out.")
  206.       safeSleep(2)
  207.     else
  208.       -- if incorrect, let user know.
  209.       term.clear()
  210.       term.setCursorPos(1, 1)
  211.       printError("Incorrect password.")
  212.       safeSleep(2)
  213.     end
  214.   end
  215. end
  216.  
  217. -- read() uses os.pullEvent, so terminate events can get through during password entry
  218. -- if someone does terminate (or some other error occurs) the error will be printed here,
  219. -- and the computer will reboot.
  220. local ok, err = pcall(main)
  221. if not ok then
  222.   printError("An error occured:")
  223.   printError(err)
  224. end
  225. print("Rebooting...")
  226. safeSleep(2)
  227. os.reboot()
Add Comment
Please, Sign In to add comment