SA-MP Forums Archive
the richest guys in the server? - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: the richest guys in the server? (/showthread.php?tid=169844)



the richest guys in the server? - Rocky Balboa - 21.08.2010

argg .. ok lets start .. i was wondering if u could get the richest guys in the server and return a sepcific function on them .. now i dont know how to do it .. can anyone tell me how ?
well i ust did this ... lol i dont know how to make it that those guys are the richest .. anyway
Код:
for(new i = 0; i < MAX_PLAYERS; i++)
		if(IsPlayerConnected(i))
		{
			if(GetPlayerMoney(i) > ?????????????????)
		}



Re: the richest guys in the server? - Rocky Balboa - 21.08.2010

.. what about 5 richest guys in the server ?


Re: the richest guys in the server? - Rocky Balboa - 21.08.2010

what ever the value i cash is


Re: the richest guys in the server? - Voldemort - 22.08.2010

Here is something better.

pawn Код:
forward GetRichestPlayer();
public GetRichestPlayer()
{
    new Maxed = 0;
    new Player = 0;
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            if(GetPlayerMoney(i) > Maxed) { Maxed = GetPlayerMoney(i); }
        }
    }
    for(new i = 0; i < MAX_PLAYERS; i++)
    {
        if(IsPlayerConnected(i))
        {
            if(GetPlayerMoney(i) == Maxed) { Player = i; }
        }
    }
    return Player;
}
also this can help if want get any other max value from all players


Re: the richest guys in the server? - nemesis- - 22.08.2010

pawn Код:
stock GetRichestPlayer()
{
    new maxc,maxpid,tcs;

    for(new a=0; a<MAX_PLAYERS; a++)
    {
        if(!IsPlayerConnected(a)) continue;
        tcs = GetPlayerMoney(a);
        if(tsc > maxc)
        {
            maxpid = a;
            maxc = tsc;
        }
    }
    return maxpid;
}
1 loop > 2 loops.


Re: the richest guys in the server? - Finn - 22.08.2010

Quote:
Originally Posted by nemesis-
Посмотреть сообщение
pawn Код:
stock GetRichestPlayer()
{
    new maxc,maxpid,tcs;

    for(new a=0; a<MAX_PLAYERS; a++)
    {
        if(!IsPlayerConnected(a)) continue;
        tcs = GetPlayerMoney(a);
        if(tsc > maxc)
        {
            maxpid = a;
            maxc = tsc;
        }
    }
    return maxpid;
}
1 loop > 2 loops.
Why so many variables?

pawn Код:
stock GetRichestPlayer()
{
    new richestplayer = INVALID_PLAYER_ID;

    for(new playerid; playerid < MAX_PLAYERS; playerid++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(richestplayer != INVALID_PLAYER_ID)
            {
                if(GetPlayerMoney(richestplayer) < GetPlayerMoney(playerid))
                {
                    richestplayer = playerid;
                }
            }
            else
            {
                richestplayer = playerid;
            }
        }
    }

    return richestplayer;
}



Re: the richest guys in the server? - Sergei - 22.08.2010

@Finn,
Quote:

if(richestplayer != INVALID_PLAYER_ID)

?

Код:
new richest = INVALID_PLAYER_ID, data[2];
foreach(Player,i)
{
     data[0] = GetPlayerMoney(i);
     if(data[0] && data[0] > data[1]) richest = i;
}
This would do the job.


Re: the richest guys in the server? - Finn - 22.08.2010

Quote:
Originally Posted by Sergei
Посмотреть сообщение
@Finn
It checks if the variable richestplayer is set to INVALID_PLAYER_ID or not.

My code can be done via foreach aswell, didn't know the OP is using it;

pawn Код:
stock GetRichestPlayer()
{
    new richestplayer = INVALID_PLAYER_ID;

    foreach(Player, playerid)
    {
        if(richestplayer != INVALID_PLAYER_ID)
        {
            if(GetPlayerMoney(richestplayer) < GetPlayerMoney(playerid))
            {
                richestplayer = playerid;
            }
        }
        else
        {
            richestplayer = playerid;
        }
    }

    return richestplayer;
}

//Or as you guys seem to like shorter code:
stock GetRichestPlayer()
{
    new r = INVALID_PLAYER_ID;
    foreach(Player, i)
    {
        if(r == INVALID_PLAYER_ID || GetPlayerMoney(r) < GetPlayerMoney(i))
        {
            r = i;
        }
    }
    return r;
}



Re: the richest guys in the server? - nemesis- - 22.08.2010

Quote:
Originally Posted by Sergei
Посмотреть сообщение
@Finn,

?

Код:
new richest = INVALID_PLAYER_ID, data[2];
foreach(Player,i)
{
     data[0] = GetPlayerMoney(i);
     if(data[0] && data[0] > data[1]) richest = i;
}
This would do the job.
Yours doesn't even work. You aren't assigning data[1]. Your use of the array for data[] is also slower than individual variables.

The OP can't even code this function and you expect him to add in foreach?

Quote:
Originally Posted by Finn
Посмотреть сообщение
Why so many variables?

pawn Код:
stock GetRichestPlayer()
{
    new richestplayer = INVALID_PLAYER_ID;

    for(new playerid; playerid < MAX_PLAYERS; playerid++)
    {
        if(IsPlayerConnected(playerid))
        {
            if(richestplayer != INVALID_PLAYER_ID)
            {
                if(GetPlayerMoney(richestplayer) < GetPlayerMoney(playerid))
                {
                    richestplayer = playerid;
                }
            }
            else
            {
                richestplayer = playerid;
            }
        }
    }

    return richestplayer;
}
3 embedded ifs and 2 calls to GetPlayerMoney? Mine is still far more efficient.


Re: the richest guys in the server? - Sergei - 23.08.2010

Quote:
Originally Posted by nemesis-
Посмотреть сообщение
Yours doesn't even work. You aren't assigning data[1]. Your use of the array for data[] is also slower than individual variables.

The OP can't even code this function and you expect him to add in foreach?
Foreach is easier to use, so yeah I'm sure he will understand it earlier than for(). And there won't be any different if you use two variables or double cell array in that small loop.