Team balancer
#1

Is this right?

pawn Код:
//at the top
new Spawned[MAX_PLAYERS];
new BBBalancer,MGBalancer,DMBalancer; //3 teams in my gm

//onplayerconnect
Spawned[playerid] = 0;

//onplayerspawn
if(Spawned[playerid] == 1)
{
    if(team[playerid] == BlueberryArmy) BBBalancer++;
    if(team[playerid] == MontgomeryArmy) MGBalancer++;
    if(team[playerid] == DillimoreArmy) DMBalancer++;
}
Spawned[playerid] = 0;

//onplayerdisconnect
public OnPlayerDisconnect(playerid, reason)
{
    if(team[playerid] == BlueberryArmy) BBBalancer--;
    if(team[playerid] == DillimoreArmy) DMBalancer--;
    if(team[playerid] == MontgomeryArmy) MGBalancer--;
    return 1;
}

public OnPlayerRequestSpawn(playerid)
{
    Spawned[playerid] = 1;
    if(team[playerid] == BlueberryArmy && BBBalancer > MGBalancer || BBBalancer > DMBalancer)
    {
        SendClientMessage(playerid,red,"[ERROR] Team full. Choose another team!");
        return 0;
    }
    if(team[playerid] == MontgomeryArmy && MGBalancer > BBBalancer || MGBalancer > DMBalancer)
    {
        SendClientMessage(playerid,red,"[ERROR] Team full. Choose another team!");
        return 0;
    }
    if(team[playerid] == DillimoreArmy && DMBalancer > MGBalancer || DMBalancer > BBBalancer)
    {
        SendClientMessage(playerid,red,"[ERROR] Team full. Choose another team!");
        return 0;
    }
    return 1;
}
Reply
#2

the code is a bit messy
i will show u an example

pawn Код:
#define MAX_TEAM_PLAYERS 10 //maximum players in a team

new Balancer[MAX_TEAM_PLAYERS];
new BlueberryArmyP;
//onplayerspawn
{
if(team[playerid] == BlueberryArmy && Balancer[BlueberryArmyP] > MAX_TEAM_PLAYERS)
     {
            //Team Full
     }else{
            BlueberryArmyP++;
     }
}
2nd way

pawn Код:
new Team1;

//onplayerspawn
{
if(team[playerid] == BlueberryArmy && Team1 >= 10) // 10 is the maximum players in a Team!
     {
            //Team Full
     }else{
            Team1++;
     }
}
Reply
#3

Why onplayerspawn? I want to prevent someone from spawning if the team they are trying to join is full. Btw, it seems that I have a problem in this script. I've tested with my laptop and pc.
- Player1 has spawned
- Player2 has connected but Player2 can't spawn. It says "[ERROR] Team full. Choose another team!"
Reply
#4

onplayerrequestclass exist for it.
Reply
#5

No. Anyone else?
Reply
#6

Might be helpfull.
I'm in academy now,code maded on my mobile phone, so this non-tested code.
If it's helpfull, rep+! Lol
pawn Код:
//top of the script
new pTeam[MAX_PLAYERS];
new TeamPlayers[3];//3 = amount of your teams 0= BB, 1=MG, 2=DM

//request class
switch(classid)
{
case 0: pTeam[playerid] = 0;
case 1: pTeam[playerid] = 1;
case 2: pTeam[playerid] = 2;
}
//request spawn
for(new i=0; i<3; i++)
{
if(pTeam[playerid]==i) TeamBalance[i]++;
for(new j=0; j<3; j++)
{
if(i !=j)
{
 if(TeamBalance[i] > TeamBalance[j])
{
 return SendClientMessage(playerid,0xFFFFFFFF,"TeamBalance: Please Choose Another Team"), TeamBalance[i]--;
}
else if(TeamBalance[i]<TeamBalance[j] || TeamBalance[i]==TeamBalance[j])
{
return 1;//spawning player
}
}
}
}
Reply
#7

I dont think that your code will work, it will only allow you to join the team with the lowest team members

pawn Код:
// global
enum Team {
    BlueberryArmy,
    MontgomeryArmy,
    DillimoreArmy
}
stock
    Team: gTeam[MAX_PLAYERS] = {Team, ...}
;
pawn Код:
// OnPlayerRequestClass
    switch(classid) {
        case 0..2: gTeam[playerid] = BlueberryArmy;
        case 3..5: gTeam[playerid] = MontgomeryArmy;
        case 6..8: gTeam[playerid] = DillimoreArmy;
    }
pawn Код:
// OnPlayerRequestSpawn
    new
        i,
        tie,
        Team: biggest,
        count[Team + Team: 1]
    ;
    for( ; i != MAX_PLAYERS; ++i) { // foreach
        count[gTeam[i]]++;
    }
    for(i = 1; i < _: Team; ++i) {
        if((tie = count[biggest] / count[Team: i]) == 0) {
            biggest = Team: i;
        } else {
            tie = (count[biggest] == count[Team: i]);
        }
    }
    if((tie == 0) && (gTeam[playerid] == biggest)) {
        return !SendClientMessage(playerid, 0xFF0000FF, "[ERROR] Team full. Choose another team!");
    }
    return true;
pawn Код:
// OnPlayerDisconnect
    gTeam[playerid] = Team;
Reply
#8

Quote:
Originally Posted by Nero_3D
Посмотреть сообщение
I dont think that your code will work, it will only allow you to join the team with the lowest team members

pawn Код:
// global
enum Team {
    BlueberryArmy,
    MontgomeryArmy,
    DillimoreArmy
}
It gave me a error here
Код:
error 001: expected token: "-identifier-", but found "-integer value-"
Btw, I've already defined my teams
pawn Код:
#define BlueberryArmy   1
#define MontgomeryArmy  2
#define DillimoreArmy   3
new team[MAX_PLAYERS];
Reply
#9

Quote:
Originally Posted by newbienoob
Посмотреть сообщение
It gave me a error here
Код:
error 001: expected token: "-identifier-", but found "-integer value-"
Btw, I've already defined my teams
pawn Код:
#define BlueberryArmy   1
#define MontgomeryArmy  2
#define DillimoreArmy   3
new team[MAX_PLAYERS];
okay, than we need some changes, that should work for you
pawn Код:
// OnPlayerRequestSpawn
    new
        i,
        tie,
        count[4],
        biggest = 1
    ;
    for( ; i < MAX_PLAYERS; ++i) { // use foreach if available
        count[team[i]]++;
    }
    for(i = 2, count[team[playerid]]--; i < sizeof count; ++i) {
        if(count[biggest] < count[i]) {
            biggest = i;
            tie = 0;
        } else {
            tie = (count[biggest] == count[i]);
        }
    }
    if((tie == 0) && (team[playerid] == biggest)) {
        return !SendClientMessage(playerid, 0xFF0000FF, "[ERROR] Team full. Choose another team!");
    }
    return true;
pawn Код:
// OnPlayerDisconnect
    team[playerid] = 0;
Reply
#10

From my server console
Код:
[23:02:18] [debug] Run time error 11: "Divide by zero"
[23:02:18] [debug] AMX backtrace:
[23:02:18] [debug] #0 0000cc70 in public OnPlayerRequestSpawn () from cw.amx
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)