PDA

View Full Version : Rank help


Face9000
22/03/2017, 12:58 AM
I made a rank system, everything works, im just having some problems with saving and loading.

This is the rank variable:

new const gRank[][e_RANK] =
{
{"Private", 0, MAX_HEALTH, 0.0},
{"Private First Class", 50, MAX_HEALTH, 0.0},
{"Specialist", 100, 75.0, 0.0},
{"Corporal", 500, MAX_HEALTH, 5.0},
{"Sergeant", 1000, MAX_HEALTH, 5.0},
{"Staff Sergeant", 1500, MAX_HEALTH, 15.0},
{"Master Sergeant", 2500, MAX_HEALTH, 15.0},
{"Sergeant Major", 4500, MAX_HEALTH, 40.0},
{"Officer", 6000, MAX_HEALTH, 50.0},
{"Warrant Officer", 7500, MAX_HEALTH, 60.0},
{"Chief Warrant Officer",9000, MAX_HEALTH, 70.0},
{"Lieutenant", 10000, MAX_HEALTH, 70.0},
{"First Lieutenant", 12500, MAX_HEALTH, 85.0},
{"Second Lieutenant", 15000, MAX_HEALTH, MAX_ARMOUR},
{"Captain", 20000, MAX_HEALTH, MAX_ARMOUR},
{"Major", 25000, MAX_HEALTH, MAX_ARMOUR},
{"Colonel", 30000, MAX_HEALTH, MAX_ARMOUR},
{"Marshall", 33000, MAX_HEALTH, MAX_ARMOUR},
{"Field Marshall", 37000, MAX_HEALTH, MAX_ARMOUR},
{"General", 40000, MAX_HEALTH, MAX_ARMOUR},
{"Brigadier General", 45000, MAX_HEALTH, MAX_ARMOUR},
{"Major General", 50000, MAX_HEALTH, MAX_ARMOUR},
{"Master Of War", 60000, MAX_HEALTH, MAX_ARMOUR},
{"God Of War", 9999999,MAX_HEALTH, MAX_ARMOUR}
};

This is what i do to load the rank after the login:

cache_get_value_int(0, "Rank", Player[playerid][Rank]);

And this is what i do after the player quits and i save the data:

UpdatePlayerData(playerid)
{
if (Player[playerid][IsLoggedIn] == false) return 0;
new query[180];
mysql_format(g_SQL, query, sizeof query, "UPDATE `players` SET `Kills` = %d,`Deaths` = %d,`Score` = %i,`Money` = %i, `Level` = %d,`Cookies` = %d, `Warns`, `Vip` = %d, `Banned` = %d, `Rank` = %s WHERE `id` = %d LIMIT 1",
Player[playerid][Kills],
Player[playerid][Deaths],
GetPlayerScore(playerid),
GetPlayerMoney(playerid),
Player[playerid][Level],
Player[playerid][Cookies],
Player[playerid][Warns],
Player[playerid][Vip],
Player[playerid][Banned],
gRank[rankName],
Player[playerid][ID]);
mysql_tquery(g_SQL, query);
return 1;
}

Everything ^ from code above works, except from saving and loading ranks.

I want to save the rankname variable in the Player[playerid][Rank].

Yes, i already made the row "Ranks" in mysql as varchar.

X337
22/03/2017, 01:14 AM
mysql_format(g_SQL, query, sizeof query, "UPDATE `players` SET `Kills` = %d,`Deaths` = %d,`Score` = %i,`Money` = %i, `Level` = %d,`Cookies` = %d, `Warns` = %d, `Vip` = %d, `Banned` = %d, `Rank` = '%s' WHERE `id` = %d LIMIT 1",
Player[playerid][Kills],
Player[playerid][Deaths],
GetPlayerScore(playerid),
GetPlayerMoney(playerid),
Player[playerid][Level],
Player[playerid][Cookies],
Player[playerid][Warns],
Player[playerid][Vip],
Player[playerid][Banned],
gRank[rankName],
Player[playerid][ID]);


There's a mistake and you have to enclose string in your query.

Face9000
22/03/2017, 01:24 AM
I don't get your answer. What's wrong?

X337
22/03/2017, 01:31 AM
I marked them with red colors.
The first mistake is in:

