Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Casino Card for Casino Machines --
- -- Created by iiAmPanda --
- -- pastebin get cDaurxvD casinocard(wip) --
- local collectorFee = 10
- local creds = 0
- local credsPerDia = 75
- local credsPerGoldIngot = 45
- local credsPerGoldNugget = 5
- local playerName = ""
- local originalDirection = 1 -- 1: north, 2: east, 3: south, 4: west
- -- Function to center text on the screen
- function centerText(y, text)
- local width = term.getSize()
- term.setCursorPos(math.floor((width - #text) / 2), y)
- term.write(text)
- end
- -- Function to wrap and display text within the screen width
- function wrapText(y, text)
- local width = term.getSize()
- for line in text:gmatch(".{1," .. width .. "}") do
- term.setCursorPos(1, y)
- term.write(line)
- y = y + 1
- end
- return y -- Return the new y position after writing
- end
- -- Function to check if the disk is a Casino Card
- function checkForCasinoCard()
- return fs.exists("disk/creds.lua")
- end
- -- Function to write card data to disk and update the disk label with credits
- function writeCard()
- local Card = fs.open("disk/creds.lua", "w")
- if not Card then
- centerText(8, "Error writing to disk!")
- sleep(2)
- return
- end
- local data = (tostring(math.random(1, 163456)) .. "11066011" .. tostring(creds) .. "11077011" .. tostring(math.random(1, 163456)))
- local file = fs.open("disk/name.txt", "w")
- file.write(playerName)
- file.close()
- Card.write(data)
- Card.close()
- -- Update the disk label to include current credits and player's name
- disk.setLabel("right",playerName .. "'s Casino Card - " .. tostring(creds) .. "C")
- end
- -- Function to read card data from the disk
- function readCard()
- if not fs.exists("disk/creds.lua") then
- return false
- end
- local Card = fs.open("disk/creds.lua", "r")
- if not Card then
- centerText(8, "Error reading from disk!")
- sleep(2)
- return false
- end
- local data = Card.readAll()
- Card.close()
- local a, b = string.find(data, "11066011")
- local c, d = string.find(data, "11077011")
- creds = tonumber(string.sub(data, b + 1, c - 1))
- return true
- end
- -- Function to insert a card
- function insertCard()
- while true do
- term.clear()
- centerText(6, "Please insert your Casino Card.")
- sleep(1)
- if fs.exists("disk/") then
- term.clear()
- if readCard() then
- -- Rename the disk to reflect player credits and name
- disk.setLabel("right",playerName .. "'s Casino Card - " .. tostring(creds) .. "C")
- break -- Exit the loop if a valid card is inserted
- else
- centerText(8, "This isn't a Casino Card.")
- sleep(2)
- centerText(10, "Would you like to create one? (Y/N)")
- local event, key = os.pullEvent("key")
- if key == keys.y or key == keys.enter then
- term.clear()
- centerText(7, "Enter your name:")
- playerName = read() -- Prompt for player's name
- centerText(8, "Creating a new Casino Card...")
- creds = 0 -- Start with 0 credits
- writeCard()
- centerText(9, "Casino Card created.")
- sleep(2)
- else
- centerText(10, "Please remove the disk.")
- sleep(2)
- end
- end
- end
- end
- end
- -- Function to check credit balance
- function checkBalance()
- term.clear()
- centerText(4, "Your Balance: " .. creds .. " Credits")
- centerText(6, "Press any key to continue...")
- os.pullEvent("key")
- end
- -- Function to deposit credits
- function depositCredits()
- if not checkForCasinoCard() then
- centerText(10, "No Casino Card Found.")
- sleep(2)
- return
- end
- term.clear()
- readCard()
- centerText(4, "Deposit Credits")
- centerText(6, "Diamonds, Gold Ingots, or Gold Nuggets.")
- local totalCreds = 0
- -- Function to find the chest behind the turtle
- local function findChest()
- originalDirection = 1 -- Reset to original direction
- for _ = 1, 4 do
- if turtle.suck(0) then
- return true -- Chest found
- end
- turtle.turnLeft() -- Keep turning until a chest is found
- end
- -- Restore original direction
- turtle.turnRight() -- Restore to original facing direction
- return false -- No chest found
- end
- if not findChest() then
- centerText(8, "No chest found behind the turtle!")
- sleep(2)
- return
- end
- for i = 1, 16 do
- if turtle.getItemCount(i) > 0 then
- local slot = turtle.getItemDetail(i)
- if slot.name == "minecraft:diamond" then
- totalCreds = totalCreds + (slot.count * credsPerDia)
- turtle.select(i)
- turtle.drop(slot.count) -- Drop diamonds to deposit
- elseif slot.name == "minecraft:gold_ingot" then
- totalCreds = totalCreds + (slot.count * credsPerGoldIngot)
- turtle.select(i)
- turtle.drop(slot.count) -- Drop gold ingots to deposit
- elseif slot.name == "minecraft:gold_nugget" then
- totalCreds = totalCreds + (slot.count * credsPerGoldNugget)
- turtle.select(i)
- turtle.drop(slot.count) -- Drop gold nuggets to deposit
- end
- end
- end
- if totalCreds > 0 then
- creds = creds + totalCreds
- writeCard() -- Update card data and disk label
- centerText(8, totalCreds .. " Credits deposited.")
- else
- centerText(8, "No items to deposit!")
- end
- centerText(10, "Press any key to continue...")
- local event, key = os.pullEvent("key")
- if key == keys.b then
- return
- end
- end
- -- Function to withdraw credits
- function withdrawCredits()
- if not checkForCasinoCard() then
- centerText(10, "No Casino Card Found.")
- sleep(2)
- return
- end
- term.clear()
- readCard()
- if creds <= 0 then -- Check if there are no credits available
- centerText(8, "The Bank is Empty!")
- centerText(10, "Press any key to return...")
- os.pullEvent("key") -- Wait for user input
- return
- end
- centerText(4, "Withdraw Credits")
- centerText(6, "Enter amount to withdraw:")
- local amount = tonumber(read())
- if amount and amount > 0 and amount <= creds then
- creds = creds - amount
- writeCard() -- Update card data and disk label
- -- Calculate how many items to withdraw based on credits
- local diamondsToWithdraw = math.floor(amount / credsPerDia)
- local goldIngotsToWithdraw = math.floor((amount % credsPerDia) / credsPerGoldIngot)
- local goldNuggetsToWithdraw = math.floor((amount % credsPerGoldIngot) / credsPerGoldNugget)
- -- Function to find the chest behind the turtle
- local function findChest()
- originalDirection = 1 -- Reset to original direction
- for _ = 1, 4 do
- if turtle.suck(0) then
- return true -- Chest found
- end
- turtle.turnLeft() -- Keep turning until a chest is found
- end
- -- Restore original direction
- turtle.turnRight() -- Restore to original facing direction
- return false -- No chest found
- end
- if not findChest() then
- centerText(8, "No chest found behind the turtle!")
- sleep(2)
- return
- end
- -- Withdraw diamonds
- for i = 1, diamondsToWithdraw do
- turtle.select(1) -- Ensure the turtle is ready to suck
- turtle.suck(1) -- Try to pull a diamond from the chest
- end
- -- Withdraw gold ingots
- for i = 1, goldIngotsToWithdraw do
- turtle.select(2) -- Ensure the turtle is ready to suck
- turtle.suck(1) -- Try to pull a gold ingot from the chest
- end
- -- Withdraw gold nuggets
- for i = 1, goldNuggetsToWithdraw do
- turtle.select(3) -- Ensure the turtle is ready to suck
- turtle.suck(1) -- Try to pull a gold nugget from the chest
- end
- centerText(8, "Withdrawn: " .. diamondsToWithdraw .. " Diamonds, " .. goldIngotsToWithdraw .. " Gold Ingots, " .. goldNuggetsToWithdraw .. " Gold Nuggets.")
- else
- centerText(8, "Invalid amount!")
- end
- centerText(10, "Press 'B' to go back or any key to continue...")
- local event, key = os.pullEvent("key")
- if key == keys.b then
- return
- end
- end
- -- Main program loop
- insertCard()
- while true do
- term.clear()
- centerText(4, "Welcome to the Casino!")
- centerText(6, "1. Check Balance")
- centerText(7, "2. Deposit Credits")
- centerText(8, "3. Withdraw Credits")
- centerText(9, "4. Exit")
- local event, key = os.pullEvent("key")
- if key == keys.one then
- checkBalance()
- elseif key == keys.two then
- depositCredits()
- elseif key == keys.three then
- withdrawCredits()
- elseif key == keys.four then
- insertCard() -- Direct to insert card screen
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment