;'Lottery payout' - timed script

LottoTicket = 488

NUMBER=#PARAM2

If #PARAM1 = 0
	SendMessageToAll("The jackpot value stands at "+GetUserFlag(0,36)+" silver!",#MSG_TELL)
	SendMessageToAll("The weekly lottery jackpot number is about to be announced...",#MSG_EMOTE)
	
	WinningNumber = Random(0,9999)
	Timer(557,10000,1,WinningNumber,0)
ElseIf #PARAM1 = 1
	SendMessageToAll("And the winning number is... ["+WinningNumber+"]!",#MSG_EMOTE)
	Timer(557,0,2,NUMBER,0)
ElseIf #PARAM1 = 2
	MAX_LOOP = 50
	LOOP = 0
	UserID = GetNextOnlineUser(#PARAM3)
	While UserID > 0
		If HasItem(UserID,LottoTicket) = 1
			Timer(557,0,3,NUMBER,UserID)
		EndIf
		
		UserID = GetNextOnlineUser(0)
		LOOP = LOOP + 1
		If LOOP = MAX_LOOP
			GoTo BREAK
		EndIf
	Wend
	Label BREAK
	
	If UserID > 0
		Timer(557,0,2,NUMBER,UserID)
	Else
		Timer(557,500,4,NUMBER,0)
	EndIf
ElseIf #PARAM1 = 3
	UserID = #PARAM3
	WIN_NUMBER=""+#PARAM2
	USER_NUMBER=""+GetUserFlag(UserID,36)
	WIN_AMOUNT = 0
	
	i=0
	While i < 4
		i=i+1
		WIN_NUM=SubString(WIN_NUMBER,i,1)
		USER_NUM=SubString(USER_NUMBER,i,1)
		If USER_NUM = WIN_NUM
			WIN_AMOUNT=WIN_AMOUNT+1
		EndIf
	Wend
	
	If WIN_AMOUNT = 1
		WIN_AMOUNT = Floor(GetUserFlag(0,36) * 0.10)
	ElseIf WIN_AMOUNT = 2
		WIN_AMOUNT = Floor(GetUserFlag(0,36) * 0.25)
	ElseIf WIN_AMOUNT = 3
		WIN_AMOUNT = Floor(GetUserFlag(0,36) * 0.50)
	ElseIf WIN_AMOUNT = 4
		WIN_AMOUNT = GetUserFlag(0,36)
	EndIf
	
	If WIN_AMOUNT > 0
		SendMessageToAll("Congratulations, "+GetUserName(UserID)+", you have won "+WIN_AMOUNT+" silver!",#MSG_TELL)
		SetUserSilver(UserID,GetUserSilver(UserID)+WIN_AMOUNT)
	EndIf
ElseIf #PARAM1 = 4
	SetUserFlag(0,36,0)
	SendMessageToAll("Current jackpot: "+GetUserFlag(0,36),#MSG_TELL)
	Timer(557,0,5,0,0)
ElseIf #PARAM1 = 5
	;Remove all lottery tickets from everyone
	
	MAX_LOOP = 50
	LOOP = 0
	
	UserID = GetNextUser(#PARAM3)
	While UserID > 0
		While HasItem(UserID,LottoTicket) = 1
			TakeItem(UserID,LottoTicket)
		EndIf
		
		UserID = GetNextOnlineUser(0)
		LOOP = LOOP + 1
		If LOOP = MAX_LOOP
			GoTo BREAK
		EndIf
	Wend
	Label BREAK
	
	If UserID > 0
		Timer(557,0,2,NUMBER,UserID)
	EndIf
EndIf

Continue = 1