Checking if a player is online using their name.
#1

I have a command to do the following:

pawn Код:
command(members, playerid, params[])
{
    if(PVar[playerid][factionID] < 1)
    {
        SendClientMessage(playerid, 0x66666666, "You are not currently in a faction");
        return 1;
    }
    else
    {
        new Query[128], members, memberrankdisp[24];
        new resultline[200], membername[24], memberrank;
        new membersonline = 0, membersstring[128];
        new memberlist1[128];
        new dialog[1024];
        format(Query, sizeof(Query), "SELECT `username`, `factionRank` FROM `playerinfo` WHERE `factionID` = '%d'", PVar[playerid][factionID]);
        mysql_query(Query);
        mysql_store_result();
       
        memberrank = PVar[playerid][factionRank];
        for(new i=0; i < mysql_num_rows(); i++)
        {
            if(mysql_fetch_row_format(resultline))
            {
                //you can now use sscanf, explode, split or strtok to split result
                sscanf(resultline,"p<|>s[24]d", membername, memberrank);
                if(IsPlayerConnected(i))
                {
                    format(dialog,sizeof(dialog),"%s\n{004000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                }
                else
                {
                    format(dialog,sizeof(dialog),"%s\n{BF0000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                }
            }
        }
        members = mysql_num_rows();
        for(new i=0; i < members; i++)
        {
            if(IsPlayerConnected(i))
            {
                membersonline++;
            }
        }
        format(membersstring, sizeof(membersstring), "%s - Members online: {FFFFFF}(%d/%d)", FVar[PVar[playerid][factionID]][factionname], membersonline, members);
        ShowPlayerDialog(playerid, MEMBERS, DIALOG_STYLE_LIST, membersstring, dialog, "Close", "");
        return 1;
    }
}
BUT, As you can see here:

pawn Код:
if(IsPlayerConnected(i))
                {
                    format(dialog,sizeof(dialog),"%s\n{004000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                }
                else
                {
                    format(dialog,sizeof(dialog),"%s\n{BF0000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                }
It checks to see if the id of the player is online, rather than the name in the list, meaning if there are two members online, the top two will go green. Any ideas how to work around this?
Reply
#2

I'm still having issues with this, any ideas?
Reply
#3

First, you don't need 128 bytes for that query. If the maximum number of factions is 999, then the longest this query could be is
Quote:

SELECT `username`, `factionRank` FROM `playerinfo` WHERE `factionID` = '999'

Which is only 76 bytes! Plus an extra byte for null termination, it needn't be longer than 77 bytes.

Also, replace your command with this (I'd recommend downloading and including foreach by ******):
pawn Код:
command(members, playerid, params[])
{
    if(PVar[playerid][factionID] < 1)
    {
        SendClientMessage(playerid, 0x66666666, "You are not currently in a faction");
        return 1;
    }
    else
    {
        new Query[77], members, memberrankdisp[24];
        new resultline[200], membername[24], memberrank;
        new membersonline = 0, membersstring[128];
        new memberlist1[128];
        new dialog[1024];
    new plr_name[MAX_PLAYER_NAME];
       
        format(Query, sizeof(Query), "SELECT `username`, `factionRank` FROM `playerinfo` WHERE `factionID` = '%d'", PVar[playerid][factionID]);
        mysql_query(Query);
        mysql_store_result();
       
        memberrank = PVar[playerid][factionRank];
        for(new i=0; i < mysql_num_rows(); i++)
        {
            if(mysql_fetch_row_format(resultline))
            {
                //you can now use sscanf, explode, split or strtok to split result
                sscanf(resultline,"p<|>s[24]d", membername, memberrank);
                #if defined foreach
                foreach(Player : i) {
                    GetPlayerName(i, plr_name, MAX_PLAYER_NAME);
                    if(strcmp(plr_name, membername, true) == 0) {
                        format(dialog,sizeof(dialog),"%s\n{004000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                    } else {
                        format(dialog,sizeof(dialog),"%s\n{BF0000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                    }
                }
                #else
                for(new i; i < GetMaxPlayers(); i++) {
                    GetPlayerName(i, plr_name, MAX_PLAYER_NAME);
                    if(strcmp(plr_name, membername, true) == 0) {
                        format(dialog,sizeof(dialog),"%s\n{004000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                    } else {
                        format(dialog,sizeof(dialog),"%s\n{BF0000}• {FFFFFF}%s, (%s | Rank: %d)",dialog, membername, FactionRankToName(PVar[playerid][factionID], memberrank), memberrank);
                    }
                }
                #endif
            }
        }
        members = mysql_num_rows();
        for(new i=0; i < members; i++)
        {
            if(IsPlayerConnected(i))
            {
                membersonline++;
            }
        }
        format(membersstring, sizeof(membersstring), "%s - Members online: {FFFFFF}(%d/%d)", FVar[PVar[playerid][factionID]][factionname], membersonline, members);
        ShowPlayerDialog(playerid, MEMBERS, DIALOG_STYLE_LIST, membersstring, dialog, "Close", "");
        return 1;
    }
}
Reply
#4

What would I use foreach for exactly? Thank you for the help.
Reply
#5

You don't need it, it just only loops through the taken player slots and avoids vacant ones for increased speed.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)