Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Get the terminal size. Helpful for displaying text
- local w, h = term.getSize()
- -- Load in Anavrins sha api
- local sha = require "/apis/sha"
- -- Set up the page that we're currently on
- local page = "title"
- -- This function will be used to draw the title page when the page is set to title
- local function drawTitle()
- -- Paint the background black
- paintutils.drawFilledBox(0, 0, w, h, colors.black)
- -- Set up the cursor position to write the "Diamond" text
- term.setCursorPos(0,3)
- x, y = term.getCursorPos()
- x = math.max(math.floor((w / 2) - (40 / 2)), 0)
- term.setCursorPos(x,y)
- -- Diamond Text
- -- D
- paintutils.drawLine(x, y, x, y + 4, colors.cyan)
- paintutils.drawLine(x + 1, y, x + 3, y, colors.cyan)
- paintutils.drawLine(x + 1, y + 4, x + 3, y + 4, colors.cyan)
- paintutils.drawLine(x + 4, y + 1, x + 4, y + 3, colors.cyan)
- -- I
- paintutils.drawLine(x + 6, y, x + 10, y, colors.cyan)
- paintutils.drawLine(x + 6, y + 4, x + 10, y + 4, colors.cyan)
- paintutils.drawLine(x + 8, y + 1, x + 8, y + 3, colors.cyan)
- -- A
- paintutils.drawLine(x + 13, y, x + 15, y, colors.cyan)
- paintutils.drawLine(x + 12, y + 1, x + 12, y + 4, colors.cyan)
- paintutils.drawLine(x + 16, y + 1, x + 16, y + 4, colors.cyan)
- paintutils.drawLine(x + 13, y + 2, x + 15, y + 2, colors.cyan)
- -- M
- paintutils.drawLine(x + 18, y, x + 18, y + 4, colors.cyan)
- paintutils.drawLine(x + 22, y, x + 22, y + 4, colors.cyan)
- paintutils.drawPixel(x + 19, y + 1, colors.cyan)
- paintutils.drawPixel(x + 20, y + 2, colors.cyan)
- paintutils.drawPixel(x + 21, y + 1, colors.cyan)
- -- O
- paintutils.drawBox(x + 24, y, x + 28, y + 4, colors.cyan)
- -- N
- paintutils.drawLine(x + 30, y, x + 30, y + 4, colors.cyan)
- paintutils.drawLine(x + 34, y, x + 34, y + 4, colors.cyan)
- paintutils.drawPixel(x + 31, y + 1, colors.cyan)
- paintutils.drawPixel(x + 32, y + 2, colors.cyan)
- paintutils.drawPixel(x + 33, y + 3, colors.cyan)
- -- D
- paintutils.drawLine(x + 36, y, x + 36, y + 4, colors.cyan)
- paintutils.drawLine(x + 37, y, x + 39, y, colors.cyan)
- paintutils.drawLine(x + 37, y + 4, x + 39, y + 4, colors.cyan)
- paintutils.drawLine(x + 40, y + 1, x + 40, y + 3, colors.cyan)
- -- Digger text
- y = y + 6
- x = math.max(math.floor((w / 2) - (35 / 2)), 0)
- -- D
- paintutils.drawLine(x, y, x, y + 4, colors.cyan)
- paintutils.drawLine(x + 1, y, x + 3, y, colors.cyan)
- paintutils.drawLine(x + 1, y + 4, x + 3, y + 4, colors.cyan)
- paintutils.drawLine(x + 4, y + 1, x + 4, y + 3, colors.cyan)
- -- I
- paintutils.drawLine(x + 6, y, x + 10, y, colors.cyan)
- paintutils.drawLine(x + 6, y + 4, x + 10, y + 4, colors.cyan)
- paintutils.drawLine(x + 8, y + 1, x + 8, y + 3, colors.cyan)
- -- G
- paintutils.drawLine(x + 12, y, x + 12, y + 4, colors.cyan)
- paintutils.drawLine(x + 13, y, x + 17, y, colors.cyan)
- paintutils.drawLine(x + 13, y + 4, x + 17, y + 4, colors.cyan)
- paintutils.drawLine(x + 14, y + 2, x + 16, y + 2, colors.cyan)
- paintutils.drawPixel(x + 17, y + 3, colors.cyan)
- -- G
- paintutils.drawLine(x + 19, y, x + 19, y + 4, colors.cyan)
- paintutils.drawLine(x + 20, y, x + 24, y, colors.cyan)
- paintutils.drawLine(x + 19, y + 4, x + 24, y + 4, colors.cyan)
- paintutils.drawLine(x + 21, y + 2, x + 23, y + 2, colors.cyan)
- paintutils.drawPixel(x + 24, y + 3, colors.cyan)
- -- E
- paintutils.drawLine(x + 26, y, x + 26, y + 4, colors.cyan)
- paintutils.drawLine(x + 27, y, x + 30, y, colors.cyan)
- paintutils.drawLine(x + 27, y + 4, x + 30, y + 4, colors.cyan)
- paintutils.drawLine(x + 27, y + 2, x + 28, y + 2, colors.cyan)
- -- R
- paintutils.drawLine(x + 32, y, x + 32, y + 4, colors.cyan)
- paintutils.drawLine(x + 33, y, x + 35, y, colors.cyan)
- paintutils.drawLine(x + 33, y + 3, x + 35, y + 3, colors.cyan)
- paintutils.drawLine(x + 36, y + 1, x + 36, y + 2, colors.cyan)
- paintutils.drawPixel(x + 36, y + 4, colors.cyan)
- term.setTextColour(colours.white)
- term.setBackgroundColor(colors.cyan)
- -- New Game button
- y = y + 7
- x = math.max(math.floor((w / 2) - (12)), 0)
- term.setCursorPos(x, y)
- term.write(" ")
- term.setCursorPos(x, y + 1)
- term.write(" New Game ")
- term.setCursorPos(x, y + 2)
- term.write(" ")
- -- Load Game button
- x = math.max(math.floor((w / 2) + (2)), 0)
- term.setCursorPos(x, y)
- term.write(" ")
- term.setCursorPos(x, y + 1)
- term.write(" Load Game ")
- term.setCursorPos(x, y + 2)
- term.write(" ")
- -- Leaderboard button
- x = math.max(math.floor((w) - (5)), 0)
- paintutils.drawPixel(x, y + 2, colors.brown)
- paintutils.drawLine(x + 1, y, x + 1, y + 2, colors.orange)
- paintutils.drawLine(x + 2, y + 1, x + 2, y + 2, colors.lightGray)
- end
- -- Use this function to correctly display a number.
- -- num is the number to format, if isInt is true, it will round to the nearest int
- local function displayNumber(num, isInt)
- local num = num or 0
- local isInt = isInt or false
- local newNum = 0
- if num >= 1000000 then
- newNum = num / 1000000
- newNum = math.floor(newNum * 100)/100
- newNum = tostring(newNum)
- if isInt == false then
- newNum = string.format("%.1f", newNum) .. "M"
- end
- elseif num >= 1000 then
- newNum = num / 1000
- newNum = math.floor(newNum * 100)/100
- newNum = tostring(newNum)
- if isInt == false then
- newNum = string.format("%.1f", newNum) .. "K"
- end
- else
- newNum = tostring(num)
- if isInt == false then
- newNum = string.format("%.1f", newNum)
- end
- end
- return newNum
- end
- -- This function draws the page for the new game page
- local function drawNewGame()
- paintutils.drawFilledBox(0, 0, w, h, colors.black)
- term.setTextColour(colours.white)
- term.setBackgroundColor(colors.cyan)
- term.setCursorPos(1, 1)
- term.write(" ")
- term.setCursorPos(1, 2)
- term.write(" Back ")
- term.setCursorPos(1, 3)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(21, 7)
- term.write("New Game")
- term.setCursorPos(0,8)
- local x, y = term.getCursorPos()
- x = math.max(math.floor((w / 2) - (15)), 0)
- term.setCursorPos(x,y)
- term.write("(Please type in the computer)")
- term.setCursorPos(15, 10)
- term.write("Username: ")
- local username = read()
- term.setCursorPos(15, 12)
- term.write("Password: ")
- local password = read("*")
- -- This function will add the user to the system
- function save()
- -- If the user file exists, let the user know and run the save function again
- if fs.exists("users/" .. username .. "_save") then
- term.setCursorPos(13, 15)
- term.clearLine()
- term.write("That user already exists!")
- term.setCursorPos(15, 10)
- term.clearLine()
- term.setCursorPos(15, 12)
- term.clearLine()
- term.setCursorPos(15, 10)
- term.write("Username: ")
- local username = read()
- term.setCursorPos(15, 12)
- term.write("Password: ")
- local password = read("*")
- save()
- else
- -- Add in a unique salt for password hashing (Thanks PG231)
- local salt = sha.digest("i7gBq4p7p+uOCuuWvrlPgg|" .. os.epoch("utc") .. "|" .. math.random(0, 2^31 - 2)):toHex()
- -- Use the sha api to generate a hash to save for password protection
- password = sha.pbkdf2(password, salt, 16):toHex()
- -- Save the users hashed password and salt to their save file
- local hWrite = fs.open("users/" .. username .. "_save", "w")
- hWrite.writeLine(password)
- hWrite.writeLine(salt)
- hWrite.close()
- -- If there is a users file already, add this user to the list
- if fs.exists("users/" .. "users") then
- local userRead = fs.open("users/" .. "users", "r")
- local lines = {}
- while true do
- local line = userRead.readLine()
- if not line then break end
- lines[#lines + 1] = line
- end
- userRead.close()
- local userWrite = fs.open("users/" .. "users", "w")
- for i, name in pairs(lines) do
- userWrite.writeLine(name)
- end
- userWrite.writeLine(username)
- userWrite.close()
- else
- -- If this is the first user, create the users file
- local userWrite = fs.open("users/" .. "users", "w")
- userWrite.writeLine(username)
- userWrite.close()
- end
- term.setCursorPos(12, 15)
- term.clearLine()
- term.write("User was created succesfully")
- sleep(3)
- -- Redirect the page to the game page and send along the user
- shell.run("monitor left screens/diamonds", username)
- end
- end
- save()
- end
- -- This function draws the page for the load game page
- local function drawLoadGame()
- paintutils.drawFilledBox(0, 0, w, h, colors.black)
- term.setTextColour(colours.white)
- term.setBackgroundColor(colors.cyan)
- term.setCursorPos(1, 1)
- term.write(" ")
- term.setCursorPos(1, 2)
- term.write(" Back ")
- term.setCursorPos(1, 3)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(20, 7)
- term.write("Load Game")
- term.setCursorPos(0,8)
- local x, y = term.getCursorPos()
- x = math.max(math.floor((w / 2) - (15)), 0)
- term.setCursorPos(x,y)
- term.write("(Please type in the computer)")
- term.setCursorPos(15, 10)
- term.write("Username: ")
- local username = read()
- term.setCursorPos(15, 12)
- term.write("Password: ")
- local password = read("*")
- -- This function will load the user from their save file
- local function load()
- -- If the users file exists, load the save
- if fs.exists("users/" .. username .. "_save") then
- -- Load the users save file
- local hRead = fs.open("users/" .. username .. "_save", "r")
- -- Get the users hashed password and salt
- if hRead then
- readPass = hRead.readLine()
- readSalt = hRead.readLine()
- end
- -- Hash the inputted password with the users salt
- hashedPass = sha.pbkdf2(password, readSalt, 16):toHex()
- -- If the hashed inputted password is the same as the saved one, load the users game
- if readPass == hashedPass then
- shell.run("monitor left screens/diamonds", username)
- else
- -- If the password is not correct, let the user know and restart the load function
- term.setCursorPos(13, 15)
- term.clearLine()
- term.write("That password was incorrect")
- term.setCursorPos(15, 10)
- term.clearLine()
- term.setCursorPos(15, 12)
- term.clearLine()
- term.setCursorPos(15, 10)
- term.write("Username: ")
- local username = read()
- term.setCursorPos(15, 12)
- term.write("Password: ")
- local password = read("*")
- load()
- end
- else
- -- If the user does not have a save file, let the user know and restart the load function
- term.setCursorPos(13, 15)
- term.clearLine()
- term.write("That user does not exist!")
- term.setCursorPos(15, 10)
- term.clearLine()
- term.setCursorPos(15, 12)
- term.clearLine()
- term.setCursorPos(15, 10)
- term.write("Username: ")
- local username = read()
- term.setCursorPos(15, 12)
- term.write("Password: ")
- local password = read("*")
- load()
- end
- end
- load()
- end
- -- This function draws the page for the leaderboard page
- local function drawLeaderboard()
- paintutils.drawFilledBox(0, 0, w, h, colors.black)
- term.setTextColour(colours.white)
- term.setBackgroundColor(colors.cyan)
- term.setCursorPos(1, 1)
- term.write(" ")
- term.setCursorPos(1, 2)
- term.write(" Back ")
- term.setCursorPos(1, 3)
- term.write(" ")
- term.setBackgroundColor(colors.black)
- term.setCursorPos(20, 5)
- term.write("Leaderboard")
- -- If the users file exists, load it
- if fs.exists("users/" .. "users") then
- -- Load the file that contains all of the usernames
- local userRead = fs.open("users/" .. "users", "r")
- -- Get all of the usernames
- local lines = {}
- while true do
- local line = userRead.readLine()
- if not line then break end
- -- Add the diamonds and the username into a table
- local fileRead = fs.open("saves/" .. line .. "_save_file", "r")
- local cps = tonumber(fileRead.readLine())
- local diamonds = tonumber(fileRead.readLine())
- fileRead.close()
- lines[diamonds] = line
- end
- -- Sort all of the users by highest score
- function getKeysSortedByValue(tbl, sortFunction)
- local keys = {}
- for key in pairs(tbl) do
- table.insert(keys, key)
- end
- table.sort(keys, function(a, b)
- return sortFunction(tbl[a], tbl[b])
- end)
- return keys
- end
- -- Sort the users table
- local userTable = getKeysSortedByValue(lines, function(a, b) return a > b end)
- local count = 0
- -- Display the top 5 users and their scores
- for _, key in pairs(userTable) do
- count = count + 1
- if count <= 5 then
- term.setCursorPos(15, 7 + count * 2)
- term.write(count .. ". " .. displayNumber(key) .. " " .. lines[key])
- else
- break
- end
- end
- else
- -- If no users have played yet, display that instead
- term.setCursorPos(12, 9)
- term.write("No scores have been recorded")
- end
- end
- drawTitle()
- -- Get the touch inputs and redirect accordingly
- while true do
- local event, side, x, y = os.pullEventRaw()
- if event == "monitor_touch" or event == "mouse_click" then
- if page == "title" then
- if ((x >= 13 and x <= 22) and (y >= 16 and y <= 18)) then
- page = "new"
- drawNewGame()
- elseif ((x >= 27 and x <= 37) and (y >= 16 and y <= 18)) then
- page = "load"
- drawLoadGame()
- elseif ((x >= 45 and x <= 47) and (y >= 16 and y <= 18)) then
- page = "leaderboard"
- drawLeaderboard()
- end
- elseif page == "new" then
- if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
- page = "title"
- drawTitle()
- end
- elseif page == "load" then
- if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
- page = "title"
- drawTitle()
- end
- elseif page == "leaderboard" then
- if ((x >= 1 and x <= 6) and (y >= 1 and y <= 3)) then
- page = "title"
- drawTitle()
- end
- end
- end
- end
Add Comment
Please, Sign In to add comment