Advertisement
incinirate

LittleBigLotto V2.1

Jun 26th, 2015
313
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.72 KB | None | 0 0
  1. -- .____    .__  __    __  .__        __________.__       .____           __    __          
  2. -- |    |   |__|/  |__/  |_|  |   ____\______   \__| ____ |    |    _____/  |__/  |_  ____  
  3. -- |    |   |  \   __\   __\  | _/ __ \|    |  _/  |/ ___\|    |   /  _ \   __\   __\/  _ \
  4. -- |    |___|  ||  |  |  | |  |_\  ___/|    |   \  / /_/  >    |__(  <_> )  |  |  | (  <_> )
  5. -- |_______ \__||__|  |__| |____/\___  >______  /__\___  /|_______ \____/|__|  |__|  \____/
  6. --         \/                        \/       \/  /_____/         \/                        
  7.  
  8. -- LittleBigLotto V2.1
  9. -- Developed by LittleBigPlanet and Incinirate
  10.  
  11. --[[ GLOBAL VARS ]]--
  12.  
  13. local loadedCmds = {}
  14.  
  15. local LottoRunning = false
  16.  
  17. local LBC = {}
  18.  
  19. local LottoUsers = {}
  20.  
  21. local LottoTickets = {}
  22.  
  23. local ChatBox --Intialized later in code
  24.  
  25. local LottoTimer = 0 --Same here
  26.  
  27. --[[ END GLOBAL VARS ]]--
  28.  
  29. --[[ CONFIG DEFAULTS ]]--
  30.  
  31. local config = {
  32.     ["Side"] = "bottom",
  33.     ["Name"] = "LittleBigLotto",
  34.     ["Admins"] = {"LBP", "Incinirate"},
  35.     ["Silent"] = "##",
  36.     ["Public"] = "..",
  37.     ["StartingLBC"] = 100,
  38.     ["LottoAdminsOnly"] = false,
  39.     ["MaxTime"] = 300,
  40.     ["CanPayOfflineUsers"] = false,
  41.     ["Tax"] = true,
  42.     ["TaxPercentage"] = 0.06
  43. }
  44.  
  45. --[[ END CONFIG DEFAULTS ]]--
  46.  
  47. --[[ COMMAND API ]]--
  48.  
  49. local tAPI = {}
  50.  
  51. tAPI.Config = {}
  52. for k,v in pairs(config) do
  53.     tAPI.Config[k] = v
  54. end
  55. tAPI.LottoUsers = LottoUsers
  56. tAPI.LottoTickets = LottoTickets  --We can do this because tables are objects not primitives
  57.  
  58. function tAPI.setLottoRunning( value )
  59.     LottoRunning = value
  60. end
  61.  
  62. function tAPI.getLottoRunning()
  63.     return LottoRunning
  64. end
  65.  
  66. function tAPI.setLottoTimer(tim)
  67.     LottoTimer = tim
  68. end
  69.  
  70. function tAPI.message( msgtype, user, msg )
  71.     if msgtype == 1 then
  72.         return ChatBox.tell(user, msg)
  73.     else
  74.         return ChatBox.say("@"..user..": "..msg)
  75.     end
  76. end
  77.  
  78. function tAPI.say( user, msg )
  79.     if user then
  80.       return ChatBox.say("@"..user..": "..msg)
  81.     else
  82.       return ChatBox.say(msg)
  83.     end
  84. end
  85.  
  86. function tAPI.tell( user, msg )
  87.     return ChatBox.tell( user, msg )    
  88. end
  89.  
  90. function tAPI.isPlayerOnline( player, cMes )
  91.     return ChatBox.tell( player, cMes and cMes or "Online Check" )
  92. end
  93.  
  94. function tAPI.getLBC( player )
  95.     player = player:lower()
  96.     print(player.." has "..tostring(LBC[player]).." LBC")
  97.     return LBC[ player ]    
  98. end
  99.  
  100. function tAPI.setLBC( player, amount )
  101.     player = player:lower()
  102.     LBC[ player ] = amount
  103.     return true    
  104. end
  105.  
  106. function tAPI.getPlayers()
  107.     local tPlayers = {}
  108.    
  109.     for k,v in pairs(LottoUsers) do
  110.         table.insert(tPlayers, k)
  111.     end
  112.    
  113.     return tPlayers
  114. end
  115.  
  116. function tAPI.getAmtTickets()
  117.     return #LottoTickets
  118. end
  119.  
  120. function tAPI.getPersonalPot( player )
  121.     return LottoUsers[player]    
  122. end
  123.  
  124. function tAPI.calcuatePercentage( player, total )
  125.     return (math.floor(player)/total)*(100)
  126. end
  127.  
  128. --[[ END COMMAND API ]]--
  129.  
  130. --[[ FUNCTIONS ]]--
  131.  
  132. function Message( msgtype, user, msg )
  133.     if msgtype == 1 then
  134.         return ChatBox.tell(user, msg)
  135.     else
  136.         return ChatBox.say("@"..user..": "..msg)
  137.     end
  138. end
  139.  
  140. function loadCmd(cmd)
  141.     if fs.exists("/cmds/"..tostring(cmd)) then
  142.         local file = fs.open("/cmds/"..cmd,"r")
  143.         if file then
  144.           print("reading" ..cmd)
  145.           local data = file.readAll()
  146.           local chunk,e = loadstring(data)
  147.           if not chunk then
  148.             printError("Error while loading "..cmd..": "..e)
  149.           end
  150.           loadedCmds[cmd]=chunk
  151.           return true
  152.         end
  153.         return false
  154.     end
  155. end
  156.  
  157. function commandExists(cmd)
  158.     if fs.exists("/cmds/"..tostring(cmd)) then
  159.     print("HI")
  160.         if not loadedCmds[cmd] then
  161.             print("Loading")
  162.             return loadCmd(cmd)
  163.         end
  164.         return true
  165.     end
  166.     return false
  167. end
  168.  
  169. function loadAllCommands()
  170.     local cmdsTBL = fs.list("/cmds/")
  171.     for k,v in pairs(cmdsTBL) do
  172.     print("Loading "..v)
  173.         loadCmd(v)
  174.     end
  175. end
  176.  
  177. function runCommand(cmd, user, args)
  178.     if commandExists(cmd) and loadedCmds[cmd] then
  179.         print("Running "..cmd)
  180.         loadedCmds[cmd](tAPI, user, unpack(args))
  181.     end
  182. end
  183.  
  184. function split(str, pat)
  185.     local tbl = {}
  186.     for v in str:gmatch("[^("..pat..")]+") do
  187.         table.insert(tbl, v)
  188.     end
  189.     return tbl
  190. end
  191.  
  192. function loadChatBox()
  193.     ChatBox = peripheral.wrap(config.Side)
  194.     tAPI.ChatBox = ChatBox -- In case of specific commands not provided to API
  195. end
  196.  
  197. function saveLBC()
  198.     local file = fs.open("lbc","w")
  199.     file.write(textutils.serialize(LBC))
  200.     file.close()
  201. end
  202.  
  203. function loadLBC()
  204.     if not fs.exists("lbc") then
  205.         local file = fs.open("lbc","w")
  206.         file.write("{}")
  207.         file.close()
  208.     end
  209.     local file = fs.open("lbc","r")
  210.     LBC = textutils.unserialize(file.readAll())
  211.     file.close()
  212.     return true
  213. end
  214.  
  215. function joinMessage( user, msgtype )
  216.     Message(1, user, "Hey! We noticed it's your first time using LBL!")
  217.     Message(1, user, "Check out the T&C with ..terms or ##terms")
  218.     print("Registered "..user)
  219.     LBC[user] = config.StartingLBC
  220.     print(user.." now has "..LBC[user].." LBC")
  221.     saveLBC()
  222. end
  223.  
  224. function performEnd()
  225.     print("About to announce winners!")
  226.     LottoRunning = false
  227.     ChatBox.say("The Lotto Has Ended!")
  228.     ChatBox.say("The Pot is: "..(#LottoTickets*(1 - config.TaxPercentage)))
  229.     ChatBox.say("The Winner is....")
  230.     local ticketnumber = math.random(1, #LottoTickets)
  231.     local winner = LottoTickets[ticketnumber]
  232.     ChatBox.say("@"..winner.." with Ticket #"..ticketnumber.." and a "..(math.floor(LottoUsers[winner])/#LottoTickets)* 100 .."% chance")
  233.     winner = winner:lower()
  234.     LBC[winner] = LBC[winner] + (#LottoTickets*(1 - config.TaxPercentage))
  235.      
  236.     for i,v in pairs(config.Admins) do
  237.         if not LBC[v:lower()] then LBC[v:lower()]=config.StartingLBC end
  238.         LBC[v:lower()] = LBC[v:lower()] + (#LottoTickets*(config.TaxPercentage/#config.Admins))
  239.     end
  240.     LottoUsers = {}
  241.     tAPI.LottoUsers = LottoUsers
  242.     LottoTickets={}
  243.     tAPI.LottoTickets = LottoTickets
  244. --    local delTable = {}
  245. --    for k,v in pairs(LottoUsers) do table.insert(delTable,k) end
  246. --    for i=1,#LottoTickets do LottoTickets[
  247.    
  248. end
  249. --[[ END FUNCTIONS ]]--
  250.  
  251.  
  252.  
  253. --[[ MAIN ]]--
  254.  
  255. loadAllCommands()
  256. loadChatBox()
  257. loadLBC()
  258. while true do
  259.     e, p1, p2, p3, p4, p5 = os.pullEvent()
  260.     if e == "chat_message" or e == "chatbox_command" then
  261.         if e=="chatbox_command" then p3 = "##"..p3 end
  262.         local user, message = p2, p3:sub(3)
  263.        
  264.         local prefix = p3:sub(1,2)
  265.         local mode = prefix == config.Silent and 1 or (prefix == config.Public and 2 or 3) --just tested this on an interpreter and it works :D #Shorthand
  266.         if mode == 1 or mode == 2 then -- If it is 3 then disregard the message
  267.             print(user.." ("..mode.."): "..message)
  268.             if not LBC[user:lower()] then
  269.                 joinMessage(user, mode)
  270.             end
  271.             local parts = split(message, " ")
  272.             local cmd = parts[1]
  273.             parts[1] = mode
  274.             print("About to run "..cmd)
  275.             runCommand( cmd, user, parts )
  276.            
  277.             saveLBC()
  278.         end
  279.     elseif e == "timer" then
  280.         local timer = p1
  281.         print("Timer, Lotto:"..tostring(LottoTimer)..", rec: "..tostring(timer))
  282.         print("ticket numbers"..#LottoTickets)
  283.         if timer == LottoTimer and #LottoTickets>0 then
  284.             performEnd()
  285.         elseif #LottoTickets<1 then
  286.             ChatBox.say("The Lotto Has Ended!")
  287.             ChatBox.say("Noone won, because the pot was empty.")
  288.         end
  289.     end
  290. end
  291.  
  292. --[[ END MAIN ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement