;'Dance Master Event' ending script

DanceMaster=#PARAM3

PlayerCount=0

Y=3
While Y < 10
	X=3
	While X < 18
		UserID=GetUserAtXY(655,X,Y)
		If UserID > 0
			PlayerCount=PlayerCount+1
		EndIf
		X=X+1
	Wend
	Y=Y+1
Wend

Share=0

Silver = GetUserFlag(0,45) * 5000

If PlayerCount > 1
	SendMessageToAll("The event is now over. The winnings are split between the winners.",#MSG_SERVER)
	PlayerCount=PlayerCount-1
	Share=Floor(Silver/PlayerCount)
	If Share > 20000
		Share = 20000
	EndIf
Else
	SendMessageToAll("The event is now over. Dance master "+GetUserName(DanceMaster)+" won the event!",#MSG_SERVER)
	SendMessageToUser(DanceMaster,"You got 25000 silver for winning the event.",#MSG_TELL)
	SetUserSilver(DanceMaster,GetUserSilver(DanceMaster)+25000)
	WarpUser(DanceMaster,509,10,11)
	GoTo END
EndIf

SendMessageToUser(DanceMaster,"You failed to dance all you could dance.",#MSG_EMOTE)
WarpUser(DanceMaster,509,10,11)

Y=3
While Y < 10
	X=3
	While X < 18
		UserID=GetUserAtXY(655,X,Y)
		If UserID > 0
			SendMessageToUser(UserID,"You got "+Share+" silver, as your share.",#MSG_TELL)
			SetUserSilver(UserID,GetUserSilver(UserID)+Share)
			WarpUser(UserID,509,10,11)
		EndIf
		X=X+1
	Wend
	Y=Y+1
Wend

SetUserFlag(0,10,0)
SetUserFlag(0,11,0)
SetUserFlag(0,45,0)

Label END

Continue = 1