Advertisement
justync7

cblotto

Jun 25th, 2015
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.15 KB | None | 0 0
  1. --   __ _ _   _   _        ___ _         __       _   _                _____
  2. --  / /(_) |_| |_| | ___  / __(_) __ _  / /  ___ | |_| |_ ___   /\   /\___ /
  3. -- / / | | __| __| |/ _ \/__\// |/ _` |/ /  / _ \| __| __/ _ \  \ \ / / |_ \
  4. --/ /__| | |_| |_| |  __/ \/  \ | (_| / /__| (_) | |_| || (_) |  \ V / ___) |
  5. --\____/_|\__|\__|_|\___\_____/_|\__, \____/\___/ \__|\__\___/    \_(_)____/
  6. --                               |___/                                      
  7.  
  8. --.------..------..------..------..------..------..------..------..------..------..------..------..------..------.     .------..------.
  9. --|L.--. ||I.--. ||T.--. ||T.--. ||L.--. ||E.--. ||B.--. ||I.--. ||G.--. ||L.--. ||O.--. ||T.--. ||T.--. ||O.--. |.-.  |V.--. ||3.--. |
  10. --| :/\: || (\/) || :/\: || :/\: || :/\: || (\/) || :(): || (\/) || :/\: || :/\: || :/\: || :/\: || :/\: || :/\: (( )) | :(): || :(): |
  11. --| (__) || :\/: || (__) || (__) || (__) || :\/: || ()() || :\/: || :\/: || (__) || :\/: || (__) || (__) || :\/: |'-.-.| ()() || ()() |
  12. --| '--'L|| '--'I|| '--'T|| '--'T|| '--'L|| '--'E|| '--'B|| '--'I|| '--'G|| '--'L|| '--'O|| '--'T|| '--'T|| '--'O| (( )) '--'V|| '--'3|
  13. --`------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------'`------'  '-'`------'`------'
  14.  
  15. -- LittleBigLotto V3
  16. -- Made By LittleBigPlanet and Incinirate
  17.  
  18. local config = {
  19.     ["TOS"] = "http://pastebin.com/jZ8YbqdL",
  20.     ["acceptTOS"] = "accept",
  21.     ["side"] = "bottom",
  22.     ["name"] = "LittleBigLotto",
  23.     ["silent"] = "##",
  24.     ["public"] = "..",
  25.     ["startingLBC"] = 50,
  26.     ["startingJackpot"] = 1000,
  27.     ["maxTime"] = 300,
  28.     ["suspenseTime"] = 3, -- because 1 would be unnoticable
  29.     ["canPayOfflineUsers"] = false,
  30.     ["tax"] = true,
  31.     ["stats"] = true,
  32.     ["taxLocation"] = "taxDummy",
  33.     ["taxPercentage"] = 0.06,
  34.     ["basePlayerFormat"] = {
  35.         lbc = 0, -- DO NOT REMOVE THIS PART OF THE BPF
  36.         wins = 0,
  37.         losses = 0,
  38.         highestWin = 0,
  39.         totalChange = 0,
  40.         acceptedTOS = false
  41.     },
  42. }
  43.  
  44. config.basePlayerFormat.lbc = config.startingLBC
  45.  
  46. local lotto = {
  47.     running = false,
  48.     tickets = 0,
  49.     users = {}
  50. }
  51.  
  52. local slots = {}
  53.  
  54. local stats = {}
  55.  
  56. local console = {}
  57.  
  58. local tools = {}
  59.  
  60. local timers = {}
  61.  
  62. local users = {}
  63.  
  64. local oerror = error
  65. local function error(msg, level, code)
  66.     code = code or 0 --0 = fine 1 = close
  67.     level = level or 1
  68.     level = level + 1
  69.     oerror({["msg"] = msg, ["code"] = code},level)
  70. end
  71.  
  72. local chatBox = {}
  73. setmetatable(chatBox,{__index = function() return function() error("ChatBox called before ChatBox was initialized",2) end end })
  74.  
  75. local timerTriggers = {
  76.     lottoend = function(timerID)
  77.         local winningTicket = math.random(1, lotto.tickets)
  78.         local taxAmt = (config.tax and config.taxPercentage or 0)
  79.         local pot = lotto.tickets*(1-taxAmt)
  80.         local tax = lotto.tickets*taxAmt
  81.        
  82.         local offset = 0
  83.         for i,v in pairs(lotto.users) do
  84.             if pot > 0 then
  85.                 if winningTicket <= v + offset then
  86.                     -- We have found our winner!
  87.                     lotto.running = false
  88.                     chatBox.say("The Lotto is over!")
  89.                     chatBox.say("The Pot is: "..pot)
  90.                     chatBox.say("The Winner is...")
  91.                     sleep(config.suspenseTime) -- for suspense
  92.                     local chance = (lotto.users[i]/lotto.tickets)*100
  93.                     chatBox.say(i.." with a "..chance.."% chance!")
  94.                    
  95.                     tools.setLBC(i, tools.getLBC(i) + pot)
  96.                     tools.setLBC(config.taxLocation, tools.getLBC(config.taxLocation) + tax)
  97.                    
  98.                     break
  99.                 else
  100.                     offset = offset + v
  101.                 end
  102.             else
  103.                 chatBox.say("No one won, because the pot was empty.")
  104.                 break
  105.             end
  106.         end
  107.     end
  108. }
  109.  
  110. local function message(msgtype, player, msg) -- fixed, 1 = loud, 2 = silent lol
  111.     if msgtype == 1 then
  112.         chatBox.say("@"..player..": "..msg)
  113.     elseif msgtype == 2 then
  114.         chatBox.tell(player,msg)
  115.     else
  116.         error("Invalid message type")
  117.     end
  118. end
  119.  
  120. function tools.copyTable(orig) -- ooooh pretty
  121.     local orig_type = type(orig)
  122.     local copy
  123.     if orig_type == 'table' then
  124.         copy = {}
  125.         for orig_key, orig_value in next, orig, nil do
  126.             copy[tools.copyTable(orig_key)] = tools.copyTable(orig_value)
  127.         end
  128.         setmetatable(copy, tools.copyTable(getmetatable(orig)))
  129.     else -- number, string, boolean, etc
  130.         copy = orig
  131.     end
  132.     return copy
  133. end
  134.  
  135. function tools.newUser(player)
  136.     if not users[player] then
  137.         users[player] = tools.copyTable(config.basePlayerFormat)
  138.     end
  139.     chatBox.message(2, player, "We noticed that this is your first time using LittleBigLotto!") --we need to force them to accept the T&C before they can do anything
  140.     chatBox.message(2, player, "Read the Terms and Conditions: "..config.TOS) -- simple, we'll make them run ##agree and we'll show this message every time until they do
  141.     chatBox.message(2, player, "Once you've read that, use ##"..config.acceptTOS..".")
  142. end
  143.  
  144. function tools.setLBC(player, newAmt)
  145.     users[player].lbc = newAmt
  146. end
  147.  
  148. function tools.getLBC(player)
  149.     return users[player].lbc
  150. end
  151.  
  152. function tools.split(str, pat)
  153.     local tbl = {}
  154.     for v in str:gmatch("[^("..pat..")]+") do
  155.         table.insert(tbl, v)
  156.     end
  157.     return tbl
  158. end
  159.  
  160. function tools.loadChatbox()
  161.     if peripheral.isPresent(config.side) then
  162.         if peripheral.getType(config.side)=="chatbox" then
  163.             chatBox = peripheral.wrap(config.side)
  164.             chatBox.message = message
  165.             chatBox.setLabel(config.name)
  166.         else
  167.             error("Peripheral "..config.side.." was set as chatbox, but is not a chatbox.",1,1)
  168.         end
  169.     else
  170.         error("No peripheral on side "..config.side,1,1)
  171.     end
  172. end
  173.  
  174. function tools.writeToFile(filename, text, append)
  175.     if append and filename and text then
  176.         if type(text) == "table" then
  177.             text = textutils.serialize(text)
  178.         end
  179.         local fi = fs.open(filename, "a")
  180.         fi.write(text)
  181.         fi.close()
  182.         return true
  183.     elseif not append and filename and text then
  184.         if type(text) == "table" then
  185.             text = textutils.serialize(text)
  186.         end
  187.         local fi = fs.open(filename, "w")
  188.         fi.write(text)
  189.         fi.close()
  190.         return true
  191.     else
  192.         error("Missing args in tools.writeToFile.",2,0)
  193.         return false
  194.     end
  195. end
  196.  
  197. function tools.readFromFile(filename, serialized)
  198.     if filename and not serialized then
  199.         local fi = fs.open(filename, "r")
  200.         local text = fi.readAll()
  201.         fi.close()
  202.         return text
  203.     elseif filename and serialized then
  204.         local fi = fs.open(filename, "r")
  205.         local text = fi.readAll()
  206.         fi.close()
  207.         return textutils.unserialize(text)
  208.     else
  209.         error("Missing args in tools.readFromFile",2,0)
  210.         return false
  211.     end
  212. end
  213.  
  214. function console.log(msg, prefix)
  215.     local msg = "["..(prefix or config.name).."] "..msg
  216.     print(msg)
  217.     tools.writeToFile("console.log", msg.."\n", true)
  218. end
  219.  
  220. local commands = {
  221.     ping = function(msgtype, player)
  222.         chatBox.message(msgtype, player, "Pong!")
  223.     end,
  224.    
  225.     startlotto = function(msgtype, player, time)
  226.         if not lotto.running and tonumber(time) then
  227.             local time = tonumber(time)
  228.             lotto.users = {}
  229.             lotto.tickets = 0
  230.             chatBox.say("A lotto is starting! Lasting: "..time.." seconds.")
  231.             chatBox.say("Use ##lotto <bet> to enter.")
  232.             lotto.running = true
  233.             timers.lottoend = {
  234.                 ID = os.startTimer(time),
  235.                 marker = "lottoend"
  236.             }
  237.         elseif not time then
  238.             chatBox.message(msgtype, player, "Incorrect syntax. startlotto <time> (must be less than "..config.maxTime..")")
  239.         else
  240.             chatBox.message(msgtype, player, "A lotto is already running.")
  241.         end
  242.     end,
  243.    
  244.     lotto = function(msgtype, player, bet)
  245.         if bet then
  246.             bet=tonumber(bet)
  247.         end
  248.         if lotto.running and bet and bet > 0 and bet <= tools.getLBC(player) then
  249.             chatBox.message(msgtype, player, "You have added "..bet.." LBC to the Lotto.")
  250.             lotto.tickets = lotto.tickets + bet
  251.             lotto.users[player] = bet
  252.             tools.setLBC(player, tools.getLBC(player) - bet)
  253.             local pot = lotto.tickets*(1-config.taxPercentage)
  254.             for i,v in pairs(lotto.users) do
  255.                 local chance = (lotto.users[i]/lotto.tickets)*100
  256.                 chatBox.message(2, i, "You have a "..chance.."% to win.")
  257.             end
  258.             chatBox.say("The current pot is: "..pot.." LBC")
  259.         elseif not lotto.running then
  260.             chatBox.message(msgtype, player, "There is not a lotto in progress.")
  261.         else
  262.             chatBox.message(msgtype, player, "Incorrect syntax. lotto <bet>")
  263.         end
  264.     end,
  265.    
  266.     lbc = function(msgtype, player)
  267.        chatBox.message(msgtype, player, "You currently have "..tools.getLBC(player).." LBC.")
  268.     end,
  269.    
  270.     slots = function(msgtype, player, bet)
  271.         if bet then
  272.             bet = tonumber(bet)
  273.         end
  274.         if bet and bet <= tools.getLBC(player) and bet > 0 then
  275.             if not slots.jackpot then
  276.                 slots.jackpot = config.startingJackpot
  277.             end
  278.             local result = {math.random(1,7),math.random(1,7),math.random(1,7)}
  279.             chatBox.message(msgtype, player, "-=======-")
  280.             chatBox.message(msgtype, player, ":-"..table.concat(result,"|").."-:") -- use dashes for signiature "win line" like rl slots :3 :P
  281.             chatBox.message(msgtype, player, ":=======:")
  282.             if result[1] == 7 and result[2] == 7 and result[3] == 7 then
  283.                 --JACKPOT!
  284.                 local amount = slots.jackpot --* math.sqrt(bet) * (bet/slots.jackpot)
  285.                 --if amount < slots.jackpot then
  286.                 --    amount = slots.jackpot    
  287.                 --end
  288.                 chatBox.message(msgtype, player, "You won ".. amount .." LBC!")
  289.                 chatBox.say(player .. " just won the jackpot of ".. amount .." LBC!")
  290.                 tools.setLBC(player, tools.getLBC(player) + amount)
  291.             elseif result[1] == result[2] and result[2] == result[3] then
  292.                 --Triple!
  293.                 local amount = bet * 3
  294.                 chatBox.message(msgtype, player, "You won ".. amount .." LBC!")
  295.                 tools.setLBC(player, tools.getLBC(player) + amount)
  296.             elseif result[1] == result[2] or result[2] == result[3] or result[1] == result[3] then
  297.                 --Double
  298.                 local amount = bet * 2
  299.                 chatBox.message(msgtype, player, "You won ".. amount .." LBC!")
  300.                 tools.setLBC(player, tools.getLBC(player) + amount)
  301.             else
  302.                 --No Win
  303.                 chatBox.message(msgtype, player, "Sorry you didn't win.")
  304.             end
  305.         elseif bet > tools.getLBC(player) or bet <= 0 then
  306.             chatBox.message(msgtype, player, "You can't bet more than you have or put a bet <= 0!")
  307.         else
  308.             chatBox.message(msgtype, player, "Incorrect syntax. slots <bet>")
  309.         end
  310.     end,
  311.    
  312.     jackpot = function(msgtype, player)
  313.         chatBox.message(msgtype, player, "Current Jackpot: "..slots.jackpot.." LBC")
  314.     end
  315. }
  316.  
  317. function mainLoop()
  318.     tools.loadChatbox()
  319.     local event, p1, p2, p3, p4, p5 = os.pullEvent()
  320.     if event == "chat_message" or event == "chatbox_command" then
  321.         local player = p2
  322.         local msg = p3
  323.         local msgtype = 1
  324.         if event == "chatbox_command" then
  325.             msg = "##"..msg
  326.             msgtype = 2
  327.         end
  328.         if msg:sub(1,2) == config.public or msgtype == 2 then -- "if it doesn't work, do the opposite." - anonymous
  329.             local parts = tools.split(msg, " ")
  330.             local cmd = parts[1]:sub(3)
  331.             table.remove(parts, 1)
  332.             if commands[cmd] then          
  333.                 if not users[player] then
  334.                     tools.newUser(player)
  335.                 elseif not users[player].acceptedTOS then
  336.                     chatBox.message(msgtype, player, "Please read the TOS ("..config.TOS..")")
  337.                     chatBox.message(msgtpye, player, "When you are done, type ##"..config.acceptTOS.." if you accept the TOS")
  338.                 else
  339.                     commands[cmd](msgtype, player, unpack(parts))
  340.                 end
  341.             elseif cmd == config.acceptTOS then
  342.                 if not users[player] then
  343.                     tools.newUser(player)
  344.                 end
  345.                 chatBox.message( 2, player, "Thanks for accepting the TOS, have fun!")
  346.                 users[player].acceptedTOS = true
  347.             end
  348.         end
  349.     elseif event == "timer" then
  350.         local timerID = p1
  351.         for i,v in pairs(timers) do
  352.             if v.ID == timerID then
  353.                 timerTriggers[v.marker](timerID)
  354.             end
  355.         end
  356.     end
  357.     tools.writeToFile("users",users)
  358.     tools.writeToFile("slots",slots)
  359. end
  360.  
  361. if not fs.exists("users") then
  362.     local fi = fs.open("users","w")
  363.     fi.write("{}")
  364.     fi.close()
  365. end
  366. users = tools.readFromFile("users", true)
  367. slots = tools.readFromFile("slots", true)
  368. if config.tax and not users[config.taxLocation] then
  369.     users[config.taxLocation] = tools.copyTable(config.basePlayerFormat)
  370.     tools.writeToFile("users", users)
  371. end
  372.  
  373.  
  374. while true do
  375.     local status, err = pcall(mainLoop)
  376.     if not status then
  377.         if type(err) == "table" then
  378.             console.log(err.msg, "Error ("..err.code..")")
  379.             if err.code == 1 then
  380.                 console.log("Critical Error Detected, shutting down.", "System")
  381.                 break
  382.             end
  383.         else
  384.             console.log(err, "Error (Lua)")
  385.         end
  386.     end
  387.     if err == "Terminated" then
  388.         break
  389.     end
  390. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement