;'Raffle' - Timed script

ITEM = #PARAM1
ITEM_DESCRIPTION=#PARAM2
TICKETS = #PARAM3
PLAYERS = #PARAM4
ACTION = #PARAM5

RaffleTicket = 0

If ACTION = 0
	;Warn players the raffle is starting in 15 minutes, and state the prize
	SendMessageToAll("The daily raffle will take place in 15 minutes",#MSG_TELL)
	SendMessageToAll("Today's item is "+ITEM_DESCRIPTION,#MSG_TELL)
ElseIf ACTION = 1
	;Warn players the raffle is starting in 5 minutes
	SendMessageToAll("The daily raffle will take place in 5 minutes",#MSG_TELL)
ElseIf ACTION = 2
	;Count Players, make sure there is more than 3
	
	PlayerCount = 0
	TicketCount = 0
	
	UserID = GetNextOnlineUser(0)
	While UserID > 0
		PlayerCount = PlayerCount + 1
		UserTickets=0
		While HasItem(UserID,RaffleTicket) = 1
			UserTickets=UserTickets+1
			TicketCount=TicketCount+1
			TakeItem(UserID,RaffleTicket)
		Wend
		While UserTickets > 0
			UserTickets=UserTickets-1
			GiveItem(UserID,RaffleTicket)
		Wend
		UserID = GetNextOnlineUser(UserID)
	Wend
	
	Timer5(556,100,ITEM,ITEM_DESCRIPTION,TicketCount,PlayerCount,3)
ElseIf ACTION = 3
	;select winner and give item
	If TICKETS < 3
		SendMessageToAll("Sorry, today's raffle is canceled due to a lack of players",#MSG_TELL)
		SendMessageToAll("Your purchased tickets can be used in tomorrow's raffle",#MSG_TELL)
		GoTo END
	EndIf
	
	TicketNumber = Random(0,TICKETS-1)
	
	UserID = GetNextOnlineUser(0)
	While TicketNumber > 0
		While HasItem(UserID,RaffleTicket) = 1
			TicketNumber = TicketNumber - 1
			TakeItem(UserID,RaffleTicket)
			If TicketNumber = 0
				GoTo BREAK
			EndIf
		Wend
		
		UserID = GetNextOnlineUser(UserID)
	Wend
	Label BREAK
	
	SendMessageToAll(GetUserName(UserID)+" has won the raffle! Tomorrow another raffle will take place.",#MSG_TELL)
	Timer(558,50,ITEM,UserID,0)
	Timer5(556,100,0,0,0,0,4)
ElseIf ACTION = 4
	;remove raffle tickets
	UserID = GetNextOnlineUser(0)
	While UserID > 0
		While HasItem(UserID,RaffleTicket) = 1
			TakeItem(UserID,RaffleTicket)
		Wend
		
		UserID = GetNextOnlineUser(UserID)
	Wend
	
	If GetDayofWeek() = 4
		SendMessageToAll("The lottery number will be broadcasted in one hour",#MSG_TELL)
		SendMessageToAll("Current jackpot: "+GetUserFlag(0,36),#MSG_TELL)
	EndIf
EndIf

Label END

Continue = 1