Team Balancer for Four Teams?
#1

Hello! I can only create a team balancer for two teams, but how about a team balancer for four teams? Can someone demonstrate a sample code for me (or a code structure to follow)? Thanks!
Reply
#2

Perhaps you should provide us with YOUR 2 team balancer so we have the basics of your system.
Reply
#3

PHP код:
#define TEAM1 0
#define TEAM2 1
#define TEAM3 2
#define TEAM4 3

new gTeam[MAX_PLAYERS];

public 
OnPlayerRequestClass(playeridclassid)
{
     switch(
classid)
     {
          case 
0: { gTeam[playerid] = TEAM1SetPlayerTeam(playerid0); }
          case 
1: { gTeam[playerid] = TEAM2SetPlayerTeam(playerid1); }
          case 
2: { gTeam[playerid] = TEAM3SetPlayerTeam(playerid2); }
          case 
3: { gTeam[playerid] = TEAM4SetPlayerTeam(playerid3); }
     }
     return 
1;
}

stock GetPlayersCountInTeam(teamid)
{
     new 
playercount 0
     for(new 
0MAX_PLAYERS++)
     {
           if(
GetPlayerState(x) == PLAYER_STATE_NONE) continue; 
           if(
GetPlayerTeam(x) != teamid) continue; 
           
playercount++;
     }
     return 
playercount;
}

public 
OnPlayerRequestSpawn(playerid)
{
     new 
team1count GetPlayersCountInTeam(TEAM1);
     new 
team2count GetPlayersCountInTeam(TEAM2); 
     new 
team3count GetPlayersCountInTeam(TEAM3);
     new 
team4count GetPlayersCountInTeam(TEAM4);
     switch(
gTeam[playerid])
     {
          case 
TEAM1:
          {
                if(
team1count team2count && team3count && team4)
                {
                      
SendClientMessage(playerid0xFF0000FF,"This team is currently full!");
                      return 
0;
                }
          }
          case 
TEAM2:
          {
                if(
team2count team1count && team3count && team4)
                {
                      
SendClientMessage(playerid0xFF0000FF,"This team is currently full!");
                      return 
0;
                }
          }
          case 
TEAM3:
          {
                if(
team3count team1count && team2count && team4)
                {
                      
SendClientMessage(playerid0xFF0000FF,"This team is currently full!");
                      return 
0;
                }
          }
          case 
TEAM4:
          {
                if(
team4count team1count && team2count && team3)
                {
                      
SendClientMessage(playerid0xFF0000FF,"This team is currently full!");
                      return 
0;
                }
          }
      }
      return 
1;

Credits: Jarnu and me
Reply
#4

Quote:
Originally Posted by ReD_HunTeR
Посмотреть сообщение
Credits: Jarnu and me
Credits? For something that isn't going to work like he wants it to?
Reply
#5

Quote:
Originally Posted by Crayder
Посмотреть сообщение
Credits? For something that isn't going to work like he wants it to?
Welcome to scripting help section
where everyone makes absolute crap code which makes no sense and hopes it works

Have seen some cool stuff here, like apparently someone's code was fixed by swapping (!value) with (value==0)



OT: honestly it's not that hard: follow these steps

OnPlayerConnect > add 1 to the initial connected players count
OnPlayerRequestSpawn > check the team he chooses,if it's >(connectedplayers/4)+BALANCE_MARGIN(your defined number, so that means there could be that many more than 1/4 of all connected players) return 0 and say it's full
same check, see if only 1 team is filled, call that team full and return 0

if those checks are okay, increase that team's count to +1 and return 1; (so the player spawns)
OnPlayerDisconnect > lower connected players by 1;

Try these, give me a code and I can fix it for you, important part is that please try yourself and don't look for a plug n play code, you will fail, have errors, we will fix them for you and you will learn

EDIT: another way to do it:


OnPlayerRequestClass > lower spawned players by 1
OnPlayerRequestSpawn > check the team he chooses, if it's (selectedteam-lowestteam)>BALANCE_MARGIN(your defined number, so that means there could be that many more than 1/4 of all connected players) return 0 and say it's full

if those checks are okay, increase that team's count to +1, add 1 to spawned players and return 1; (so the player spawns)

OnPlayerDisconnect > if the player was spawned (the variable is true) lower spawned players by 1
Reply
#6

No, I wasnt looking for a plug and play code. I am looking for ideas anyways thank you all for the inputs
Reply
#7

Use array sorting. Store down all team values/counts in an array and then sort it (descending).

So you can resist player to choose the first sorted team as its having the most players, and so on.

http://forum.sa-mp.com/showpost.php?...postcount=1737
https://sampforum.blast.hk/showthread.php?tid=343172
Reply
#8

I suggest just using y_iterate over anything. It already includes a Player iterator so you can just do this:

pawn Код:
OnPlayerConnect...
new online = Iter_Count(Player)...
online /= NUMBER_OF_TEAMS...

new count;
foreach(new i: Player) {
    SetPlayerTeam(i, count % NUMBER_OF_TEAMS);
    count++;
}
That's just an example of setting their teams everytime a player connects. Yes this code would work. No it's not the best. And yes, it's just pseudo.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)