GravityCube

VirtualSlotMachine

Nov 28th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.15 KB | None | 0 0
  1. ------------------------------------------
  2. --           Slot Machine
  3. --         
  4. --          By: GravityCube
  5. ------------------------------------------
  6.  
  7. --------------------------------------------------------
  8. -->                      Setup                       <--
  9. --------------------------------------------------------
  10.  
  11. rewardPerMatch = 0.9
  12. monitorName = "monitor_30"
  13.  
  14. --------------------------------------------------------
  15. -->                    Variables                     <--
  16. --------------------------------------------------------
  17. os.loadAPI("gcapi")
  18.  
  19. seqs = {}
  20.  
  21. actualBets= {} --{["GravityCube"]=100),["example"]=20}
  22. nextSpin = 10
  23.  
  24. reloj = 0
  25.  
  26. --------------------------------------------------------
  27. -->                     Current position                      <--
  28. --------------------------------------------------------
  29. function getPos(p)
  30.     if seqs[p] == nil then
  31.         return {1,2,3,4}
  32.     end
  33.     return seqs[p]
  34. end
  35.  
  36. function setPos(p, lseq)
  37.     seqs[p] = lseq
  38. end
  39.  
  40. function addPos(p)
  41.     local seq = getPos(p)
  42.     for i=1,3,1 do
  43.             seq[i] = tonumber(seq[i+1])
  44.     end
  45.     seq[4] = tonumber(getRandom(1,14))
  46.     setPos(p,seq)
  47. end
  48.  
  49. --------------------------------------------------------
  50. -->                     Economy                      <--
  51. --------------------------------------------------------
  52. --Economy
  53. function getMoney(player)
  54.     gcapi.getMoney(player)
  55. end
  56.  
  57. function setMoney(player, amount)
  58.     gcapi.setMoney(player, amount)
  59. end
  60.  
  61. function withdraw(player, amount)
  62.     gcapi.withdraw(player, amount)
  63. end
  64.  
  65. function addMoney(player, amount)
  66.     gcapi.addMoney(player, amount)
  67. end
  68. --End Economy
  69. --------------------------------------------------------
  70. -->                       Log                        <--
  71. --------------------------------------------------------
  72. function printLog(line)
  73.     line = reloj .. " - " .. line
  74.     print(line)
  75.    
  76.     file = fs.open("/disk/log","a")
  77.     file.writeLine(line)
  78.     file.close()
  79. end
  80.  
  81. --------------------------------------------------------
  82. -->                      Spin                        <--
  83. --------------------------------------------------------
  84. function spin(n1,n2,n3,n4,n5)
  85.    
  86.     while n1 > 0 or n2 > 0 or n3 > 0 or n4 > 0 or n5 >0 do
  87.        
  88.         if n1 > 0 then
  89.             addPos(1)
  90.         end
  91.         if n2 > 0 then
  92.             addPos(2)
  93.         end
  94.         if n3 > 0 then
  95.             addPos(3)
  96.         end
  97.         if n4 > 0 then
  98.             addPos(4)
  99.         end
  100.         if n5 > 0 then
  101.             addPos(5)
  102.         end
  103.         if n1 == 1 or n2 == 1 or n3 == 1 or n4 == 1 or n5 == 1 then
  104.             local noteblock = peripheral.find("music")
  105.             noteblock.setPitch(10)
  106.             noteblock.triggerNote()
  107.         end
  108.        
  109.         n1 = n1 - 1
  110.         n2 = n2 - 1
  111.         n3 = n3 - 1
  112.         n4 = n4 - 1
  113.         n5 = n5 - 1
  114.        
  115.         monitorAnimation()
  116.        
  117.         updateMonitor()
  118.     end
  119. end
  120. function getMultiplier()
  121.    
  122.     local multiplier = 0
  123.    
  124.     for c=1,4,1 do
  125.         for r=1,3,1 do
  126.             o = getPos(c)[r]
  127.             if o == getPos(c+1)[r] then
  128.                 multiplier = multiplier + rewardPerMatch
  129.             end
  130.         end
  131.     end
  132.    
  133.     return multiplier
  134. end
  135. --------------------------------------------------------
  136. -->                     Glasses                      <--
  137. --------------------------------------------------------
  138.  
  139. function displayHelp(player)
  140.     modems = {gcapi.findPeripheral("modem")}
  141.     for _,modem in pairs(modems) do
  142.         msg = {["command"]="help",["player"]=player}
  143.         modem.transmit(3,1,textutils.serialize(msg))
  144.     end
  145. end
  146.  
  147. function displayMessage(player, message, color)
  148.     modems = {gcapi.findPeripheral("modem")}
  149.     for _,modem in pairs(modems) do
  150.         msg = {["command"]="message",["player"]=player,["message"]=message,["color"]=color}
  151.         modem.transmit(3,1,textutils.serialize(msg))
  152.     end
  153. end
  154.  
  155. function displayError(player, message)
  156.     modems = {gcapi.findPeripheral("modem")}
  157.     for _,modem in pairs(modems) do
  158.         msg = {["command"]="error",["player"]=player,["message"]=message}
  159.         modem.transmit(3,1,textutils.serialize(msg))
  160.     end
  161. end
  162.  
  163. --------------------------------------------------------
  164. -->                      Monitor                     <--
  165. --------------------------------------------------------
  166. function getMonitor()
  167.     if monitorName ~= nil and peripheral.isPresent(monitorName) then
  168.         return peripheral.wrap(monitorName)
  169.     end
  170.     mon = gcapi.findPeripheral("monitor")
  171.     if mon == nil then
  172.         error("Monitor not found")
  173.     end
  174.     return mon
  175. end
  176.  
  177. function updateMonitor()
  178.     local mon = getMonitor()
  179.     mon.setBackgroundColor(1)
  180.     mon.clear()
  181.    
  182.     displaySlots()
  183.    
  184.     mon.setBackgroundColor(1)
  185.     mon.setTextColor(2^15)
  186.    
  187.     mon.setCursorPos(3,3)
  188.     mon.write(tostring(nextSpin))
  189.    
  190.     mon.setCursorPos(1,15)
  191.     for _=1,76,1 do mon.write("-") end
  192.    
  193.     mon.setBackgroundColor(2^15)
  194.    
  195.     --BOX
  196.     xf, yf = mon.getSize()
  197.    
  198.     for x=1,xf,1 do
  199.         for y=1,yf,1 do
  200.             if x==1 or y==1 or x==xf or y==yf then
  201.                 mon.setCursorPos(x,y)
  202.                 mon.write(" ")
  203.             end
  204.         end
  205.     end
  206.    
  207.    
  208.     mon.setBackgroundColor(1)
  209.    
  210.     c = 0
  211.     for player,bet in pairs(actualBets) do
  212.         if c < 12 then
  213.             mon.setCursorPos(3,16+c)
  214.             mon.write(player)
  215.            
  216.             mon.setCursorPos(20,16+c)
  217.             mon.write("$"..bet)
  218.         end
  219.        
  220.         if c > 11 and c < 24 then
  221.             mon.setCursorPos(24,16+c)
  222.             mon.write(player)
  223.            
  224.             mon.setCursorPos(41,16+c)
  225.             mon.write("$"..tostring(bet))
  226.         end
  227.        
  228.         c = c+1
  229.     end
  230. end
  231.  
  232. function monitorAnimation()
  233.     for nti=2,0,-1 do
  234.         --Each Column
  235.         for x = 4, 0, -1 do
  236.             s = getPos(x+1)
  237.             --Each Row
  238.             for y = 0, 2, 1 do
  239.                 colorID = s[y+1]
  240.                 displaySlot(x*6+17,y*3+4,colorID,nti)
  241.             end
  242.         end
  243.         sleep(0.05)
  244.     end
  245. end
  246.  
  247. function displaySlots()
  248.     --Each Column
  249.     for x = 0, 4, 1 do
  250.         s = getPos(x+1)
  251.         --Each Row
  252.         for y = 0, 2, 1 do
  253.             colorID = s[y+1]
  254.             displaySlot(x*6+17,y*3+4,colorID)
  255.         end
  256.     end
  257. end
  258.  
  259. function displaySlot(xi,yi,colourID,n)
  260.     if n == nil then
  261.         n = 0
  262.     end
  263.     local mon = getMonitor()
  264.     mon.setBackgroundColor(2^colourID)
  265.     mon.setTextScale(1)
  266.    
  267.     for i=2,n,-1 do
  268.         mon.setCursorPos(xi,yi+i)
  269.         mon.write("    ")
  270.     end
  271. end
  272. --------------------------------------------------------
  273. -->                       Tools                      <--
  274. --------------------------------------------------------
  275. --Tool to split
  276. function split(s, delimiter)
  277.     result = {};
  278.     for match in (s..delimiter):gmatch("(.-)"..delimiter) do
  279.         table.insert(result, match);
  280.     end
  281.     return result;
  282. end
  283. function hasPermissions(player)
  284.     return gcapi.hasPermissions(player)
  285. end
  286.  
  287. function hasABet(player)
  288.     for name,bet in pairs(actualBets) do
  289.         if player == name then
  290.             return true
  291.         end
  292.     end
  293.     return false
  294. end
  295.  
  296. function getAmountOfBets()
  297.     b = 0
  298.     for k,v in pairs(actualBets) do
  299.         b = b + 1
  300.     end
  301.     return b
  302. end
  303.  
  304. math.randomseed(os.time()*100)
  305. function getRandom(c, g)
  306.     return math.random(c,g)
  307. end
  308. --------------------------------------------------------
  309. -->                 Command executor                 <--
  310. --------------------------------------------------------
  311. function executeCommand(player, co)
  312.     co = co .. " "
  313.     command = {}
  314.     command = split(co," ")
  315.     --spin
  316.     if command[1] == "spin" then
  317.         if hasPermissions(player) then
  318.             if command[2] ~= nil and command[3] ~= nil and command[4] ~= nil then
  319.                 if tonumber(command[2]) and tonumber(command[3]) and tonumber(command[4]) and tonumber(command[5]) and tonumber(command[6]) then
  320.                     n1 = tonumber(command[2])
  321.                     n2 = tonumber(command[3])
  322.                     n3 = tonumber(command[4])
  323.                     n4 = tonumber(command[5])
  324.                     n5 = tonumber(command[6])
  325.                    
  326.                     spin(n1,n2,n3,n4,n5)
  327.                     else
  328.                     print("Estos no son numeros")
  329.                 end
  330.                 else
  331.                 print("Estos no son numeros")
  332.             end
  333.             else
  334.             print(player .. " no tiene permisos.")
  335.         end
  336.     end
  337.    
  338.     if command[1] == "bet" then
  339.         if spining == false and getAmountOfBets() < 24 then
  340.             if tonumber(command[2]) then
  341.                 b = tonumber(command[2])
  342.                 if b >= 10 then
  343.                     if withdraw(player,b) then
  344.                         oldB = actualBets[player]
  345.                         if oldB == nil then
  346.                             oldB = 0
  347.                         end
  348.                         actualBets[player]=b+oldB
  349.                         print(player.." a puesto una apuesta de $"..b)
  350.                     else
  351.                         displayError(player, "You dont have enough money")
  352.                     end
  353.                 else
  354.                     displayError(player, "The minimum bet is $10")
  355.                 end
  356.             else
  357.                 displayError(player, "You need to enter a valid amount")
  358.             end
  359.         else
  360.             displayError(player, "There are too many bets")
  361.         end
  362.     end
  363.    
  364. end
  365.  
  366. --------------------------------------------------------
  367. -->                       Music                      <--
  368. --------------------------------------------------------
  369. function musicForStart()
  370.     local  noteblock = gcapi.findPeripheral("music")
  371.     noteblock.setPitch(17 - nextSpin)
  372.     noteblock.triggerNote()
  373. end
  374. --------------------------------------------------------
  375. -->                    Main program                  <--
  376. --------------------------------------------------------
  377.  
  378. function mainProgram()
  379.     nextTimeEventID = os.startTimer(0.1)
  380.     while true do
  381.         tEvent = {os.pullEvent()}
  382.        
  383.         if "timer" == tEvent[1] then
  384.             if tEvent[2] == nextTimeEventID then
  385.                 relojAnterior = reloj
  386.                 reloj = reloj + 2
  387.                 if math.floor(reloj/20) ~= math.floor(relojAnterior/20) then
  388.                     if getAmountOfBets() > 0 then
  389.                         nextSpin = nextSpin - 1
  390.                         print("Next spin in " .. tostring(nextSpin) .. "seconds")
  391.                         musicForStart()
  392.                     end
  393.                     updateMonitor()
  394.                 end
  395.                 if nextSpin < 1 then
  396.                     print("Spining!")
  397.                     --SPIN
  398.                     rand = getRandom(5, 15)
  399.                     rand2 = rand + getRandom(5, 15)
  400.                     rand3 = rand2 + getRandom(5, 15)
  401.                     rand4 = rand3 + getRandom(5, 15)
  402.                     rand5 = rand4 + getRandom(5, 15)
  403.                     spin(rand,rand2,rand3,rand4,rand5)
  404.                    
  405.                     mu = getMultiplier()
  406.                     for player, bet in pairs(actualBets) do
  407.                         addMoney(player, (bet*mu))
  408.                     end
  409.                    
  410.                     mon = getMonitor()
  411.                     mon.setBackgroundColor(1)
  412.                     mon.setTextColor(2^15)
  413.                    
  414.                     mon.setCursorPos(23,2)
  415.                     mon.write("Multiplier: x" .. tostring(mu))
  416.                     sleep(5)
  417.                    
  418.                     nextSpin = 10
  419.                     actualBets = {}
  420.                 end
  421.                 nextTimeEventID = os.startTimer(0.1)
  422.             end
  423.         else
  424.             if tEvent[1] == "chatEvent" then
  425.                 player = tostring(tEvent[2])
  426.                 co = tostring(tEvent[3])
  427.                 print(player .. " a ejecutado " .. co)
  428.                 executeCommand(player,co)
  429.             end
  430.            
  431.             nextTimeEventID = os.startTimer(0.1)
  432.         end
  433.     end
  434. end
  435.  
  436. parallel.waitForAny(gcapi.startChatEventQueue, mainProgram)
Add Comment
Please, Sign In to add comment