23 #include "serverlistcounttracker.h" 25 #include "serverapi/playerslist.h" 26 #include "serverapi/server.h" 30 DClass<ServerListCountTracker>
36 bool hasRegisterBeenCalled;
38 bool isPassingPluginFilter(
const ServerPtr &server)
const 40 return plugin == NULL || server->plugin() == plugin;
49 d->hasRegisterBeenCalled =
false;
57 void ServerListCountTracker::setPluginFilter(
const EnginePlugin *plugin)
59 assert(!d->hasRegisterBeenCalled &&
"don't change filter after server has already been registered");
63 void ServerListCountTracker::deregisterServer(ServerPtr server)
65 if (d->isPassingPluginFilter(server))
67 server->disconnect(
this);
68 d->count.discountServer(server);
69 if (server->isRefreshing())
71 --d->count.numRefreshing;
75 d->count.discountPlayers(server);
81 void ServerListCountTracker::registerServer(ServerPtr server)
83 d->hasRegisterBeenCalled =
true;
84 if (d->isPassingPluginFilter(server))
86 this->connect(server.data(), SIGNAL(begunRefreshing(ServerPtr)),
87 SLOT(onServerBegunRefreshing(ServerPtr)));
88 this->connect(server.data(), SIGNAL(updated(ServerPtr,
int)),
89 SLOT(onServerUpdated(ServerPtr)));
91 d->count.countServer(server);
92 if (server->isRefreshing())
94 ++d->count.numRefreshing;
98 d->count.countPlayers(server);
104 void ServerListCountTracker::onServerBegunRefreshing(ServerPtr server)
106 d->count.discountPlayers(server);
107 ++d->count.numRefreshing;
111 void ServerListCountTracker::onServerUpdated(ServerPtr server)
113 d->count.countPlayers(server);
114 --d->count.numRefreshing;
120 ServerListCount::ServerListCount()
126 numCustomServers = 0;
127 numGenericServers = 0;
133 void ServerListCount::countPlayers(
const ServerPtr &server)
137 numBots += players.numBots();
138 numHumanPlayers += players.numClientsWithoutBots();
142 void ServerListCount::discountPlayers(
const ServerPtr &server)
146 numBots -= players.numBots();
147 numHumanPlayers -= players.numClientsWithoutBots();
151 void ServerListCount::countServer(
const ServerPtr &server)
154 numGenericServers += !server->isSpecial() ? 1 : 0;
155 numLanServers += server->isLan() ? 1 : 0;
156 numCustomServers += server->isCustom() ? 1 : 0;
159 void ServerListCount::discountServer(
const ServerPtr &server)
162 numGenericServers -= !server->isSpecial() ? 1 : 0;
163 numLanServers -= server->isLan() ? 1 : 0;
164 numCustomServers -= server->isCustom() ? 1 : 0;
167 int ServerListCount::refreshedPercent()
const 169 if (numRefreshing == 0 || numServers == 0)
175 float refreshingFactor =
static_cast<float>(numRefreshing) /
176 static_cast<float>(numServers);
177 return static_cast<int>(floor(100.0 - 100.0 * refreshingFactor));
183 numBots += other.numBots;
184 numHumanPlayers += other.numHumanPlayers;
185 numPlayers += other.numPlayers;
187 numCustomServers += other.numCustomServers;
188 numGenericServers += other.numGenericServers;
189 numLanServers += other.numLanServers;
190 numServers += other.numServers;
191 numRefreshing += other.numRefreshing;
int numClients() const
Overall number of people and bots on the server.