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,只需要你实际使用的ip。

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

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