Forum

> > CS2D > Scripts > Randomize Teams and Leave Hook
Forums overviewCS2D overview Scripts overviewLog in to reply

English Randomize Teams and Leave Hook

10 replies
To the start Previous 1 Next To the start

old Randomize Teams and Leave Hook

mrc OP
User Off Offline

Quote
-- Randomize Teams
I just need a script for that.

Detect current players at T and CT then randomize and pick 5 for T and 5 for CT, the rest is moved to spectators.

--Leave Hook

If a player leave (reason id 0) then temp. ban else don't ban.
edited 2×, last 16.03.19 04:51:17 am

old Re: Randomize Teams and Leave Hook

Rainoth
Moderator Off Offline

Quote
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function shuffle()
teams = {
  ct = {},
  t = {},
  spec = {},
  counter = 1
}
  for k,v in pairs (player(0,"team1")) do
    if teams.counter <= 5 then 
      teams.counter = teams.counter + 1
      teams.t[#teams.t+1] = v
    else
      teams.spec[#teams.spec+1] = v
    end
  end
  teams.counter = 1
  for k,v in pairs (player(0,"team2")) do
    if teams.counter <= 5 then 
      teams.counter = teams.counter + 1
      teams.ct[#teams.ct+1] = v
    else
      teams.spec[#teams.spec+1] = v
    end
  end
  for k,v in pairs (teams) do
    if type(v) == "table" then
      for _, id in pairs (v) do
        parse("make"..k.." "..id)
      end
    end
  end
end


1
2
3
4
5
6
addhook("leave","_leave")
function _leave(id,reason)
  if reason == 0 then
    parse("banip "..player(id,"ip").." -1")
  end
end

Untested. Good luck.
edited 1×, last 17.03.19 11:50:27 am

old Re: Randomize Teams and Leave Hook

mrc OP
User Off Offline

Quote
The Randomize Teams script is always moving to T. Maybe another way is easier to do? Checking all IDs of T and CT only and mix them. No need to move for spectators.
edited 7×, last 17.03.19 04:39:30 am

old Re: Randomize Teams and Leave Hook

DC
Admin Off Offline

Quote
I found a simple table shuffle here:
1
2
3
4
5
6
7
function shuffle(tbl)
	for i = #tbl, 2, -1 do
		local j = math.random(i)
		tbl[i], tbl[j] = tbl[j], tbl[i]
		end
	return tbl
end

With that all we have to do is
• get all Ts and CTs and put their IDs in one big array (
getPlayersInTeams
)
• shuffle that array (swap entries randomly) (
shuffle
)
• make first 5 T, next 5 CT and rest spec (
assignRandomTeams
)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function getPlayersInTeams()
	-- Get and join team1 and team2 players
	local t = player(0,"team1")
	local tCount = #t

	local ct = player(0,"team2")
	local ctCount = #ct

	for i = 1, ctCount do
		t[tCount + i] = ct[i]
	end

	return t
end

function assignRandomTeams(playersPerTeam)
	-- Get and shuffle players
	local ids = getPlayersInTeams()
	shuffle(ids)

	-- Assign teams
	local playerCount = #ids
	for i = 1, playerCount do
		if (i <= playersPerTeam) then
			parse("maket " .. ids[i])
		else if (i <= playersPerTeam * 2) then
			parse("makect " .. ids[i])
		else
			parse("makespec " .. ids[i])
		endif
	end
end

Needs to be called like
assignRandomTeams(5)


I didn't test that and there's certainly something wrong but I hope you get the idea.

old Re: Randomize Teams and Leave Hook

Rainoth
Moderator Off Offline

Quote
@user mrc: Oops!
I copy pasted the code from moving to t to moving ct and I just needed to add a letter "c" in front of every "t" but I managed to write
teamsc.t
instead of
teams.ct

I fixed it in my original post.
Sorry >.< !

old Re: Randomize Teams and Leave Hook

mrc OP
User Off Offline

Quote
@user DC:

I've tested and the script is moving to the team that players already are (for example T to T and CT to CT) 100% of the time.

@user Rainoth:

Same thing happens.
edited 2×, last 17.03.19 03:26:49 pm

old Re: Randomize Teams and Leave Hook

Hajt
User Off Offline

Quote
@user mrc: After usage
parse("banusgn "..id.." 10")
player leave the server so this variable id doesn't belong to him anymore. You should storage his steamid, ip and name in temporary variables.

Moreover I don't get why you put loop in that leave hook.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview