'teamrand
'1.331
'&Team Randomizer:SoCxFiftyToo
'&pickteams
'&
'&Type "/me" or "/emote" to register yourself while teams are being picked

' written by SoCxFiftyToo
' 07/05/2005

'// ver 1.31 - Jack
'//   *Removed call to CreateTimer

' Ver 1.34 - TheShadow
'Add the Calls And Increase number of random to 8 teams


Public Const teamrand_iSeconds = 20
Public Const teamrand_iMaxPlayers = 32

Public teamrand_bRegister, Total_MiliSec, teamrand_iPlayerCount, teamrand_oDict

Sub Event_Load()
	AddChat vbGreen, "Rand8 Version 1.34 By SoCxFiftyToo/Jack Modified By TheShadow is Loaded."
	
	Randomize
	teamrand_bRegister = False
	teamrand_iPlayerCount = 0	
	
	Total_MiliSec = teamrand_iSeconds * 1000
	Send Total_MiliSec
	Call AddTimer ("TeamRand", "Rand_Timer", Total_MiliSec, true)
	
	Call CreateCommand("randpick", "TeamRand", "Command_RandPick", "", "", "Help for RandPick command")
	Call CreateCommand("randver", "TeamRand", "Command_RandVer", "", "", "Help for RandVer command")
	Call CreateCommand("randstatus", "TeamRand", "Command_RandStatus", "", "", "Help for RandStatus command")
	
	Set teamrand_oDict = CreateObject("Scripting.Dictionary")
End Sub

Sub Event_Unload()
	AddChat vbRed, "Rand8 Unloaded!"
End Sub

Sub Command_RandPick(CS)
	Send "Randomizing teams in " & teamrand_iSeconds & " seconds. Type '/me' to enter one of the teams."
	teamrand_bRegister = True
	teamrand_iPlayerCount = 0
	Call StartTimer ("TeamRand", "Rand_Timer")
End Sub

Sub Command_RandVer(CS)
	Send "/me Rand8 Version 1.34 By SoCxFiftyToo/Jack Modified By TheShadow."
End Sub

Sub Command_RandStatus(CS)
	Send "Remaining :"  & " MISSING CODE!!!"
End Sub

Sub Event_UserEmote(CE)
	'Just a temp msg
	Send teamrand_bRegister & " " & "msg :" & CE.Message
	
	If teamrand_bRegister = True And CE.Message = "" Then
		If teamrand_iPlayerCount = teamrand_iMaxPlayers Then
			Send teamrand_iMaxPlayers & " players are already registered."
		Else
			If teamrand_oDict.Exists(CE.Username) Then
				Send CE.Username & " is already registered."
			Else
				Send "Added " & CE.Username & " to list."
				teamrand_iPlayerCount = teamrand_iPlayerCount + 1
				teamrand_oDict.Add CE.Username, "Player" & teamrand_iPlayerCount
			End If
		End If
	End If
End Sub


Sub Rand_Timer()
	'Just a temp msg
	Send "time function"
	
	Call StopTimer ("TeamRand", "Rand_Timer")
	Dim aPlayers(12)
	teamrand_bRegister = False
	Send "Registration is complete. " & teamrand_iPlayerCount & " players have signed up."

	If teamrand_iPlayerCount < 4 Then
		Send "Not enough players registered. Please try again."
		teamrand_oDict.RemoveAll
		Exit Sub
	End If

	Send "Randomizing teams now..."

	'// Populate Array based on dictionary
	X = 0
	For Each Username In teamrand_oDict.Keys
		X = X + 1
		aPlayers(X) = Username
	Next

	if teamrand_iPlayerCount > 16 Then
		'// Swap random elements of the array to shuffle the players
		For X = 1 to 500
			iA = teamrand_RanNum(1,teamrand_iPlayerCount)
			iB = teamrand_RanNum(1,teamrand_iPlayerCount)
			iC = teamrand_RanNum(1,teamrand_iPlayerCount)
			iD = teamrand_RanNum(1,teamrand_iPlayerCount)
			iE = teamrand_RanNum(1,teamrand_iPlayerCount)
			iFF = teamrand_RanNum(1,teamrand_iPlayerCount)
			iG = teamrand_RanNum(1,teamrand_iPlayerCount)
			iH = teamrand_RanNum(1,teamrand_iPlayerCount)

			sA = aPlayers(iA)
			sB = aPlayers(iB)
			sC = aPlayers(iC)
			sD = aPlayers(iD)
			sE = aPlayers(iE)
			sF = aPlayers(iFF)
			sG = aPlayers(iG)
			sH = aPlayers(iH)

			aPlayers(iA) = sH
			aPlayers(iB) = sG
			aPlayers(iC) = sF
			aPlayers(iD) = sE
			aPlayers(iE) = sD
			aPlayers(iFF) = sC
			aPlayers(iG) = sB
			aPlayers(iH) = sA

		Next
	Else
		'// Swap random elements of the array to shuffle the players
		For X = 1 to 500
			iA = teamrand_RanNum(1,teamrand_iPlayerCount)
			iB = teamrand_RanNum(1,teamrand_iPlayerCount)
			iC = teamrand_RanNum(1,teamrand_iPlayerCount)
			iD = teamrand_RanNum(1,teamrand_iPlayerCount)
			iE = teamrand_RanNum(1,teamrand_iPlayerCount)
			iFF = teamrand_RanNum(1,teamrand_iPlayerCount)
			iG = teamrand_RanNum(1,teamrand_iPlayerCount)
			iH = teamrand_RanNum(1,teamrand_iPlayerCount)

			sA = aPlayers(iA)
			sB = aPlayers(iB)
			sC = aPlayers(iC)
			sD = aPlayers(iD)
			sE = aPlayers(iE)
			sF = aPlayers(iFF)
			sG = aPlayers(iG)
			sH = aPlayers(iH)

			aPlayers(iA) = sH
			aPlayers(iB) = sG
			aPlayers(iC) = sF
			aPlayers(iD) = sE
			aPlayers(iE) = sD
			aPlayers(iFF) = sC
			aPlayers(iG) = sB
			aPlayers(iH) = sA
		Next
	End if

	sTeam1 = ""
	sTeam2 = ""
	sTeam3 = ""
	sTeam4 = ""
	sTeam5 = ""
	sTeam6 = ""
	sTeam7 = ""
	sTeam8 = ""

	sTeam1 = sTeam1 & aPlayers(1) & " "
	sTeam1 = sTeam1 & aPlayers(2) & " "
	sTeam1 = sTeam1 & aPlayers(3) & " "
	sTeam1 = sTeam1 & aPlayers(4) & " "
	sTeam1 = sTeam1 & aPlayers(5) & " "
	sTeam1 = sTeam1 & aPlayers(6) & " "
	sTeam1 = sTeam1 & aPlayers(7) & " "
	sTeam1 = sTeam1 & aPlayers(8) & " "

	Send "Team 1: " & sTeam1
	Send "Team 2: " & sTeam2
	Send "Team 3: " & sTeam3
	Send "Team 4: " & sTeam4
	Send "Team 5: " & sTeam5
	Send "Team 6: " & sTeam6
	Send "Team 7: " & sTeam7
	Send "Team 8: " & sTeam8

	teamrand_oDict.RemoveAll
End Sub

'// Used to get a random number between minimum and maximum.
'// You might need to check that the number has not been already assigned to a user
'// otherwise there could be double-ups in the teams.
'// Also why is there 8 teams? Thats the equivalent of 1v1v1v1v1v1v1v1
Function teamrand_RanNum(minimum, maximum)
	teamrand_RanNum = Int(((maximum + 1) - (minimum)) * Rnd + (minimum))
End Function