playerslist.cpp
1 //------------------------------------------------------------------------------
2 // playerslist.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301, USA.
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "playerslist.h"
24 
25 DClass<PlayersList>
26 {
27  public:
28  QList<Player> players;
29 };
30 
31 DPointered(PlayersList)
32 
34 {
35  // Nothing to store yet. Future backward compatibility with plugins.
36 }
37 
38 PlayersList::PlayersList(const PlayersList& other)
39 {
40  d = other.d;
41 }
42 
43 PlayersList& PlayersList::operator=(const PlayersList& other)
44 {
45  d = other.d;
46  return *this;
47 }
48 
49 PlayersList::~PlayersList()
50 {
51 }
52 
53 void PlayersList::bots(PlayersList& botsList) const
54 {
55  botsList.clear();
56 
57  foreach(Player player, players())
58  {
59  if (player.isBot())
60  {
61  botsList << player;
62  }
63  }
64 }
65 
67 {
68  botsList.clear();
69 
70  foreach(Player player, players())
71  {
72  if (player.isBot() && player.teamNum() == Player::TEAM_NONE)
73  {
74  botsList << player;
75  }
76  }
77 }
78 
79 void PlayersList::clear()
80 {
81  d->players.clear();
82 }
83 
84 int PlayersList::numBots() const
85 {
86  int bots = 0;
87  foreach(Player player, players())
88  {
89  if (player.isBot())
90  {
91  ++bots;
92  }
93  }
94 
95  return bots;
96 }
97 
98 int PlayersList::numBotsOnTeam(int team) const
99 {
100  int bots = 0;
101  foreach(Player player, players())
102  {
103  if (player.isBot() && player.teamNum() == team)
104  {
105  ++bots;
106  }
107  }
108 
109  return bots;
110 }
111 
112 int PlayersList::numBotsWithoutTeam() const
113 {
114  int bots = 0;
115  foreach(Player player, players())
116  {
117  if (player.isBot() && player.teamNum() == Player::TEAM_NONE)
118  {
119  ++bots;
120  }
121  }
122 
123  return bots;
124 }
125 
127 {
128  return size();
129 }
130 
131 int PlayersList::numClientsWithoutBots() const
132 {
133  return size() - numBots();
134 }
135 
136 int PlayersList::numHumansInGame() const
137 {
138  int humansInGame = 0;
139 
140  foreach(Player player, players())
141  {
142  if (!player.isBot() && !player.isSpectating())
143  {
144  ++humansInGame;
145  }
146  }
147 
148  return humansInGame;
149 }
150 
151 int PlayersList::numHumansOnTeam(int team) const
152 {
153  int humans = 0;
154  foreach(Player player, players())
155  {
156  if (!player.isBot()
157  && !player.isSpectating()
158  && player.teamNum() == team)
159  {
160  ++humans;
161  }
162  }
163 
164  return humans;
165 }
166 
167 int PlayersList::numHumansWithoutTeam() const
168 {
169  int humans = 0;
170  foreach(Player player, players())
171  {
172  if (!player.isBot()
173  && !player.isSpectating()
174  && player.teamNum() == Player::TEAM_NONE)
175  {
176  ++humans;
177  }
178  }
179 
180  return humans;
181 }
182 
183 int PlayersList::numPlayersOnTeam(int team) const
184 {
185  int teamSize = 0;
186  foreach(Player player, players())
187  {
188  if (player.teamNum() == team)
189  {
190  ++teamSize;
191  }
192  }
193 
194  return teamSize;
195 }
196 
197 int PlayersList::numSpectators() const
198 {
199  int spectators = 0;
200 
201  foreach(Player player, players())
202  {
203  if (player.isSpectating())
204  {
205  ++spectators;
206  }
207  }
208 
209  return spectators;
210 }
211 
212 PlayersList &PlayersList::operator<<(const Player &player)
213 {
214  d->players << player;
215  return *this;
216 }
217 
218 Player &PlayersList::operator[](int index)
219 {
220  return d->players[index];
221 }
222 
223 const Player &PlayersList::operator[](int index) const
224 {
225  return d->players[index];
226 }
227 
228 const QList<Player> &PlayersList::players() const
229 {
230  return d->players;
231 }
232 
233 void PlayersList::inGamePlayersByTeams(PlayersByTeams& playersListMap) const
234 {
235  playersListMap.clear();
236 
237  foreach(const Player &player, players())
238  {
239  if (!player.isSpectating() && !player.isTeamlessBot())
240  {
241  int teamIndex = player.teamNum();
242  if (playersListMap.contains(teamIndex))
243  {
244  playersListMap[teamIndex] << player;
245  }
246  else
247  {
248  PlayersList newList;
249  newList << player;
250  playersListMap.insert(teamIndex, newList);
251  }
252  }
253  }
254 }
255 
256 int PlayersList::size() const
257 {
258  return d->players.size();
259 }
260 
261 void PlayersList::spectators(PlayersList& spectatorsList) const
262 {
263  spectatorsList.clear();
264 
265  foreach(Player player, players())
266  {
267  if (player.isSpectating())
268  {
269  spectatorsList << player;
270  }
271  }
272 }
void inGamePlayersByTeams(PlayersByTeams &playersListMap) const
Divides players and bots to lists ordered by teams.
void bots(PlayersList &botsList) const
Lists all bots regardless if they are on a team or not.
Definition: playerslist.cpp:53
Data structure that holds information about players in a server.
Definition: player.h:37
void botsWithoutTeam(PlayersList &botsList) const
Lists only those bots that are not on a team.
Definition: playerslist.cpp:66
int numClients() const
Overall number of people and bots on the server.