English Randomize Teams and Leave Hook

10 replies
Goto Page
To the start Previous 1 Next To the start
Up
mrc
User
Offline Off
-- 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 05:51:17 am
CS2D GLOBAL's Discord Server: https://discord.gg/kgfwH4P
16.03.19 11:50:44 am
Up
Rainoth
Moderator
Offline Off
Code:
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



Code:
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 12:50:27 pm
16.03.19 09:52:22 pm
Up
Quattro
GAME BANNED
Offline Off


Good luck, you will need it
17.03.19 01:57:19 am
Up
mrc
User
Offline Off
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 05:39:30 am
CS2D GLOBAL's Discord Server: https://discord.gg/kgfwH4P
17.03.19 11:39:42 am
Up
DC
Admin
Offline Off
I found a simple table shuffle here:
Code:
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
)
Code:
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.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
17.03.19 12:53:21 pm
Up
Rainoth
Moderator
Offline Off
@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 >.< !
17.03.19 03:57:31 pm
Up
mrc
User
Offline Off
@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 04:26:49 pm
CS2D GLOBAL's Discord Server: https://discord.gg/kgfwH4P
17.03.19 04:29:01 pm
Up
DC
Admin
Offline Off
Make sure cs2d cmd mp_autoteambalance is off. Otherwise it will probably prevent team switching.
(actually I'm not sure if it affects cs2d cmd maket / cs2d cmd makect but you should give it a try)

If this doesn't help: When (in which hook) do you execute the script?
cs2d lua hook spawn (very bad idea!) and cs2d lua hook startround might not work. Try another one. E.g. cs2d lua hook startround_prespawn or something completely unrelated to the round system.
www.UnrealSoftware.de | www.CS2D.com | www.CarnageContest.com | Use the forum & avoid PMs!
17.03.19 05:00:22 pm
Up
mrc
User
Offline Off
Thanks @user DC: its working now!
edited 5×, last 18.03.19 10:42:11 pm
CS2D GLOBAL's Discord Server: https://discord.gg/kgfwH4P
17.03.19 07:01:08 pm
Up
Hajt
User
Offline Off
@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.
Yooo. We are playing mix matches 5on5 in evening hours on the server CS2D.TK | Mix/Match, you can join us, server password is match. Statistics, leaderboard can be found under the following link.
17.03.19 08:50:58 pm
Up
mrc
User
Offline Off
I made some stuff and everything is working now, thanks guys!
edited 1×, last 18.03.19 10:41:16 pm
CS2D GLOBAL's Discord Server: https://discord.gg/kgfwH4P
To the start Previous 1 Next To the start