Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ------------------------------------------
- -- Slot Machine
- --
- -- By: GravityCube
- ------------------------------------------
- --------------------------------------------------------
- --> Setup <--
- --------------------------------------------------------
- rewardPerMatch = 0.9
- monitorName = "monitor_30"
- --------------------------------------------------------
- --> Variables <--
- --------------------------------------------------------
- os.loadAPI("gcapi")
- seqs = {}
- actualBets= {} --{["GravityCube"]=100),["example"]=20}
- nextSpin = 10
- reloj = 0
- --------------------------------------------------------
- --> Current position <--
- --------------------------------------------------------
- function getPos(p)
- if seqs[p] == nil then
- return {1,2,3,4}
- end
- return seqs[p]
- end
- function setPos(p, lseq)
- seqs[p] = lseq
- end
- function addPos(p)
- local seq = getPos(p)
- for i=1,3,1 do
- seq[i] = tonumber(seq[i+1])
- end
- seq[4] = tonumber(getRandom(1,14))
- setPos(p,seq)
- end
- --------------------------------------------------------
- --> Economy <--
- --------------------------------------------------------
- --Economy
- function getMoney(player)
- gcapi.getMoney(player)
- end
- function setMoney(player, amount)
- gcapi.setMoney(player, amount)
- end
- function withdraw(player, amount)
- gcapi.withdraw(player, amount)
- end
- function addMoney(player, amount)
- gcapi.addMoney(player, amount)
- end
- --End Economy
- --------------------------------------------------------
- --> Log <--
- --------------------------------------------------------
- function printLog(line)
- line = reloj .. " - " .. line
- print(line)
- file = fs.open("/disk/log","a")
- file.writeLine(line)
- file.close()
- end
- --------------------------------------------------------
- --> Spin <--
- --------------------------------------------------------
- function spin(n1,n2,n3,n4,n5)
- while n1 > 0 or n2 > 0 or n3 > 0 or n4 > 0 or n5 >0 do
- if n1 > 0 then
- addPos(1)
- end
- if n2 > 0 then
- addPos(2)
- end
- if n3 > 0 then
- addPos(3)
- end
- if n4 > 0 then
- addPos(4)
- end
- if n5 > 0 then
- addPos(5)
- end
- if n1 == 1 or n2 == 1 or n3 == 1 or n4 == 1 or n5 == 1 then
- local noteblock = peripheral.find("music")
- noteblock.setPitch(10)
- noteblock.triggerNote()
- end
- n1 = n1 - 1
- n2 = n2 - 1
- n3 = n3 - 1
- n4 = n4 - 1
- n5 = n5 - 1
- monitorAnimation()
- updateMonitor()
- end
- end
- function getMultiplier()
- local multiplier = 0
- for c=1,4,1 do
- for r=1,3,1 do
- o = getPos(c)[r]
- if o == getPos(c+1)[r] then
- multiplier = multiplier + rewardPerMatch
- end
- end
- end
- return multiplier
- end
- --------------------------------------------------------
- --> Glasses <--
- --------------------------------------------------------
- function displayHelp(player)
- modems = {gcapi.findPeripheral("modem")}
- for _,modem in pairs(modems) do
- msg = {["command"]="help",["player"]=player}
- modem.transmit(3,1,textutils.serialize(msg))
- end
- end
- function displayMessage(player, message, color)
- modems = {gcapi.findPeripheral("modem")}
- for _,modem in pairs(modems) do
- msg = {["command"]="message",["player"]=player,["message"]=message,["color"]=color}
- modem.transmit(3,1,textutils.serialize(msg))
- end
- end
- function displayError(player, message)
- modems = {gcapi.findPeripheral("modem")}
- for _,modem in pairs(modems) do
- msg = {["command"]="error",["player"]=player,["message"]=message}
- modem.transmit(3,1,textutils.serialize(msg))
- end
- end
- --------------------------------------------------------
- --> Monitor <--
- --------------------------------------------------------
- function getMonitor()
- if monitorName ~= nil and peripheral.isPresent(monitorName) then
- return peripheral.wrap(monitorName)
- end
- mon = gcapi.findPeripheral("monitor")
- if mon == nil then
- error("Monitor not found")
- end
- return mon
- end
- function updateMonitor()
- local mon = getMonitor()
- mon.setBackgroundColor(1)
- mon.clear()
- displaySlots()
- mon.setBackgroundColor(1)
- mon.setTextColor(2^15)
- mon.setCursorPos(3,3)
- mon.write(tostring(nextSpin))
- mon.setCursorPos(1,15)
- for _=1,76,1 do mon.write("-") end
- mon.setBackgroundColor(2^15)
- --BOX
- xf, yf = mon.getSize()
- for x=1,xf,1 do
- for y=1,yf,1 do
- if x==1 or y==1 or x==xf or y==yf then
- mon.setCursorPos(x,y)
- mon.write(" ")
- end
- end
- end
- mon.setBackgroundColor(1)
- c = 0
- for player,bet in pairs(actualBets) do
- if c < 12 then
- mon.setCursorPos(3,16+c)
- mon.write(player)
- mon.setCursorPos(20,16+c)
- mon.write("$"..bet)
- end
- if c > 11 and c < 24 then
- mon.setCursorPos(24,16+c)
- mon.write(player)
- mon.setCursorPos(41,16+c)
- mon.write("$"..tostring(bet))
- end
- c = c+1
- end
- end
- function monitorAnimation()
- for nti=2,0,-1 do
- --Each Column
- for x = 4, 0, -1 do
- s = getPos(x+1)
- --Each Row
- for y = 0, 2, 1 do
- colorID = s[y+1]
- displaySlot(x*6+17,y*3+4,colorID,nti)
- end
- end
- sleep(0.05)
- end
- end
- function displaySlots()
- --Each Column
- for x = 0, 4, 1 do
- s = getPos(x+1)
- --Each Row
- for y = 0, 2, 1 do
- colorID = s[y+1]
- displaySlot(x*6+17,y*3+4,colorID)
- end
- end
- end
- function displaySlot(xi,yi,colourID,n)
- if n == nil then
- n = 0
- end
- local mon = getMonitor()
- mon.setBackgroundColor(2^colourID)
- mon.setTextScale(1)
- for i=2,n,-1 do
- mon.setCursorPos(xi,yi+i)
- mon.write(" ")
- end
- end
- --------------------------------------------------------
- --> Tools <--
- --------------------------------------------------------
- --Tool to split
- function split(s, delimiter)
- result = {};
- for match in (s..delimiter):gmatch("(.-)"..delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- function hasPermissions(player)
- return gcapi.hasPermissions(player)
- end
- function hasABet(player)
- for name,bet in pairs(actualBets) do
- if player == name then
- return true
- end
- end
- return false
- end
- function getAmountOfBets()
- b = 0
- for k,v in pairs(actualBets) do
- b = b + 1
- end
- return b
- end
- math.randomseed(os.time()*100)
- function getRandom(c, g)
- return math.random(c,g)
- end
- --------------------------------------------------------
- --> Command executor <--
- --------------------------------------------------------
- function executeCommand(player, co)
- co = co .. " "
- command = {}
- command = split(co," ")
- --spin
- if command[1] == "spin" then
- if hasPermissions(player) then
- if command[2] ~= nil and command[3] ~= nil and command[4] ~= nil then
- if tonumber(command[2]) and tonumber(command[3]) and tonumber(command[4]) and tonumber(command[5]) and tonumber(command[6]) then
- n1 = tonumber(command[2])
- n2 = tonumber(command[3])
- n3 = tonumber(command[4])
- n4 = tonumber(command[5])
- n5 = tonumber(command[6])
- spin(n1,n2,n3,n4,n5)
- else
- print("Estos no son numeros")
- end
- else
- print("Estos no son numeros")
- end
- else
- print(player .. " no tiene permisos.")
- end
- end
- if command[1] == "bet" then
- if spining == false and getAmountOfBets() < 24 then
- if tonumber(command[2]) then
- b = tonumber(command[2])
- if b >= 10 then
- if withdraw(player,b) then
- oldB = actualBets[player]
- if oldB == nil then
- oldB = 0
- end
- actualBets[player]=b+oldB
- print(player.." a puesto una apuesta de $"..b)
- else
- displayError(player, "You dont have enough money")
- end
- else
- displayError(player, "The minimum bet is $10")
- end
- else
- displayError(player, "You need to enter a valid amount")
- end
- else
- displayError(player, "There are too many bets")
- end
- end
- end
- --------------------------------------------------------
- --> Music <--
- --------------------------------------------------------
- function musicForStart()
- local noteblock = gcapi.findPeripheral("music")
- noteblock.setPitch(17 - nextSpin)
- noteblock.triggerNote()
- end
- --------------------------------------------------------
- --> Main program <--
- --------------------------------------------------------
- function mainProgram()
- nextTimeEventID = os.startTimer(0.1)
- while true do
- tEvent = {os.pullEvent()}
- if "timer" == tEvent[1] then
- if tEvent[2] == nextTimeEventID then
- relojAnterior = reloj
- reloj = reloj + 2
- if math.floor(reloj/20) ~= math.floor(relojAnterior/20) then
- if getAmountOfBets() > 0 then
- nextSpin = nextSpin - 1
- print("Next spin in " .. tostring(nextSpin) .. "seconds")
- musicForStart()
- end
- updateMonitor()
- end
- if nextSpin < 1 then
- print("Spining!")
- --SPIN
- rand = getRandom(5, 15)
- rand2 = rand + getRandom(5, 15)
- rand3 = rand2 + getRandom(5, 15)
- rand4 = rand3 + getRandom(5, 15)
- rand5 = rand4 + getRandom(5, 15)
- spin(rand,rand2,rand3,rand4,rand5)
- mu = getMultiplier()
- for player, bet in pairs(actualBets) do
- addMoney(player, (bet*mu))
- end
- mon = getMonitor()
- mon.setBackgroundColor(1)
- mon.setTextColor(2^15)
- mon.setCursorPos(23,2)
- mon.write("Multiplier: x" .. tostring(mu))
- sleep(5)
- nextSpin = 10
- actualBets = {}
- end
- nextTimeEventID = os.startTimer(0.1)
- end
- else
- if tEvent[1] == "chatEvent" then
- player = tostring(tEvent[2])
- co = tostring(tEvent[3])
- print(player .. " a ejecutado " .. co)
- executeCommand(player,co)
- end
- nextTimeEventID = os.startTimer(0.1)
- end
- end
- end
- parallel.waitForAny(gcapi.startChatEventQueue, mainProgram)
Add Comment
Please, Sign In to add comment