SA-MP Forums Archive
How to get top 5 kills of each team? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: How to get top 5 kills of each team? (/showthread.php?tid=530771)



How to get top 5 kills of each team? - Onfroi - 09.08.2014

Like the title says, any ideas?


Respuesta: How to get top 5 kills of each team? - ThePhenix - 09.08.2014

Can you explain your question of a better way?


Re: How to get top 5 kills of each team? - Andre02 - 09.08.2014

I think he means, diferent teams are fighting, each team have a number of players and at the end of the round or w/e it is, he want to get the top 5 players of each team who killed more players


Re: How to get top 5 kills of each team? - Onfroi - 09.08.2014

Quote:
Originally Posted by Andre02
Посмотреть сообщение
I think he means, diferent teams are fighting, each team have a number of players and at the end of the round or w/e it is, he want to get the top 5 players of each team who killed more players
Exactly.


Re: How to get top 5 kills of each team? - SickAttack - 09.08.2014

http://forum.sa-mp.com/showpost.php?...postcount=1737
http://forum.sa-mp.com/showpost.php?...postcount=1760


Re: How to get top 5 kills of each team? - Zezombia - 09.08.2014

Hey, just stopped in for a peek, been out of the loop for a while and this is not the most efficient way to do this. I just wrote it up quick and believe it should work. *crosses fingers*

Top of script:
pawn Код:
new KillCount[MAX_PLAYERS];
In OnPlayerConnect:
pawn Код:
KillCount[playerid] = 0;
In OnPlayerDeath:
pawn Код:
KillCount[killerid]++;
And at the bottom of your script:
pawn Код:
GetTopFive(team)
{
    new TopFive[5];

    for(new i = 0; i < 5; i++)
    {
        for(new a = 0; a < MAX_PLAYERS; a++)
        {
            if(GetPlayerTeam(a) == team)
            {
                for(new b = 0; b < MAX_PLAYERS; b++)
                {
                    if(KillCount[a] > KillCount[b] && GetPlayerTeam(b) == team)
                    {
                        if(i == 0 && TopFive[0] < KillCount[a])
                        {
                            TopFive[0] = a;
                        }
                        else if(TopFive[i] < KillCount[a] && TopFive[i - 1] > KillCount[a])
                        {
                            TopFive[i] = a;
                        }
                    }
                }
            }
        }
    }
   
    return TopFive;
}
Now, when you want to find the top five:
pawn Код:
new variable[5];
variable = GetTopFive(team);
Team obviously being the team you wish to get the top five in. variable[0] through variable[4] will have the top five playerids from highest to lowest. Hope this helps.


Re: How to get top 5 kills of each team? - Onfroi - 10.08.2014

Quote:
Originally Posted by Zezombia
Посмотреть сообщение
Hey, just stopped in for a peek, been out of the loop for a while and this is not the most efficient way to do this. I just wrote it up quick and believe it should work. *crosses fingers*

Top of script:
pawn Код:
new KillCount[MAX_PLAYERS];
forward GetTopFive(team);
In OnPlayerConnect:
pawn Код:
KillCount[playerid] = 0;
In OnPlayerDeath:
pawn Код:
KillCount[killerid]++;
And at the bottom of your script:
pawn Код:
GetTopFive(team)
{
    new TopFive[5];

    for(new i = 0; i < 5; i++)
    {
        for(new a = 0; a < MAX_PLAYERS; a++)
        {
            if(GetPlayerTeam(a) == team)
            {
                for(new b = 0; b < MAX_PLAYERS; b++)
                {
                    if(KillCount[a] > KillCount[b] && GetPlayerTeam(b) == team)
                    {
                        if(i == 0 && TopFive[0] < KillCount[a])
                        {
                            TopFive[0] = a;
                        }
                        else if(TopFive[i] < KillCount[a] && TopFive[i - 1] > KillCount[a])
                        {
                            TopFive[i] = a;
                        }
                    }
                }
            }
        }
    }
   
    return TopFive;
}
Now, when you want to find the top five:
pawn Код:
new variable[5];
variable = GetTopFive(team);
Team obviously being the team you wish to get the top five in. variable[0] through variable[4] will have the top five playerids from highest to lowest. Hope this helps.
Thank you very much, but I was testing it and say I do something like this:
pawn Код:
new Top[5],string[256];
Top = GetTopFive(Team1);
format(string, sizeof(string), "Top 1 | Name: %s | Kills: %d | Deaths: %d", pName(Top[0]), KillsCount(Top[0]), deaths[0]));
SendClientMessage(playerid, -1, string);
format(string, sizeof(string), "Top 2 | Name: %s | Kills: %d | Deaths: %d", pName(Top[1]), KillsCount(Top[1]), deaths[1]));
SendClientMessage(playerid, -1, string);
// and continue doing that until I get to Top[4]...
The problem is that if there is 1 player online (like I was testing it alone) the same player's info keeps repeating.


Re: How to get top 5 kills of each team? - Lordzy - 10.08.2014

Check if player is connected or not inside the loop and then return the values accordingly. Or you can just look if the player is connected or not with the returned values.


Re: How to get top 5 kills of each team? - Onfroi - 10.08.2014

Quote:
Originally Posted by Lordzy
Посмотреть сообщение
Check if player is connected or not inside the loop and then return the values accordingly. Or you can just look if the player is connected or not with the returned values.
I changed some of the loops for foreach, that as far as I know, it already checks for connected players.
Also, if you're in the opposite team and there is 1 player, your info gets shown on the top 5 of the team you're not in.


Re: How to get top 5 kills of each team? - Zezombia - 10.08.2014

Updated GetTopFive:

pawn Код:
GetTopFive(team)
{
    new num[MAX_PLAYERS], numid[MAX_PLAYERS] = {-1, -1, -1, -1, -1}, sum[5];
    for(new a = 0; a < MAX_PLAYERS; a++) for(new b = 0; b < MAX_PLAYERS; b++) {
        if(num[a] <= KillCount[b]) {
            for(new c = 0; c < 5; c++) if(b == numid[c]) goto one;
            if(IsPlayerConnected(b) == 0 || GetPlayerTeam(b) != team) goto one;
            num[a] = KillCount[b]; numid[a] = b;
one:
        }
    }

    for(new i = 0; i < 5; i++) sum[i] = numid[i];
    return sum;
}

    //And for your message, something like this:
    new Top[5], string[128];
    Top = GetTopFive(Team1);
   
    for(new i = 0; i < 5; i++) {
        if(Top[i] == -1) break; //Top will be -1 if player is not connected
        format(string, sizeof(string), "Top %d | Name: %s | Kills: %d | Deaths: %d", i + 1, pName(Top[i]), KillCount[i], deaths[i]);
        SendClientMessage(playerid, -1, string);
    }
Hope this fixes the problem.