`Cookies` = %d, `Warns`, `Vip` = %d

which should be:

`Cookies` = %d, `Warns` = %d, `Vip` = %d



And the second one is:

`Rank` = %s

which should be

`Rank` = '%s'

Face9000
22/03/2017, 01:42 AM
Ok seems fine, one problem. In the "rank" row in mysql, it shows always "Private" (the 1st rank), doesn't matter even i'm Specialist or another rank.

How to fix this?

X337
22/03/2017, 01:52 AM
You have to provide array index of gRank, in this line:

Player[playerid][Banned],
gRank[rankName],
Player[playerid][ID]);


And also, i don't get it why do you save player's rank as varchar and load them as an integer?

Face9000
22/03/2017, 09:00 PM
What array index should i provide?

Flamehaze7
23/03/2017, 01:47 AM
What array index should i provide?

I don't wanna get wrong but that would be something like this:

new PlayerDataRank[MAX_PLAYERS][gRank];

PlayerDataRank[playerid][rankName];

X337
23/03/2017, 03:07 AM
Put this somewhere in your script:

GetRankIndex(str[])
{
static size = sizeof(gRank);
for(new i; i < size; i++)
{
if(!strcmp(str, gRank[i][rankName]))
return i;
}
return 0;
}


And use this to load player's rank:

new str[56];
cache_get_value(0, "Rank", str, 56); // Rank column is varchar, so load them as string
Player[playerid][Rank] = GetRankIndex(str); // Player[playerid][Rank] = index of gRank array.

Player[playerid][Rank] is now index of gRank, so if you want to increase player's rank you can simply increase its value. And you can use this variable as an array index of gRank in mysql_format inside UpdatePlayerData function.

Player[playerid][Banned],
gRank[Player[playerid][Rank]][rankName],
Player[playerid][ID]);

Face9000
23/03/2017, 10:12 AM
To increase it's value you mean to do, when a player ranks up, Player[playerid][Rank] ++; right?

X337
23/03/2017, 10:53 AM
To increase it's value you mean to do, when a player ranks up, Player[playerid][Rank] ++; right?

Yes, but you have to add some check before increasing its value, so Player[playerid][Rank] won't be higher than the array size (gRank).

Face9000
23/03/2017, 10:54 AM
What about this:

new rank = GetPlayerRank(playerid);
if (Player[playerid][Rank] > rank)
{
new buf[150];
format(buf, sizeof(buf), "You ranked down to ~r~%s(%i)", gRank[rank][rankName], rank);
NotifyPlayer(playerid, buf, 5000);

format(buf, sizeof(buf), "You have just ranked down to \"%s (%i)\" from \"%s (%i)\".", gRank[rank][rankName], rank, gRank[Player[playerid][Rank]][rankName], Player[playerid][Rank]);
SendClientMessage(playerid, COLOR_TOMATO, buf);

SetPlayerScore(playerid, GetPlayerScore(playerid) - 5);

PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);

Player[playerid][Rank] = rank;
}
else if (Player[playerid][Rank] < rank)
{
new buf[150];
format(buf, sizeof(buf), "You ranked up to ~g~%s(%i)", gRank[rank][rankName], rank);
NotifyPlayer(playerid, buf, 5000);

format(buf, sizeof(buf), "You have just ranked up to \"%s (%i)\" from \"%s (%i)\".", gRank[rank][rankName], rank, gRank[Player[playerid][Rank]][rankName], Player[playerid][Rank]);
SendClientMessage(playerid, COLOR_TOMATO, buf);
SendClientMessage(playerid, COLOR_GREEN, "+$10000 and +10 Score as your reward.");

SetPlayerScore(playerid, GetPlayerScore(playerid) + 10);
GivePlayerMoney(playerid, 10000);

PlayerPlaySound(playerid, 1057, 0.0, 0.0, 0.0);

Player[playerid][Rank] = rank;
}

X337
23/03/2017, 11:27 AM
Depends on the value your GetPlayerRank returns. If it's only returns value between 0 - gRank's size, so that code is okay. Otherwise, it'll trigger a runtime error about array index out of bounds if returned value lower than 0 or higher than gRank's size.

Face9000
23/03/2017, 11:41 AM
Seems no problems since the rank is saved in the database row and gets loaded correctly.