0

このコードは私がやりたいことを説明しています:

unsigned long g_PlayerIP[MAX_AMOUNT_OF_PLAYERS];//save the IP address of each player
int g_max_ip = MAX_CONNECTED_FROM_ONE_IP;

PLUGIN_EXPORT bool PLUGIN_CALL OnPlayerConnect(int playerid)
{
    PlayerLoopList.push_back(playerid);

    char szIP[32];
    GetPlayerIp(playerid,szIP);//get the ip of the player into szIP
    unsigned short explodeIP[4];//declare 4 eight bit variables, to store the exploded ip
    sscanf(szIP, " %d[^.].%d[^.].%d[^.].%d", &explodeIP[0], &explodeIP[1], &explodeIP[2], &explodeIP[3]);//explode ip into 4 pats ranging from 0 to 255
    g_PlayerIP[playerid] = (explodeIP[0] + (explodeIP[1] << 8) + (explodeIP[2] << 16) + (explodeIP[3] << 24));//store the ip as a 32bit integer
    int connected = 0;//variable for counting connected players fron one ip
    for(list <int>::iterator i = PlayerLoopList.begin(); i != PlayerLoopList.end(); ++i)
    {
        if(g_PlayerIP[playerid] == g_PlayerIP[*i])
        {
            ++connected;
        }
    }
    if(connected >= g_max_ip)
    {
        Report(playerid,IP_FLOOD,g_PlayerIP[playerid]);//too many connected from one ip, report it.
    }
    return true;
}

そしてこの部分を見て:

    sscanf(szIP, " %d[^.].%d[^.].%d[^.].%d", &explodeIP[0], &explodeIP[1], &explodeIP[2], &explodeIP[3]);//explode ip into 4 pats ranging from 0 to 255
    g_PlayerIP[playerid] = (explodeIP[0] + (explodeIP[1] << 8) + (explodeIP[2] << 16) + (explodeIP[3] << 24));//store the ip as a 32bit integer
    int connected = 0;//variable for counting connected players fron one ip
    for(list <int>::iterator i = PlayerLoopList.begin(); i != PlayerLoopList.end(); ++i)
    {
        if(g_PlayerIP[playerid] == g_PlayerIP[*i])
        {
            ++connected;
        }
    }

私を少しよくします、それがより速くできるかどうかについて私に質問します、c ++で素晴らしい方法がなければなりませんか?

4

1 に答える 1

5

std::mapを使用するだけです。そうすれば、可能なすべてのIPに1つ必要ではなく、実際に使用しているIPのみが必要になります。

std::map<int, unsigned int> counts;

counts[playerid]++;
if (counts[playid] >= g_max_ip){ //Report
}
于 2012-07-13T01:17:37.957 に答える