Susceptance

crash_start

May 31st, 2022 (edited)
575
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.03 KB | None | 0 0
  1. -- Settings
  2. backgroundColor = 8192
  3.  
  4. stepRate = 600
  5.  
  6. maxWin = 10000
  7.  
  8. bet = 100
  9.  
  10. -- Globals
  11. step = 1
  12. cashOutPoint = 0
  13.  
  14. inPlay = false
  15. crashed = false
  16. cashedOut = false
  17.  
  18. buttonLabel = 'CRASH'
  19.  
  20. -- Init
  21. rednet.open('bottom')
  22. database = rednet.lookup("C_ACC", 'database')
  23. sound = peripheral.wrap("right")
  24. database = rednet.lookup("C_ACC", 'database')
  25.  
  26. monitor = peripheral.wrap("top")
  27. monitor.setTextScale(0.5)
  28. wX,wY = term.getSize()
  29. old = term.redirect(monitor)
  30. mX,mY = term.getSize()
  31.  
  32. term.setBackgroundColor(backgroundColor)
  33.  
  34. term.clear()
  35. term.setCursorPos(1,1)
  36.  
  37. account = nil
  38.  
  39. width, height = term.getSize()
  40.  
  41.  
  42. function GetAccount(pin)
  43.     rednet.send(database, textutils.serialise({'account', pin}), 'C_ACC')
  44.     sender,message,distance = rednet.receive('C_ACC')
  45.     account = textutils.unserialise(message)
  46.  
  47.     if account ~= nil then
  48.         return account
  49.     else
  50.         return nil
  51.     end
  52. end
  53.  
  54. function GetCrashPoint()
  55.     rand = math.random(1000)-1
  56.     crash = (1000-(rand/50))/(1000-rand)
  57.     crash = math.floor(crash * 100) / 100
  58.  
  59.     if rand%20 == 0 then
  60.         return 1
  61.     end
  62.  
  63.     return crash
  64. end
  65.  
  66. function Step()
  67.     f = 1/stepRate
  68.     step = step + f*(step^2+0.5)
  69. end
  70.  
  71. -- Game rendering
  72. function RedrawMonitor(label, borderCol)
  73.     borderCol = borderCol or '8'
  74.  
  75.     -- Cashout button
  76.     for i=0, 5 do
  77.         term.setCursorPos(1,mY-i)
  78.         term.blit(string.rep(' ', mX),string.rep('f', mX),string.rep(borderCol, mX))
  79.     end
  80.     -- Button text
  81.     term.setCursorPos((mX/2)-(#label/2),mY-2)
  82.     term.blit(label,string.rep('f', #label),string.rep(borderCol, #label))
  83.  
  84.     term.setCursorPos(1,1)
  85. end
  86.  
  87. function Payout()
  88.     return bet*cashOutPoint
  89. end
  90.  
  91. function CashoutCheck()
  92.     while true do
  93.         event, button, x, y = os.pullEvent("monitor_touch")
  94.         if inPlay and crashed == false and cashedOut == false then
  95.             if y >= mY - 4 then
  96.                 cashOutPoint = step
  97.                 cashedOut = true
  98.                 winnings = Payout()
  99.                 buttonLabel = 'YOU WIN: '..winnings..' CREDITS'
  100.                 rednet.send(database, textutils.serialise({'add', pin, winnings}), 'C_ACC')
  101.             end
  102.         end
  103.     end
  104. end
  105.  
  106. -- Game logic
  107. function PlayMonitor()
  108.     while true do
  109.         -- Reset
  110.         step = 1
  111.  
  112.         inPlay = false
  113.         crashed = false
  114.         cashedOut = false
  115.  
  116.         -- Login and play loop
  117.         while true do
  118.             while account == nil do
  119.                 RedrawMonitor('---Login on terminal to play---', 'e')
  120.                 sleep(0.5)
  121.                 RedrawMonitor('---CRASH---', 'e')
  122.                 sleep(0.5)
  123.             end
  124.  
  125.             RedrawMonitor('Right click to play. '..bet..' credits', '5')
  126.             event, button, x, y = os.pullEvent("monitor_touch")
  127.  
  128.             if account ~= nil then
  129.                 rednet.send(database, textutils.serialise({'add', pin, -bet}), 'C_ACC')
  130.                 sender,message,distance = rednet.receive('C_ACC')
  131.  
  132.                 -- Player has enough credit to play
  133.                 if textutils.unserialise(message) == true then
  134.                     break
  135.                 end
  136.  
  137.                 RedrawMonitor('Not enough credits to play', '8')
  138.                 sleep(1.5)
  139.             end
  140.         end
  141.  
  142.         -- Start game
  143.         buttonLabel = 'CASH OUT'
  144.         inPlay = true
  145.         crashPoint = GetCrashPoint()
  146.         while crashed == false do
  147.             piss = piss+1
  148.             buttonBG = '5'
  149.             RedrawMonitor(buttonLabel, '5')
  150.             Step()
  151.             if step >= crashPoint then
  152.                 inPlay = false
  153.                 crashed = true
  154.                 if cashedOut == false then
  155.                     buttonLabel = 'CRASHED'
  156.                 end
  157.             end
  158.  
  159.             sleep(0.1)
  160.         end
  161.  
  162.         RedrawMonitor(buttonLabel)
  163.         sleep(1.5)
  164.     end
  165. end
  166.  
  167. function PlayScreen()
  168.     login = window.create(old,1,1, wX,wY)
  169.     while true do
  170.         login.setBackgroundColor(2048)
  171.         login.clear()
  172.         login.setCursorPos(1,1)
  173.         login.write("Enter pin: ")
  174.         pin = ''
  175.         input = ''
  176.         account = nil
  177.         for i=1, 4 do
  178.             event, input = os.pullEvent("char")
  179.             pin = pin..input
  180.             login.write('*')
  181.         end
  182.         pin = pin
  183.  
  184.         login.clear()
  185.         login.setCursorPos(1,1)
  186.         login.write('Logging in...')
  187.  
  188.         account = GetAccount(pin)
  189.         if account ~= nil then
  190.             login.clear()
  191.             login.setCursorPos(1,1)
  192.             login.write('Welcome '..account[1])
  193.             login.setCursorPos(1,2)
  194.             login.write("Exit this screen to play!")
  195.             login.setCursorPos(1,3)
  196.             login.write("Press any key to log out")
  197.  
  198.             os.pullEvent("char")
  199.         else
  200.             login.clear()
  201.             login.setCursorPos(1,1)
  202.             login.write('Invalid pin!')
  203.             sleep(1)
  204.         end
  205.     end
  206. end
  207.  
  208. parallel.waitForAll(PlayMonitor, PlayScreen, CashoutCheck)
Add Comment
Please, Sign In to add comment