Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Casino Central Computer
- -- Optimized for 2x2 Advanced Monitor with reset card functionality
- -- Initialize monitor and save original terminal
- local monitor = peripheral.find("monitor")
- local oldTerm = term.current()
- if monitor then
- monitor.setTextScale(0.5) -- Optimal text scale for 2x2 advanced monitor
- monitor.clear()
- end
- -- Find disk drive
- local function findDiskDrive()
- local sides = {"top", "bottom", "left", "right", "front", "back"}
- for _, side in ipairs(sides) do
- if peripheral.getType(side) == "drive" then
- return side
- end
- end
- return nil
- end
- local diskDrive = findDiskDrive()
- -- Colors
- local COLORS = {
- background = colors.black,
- header = colors.blue,
- headerText = colors.white,
- primary = colors.white,
- highlight = colors.yellow,
- success = colors.lime,
- error = colors.red,
- balance = colors.lime,
- balanceNegative = colors.red,
- button = colors.lightGray,
- buttonText = colors.black,
- buttonHighlight = colors.yellow,
- resetButton = colors.red,
- resetText = colors.white
- }
- local function centerText(text, y, color, targetTerm)
- targetTerm = targetTerm or term.current()
- local w, h = targetTerm.getSize()
- local x = math.floor((w - #text) / 2) + 1
- if y then
- targetTerm.setCursorPos(x, y)
- if color then
- targetTerm.setTextColor(color)
- end
- targetTerm.write(text)
- end
- return x
- end
- local function drawBox(x, y, width, height, bgColor, fgColor, targetTerm)
- targetTerm = targetTerm or term.current()
- targetTerm.setBackgroundColor(bgColor or COLORS.button)
- targetTerm.setTextColor(fgColor or COLORS.buttonText)
- for i = 1, height do
- targetTerm.setCursorPos(x, y + i - 1)
- targetTerm.write(string.rep(" ", width))
- end
- end
- local function drawButton(x, y, text, bgColor, fgColor, targetTerm)
- targetTerm = targetTerm or term.current()
- local padding = 2
- local width = #text + (padding * 2)
- drawBox(x, y, width, 3, bgColor, fgColor, targetTerm)
- targetTerm.setCursorPos(x + padding, y + 1)
- targetTerm.write(text)
- return {
- x1 = x,
- y1 = y,
- x2 = x + width,
- y2 = y + 2,
- text = text
- }
- end
- local function isInside(x, y, button)
- return x >= button.x1 and x <= button.x2 and
- y >= button.y1 and y <= button.y2
- end
- local function drawHeader(targetTerm)
- targetTerm = targetTerm or term.current()
- local w, h = targetTerm.getSize()
- -- Header background
- targetTerm.setBackgroundColor(COLORS.header)
- targetTerm.setTextColor(COLORS.headerText)
- for i = 1, 3 do
- targetTerm.setCursorPos(1, i)
- targetTerm.write(string.rep(" ", w))
- end
- -- Casino logo/title
- local title = "CASINO" -- Shortened for 2x2 monitor
- centerText(title, 2, COLORS.headerText, targetTerm)
- -- Reset for main content
- targetTerm.setBackgroundColor(COLORS.background)
- end
- local function waitForDisk()
- if not diskDrive then
- if monitor then term.redirect(monitor) end
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.error)
- centerText("ERROR: No disk drive", 6)
- centerText("Connect a disk drive", 8)
- -- Wait for disk drive to be connected
- while not diskDrive do
- sleep(1)
- diskDrive = findDiskDrive()
- end
- end
- if monitor then term.redirect(monitor) end
- local w, h = term.getSize()
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.highlight)
- centerText("Welcome to the Casino", 5)
- centerText("Please insert your", 7)
- centerText("player card", 8)
- -- Draw a card slot visual (simplified for 2x2 monitor)
- local slotWidth = 16
- local slotX = math.floor((w - slotWidth) / 2)
- local slotY = 10
- drawBox(slotX, slotY, slotWidth, 3, colors.gray)
- drawBox(slotX + 1, slotY + 1, slotWidth - 2, 1, colors.black)
- centerText("INSERT CARD", slotY + 4, COLORS.highlight)
- -- Wait for disk to be inserted
- while not disk.isPresent(diskDrive) do
- sleep(0.5)
- -- Check if disk drive is still connected
- if not peripheral.isPresent(diskDrive) then
- diskDrive = findDiskDrive()
- if not diskDrive then
- term.setTextColor(COLORS.error)
- centerText("Disk drive disconnected!", slotY + 6)
- sleep(1)
- return waitForDisk() -- Restart the process
- end
- end
- end
- term.setTextColor(COLORS.success)
- centerText("Card detected!", slotY + 6)
- sleep(1)
- return diskDrive
- end
- local function initializeNewCard(drive)
- -- First display the instructions on the monitor
- if monitor then term.redirect(monitor) end
- local w, h = term.getSize()
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.primary)
- centerText("New player card", 5)
- centerText("Enter your name on", 7)
- centerText("the computer", 8)
- -- Switch back to the computer terminal for input
- term.redirect(oldTerm)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.yellow)
- print("CASINO REGISTRATION")
- print("-----------------")
- print("")
- term.setTextColor(colors.white)
- print("Please enter your name:")
- local name = read()
- -- Create player data
- local playerData = {
- name = name,
- credits = 50000,
- created = os.day(),
- lastPlayed = os.day(),
- games = {}
- }
- -- Save to disk
- local file = fs.open(disk.getMountPath(drive) .. "/player.dat", "w")
- file.write(textutils.serialize(playerData))
- file.close()
- -- Switch back to monitor to show confirmation
- if monitor then term.redirect(monitor) end
- term.setTextColor(COLORS.success)
- centerText("Account created!", 12)
- centerText("Balance: 50,000", 13)
- sleep(2)
- return playerData
- end
- local function resetCard(drive)
- if monitor then term.redirect(monitor) end
- local w, h = term.getSize()
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.highlight)
- centerText("Reset Card", 5)
- centerText("Are you sure?", 7)
- centerText("This will erase all data", 9)
- -- Confirm/Cancel buttons
- local confirmButton = drawButton(5, 12, "Reset", COLORS.resetButton, COLORS.resetText)
- local cancelButton = drawButton(w - 15, 12, "Cancel", COLORS.button, COLORS.buttonText)
- -- Check for card removal during confirmation
- local cardRemoved = false
- while true do
- -- Set up a timer to check for card removal
- local cardTimer = os.startTimer(0.5)
- local event, param, x, y = os.pullEvent()
- if event == "timer" and param == cardTimer then
- -- Check if card is still present
- if not disk.isPresent(drive) then
- cardRemoved = true
- break
- end
- elseif event == "monitor_touch" then
- if isInside(x, y, confirmButton) then
- -- Delete player data file
- local path = disk.getMountPath(drive) .. "/player.dat"
- if fs.exists(path) then
- fs.delete(path)
- end
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.success)
- centerText("Card has been reset", 8)
- centerText("Creating new account...", 10)
- sleep(2)
- -- Create new player data
- return initializeNewCard(drive)
- elseif isInside(x, y, cancelButton) then
- -- Load existing data
- local file = fs.open(disk.getMountPath(drive) .. "/player.dat", "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- return data
- end
- end
- end
- -- If card was removed during confirmation, return nil to signal this
- if cardRemoved then
- return nil
- end
- end
- local function loadPlayerData(drive)
- local path = disk.getMountPath(drive) .. "/player.dat"
- if not fs.exists(path) then
- return initializeNewCard(drive)
- end
- local file = fs.open(path, "r")
- local data = textutils.unserialize(file.readAll())
- file.close()
- -- Update last played
- data.lastPlayed = os.day()
- -- Save updated data
- local file = fs.open(path, "w")
- file.write(textutils.serialize(data))
- file.close()
- return data
- end
- local function formatCredits(amount)
- local formatted = tostring(amount)
- local pos = #formatted - 3
- while pos > 0 do
- formatted = formatted:sub(1, pos) .. "," .. formatted:sub(pos + 1)
- pos = pos - 3
- end
- return formatted
- end
- local function showMainMenu(playerData, drive)
- if monitor then term.redirect(monitor) end
- local w, h = term.getSize()
- while disk.isPresent(drive) do
- term.clear()
- drawHeader()
- -- Welcome and balance (shortened for 2x2 monitor)
- term.setTextColor(COLORS.primary)
- centerText("Welcome", 6)
- centerText(playerData.name, 8)
- -- Balance display (centered and prominent)
- centerText("Balance:", 11, COLORS.primary)
- local balanceColor = playerData.credits >= 0 and COLORS.balance or COLORS.balanceNegative
- centerText(formatCredits(playerData.credits), 13, balanceColor)
- -- Done and Reset buttons
- local doneButton = drawButton(5, h - 5, "Done", COLORS.button, COLORS.buttonText)
- local resetButton = drawButton(w - 15, h - 5, "Reset", COLORS.resetButton, COLORS.resetText)
- -- Set up a timer to check for card removal
- local cardTimer = os.startTimer(0.5)
- -- Wait for button click or card removal
- local event, param, x, y = os.pullEvent()
- if event == "timer" and param == cardTimer then
- -- Just continue the loop which will check if the card is present
- elseif event == "monitor_touch" then
- -- Handle touch events
- if isInside(x, y, doneButton) then
- -- Show exit message
- term.clear()
- drawHeader()
- term.setTextColor(COLORS.highlight)
- centerText("Thank you!", 8)
- centerText("Remove your card", 10)
- -- Wait for disk to be removed
- while disk.isPresent(drive) do
- sleep(0.5)
- -- Check if disk drive is still connected
- if not peripheral.isPresent(drive) then
- diskDrive = findDiskDrive()
- break
- end
- end
- return -- Exit to main loop
- elseif isInside(x, y, resetButton) then
- local newPlayerData = resetCard(drive)
- -- If resetCard returns nil, it means the card was removed during reset
- if newPlayerData == nil then
- return -- Exit to main loop
- else
- playerData = newPlayerData
- end
- end
- end
- end
- -- If we get here, the card was removed
- return
- end
- -- Main program loop - runs continuously
- while true do
- local drive = waitForDisk()
- local playerData = loadPlayerData(drive)
- showMainMenu(playerData, drive)
- -- No need for additional exit message here since it's handled in showMainMenu
- -- Just continue the loop to go back to waiting for a disk
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement