Advertisement
alexnolan

signup

Mar 26th, 2025 (edited)
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.79 KB | None | 0 0
  1. -- Casino Central Computer
  2. -- Optimized for 2x2 Advanced Monitor with reset card functionality
  3.  
  4. -- Initialize monitor and save original terminal
  5. local monitor = peripheral.find("monitor")
  6. local oldTerm = term.current()
  7.  
  8. if monitor then
  9.   monitor.setTextScale(0.5) -- Optimal text scale for 2x2 advanced monitor
  10.   monitor.clear()
  11. end
  12.  
  13. -- Find disk drive
  14. local function findDiskDrive()
  15.   local sides = {"top", "bottom", "left", "right", "front", "back"}
  16.  
  17.   for _, side in ipairs(sides) do
  18.     if peripheral.getType(side) == "drive" then
  19.       return side
  20.     end
  21.   end
  22.  
  23.   return nil
  24. end
  25.  
  26. local diskDrive = findDiskDrive()
  27.  
  28. -- Colors
  29. local COLORS = {
  30.   background = colors.black,
  31.   header = colors.blue,
  32.   headerText = colors.white,
  33.   primary = colors.white,
  34.   highlight = colors.yellow,
  35.   success = colors.lime,
  36.   error = colors.red,
  37.   balance = colors.lime,
  38.   balanceNegative = colors.red,
  39.   button = colors.lightGray,
  40.   buttonText = colors.black,
  41.   buttonHighlight = colors.yellow,
  42.   resetButton = colors.red,
  43.   resetText = colors.white
  44. }
  45.  
  46. local function centerText(text, y, color, targetTerm)
  47.   targetTerm = targetTerm or term.current()
  48.   local w, h = targetTerm.getSize()
  49.   local x = math.floor((w - #text) / 2) + 1
  50.  
  51.   if y then
  52.     targetTerm.setCursorPos(x, y)
  53.     if color then
  54.       targetTerm.setTextColor(color)
  55.     end
  56.     targetTerm.write(text)
  57.   end
  58.  
  59.   return x
  60. end
  61.  
  62. local function drawBox(x, y, width, height, bgColor, fgColor, targetTerm)
  63.   targetTerm = targetTerm or term.current()
  64.   targetTerm.setBackgroundColor(bgColor or COLORS.button)
  65.   targetTerm.setTextColor(fgColor or COLORS.buttonText)
  66.  
  67.   for i = 1, height do
  68.     targetTerm.setCursorPos(x, y + i - 1)
  69.     targetTerm.write(string.rep(" ", width))
  70.   end
  71. end
  72.  
  73. local function drawButton(x, y, text, bgColor, fgColor, targetTerm)
  74.   targetTerm = targetTerm or term.current()
  75.   local padding = 2
  76.   local width = #text + (padding * 2)
  77.  
  78.   drawBox(x, y, width, 3, bgColor, fgColor, targetTerm)
  79.   targetTerm.setCursorPos(x + padding, y + 1)
  80.   targetTerm.write(text)
  81.  
  82.   return {
  83.     x1 = x,
  84.     y1 = y,
  85.     x2 = x + width,
  86.     y2 = y + 2,
  87.     text = text
  88.   }
  89. end
  90.  
  91. local function isInside(x, y, button)
  92.   return x >= button.x1 and x <= button.x2 and
  93.          y >= button.y1 and y <= button.y2
  94. end
  95.  
  96. local function drawHeader(targetTerm)
  97.   targetTerm = targetTerm or term.current()
  98.   local w, h = targetTerm.getSize()
  99.  
  100.   -- Header background
  101.   targetTerm.setBackgroundColor(COLORS.header)
  102.   targetTerm.setTextColor(COLORS.headerText)
  103.   for i = 1, 3 do
  104.     targetTerm.setCursorPos(1, i)
  105.     targetTerm.write(string.rep(" ", w))
  106.   end
  107.  
  108.   -- Casino logo/title
  109.   local title = "CASINO"  -- Shortened for 2x2 monitor
  110.   centerText(title, 2, COLORS.headerText, targetTerm)
  111.  
  112.   -- Reset for main content
  113.   targetTerm.setBackgroundColor(COLORS.background)
  114. end
  115.  
  116. local function waitForDisk()
  117.   if not diskDrive then
  118.     if monitor then term.redirect(monitor) end
  119.     term.clear()
  120.     drawHeader()
  121.     term.setTextColor(COLORS.error)
  122.     centerText("ERROR: No disk drive", 6)
  123.     centerText("Connect a disk drive", 8)
  124.    
  125.     -- Wait for disk drive to be connected
  126.     while not diskDrive do
  127.       sleep(1)
  128.       diskDrive = findDiskDrive()
  129.     end
  130.   end
  131.  
  132.   if monitor then term.redirect(monitor) end
  133.   local w, h = term.getSize()
  134.   term.clear()
  135.   drawHeader()
  136.  
  137.   term.setTextColor(COLORS.highlight)
  138.   centerText("Welcome to the Casino", 5)
  139.   centerText("Please insert your", 7)
  140.   centerText("player card", 8)
  141.  
  142.   -- Draw a card slot visual (simplified for 2x2 monitor)
  143.   local slotWidth = 16
  144.   local slotX = math.floor((w - slotWidth) / 2)
  145.   local slotY = 10
  146.  
  147.   drawBox(slotX, slotY, slotWidth, 3, colors.gray)
  148.   drawBox(slotX + 1, slotY + 1, slotWidth - 2, 1, colors.black)
  149.  
  150.   centerText("INSERT CARD", slotY + 4, COLORS.highlight)
  151.  
  152.   -- Wait for disk to be inserted
  153.   while not disk.isPresent(diskDrive) do
  154.     sleep(0.5)
  155.     -- Check if disk drive is still connected
  156.     if not peripheral.isPresent(diskDrive) then
  157.       diskDrive = findDiskDrive()
  158.       if not diskDrive then
  159.         term.setTextColor(COLORS.error)
  160.         centerText("Disk drive disconnected!", slotY + 6)
  161.         sleep(1)
  162.         return waitForDisk() -- Restart the process
  163.       end
  164.     end
  165.   end
  166.  
  167.   term.setTextColor(COLORS.success)
  168.   centerText("Card detected!", slotY + 6)
  169.   sleep(1)
  170.   return diskDrive
  171. end
  172.  
  173. local function initializeNewCard(drive)
  174.   -- First display the instructions on the monitor
  175.   if monitor then term.redirect(monitor) end
  176.   local w, h = term.getSize()
  177.   term.clear()
  178.   drawHeader()
  179.  
  180.   term.setTextColor(COLORS.primary)
  181.   centerText("New player card", 5)
  182.   centerText("Enter your name on", 7)
  183.   centerText("the computer", 8)
  184.  
  185.   -- Switch back to the computer terminal for input
  186.   term.redirect(oldTerm)
  187.   term.clear()
  188.   term.setCursorPos(1, 1)
  189.   term.setTextColor(colors.yellow)
  190.   print("CASINO REGISTRATION")
  191.   print("-----------------")
  192.   print("")
  193.   term.setTextColor(colors.white)
  194.   print("Please enter your name:")
  195.  
  196.   local name = read()
  197.  
  198.   -- Create player data
  199.   local playerData = {
  200.     name = name,
  201.     credits = 50000,
  202.     created = os.day(),
  203.     lastPlayed = os.day(),
  204.     games = {}
  205.   }
  206.  
  207.   -- Save to disk
  208.   local file = fs.open(disk.getMountPath(drive) .. "/player.dat", "w")
  209.   file.write(textutils.serialize(playerData))
  210.   file.close()
  211.  
  212.   -- Switch back to monitor to show confirmation
  213.   if monitor then term.redirect(monitor) end
  214.   term.setTextColor(COLORS.success)
  215.   centerText("Account created!", 12)
  216.   centerText("Balance: 50,000", 13)
  217.   sleep(2)
  218.  
  219.   return playerData
  220. end
  221.  
  222. local function resetCard(drive)
  223.   if monitor then term.redirect(monitor) end
  224.   local w, h = term.getSize()
  225.   term.clear()
  226.   drawHeader()
  227.  
  228.   term.setTextColor(COLORS.highlight)
  229.   centerText("Reset Card", 5)
  230.   centerText("Are you sure?", 7)
  231.   centerText("This will erase all data", 9)
  232.  
  233.   -- Confirm/Cancel buttons
  234.   local confirmButton = drawButton(5, 12, "Reset", COLORS.resetButton, COLORS.resetText)
  235.   local cancelButton = drawButton(w - 15, 12, "Cancel", COLORS.button, COLORS.buttonText)
  236.  
  237.   -- Check for card removal during confirmation
  238.   local cardRemoved = false
  239.  
  240.   while true do
  241.     -- Set up a timer to check for card removal
  242.     local cardTimer = os.startTimer(0.5)
  243.    
  244.     local event, param, x, y = os.pullEvent()
  245.    
  246.     if event == "timer" and param == cardTimer then
  247.       -- Check if card is still present
  248.       if not disk.isPresent(drive) then
  249.         cardRemoved = true
  250.         break
  251.       end
  252.     elseif event == "monitor_touch" then
  253.       if isInside(x, y, confirmButton) then
  254.         -- Delete player data file
  255.         local path = disk.getMountPath(drive) .. "/player.dat"
  256.         if fs.exists(path) then
  257.           fs.delete(path)
  258.         end
  259.        
  260.         term.clear()
  261.         drawHeader()
  262.         term.setTextColor(COLORS.success)
  263.         centerText("Card has been reset", 8)
  264.         centerText("Creating new account...", 10)
  265.         sleep(2)
  266.        
  267.         -- Create new player data
  268.         return initializeNewCard(drive)
  269.       elseif isInside(x, y, cancelButton) then
  270.         -- Load existing data
  271.         local file = fs.open(disk.getMountPath(drive) .. "/player.dat", "r")
  272.         local data = textutils.unserialize(file.readAll())
  273.         file.close()
  274.         return data
  275.       end
  276.     end
  277.   end
  278.  
  279.   -- If card was removed during confirmation, return nil to signal this
  280.   if cardRemoved then
  281.     return nil
  282.   end
  283. end
  284.  
  285. local function loadPlayerData(drive)
  286.   local path = disk.getMountPath(drive) .. "/player.dat"
  287.  
  288.   if not fs.exists(path) then
  289.     return initializeNewCard(drive)
  290.   end
  291.  
  292.   local file = fs.open(path, "r")
  293.   local data = textutils.unserialize(file.readAll())
  294.   file.close()
  295.  
  296.   -- Update last played
  297.   data.lastPlayed = os.day()
  298.  
  299.   -- Save updated data
  300.   local file = fs.open(path, "w")
  301.   file.write(textutils.serialize(data))
  302.   file.close()
  303.  
  304.   return data
  305. end
  306.  
  307. local function formatCredits(amount)
  308.   local formatted = tostring(amount)
  309.   local pos = #formatted - 3
  310.   while pos > 0 do
  311.     formatted = formatted:sub(1, pos) .. "," .. formatted:sub(pos + 1)
  312.     pos = pos - 3
  313.   end
  314.   return formatted
  315. end
  316.  
  317. local function showMainMenu(playerData, drive)
  318.   if monitor then term.redirect(monitor) end
  319.   local w, h = term.getSize()
  320.  
  321.   while disk.isPresent(drive) do
  322.     term.clear()
  323.     drawHeader()
  324.    
  325.     -- Welcome and balance (shortened for 2x2 monitor)
  326.     term.setTextColor(COLORS.primary)
  327.     centerText("Welcome", 6)
  328.     centerText(playerData.name, 8)
  329.    
  330.     -- Balance display (centered and prominent)
  331.     centerText("Balance:", 11, COLORS.primary)
  332.     local balanceColor = playerData.credits >= 0 and COLORS.balance or COLORS.balanceNegative
  333.     centerText(formatCredits(playerData.credits), 13, balanceColor)
  334.    
  335.     -- Done and Reset buttons
  336.     local doneButton = drawButton(5, h - 5, "Done", COLORS.button, COLORS.buttonText)
  337.     local resetButton = drawButton(w - 15, h - 5, "Reset", COLORS.resetButton, COLORS.resetText)
  338.    
  339.     -- Set up a timer to check for card removal
  340.     local cardTimer = os.startTimer(0.5)
  341.    
  342.     -- Wait for button click or card removal
  343.     local event, param, x, y = os.pullEvent()
  344.    
  345.     if event == "timer" and param == cardTimer then
  346.       -- Just continue the loop which will check if the card is present
  347.     elseif event == "monitor_touch" then
  348.       -- Handle touch events
  349.       if isInside(x, y, doneButton) then
  350.         -- Show exit message
  351.         term.clear()
  352.         drawHeader()
  353.         term.setTextColor(COLORS.highlight)
  354.         centerText("Thank you!", 8)
  355.         centerText("Remove your card", 10)
  356.        
  357.         -- Wait for disk to be removed
  358.         while disk.isPresent(drive) do
  359.           sleep(0.5)
  360.           -- Check if disk drive is still connected
  361.           if not peripheral.isPresent(drive) then
  362.             diskDrive = findDiskDrive()
  363.             break
  364.           end
  365.         end
  366.        
  367.         return -- Exit to main loop
  368.       elseif isInside(x, y, resetButton) then
  369.         local newPlayerData = resetCard(drive)
  370.         -- If resetCard returns nil, it means the card was removed during reset
  371.         if newPlayerData == nil then
  372.           return -- Exit to main loop
  373.         else
  374.           playerData = newPlayerData
  375.         end
  376.       end
  377.     end
  378.   end
  379.  
  380.   -- If we get here, the card was removed
  381.   return
  382. end
  383.  
  384. -- Main program loop - runs continuously
  385. while true do
  386.   local drive = waitForDisk()
  387.   local playerData = loadPlayerData(drive)
  388.   showMainMenu(playerData, drive)
  389.  
  390.   -- No need for additional exit message here since it's handled in showMainMenu
  391.   -- Just continue the loop to go back to waiting for a disk
  392. end
  393.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement