Roodiger

Title

Aug 6th, 2021 (edited)
1,108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 16.26 KB | None | 0 0
  1. -- Get the terminal size. Helpful for displaying text
  2. local w, h = term.getSize()
  3.  
  4. -- Load in Anavrins sha api
  5. local sha = require "/apis/sha"
  6.  
  7. -- Set up the page that we're currently on
  8. local page = "title"
  9.  
  10. -- This function will be used to draw the title page when the page is set to title
  11. local function drawTitle()
  12.     -- Paint the background black
  13.     paintutils.drawFilledBox(0, 0, w, h, colors.black)
  14.    
  15.     -- Set up the cursor position to write the "Diamond" text
  16.     term.setCursorPos(0,3)
  17.     x, y = term.getCursorPos()
  18.     x = math.max(math.floor((w / 2) - (40 / 2)), 0)
  19.     term.setCursorPos(x,y)
  20.  
  21.     -- Diamond Text
  22.     -- D
  23.     paintutils.drawLine(x, y, x, y + 4, colors.cyan)
  24.     paintutils.drawLine(x + 1, y, x + 3, y, colors.cyan)
  25.     paintutils.drawLine(x + 1, y + 4, x + 3, y + 4, colors.cyan)
  26.     paintutils.drawLine(x + 4, y + 1, x + 4, y + 3, colors.cyan)
  27.     -- I
  28.     paintutils.drawLine(x + 6, y, x + 10, y, colors.cyan)
  29.     paintutils.drawLine(x + 6, y + 4, x + 10, y + 4, colors.cyan)
  30.     paintutils.drawLine(x + 8, y + 1, x + 8, y + 3, colors.cyan)
  31.     -- A
  32.     paintutils.drawLine(x + 13, y, x + 15, y, colors.cyan)
  33.     paintutils.drawLine(x + 12, y + 1, x + 12, y + 4, colors.cyan)
  34.     paintutils.drawLine(x + 16, y + 1, x + 16, y + 4, colors.cyan)
  35.     paintutils.drawLine(x + 13, y + 2, x + 15, y + 2, colors.cyan)
  36.     -- M
  37.     paintutils.drawLine(x + 18, y, x + 18, y + 4, colors.cyan)
  38.     paintutils.drawLine(x + 22, y, x + 22, y + 4, colors.cyan)
  39.     paintutils.drawPixel(x + 19, y + 1, colors.cyan)
  40.     paintutils.drawPixel(x + 20, y + 2, colors.cyan)
  41.     paintutils.drawPixel(x + 21, y + 1, colors.cyan)
  42.     -- O
  43.     paintutils.drawBox(x + 24, y, x + 28, y + 4, colors.cyan)
  44.     -- N
  45.     paintutils.drawLine(x + 30, y, x + 30, y + 4, colors.cyan)
  46.     paintutils.drawLine(x + 34, y, x + 34, y + 4, colors.cyan)
  47.     paintutils.drawPixel(x + 31, y + 1, colors.cyan)
  48.     paintutils.drawPixel(x + 32, y + 2, colors.cyan)
  49.     paintutils.drawPixel(x + 33, y + 3, colors.cyan)
  50.     -- D
  51.     paintutils.drawLine(x + 36, y, x + 36, y + 4, colors.cyan)
  52.     paintutils.drawLine(x + 37, y, x + 39, y, colors.cyan)
  53.     paintutils.drawLine(x + 37, y + 4, x + 39, y + 4, colors.cyan)
  54.     paintutils.drawLine(x + 40, y + 1, x + 40, y + 3, colors.cyan)
  55.  
  56.     -- Digger text
  57.  
  58.     y = y + 6
  59.     x = math.max(math.floor((w / 2) - (35 / 2)), 0)
  60.  
  61.     -- D
  62.     paintutils.drawLine(x, y, x, y + 4, colors.cyan)
  63.     paintutils.drawLine(x + 1, y, x + 3, y, colors.cyan)
  64.     paintutils.drawLine(x + 1, y + 4, x + 3, y + 4, colors.cyan)
  65.     paintutils.drawLine(x + 4, y + 1, x + 4, y + 3, colors.cyan)
  66.     -- I
  67.     paintutils.drawLine(x + 6, y, x + 10, y, colors.cyan)
  68.     paintutils.drawLine(x + 6, y + 4, x + 10, y + 4, colors.cyan)
  69.     paintutils.drawLine(x + 8, y + 1, x + 8, y + 3, colors.cyan)
  70.     -- G
  71.     paintutils.drawLine(x + 12, y, x + 12, y + 4, colors.cyan)
  72.     paintutils.drawLine(x + 13, y, x + 17, y, colors.cyan)
  73.     paintutils.drawLine(x + 13, y + 4, x + 17, y + 4, colors.cyan)
  74.     paintutils.drawLine(x + 14, y + 2, x + 16, y + 2, colors.cyan)
  75.     paintutils.drawPixel(x + 17, y + 3, colors.cyan)
  76.     -- G
  77.     paintutils.drawLine(x + 19, y, x + 19, y + 4, colors.cyan)
  78.     paintutils.drawLine(x + 20, y, x + 24, y, colors.cyan)
  79.     paintutils.drawLine(x + 19, y + 4, x + 24, y + 4, colors.cyan)
  80.     paintutils.drawLine(x + 21, y + 2, x + 23, y + 2, colors.cyan)
  81.     paintutils.drawPixel(x + 24, y + 3, colors.cyan)
  82.     -- E
  83.     paintutils.drawLine(x + 26, y, x + 26, y + 4, colors.cyan)
  84.     paintutils.drawLine(x + 27, y, x + 30, y, colors.cyan)
  85.     paintutils.drawLine(x + 27, y + 4, x + 30, y + 4, colors.cyan)
  86.     paintutils.drawLine(x + 27, y + 2, x + 28, y + 2, colors.cyan)
  87.     -- R
  88.     paintutils.drawLine(x + 32, y, x + 32, y + 4, colors.cyan)
  89.     paintutils.drawLine(x + 33, y, x + 35, y, colors.cyan)
  90.     paintutils.drawLine(x + 33, y + 3, x + 35, y + 3, colors.cyan)
  91.     paintutils.drawLine(x + 36, y + 1, x + 36, y + 2, colors.cyan)
  92.     paintutils.drawPixel(x + 36, y + 4, colors.cyan)
  93.  
  94.     term.setTextColour(colours.white)
  95.     term.setBackgroundColor(colors.cyan)
  96.  
  97.     -- New Game button
  98.  
  99.     y = y + 7
  100.     x = math.max(math.floor((w / 2) - (12)), 0)
  101.  
  102.     term.setCursorPos(x, y)
  103.     term.write("          ")
  104.     term.setCursorPos(x, y + 1)
  105.     term.write(" New Game ")
  106.     term.setCursorPos(x, y + 2)
  107.     term.write("          ")
  108.  
  109.     -- Load Game button
  110.  
  111.     x = math.max(math.floor((w / 2) + (2)), 0)
  112.  
  113.     term.setCursorPos(x, y)
  114.     term.write("           ")
  115.     term.setCursorPos(x, y + 1)
  116.     term.write(" Load Game ")
  117.     term.setCursorPos(x, y + 2)
  118.     term.write("           ")
  119.  
  120.     -- Leaderboard button
  121.  
  122.     x = math.max(math.floor((w) - (5)), 0)
  123.    
  124.     paintutils.drawPixel(x, y + 2, colors.brown)
  125.     paintutils.drawLine(x + 1, y, x + 1, y + 2, colors.orange)
  126.     paintutils.drawLine(x + 2, y + 1, x + 2, y + 2, colors.lightGray)
  127.    
  128. end
  129.  
  130. -- Use this function to correctly display a number.
  131. -- num is the number to format, if isInt is true, it will round to the nearest int
  132. local function displayNumber(num, isInt)
  133.  
  134.     local num = num or 0
  135.     local isInt = isInt or false
  136.     local newNum = 0
  137.    
  138.     if num >= 1000000 then
  139.         newNum = num / 1000000
  140.         newNum = math.floor(newNum * 100)/100
  141.         newNum = tostring(newNum)
  142.         if isInt == false then
  143.             newNum = string.format("%.1f", newNum) .. "M"
  144.         end
  145.     elseif num >= 1000 then
  146.         newNum = num / 1000
  147.         newNum = math.floor(newNum * 100)/100
  148.         newNum = tostring(newNum)
  149.         if isInt == false then
  150.             newNum = string.format("%.1f", newNum) .. "K"
  151.         end
  152.     else
  153.         newNum = tostring(num)
  154.         if isInt == false then
  155.             newNum = string.format("%.1f", newNum)
  156.         end
  157.     end
  158.    
  159.     return newNum
  160. end
  161.  
  162. -- This function draws the page for the new game page
  163. local function drawNewGame()
  164.    
  165.     paintutils.drawFilledBox(0, 0, w, h, colors.black)
  166.     term.setTextColour(colours.white)
  167.     term.setBackgroundColor(colors.cyan)
  168.    
  169.     term.setCursorPos(1, 1)
  170.     term.write("      ")
  171.     term.setCursorPos(1, 2)
  172.     term.write(" Back ")
  173.     term.setCursorPos(1, 3)
  174.     term.write("      ")
  175.    
  176.     term.setBackgroundColor(colors.black)
  177.     term.setCursorPos(21, 7)
  178.     term.write("New Game")
  179.    
  180.     term.setCursorPos(0,8)
  181.     local x, y = term.getCursorPos()
  182.     x = math.max(math.floor((w / 2) - (15)), 0)
  183.     term.setCursorPos(x,y)
  184.     term.write("(Please type in the computer)")
  185.    
  186.     term.setCursorPos(15, 10)
  187.     term.write("Username: ")
  188.     local username = read()
  189.        
  190.     term.setCursorPos(15, 12)
  191.     term.write("Password: ")
  192.     local password = read("*")
  193.    
  194.     -- This function will add the user to the system
  195.     function save()
  196.         -- If the user file exists, let the user know and run the save function again
  197.         if fs.exists("users/" .. username .. "_save") then
  198.             term.setCursorPos(13, 15)
  199.             term.clearLine()
  200.             term.write("That user already exists!")
  201.            
  202.             term.setCursorPos(15, 10)
  203.             term.clearLine()
  204.            
  205.             term.setCursorPos(15, 12)
  206.             term.clearLine()
  207.  
  208.             term.setCursorPos(15, 10)
  209.             term.write("Username: ")
  210.             local username = read()
  211.  
  212.             term.setCursorPos(15, 12)
  213.             term.write("Password: ")
  214.             local password = read("*")
  215.            
  216.             save()
  217.         else
  218.            
  219.             -- Add in a unique salt for password hashing (Thanks PG231)
  220.             local salt = sha.digest("i7gBq4p7p+uOCuuWvrlPgg|" .. os.epoch("utc") .. "|" .. math.random(0, 2^31 - 2)):toHex()
  221.            
  222.             -- Use the sha api to generate a hash to save for password protection
  223.             password = sha.pbkdf2(password, salt, 16):toHex()
  224.            
  225.             -- Save the users hashed password and salt to their save file
  226.             local hWrite = fs.open("users/" .. username .. "_save", "w")
  227.             hWrite.writeLine(password)
  228.             hWrite.writeLine(salt)
  229.             hWrite.close()
  230.            
  231.             -- If there is a users file already, add this user to the list
  232.             if fs.exists("users/" .. "users") then
  233.                 local userRead = fs.open("users/" .. "users", "r")
  234.                
  235.                 local lines = {}
  236.                 while true do
  237.                     local line = userRead.readLine()
  238.  
  239.                     if not line then break end
  240.  
  241.                     lines[#lines + 1] = line
  242.                 end
  243.                
  244.                 userRead.close()
  245.                            
  246.                 local userWrite = fs.open("users/" .. "users", "w")
  247.                 for i, name in pairs(lines) do
  248.                   userWrite.writeLine(name)
  249.                 end
  250.                 userWrite.writeLine(username)
  251.  
  252.                 userWrite.close()
  253.                
  254.             else
  255.                 -- If this is the first user, create the users file
  256.                 local userWrite = fs.open("users/" .. "users", "w")
  257.                 userWrite.writeLine(username)
  258.                 userWrite.close()
  259.             end
  260.            
  261.             term.setCursorPos(12, 15)
  262.             term.clearLine()
  263.             term.write("User was created succesfully")
  264.  
  265.             sleep(3)
  266.            
  267.             -- Redirect the page to the game page and send along the user
  268.             shell.run("monitor left screens/diamonds", username)
  269.  
  270.         end    
  271.      end
  272.     save()
  273. end
  274.  
  275. -- This function draws the page for the load game page
  276. local function drawLoadGame()
  277.     paintutils.drawFilledBox(0, 0, w, h, colors.black)
  278.     term.setTextColour(colours.white)
  279.     term.setBackgroundColor(colors.cyan)
  280.    
  281.     term.setCursorPos(1, 1)
  282.     term.write("      ")
  283.     term.setCursorPos(1, 2)
  284.     term.write(" Back ")
  285.     term.setCursorPos(1, 3)
  286.     term.write("      ")
  287.    
  288.     term.setBackgroundColor(colors.black)
  289.     term.setCursorPos(20, 7)
  290.     term.write("Load Game")
  291.    
  292.     term.setCursorPos(0,8)
  293.     local x, y = term.getCursorPos()
  294.     x = math.max(math.floor((w / 2) - (15)), 0)
  295.     term.setCursorPos(x,y)
  296.     term.write("(Please type in the computer)")
  297.    
  298.     term.setCursorPos(15, 10)
  299.     term.write("Username: ")
  300.     local username = read()
  301.        
  302.     term.setCursorPos(15, 12)
  303.     term.write("Password: ")
  304.     local password = read("*")
  305.    
  306.     -- This function will load the user from their save file
  307.     local function load()
  308.        
  309.         -- If the users file exists, load the save
  310.         if fs.exists("users/" .. username .. "_save") then
  311.            
  312.             -- Load the users save file
  313.             local hRead = fs.open("users/" .. username .. "_save", "r")
  314.            
  315.             -- Get the users hashed password and salt
  316.             if hRead then
  317.                 readPass = hRead.readLine()
  318.                 readSalt = hRead.readLine()
  319.             end
  320.            
  321.             -- Hash the inputted password with the users salt
  322.             hashedPass = sha.pbkdf2(password, readSalt, 16):toHex()
  323.            
  324.             -- If the hashed inputted password is the same as the saved one, load the users game
  325.             if readPass == hashedPass then
  326.                 shell.run("monitor left screens/diamonds", username)
  327.             else
  328.                
  329.                 -- If the password is not correct, let the user know and restart the load function
  330.                 term.setCursorPos(13, 15)
  331.                 term.clearLine()
  332.                 term.write("That password was incorrect")
  333.                
  334.                 term.setCursorPos(15, 10)
  335.                 term.clearLine()
  336.                
  337.                 term.setCursorPos(15, 12)
  338.                 term.clearLine()
  339.  
  340.                 term.setCursorPos(15, 10)
  341.                 term.write("Username: ")
  342.                 local username = read()
  343.  
  344.                 term.setCursorPos(15, 12)
  345.                 term.write("Password: ")
  346.                 local password = read("*")
  347.  
  348.                 load()
  349.             end
  350.         else
  351.            
  352.             -- If the user does not have a save file, let the user know and restart the load function
  353.             term.setCursorPos(13, 15)
  354.             term.clearLine()
  355.             term.write("That user does not exist!")
  356.  
  357.             term.setCursorPos(15, 10)
  358.             term.clearLine()
  359.  
  360.             term.setCursorPos(15, 12)
  361.             term.clearLine()
  362.  
  363.             term.setCursorPos(15, 10)
  364.             term.write("Username: ")
  365.             local username = read()
  366.  
  367.             term.setCursorPos(15, 12)
  368.             term.write("Password: ")
  369.             local password = read("*")
  370.  
  371.             load()
  372.  
  373.         end
  374.     end
  375.    
  376.     load()
  377. end
  378.  
  379. -- This function draws the page for the leaderboard page
  380. local function drawLeaderboard()
  381.     paintutils.drawFilledBox(0, 0, w, h, colors.black)
  382.     term.setTextColour(colours.white)
  383.     term.setBackgroundColor(colors.cyan)
  384.    
  385.     term.setCursorPos(1, 1)
  386.     term.write("      ")
  387.     term.setCursorPos(1, 2)
  388.     term.write(" Back ")
  389.     term.setCursorPos(1, 3)
  390.     term.write("      ")
  391.    
  392.     term.setBackgroundColor(colors.black)
  393.     term.setCursorPos(20, 5)
  394.     term.write("Leaderboard")
  395.    
  396.     -- If the users file exists, load it
  397.     if fs.exists("users/" .. "users") then
  398.        
  399.         -- Load the file that contains all of the usernames
  400.         local userRead = fs.open("users/" .. "users", "r")
  401.        
  402.         -- Get all of the usernames
  403.         local lines = {}
  404.         while true do
  405.             local line = userRead.readLine()
  406.  
  407.             if not line then break end
  408.            
  409.             -- Add the diamonds and the username into a table
  410.             local fileRead = fs.open("saves/" .. line .. "_save_file", "r")
  411.             local cps = tonumber(fileRead.readLine())
  412.             local diamonds = tonumber(fileRead.readLine())
  413.             fileRead.close()
  414.  
  415.             lines[diamonds] = line
  416.         end
  417.        
  418.         -- Sort all of the users by highest score
  419.         function getKeysSortedByValue(tbl, sortFunction)
  420.             local keys = {}
  421.             for key in pairs(tbl) do
  422.                 table.insert(keys, key)
  423.             end
  424.  
  425.             table.sort(keys, function(a, b)
  426.                 return sortFunction(tbl[a], tbl[b])
  427.             end)
  428.  
  429.             return keys
  430.         end
  431.        
  432.         -- Sort the users table
  433.         local userTable = getKeysSortedByValue(lines, function(a, b) return a > b end)
  434.        
  435.         local count = 0
  436.        
  437.         -- Display the top 5 users and their scores
  438.         for _, key in pairs(userTable) do
  439.             count = count + 1
  440.             if count <= 5 then
  441.                 term.setCursorPos(15, 7 + count * 2)
  442.                 term.write(count .. ". " .. displayNumber(key) .. " " .. lines[key])
  443.             else
  444.                 break
  445.             end
  446.        
  447.         end
  448.        
  449.     else
  450.        
  451.         -- If no users have played yet, display that instead
  452.         term.setCursorPos(12, 9)
  453.         term.write("No scores have been recorded")
  454.     end
  455. end
  456.  
  457. drawTitle()
  458.  
  459. -- Get the touch inputs and redirect accordingly
  460. while true do
  461.  
  462.     local event, side, x, y = os.pullEventRaw()
  463.  
  464.     if event == "monitor_touch" or event == "mouse_click" then
  465.  
  466.         if page == "title" then
  467.            
  468.             if ((x >= 13 and x <= 22) and (y >= 16 and y <= 18)) then
  469.                 page = "new"
  470.                 drawNewGame()
  471.             elseif ((x >= 27 and x <= 37) and (y >= 16 and y <= 18)) then
  472.                 page = "load"
  473.                 drawLoadGame()
  474.             elseif ((x >= 45 and x <= 47) and (y >= 16 and y <= 18)) then
  475.                 page = "leaderboard"
  476.                 drawLeaderboard()
  477.             end
  478.          elseif page == "new" then
  479.             if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
  480.                 page = "title"
  481.                 drawTitle()
  482.             end
  483.          elseif page == "load" then
  484.             if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
  485.                 page = "title"
  486.                 drawTitle()
  487.             end
  488.          elseif page == "leaderboard" then
  489.             if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
  490.                 page = "title"
  491.                 drawTitle()
  492.             end
  493.          end
  494.     end  
  495. end
Add Comment
Please, Sign In to add comment