31.07.2016, 17:17
I am certain you execute more queries than needed to retrieve all the information. Another important thing to keep in mind is that using non-threaded queries and not removing the cache afterwards will cause memory leaks.
If more than 1 player use the same dialog, "members2" will be overwritten so the best way (at least for me) would be to save the cache in memory. In the dialog's response, if the player clicked on next page, set the cache and load the rest otherwise delete it.
Rows returned is the number of members so no need to keep calling GetMaxMembers over and over again. You can select rank along with "Name" in the query so you don't have to call GetPlayerRank and definitely using sscanf is much better than GetPlayerId.
Something last, variables are used to store temporary (as long as the player is online) their data so don't worry about using them. It is still better than keep calling queries to get the player's clan.
The above altogether (also assuming that a clan have have max. of 30 members) and modify it to your needs:
If more than 1 player use the same dialog, "members2" will be overwritten so the best way (at least for me) would be to save the cache in memory. In the dialog's response, if the player clicked on next page, set the cache and load the rest otherwise delete it.
Rows returned is the number of members so no need to keep calling GetMaxMembers over and over again. You can select rank along with "Name" in the query so you don't have to call GetPlayerRank and definitely using sscanf is much better than GetPlayerId.
Something last, variables are used to store temporary (as long as the player is online) their data so don't worry about using them. It is still better than keep calling queries to get the player's clan.
The above altogether (also assuming that a clan have have max. of 30 members) and modify it to your needs:
PHP код:
// global:
new Cache: gPlayer_ClanMembers[MAX_PLAYERS];
// OnPlayerConnect:
gPlayer_ClanMembers[playerid] = Cache: 0;
// wherever you execute the query
new Query[50];
mysql_format(g_SQL, Query, sizeof Query, "SELECT Name,Rank FROM Users WHERE Clan = %d", GetPlayerClan(playerid, g_SQL)); // better a variable that stores player's clan
mysql_tquery(g_SQL, Query, "OnClanMembersLoad", "i", playerid);
// the specified callback
forward OnClanMembersLoad(playerid);
public OnClanMembersLoad(playerid)
{
new rows = cache_get_row_count();
if (!rows) return ... // error that there are no members
else if (rows > 15)
{
gPlayer_ClanMembers[playerid] = cache_save();
rows = 15;
// load the first 15 results and store the rest in memory
}
new name[25], rank[25], s[48], targetid;
members[0] = EOS; // reset
for (new i; i != rows; i++)
{
cache_get_row(i, 0, name);
cache_get_row(i, 1, rank);
sscanf(name, "r", targetid);
format(s, sizeof s, "%s\t%s\t%s\n", name, rank, targetid != INVALID_PLAYER_ID ? ("{00FF2F}Online") : ("{FF0000}Offline"));
strcat(members, s);
}
}
ShowPlayerDialog(playerid, 13, DIALOG_STYLE_TABLIST, "Clan Members", members, rows > 15 ? ("Next") : ("Close"), "Cancel");
// OnDialogResponse (dialogid 13):
if (!response)
{
cache_delete(gPlayer_ClanMembers[playerid]);
gPlayer_ClanMembers[playerid] = Cache: 0;
}
else
{
if (cache_is_valid(gPlayer_ClanMembers[playerid]))
{
cache_set_active(gPlayer_ClanMembers[playerid]);
new rows = cache_get_row_count(), name[25], rank[25], s[48], targetid;
members[0] = EOS; // reset
for (new i = 14; i != rows; i++) // 15 first (rowid 0-14) have been loaded and we want the rest
{
cache_get_row(i, 0, name);
cache_get_row(i, 1, rank);
sscanf(name, "r", targetid);
format(s, sizeof s, "%s\t%s\t%s\n", name, rank, targetid != INVALID_PLAYER_ID ? ("{00FF2F}Online") : ("{FF0000}Offline"));
strcat(members, s);
}
ShowPlayerDialog(playerid, 13, DIALOG_STYLE_TABLIST, "Clan Members", members, "Close", "Cancel");
cache_set_active(Cache: 0);
cache_delete(gPlayer_ClanMembers[playerid]);
gPlayer_ClanMembers[playerid] = Cache: 0;
}
}

