Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- .____ .__ __ __ .__ __________.__ .____ __ __
- -- | | |__|/ |__/ |_| | ____\______ \__| ____ | | _____/ |__/ |_ ____
- -- | | | \ __\ __\ | _/ __ \| | _/ |/ ___\| | / _ \ __\ __\/ _ \
- -- | |___| || | | | | |_\ ___/| | \ / /_/ > |__( <_> ) | | | ( <_> )
- -- |_______ \__||__| |__| |____/\___ >______ /__\___ /|_______ \____/|__| |__| \____/
- -- \/ \/ \/ /_____/ \/
- -- LittleBigLotto V2.1
- -- Developed by LittleBigPlanet and Incinirate
- --[[ GLOBAL VARS ]]--
- local loadedCmds = {}
- local LottoRunning = false
- local LBC = {}
- local LottoUsers = {}
- local LottoTickets = {}
- local ChatBox --Intialized later in code
- local LottoTimer = 0 --Same here
- --[[ END GLOBAL VARS ]]--
- --[[ CONFIG DEFAULTS ]]--
- local config = {
- ["Side"] = "bottom",
- ["Name"] = "LittleBigLotto",
- ["Admins"] = {"LBP", "Incinirate"},
- ["Silent"] = "##",
- ["Public"] = "..",
- ["StartingLBC"] = 100,
- ["LottoAdminsOnly"] = false,
- ["MaxTime"] = 300,
- ["CanPayOfflineUsers"] = false,
- ["Tax"] = true,
- ["TaxPercentage"] = 0.06
- }
- --[[ END CONFIG DEFAULTS ]]--
- --[[ COMMAND API ]]--
- local tAPI = {}
- tAPI.Config = {}
- for k,v in pairs(config) do
- tAPI.Config[k] = v
- end
- tAPI.LottoUsers = LottoUsers
- tAPI.LottoTickets = LottoTickets --We can do this because tables are objects not primitives
- function tAPI.setLottoRunning( value )
- LottoRunning = value
- end
- function tAPI.getLottoRunning()
- return LottoRunning
- end
- function tAPI.setLottoTimer(tim)
- LottoTimer = tim
- end
- function tAPI.message( msgtype, user, msg )
- if msgtype == 1 then
- return ChatBox.tell(user, msg)
- else
- return ChatBox.say("@"..user..": "..msg)
- end
- end
- function tAPI.say( user, msg )
- if user then
- return ChatBox.say("@"..user..": "..msg)
- else
- return ChatBox.say(msg)
- end
- end
- function tAPI.tell( user, msg )
- return ChatBox.tell( user, msg )
- end
- function tAPI.isPlayerOnline( player, cMes )
- return ChatBox.tell( player, cMes and cMes or "Online Check" )
- end
- function tAPI.getLBC( player )
- player = player:lower()
- print(player.." has "..tostring(LBC[player]).." LBC")
- return LBC[ player ]
- end
- function tAPI.setLBC( player, amount )
- player = player:lower()
- LBC[ player ] = amount
- return true
- end
- function tAPI.getPlayers()
- local tPlayers = {}
- for k,v in pairs(LottoUsers) do
- table.insert(tPlayers, k)
- end
- return tPlayers
- end
- function tAPI.getAmtTickets()
- return #LottoTickets
- end
- function tAPI.getPersonalPot( player )
- return LottoUsers[player]
- end
- function tAPI.calcuatePercentage( player, total )
- return (math.floor(player)/total)*(100)
- end
- --[[ END COMMAND API ]]--
- --[[ FUNCTIONS ]]--
- function Message( msgtype, user, msg )
- if msgtype == 1 then
- return ChatBox.tell(user, msg)
- else
- return ChatBox.say("@"..user..": "..msg)
- end
- end
- function loadCmd(cmd)
- if fs.exists("/cmds/"..tostring(cmd)) then
- local file = fs.open("/cmds/"..cmd,"r")
- if file then
- print("reading" ..cmd)
- local data = file.readAll()
- local chunk,e = loadstring(data)
- if not chunk then
- printError("Error while loading "..cmd..": "..e)
- end
- loadedCmds[cmd]=chunk
- return true
- end
- return false
- end
- end
- function commandExists(cmd)
- if fs.exists("/cmds/"..tostring(cmd)) then
- print("HI")
- if not loadedCmds[cmd] then
- print("Loading")
- return loadCmd(cmd)
- end
- return true
- end
- return false
- end
- function loadAllCommands()
- local cmdsTBL = fs.list("/cmds/")
- for k,v in pairs(cmdsTBL) do
- print("Loading "..v)
- loadCmd(v)
- end
- end
- function runCommand(cmd, user, args)
- if commandExists(cmd) and loadedCmds[cmd] then
- print("Running "..cmd)
- loadedCmds[cmd](tAPI, user, unpack(args))
- end
- end
- function split(str, pat)
- local tbl = {}
- for v in str:gmatch("[^("..pat..")]+") do
- table.insert(tbl, v)
- end
- return tbl
- end
- function loadChatBox()
- ChatBox = peripheral.wrap(config.Side)
- tAPI.ChatBox = ChatBox -- In case of specific commands not provided to API
- end
- function saveLBC()
- local file = fs.open("lbc","w")
- file.write(textutils.serialize(LBC))
- file.close()
- end
- function loadLBC()
- if not fs.exists("lbc") then
- local file = fs.open("lbc","w")
- file.write("{}")
- file.close()
- end
- local file = fs.open("lbc","r")
- LBC = textutils.unserialize(file.readAll())
- file.close()
- return true
- end
- function joinMessage( user, msgtype )
- Message(1, user, "Hey! We noticed it's your first time using LBL!")
- Message(1, user, "Check out the T&C with ..terms or ##terms")
- print("Registered "..user)
- LBC[user] = config.StartingLBC
- print(user.." now has "..LBC[user].." LBC")
- saveLBC()
- end
- function performEnd()
- print("About to announce winners!")
- LottoRunning = false
- ChatBox.say("The Lotto Has Ended!")
- ChatBox.say("The Pot is: "..(#LottoTickets*(1 - config.TaxPercentage)))
- ChatBox.say("The Winner is....")
- local ticketnumber = math.random(1, #LottoTickets)
- local winner = LottoTickets[ticketnumber]
- ChatBox.say("@"..winner.." with Ticket #"..ticketnumber.." and a "..(math.floor(LottoUsers[winner])/#LottoTickets)* 100 .."% chance")
- winner = winner:lower()
- LBC[winner] = LBC[winner] + (#LottoTickets*(1 - config.TaxPercentage))
- for i,v in pairs(config.Admins) do
- if not LBC[v:lower()] then LBC[v:lower()]=config.StartingLBC end
- LBC[v:lower()] = LBC[v:lower()] + (#LottoTickets*(config.TaxPercentage/#config.Admins))
- end
- LottoUsers = {}
- tAPI.LottoUsers = LottoUsers
- LottoTickets={}
- tAPI.LottoTickets = LottoTickets
- -- local delTable = {}
- -- for k,v in pairs(LottoUsers) do table.insert(delTable,k) end
- -- for i=1,#LottoTickets do LottoTickets[
- end
- --[[ END FUNCTIONS ]]--
- --[[ MAIN ]]--
- loadAllCommands()
- loadChatBox()
- loadLBC()
- while true do
- e, p1, p2, p3, p4, p5 = os.pullEvent()
- if e == "chat_message" or e == "chatbox_command" then
- if e=="chatbox_command" then p3 = "##"..p3 end
- local user, message = p2, p3:sub(3)
- local prefix = p3:sub(1,2)
- 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
- if mode == 1 or mode == 2 then -- If it is 3 then disregard the message
- print(user.." ("..mode.."): "..message)
- if not LBC[user:lower()] then
- joinMessage(user, mode)
- end
- local parts = split(message, " ")
- local cmd = parts[1]
- parts[1] = mode
- print("About to run "..cmd)
- runCommand( cmd, user, parts )
- saveLBC()
- end
- elseif e == "timer" then
- local timer = p1
- print("Timer, Lotto:"..tostring(LottoTimer)..", rec: "..tostring(timer))
- print("ticket numbers"..#LottoTickets)
- if timer == LottoTimer and #LottoTickets>0 then
- performEnd()
- elseif #LottoTickets<1 then
- ChatBox.say("The Lotto Has Ended!")
- ChatBox.say("Noone won, because the pot was empty.")
- end
- end
- end
- --[[ END MAIN ]]--
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement