Jump to content

Lottery script


Recommended Posts

hi guys i have this script of lottery by Al3grab

I want to use this resource but I have noticed that the players can buy the amount of tickets they want without any limit, I have tried several ways to make them only buy 1 ticket and so far I have not been able to even try the removeCommandHandler

If anyone can help me on this I would appreciate it

 

 

LOT_PRIZE_NUM = 1000 -- The lottery prize .
LOT_PRIZE_PLUS = 100 -- If no one won the prize it will be increased , this is the plus number .
LOT_PRIZE_PLUS_TIMES = 5 -- If no one won the prize it will be increased , this is how many times it will be increased
--
LOT_TICKET_PRICE = 50 -- The Lottery ticket prize .
LOT_NUM_LIMIT = 10 -- What is the higher lottery number .
--
LOT_TIME_LIMIT_FROM,LOT_TIME_LIMIT_TO = 30,60 -- Random timer limit to start the lottery in seconds .
LOT_RANDOM_NUMBER = 20 -- Random timer to make lottery in minutes .
--- Script ---
rRoot = resourceRoot
--
LOT_NUM = nil
LOT_PRIZE = nil
LOT_ON = false
LOT_INC = 0
LOT_TICKETS = {}
LOT_WINNERS = {}
--
function outLot(msg,to)
	if to then
	--
	outputChatBox("**#00FF00[Lottery]#FFFF00 "..msg,to,255,0,0,true)
	else
	outputChatBox("**#00FF00[Lottery]#FFFF00 "..msg,root,255,0,0,true)
	end
end 


function onLotStart()
	outputDebugString("Lottery By Al3grab | Started")
	--
	local help = getResourceFromName("helpmanager")
	if not help then
		outLot("By Al3grab Started")
	else
		local state = getResourceState(help)
		if state == "running"  then
			outLot("By Al3grab Started , Press F9 For Help")
		else
			outLot("By Al3grab Started")
		end
	end
	--
	createRandomLot()
end
addEventHandler("onResourceStart",rRoot,onLotStart)

function createRandomLot()
	local random:~ = math.random(1,tonumber(LOT_RANDOM_NUMBER))
	if random:~ then
		outLot("Next Lottery Will Start in #00FFFF"..tonumber(random:~).."#FFFF00 Minutes")
		random:~Timer = setTimer ( createLot , random:~ * 60 * 1000 , 1 )
	end
end

function createLot(prize)
	if random:~Timer then if isTimer(random:~Timer) then killTimer(random:~Timer) end end
	LOT_NUM_LIMIT = LOT_NUM_LIMIT or 30
	prize = prize or LOT_PRIZE_NUM
	--
	local lot = math.floor(math.random(1,tonumber(LOT_NUM_LIMIT)))
	local lotTime = math.floor(math.random(LOT_TIME_LIMIT_FROM,LOT_TIME_LIMIT_TO))
	local lotTime = lotTime * 1000
	if lot and lotTime then
		startLottery( 
		lot ,
		lotTime ,
		prize or 50000 ,
		LOT_PRIZE_PLUS or 5000 ,
		LOT_TICKET_PRICE or 5000
		)
	end
end

addCommandHandler("lottery",function(me,cmd,prize)
	if hasObjectPermissionTo(me,"command.shutdown") then
		createLot(prize or nil)
	end
end , false ,false )

function startLottery ( LotNumber , StartAfter , Prize , Plus , TicketPrice )
	if ( LotNumber and StartAfter and Prize and Plus and TicketPrice ) then
		outLot("The Lottery Will Start in #00FFFF"..tonumber(math.floor(StartAfter/1000)).."#FFFF00 Seconds , The Prize is #00FF00"..Prize.."$")
		outLot("Use /Ticket <number (1-"..tonumber(LOT_NUM_LIMIT)..")> Command to buy a ticket , the ticket costs #00FF00"..TicketPrice.."$")
		LOT_NUM = LotNumber
		LOT_PRIZE = Prize
		LOT_INC = 0
		LOT_ON = true
		setTimer( function ()
			LOT_WINNERS = {}
			if ( LOT_TICKETS and type(LOT_TICKETS) == "table" ) then
				for k,v in ipairs ( LOT_TICKETS ) do
					local Player = v[1]
					local TicketNumber = v[2]
					if ( Player and TicketNumber ) then
						if TicketNumber == LOT_NUM then
							table.insert(LOT_WINNERS,Player)
						end
					end
				end
				--
				if ( LOT_WINNERS ) then
					onLotteryFinish(LOT_WINNERS)
				end
			end
		end , StartAfter , 1 )
		
	end
