I want to assign everyone to a random team. This following code loops through all the players and assigns them to a random team if the team is not maxed out, otherwise it will assign them to the other team.
PHP Code:
for (i = 0; i < numberOfPlayers; i++) {
rand = random_num(0, 1)
switch(rand) {
case 1: {
if (ctCount < maxPlayersPerTeam) {
cs_set_user_team(players[i], CS_TEAM_CT)
ctCount++
} else {
cs_set_user_team(players[i], CS_TEAM_T)
tCount++
}
}
default: {
if (tCount < maxPlayersPerTeam) {
cs_set_user_team(players[i], CS_TEAM_T)
tCount++
} else {
cs_set_user_team(players[i], CS_TEAM_CT)
ctCount++
}
}
}
}
It works as intended, but I have two questions about optimization:
- I know you are not supposed to re-index arrays, but is the above code fine given that I believe you are only ever accessing players[i] once per for-loop?
- It is possible that the above code encounters someone on a team and the random variable will have them "transferred" over to the same team. Would it be more optimal to have a check before transferring? Something like:
PHP Code:
if (cs_get_user_team(players[i]) != CS_TEAM_CT) cs_set_user_team(players[i], CS_TEAM_CT)
I know the above would re-index the array, I would change that afterwards if this is the better way to go.
Edit: One bonus 3rd question: Why do I always see variables in this forum start with "sz"? Like szVariableName?