kung foo man (13th July 2013)
You're right, not the best solution to set different types for a variable, but this is the simplest way. Actually the first condition (! isDefined(first["player"])) is undefined, so the script will not check the second condition, and the first["exp"] will get a numeric value. In the practice it don't be a problem. In theory![]()
Looking at the code a bit more, I can see that all it does is assign the very first player to the value first["player"] without any conditions at all. So, pretty pointless really. What he needs to do is set a condition for who the first player is (possibly by setting a numerical value for first["exp"]) and attenuate the players on that basis.
What I would do is compare every player's XP, and attenuate the players on that basis. Pretty much like 1st, 2nd, and 3rd are attenuated placements based on scores in other well known end-of-game scripts.
Well, it would the best sort the players by exp, and only get the first 3 elements of the array.
http://en.wikipedia.org/wiki/Insertion_sort
This script is only get the best player, if you wanna get the 2nd and 3rd players, then you have to rewrite the all function. See above.
Last edited by randall; 13th July 2013 at 10:12.
PHP Code:
secondplayer()
{
level.brakdanych = "^1BRAK DANYCH";
second = [];
second["player"] = undefined;
second["name"] = level.brakdanych;
second["exp"] = level.brakdanych;
second["rank"] = level.brakdanych;
players = getEntarray("player", "classname");
for (i = 0; i < players.size; i++)
{
player = players[ i ];
if (! isDefined(player.pers["team"]) || player.pers["team"] == "spectator")
continue;
if (! isDefined(second["player"]) || player.sexp > second["exp"])
{
second["player"] = player;
second["name"] = player.name;
second["exp"] = player.sexp;
second["rank"] = player.poziom;
}
}
for (i = 0; i < players.size; i++)
{
player = players[ i ];
player setClientCvar("second_player",
"second Player: Name:" + second["name"] +
" His Exp IS:" + second["exp"] +
" His rank IS:" + second["rank"]);
}
}
how get best second player ?PHP Code:
if (! isDefined(second["player"]) || player.sexp > second["exp"])
{
second["player"] = player;
second["name"] = player.name;
second["exp"] = player.sexp;
second["rank"] = player.poziom;
}
If you rename a variable the result still not going to change xd Forget this script, i wrote it before you said you need 2nd and 3rd players too. I copied a link above about the insertion sort, use that to sort the players by exp. After you're ready, you have to only print (or put in cvars) the first 3 elements of the sorted array.
EDIT: I thought you will never write the script, so:
PHP Code:
checkStatus()
{
level.brakdanych = "^1BRAK DANYCH";
places = [];
places[0] = []; // 1st player
places[1] = []; // 2nd player
places[2] = []; // 3rd player
players = getEntArray("player", "classname");
// take invalid players from the array
array = [];
for (i = 0; i < players.size; i++)
{
player = players[ i ];
if (! isDefined(player.pers["team"]) || player.pers["team"] == "spectator")
continue;
array[ array.size ] = players[ i ];
}
// sort the 'players' array by exp
array = array_sort_by_exp(array);
// set places
for (i = 0; i < places.size; i++)
{
if (isDefined(array[ i ])) {
places[ i ]["player"] = array[ i ];
places[ i ]["name"] = array[ i ].name;
places[ i ]["exp"] = array[ i ].sexp;
places[ i ]["rank"] = array[ i ].poziom;
} else {
places[ i ]["player"] = undefined;
places[ i ]["name"] = level.brakdanych;
places[ i ]["exp"] = level.brakdanych;
places[ i ]["rank"] = level.brakdanych;
}
}
// set cvars for all players
for (i = 0; i < players.size; i++)
{
player = players[ i ];
player setClientCvar("first_player",
"first Player: Name:" + places[0]["name"] +
" His Exp IS:" + places[0]["exp"] +
" His rank IS:" + places[0]["rank"]);
player setClientCvar("second_player",
"second Player: Name:" + places[1]["name"] +
" His Exp IS:" + places[1]["exp"] +
" His rank IS:" + places[1]["rank"]);
player setClientCvar("third_player",
"third Player: Name:" + places[2]["name"] +
" His Exp IS:" + places[2]["exp"] +
" His rank IS:" + places[2]["rank"]);
}
}
array_sort_by_exp(array)
{
temp = array;
for (i = 0; i < temp.size - 1; i++)
{
for (j = i + 1; j < temp.size; j++)
{
if (temp[ i ].sexp > temp[ j ].sexp)
{
var = temp[ i ];
temp[ i ] = temp[ j ];
temp[ j ] = var;
}
}
}
return temp;
}
Last edited by randall; 13th July 2013 at 14:37.
NemanjA (14th July 2013)
Untested codePHP Code:
getbestplayers(amount)
{
players = getentarray("player", "classname");
if(amount > players.size)
amount = players.size;
result = [];
for(i = 0; i < amount; i++)
{
best = i;
for(j = i + 1; j < players.size; j++)
{
if(players[j].score > players[best].score)
best = j;
}
tmp = players[best];
players[best] = players[i];
players[i] = tmp;
result[result.size] = tmp;
}
return result;
}
kung foo man (13th July 2013),NemanjA (14th July 2013)
randal your script almost work ,but script show top3 worst player not the best.
can u fix it?