Virtual_Dndz

Casino

Apr 17th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | Gaming | 0 0
  1.  
  2. -- OpenComputers Casino Program (Full Version)
  3. -- Author: ChatGPT / User Project
  4. -- Features: GUI, multiple games, PIM item input, ME item output, settings saving
  5.  
  6. local component = require("component")
  7. local event = require("event")
  8. local term = require("term")
  9. local fs = require("filesystem")
  10. local serialization = require("serialization")
  11. local computer = require("computer")
  12. local gpu = component.gpu
  13.  
  14. -- Optional components
  15. local me = component.isAvailable("me_interface") and component.me_interface
  16. local pim = component.isAvailable("pim") and component.pim
  17.  
  18. -- Paths
  19. local configPath = "/casino_config.dat"
  20.  
  21. -- Global state
  22. local screenWidth, screenHeight = gpu.getResolution()
  23. local balance = 0
  24. local settings = {
  25.   payoutItem = "minecraft:diamond",
  26.   payoutAmount = 1,
  27.   games = { "slots", "coinflip" },
  28. }
  29.  
  30. -- GUI Utilities
  31. local function centerText(y, text)
  32.   local x = math.floor((screenWidth - #text) / 2)
  33.   gpu.set(x, y, text)
  34. end
  35.  
  36. local function drawFrame()
  37.   gpu.fill(1, 1, screenWidth, screenHeight, " ")
  38.   gpu.setForeground(0xFFFFFF)
  39.   centerText(1, "=== Казино 9000 ===")
  40.   gpu.set(2, 3, "Ваш баланс: " .. tostring(balance) .. " монет")
  41. end
  42.  
  43. -- Save/Load config
  44. local function saveSettings()
  45.   local file = io.open(configPath, "w")
  46.   if file then
  47.     file:write(serialization.serialize(settings))
  48.     file:close()
  49.   end
  50. end
  51.  
  52. local function loadSettings()
  53.   if fs.exists(configPath) then
  54.     local file = io.open(configPath, "r")
  55.     if file then
  56.       local data = file:read("*a")
  57.       settings = serialization.unserialize(data) or settings
  58.       file:close()
  59.     end
  60.   end
  61. end
  62.  
  63. -- PIM item payment (insert diamond to play)
  64. local function waitForPayment()
  65.   if not pim then return end
  66.   centerText(5, "Ожидание оплаты (вставьте алмаз в PIM)...")
  67.   while true do
  68.     local _, _, address, slot = event.pull("pim_inventory_changed")
  69.     local stack = pim.getStackInSlot(slot)
  70.     if stack and stack.name == settings.payoutItem then
  71.       pim.pushItem(slot)
  72.       balance = balance + 1
  73.       break
  74.     end
  75.   end
  76. end
  77.  
  78. -- ME item output (выдача выигрыша)
  79. local function payout()
  80.   if not me then return end
  81.   me.exportItem({name=settings.payoutItem, count=settings.payoutAmount}, "down")
  82. end
  83.  
  84. -- Game logic
  85. local function playSlots()
  86.   local result = {"🍒", "🔔", "💎", "🍋"}
  87.   local spin = {}
  88.   for i = 1, 3 do
  89.     spin[i] = result[math.random(1, #result)]
  90.   end
  91.   centerText(10, table.concat(spin, " | "))
  92.   if spin[1] == spin[2] and spin[2] == spin[3] then
  93.     centerText(12, "Вы выиграли!")
  94.     payout()
  95.   else
  96.     centerText(12, "Попробуйте снова!")
  97.   end
  98. end
  99.  
  100. local function playCoinflip()
  101.   local outcome = math.random(1, 2)
  102.   local side = outcome == 1 and "Орел 🪙" or "Решка 🪙"
  103.   centerText(10, "Результат: " .. side)
  104.   if outcome == 1 then
  105.     centerText(12, "Вы выиграли!")
  106.     payout()
  107.   else
  108.     centerText(12, "Попробуйте снова!")
  109.   end
  110. end
  111.  
  112. local function gameMenu()
  113.   local choice = 1
  114.   while true do
  115.     drawFrame()
  116.     centerText(6, "Выберите игру:")
  117.     for i, game in ipairs(settings.games) do
  118.       gpu.set(4, 7+i, (i == choice and "-> " or "   ") .. game)
  119.     end
  120.     local _, _, _, key = event.pull("key_down")
  121.     if key == 200 then -- up
  122.       choice = choice > 1 and choice - 1 or #settings.games
  123.     elseif key == 208 then -- down
  124.       choice = choice < #settings.games and choice + 1 or 1
  125.     elseif key == 28 then -- enter
  126.       term.clear()
  127.       if settings.games[choice] == "slots" then
  128.         playSlots()
  129.       elseif settings.games[choice] == "coinflip" then
  130.         playCoinflip()
  131.       end
  132.       os.sleep(3)
  133.       break
  134.     end
  135.   end
  136. end
  137.  
  138. -- MAIN
  139. math.randomseed(os.time())
  140. loadSettings()
  141.  
  142. while true do
  143.   term.clear()
  144.   drawFrame()
  145.   waitForPayment()
  146.   gameMenu()
  147. end
  148.  
Add Comment
Please, Sign In to add comment