end 

function onLotteryFinish ( table )
	if ( table ) then
		outLot("The Lucky Number Was #00FFFF"..tonumber(LOT_NUM).."#FFFF00 !")
		if #table > 0 then
			outLot("Winners Are :")
			for k,v in ipairs ( table ) do
				if isElement(v) then
					outputChatBox("- "..getPlayerName(v),root,255,255,0,true)
					givePlayerMoney(v,LOT_PRIZE)
				end
			end
			--
			destroyLottery(true)
		else
			outLot("No one won the lottery !")
			increaseLottery()
		end
	end
end

function increaseLottery()
	if (  tonumber(LOT_PRIZE_PLUS) ) then
		if LOT_INC ~= LOT_PRIZE_PLUS_TIMES then
			local LOT_INC  = tonumber(LOT_PRIZE) + tonumber(LOT_PRIZE_PLUS)
			--
			destroyLottery(false)
			outLot("Lottery has been increased to #00FF00"..tonumber(LOT_INC).."$#FFFF00 !")
			createLot(LOT_INC)
			--
			LOT_INC = LOT_INC + 1
		else
			destroyLottery(true)
		end
	end
end

function destroyLottery(startNew)
	LOT_NUM = nil
	LOT_PRIZE = nil
	LOT_ON = false
	LOT_INC = 0
	LOT_TICKETS = {}
	LOT_WINNERS = {}
	--
	if startNew then
		createRandomLot()
	end
end

addCommandHandler("ticket",function ( me , cmd , number )
	if LOT_ON then
		if tonumber(number) and tonumber(number) >= 1 and tonumber(number) <= tonumber(LOT_NUM_LIMIT) then
			if getPlayerMoney(me) >= LOT_TICKET_PRICE then
				outputChatBox("**#00FF00[Lottery]#FFFF00 Your number is "..number.." good luck !",me,255,0,0,true)
				takePlayerMoney(me,LOT_TICKET_PRICE)
				--
				table.insert(LOT_TICKETS,{me,tonumber(number)})
			else
				outputChatBox("**#00FF00[Lottery]#FFFF00 You Don't have #00FF00"..tonumber(LOT_TICKET_PRICE).."$",me,255,0,0,true)
			end
		else
			outputChatBox("**#00FF00[Lottery]#FFFF00 Ticket number must be be between 1-"..LOT_NUM_LIMIT.."",me,255,0,0,true)
		end
	else
		outputChatBox("**#00FF00[Lottery]#FFFF00 No lottery is running now !",me,255,0,0,true)
	end
end , false , false )

 

Link to comment
function hasPlayerLotteryTicket(player)
  local hasTicket = false
  for k, v in ipairs(LOT_TICKETS) do
    local tPlr = v[1]
    local tNum = v[2]
    if isElement(tPlr) and player == tPlr then
      outputChatBox("You already bought a ticket, your number is: " .. tNum, player )
      hasTicket = true
      break
    end
  end 
  return hasTicket
end

Add the above code to your script and change line 162 to this:

if LOT_ON and not hasPlayerLotteryTicket(me) then

That should do it, although I haven't tested it myself, if it's not working correctly, use /debugscript 3 and come back with the error.

 

Explanation:

There's a table called LOT_TICKETS which stores all the tickets that are currently owned by players. Every single 'row' of this table is another, nested table, which looks like this:

LOT_TICKETS = {
  {"player1", 10},
  {"player2", 87},
  {"player3", 55},    
}

"player1", "player2", "player3" are actually not strings, but userdata values which point to a player element, so if you loop through the table and check them one by one, you will be able to check it against the source element of the /ticket command

  • Like 2
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...