playertable.cpp
1 //------------------------------------------------------------------------------
2 // playertable.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 "playertable.h"
24 #include "serverapi/server.h"
26 #include <climits>
27 
28 DClass<PlayerTable>
29 {
30  public:
31  int numOfColumns;
32  ServerCPtr server;
33 };
34 
35 DPointered(PlayerTable)
36 
37 PlayerTable::PlayerTable(const ServerCPtr &server)
38 {
39  d->numOfColumns = 0;
40  d->server = server;
41  setNumberOfColumns();
42 }
43 
44 PlayerTable::~PlayerTable()
45 {
46 }
47 
48 QString PlayerTable::generateHTML()
49 {
50  QString tableStyle = "background-color: #FFFFFF; color: #000000";
51  QString table = "<table cellspacing=\"4\" style=\"" + tableStyle + "\">";
52  table += tableHeader();
53  table += tableContent();
54  table += "</table>";
55  return table;
56 }
57 
58 void PlayerTable::setNumberOfColumns()
59 {
60  if (d->server->gameMode().isTeamGame())
61  {
62  d->numOfColumns = 5;
63  }
64  else
65  {
66  d->numOfColumns = 4;
67  }
68 }
69 
70 QString PlayerTable::spawnPartOfPlayerTable(const PlayersList& list, bool bAppendEmptyRowAtBeginning)
71 {
72  QString ret;
73  if (list.count() != 0)
74  {
75  if (bAppendEmptyRowAtBeginning)
76  {
77  ret = QString("<tr><td colspan=\"%1\">&nbsp;</td></tr>").arg(d->numOfColumns);
78  }
79 
80  for (int i = 0; i < list.count(); ++i)
81  {
82  const Player& player = list[i];
83 
84  QString status = "";
85  if (player.isBot())
86  {
87  status = tr("BOT");
88  }
89  else if (player.isSpectating())
90  {
91  status = tr("SPECTATOR");
92  }
93 
94  QString strPlayer = "<tr>";
95  if (d->server->gameMode().isTeamGame())
96  {
97  strPlayer += QString("<td>%1</td>").arg(d->server->teamName(player.teamNum()));
98  }
99  strPlayer += "<td>%1</td><td align=\"right\">%2</td><td align=\"right\">%3</td><td>%4</td></tr>";
100  QString ping;
101  if (player.ping() != USHRT_MAX)
102  {
103  ping = QString::number(player.ping());
104  }
105  strPlayer = strPlayer.arg(player.nameFormatted()).arg(player.score()).arg(ping);
106  strPlayer = strPlayer.arg(status);
107 
108  ret += strPlayer;
109  }
110  }
111 
112  return ret;
113 }
114 
115 QString PlayerTable::spawnPlayersRows(const PlayersByTeams& playersByTeams)
116 {
117  QString playersRows;
118 
119  bool bAppendEmptyRowAtBeginning = false;
120  foreach (const PlayersList &playersList, playersByTeams.values())
121  {
122  playersRows += spawnPartOfPlayerTable(playersList, bAppendEmptyRowAtBeginning);
123  if (!bAppendEmptyRowAtBeginning)
124  {
125  bAppendEmptyRowAtBeginning = true;
126  }
127  }
128 
129  return playersRows;
130 }
131 
132 QString PlayerTable::tableContent()
133 {
134  PlayersByTeams playersByTeams;
135  PlayersList bots, spectators;
136 
137  const PlayersList &players = d->server->players();
138 
139  players.inGamePlayersByTeams(playersByTeams);
140  players.botsWithoutTeam(bots);
141  players.spectators(spectators);
142 
143  bool bAppendEmptyRowAtBeginning = false;
144  QString playersRows = spawnPlayersRows(playersByTeams);
145 
146  bAppendEmptyRowAtBeginning = !playersRows.isEmpty();
147  QString botsRows = spawnPartOfPlayerTable(bots, bAppendEmptyRowAtBeginning);
148 
149  bAppendEmptyRowAtBeginning = !(botsRows.isEmpty() && playersRows.isEmpty());
150  QString spectatorsRows = spawnPartOfPlayerTable(spectators, bAppendEmptyRowAtBeginning);
151 
152  QString content = playersRows + botsRows + spectatorsRows;
153 
154  return content;
155 }
156 
157 QString PlayerTable::tableHeader()
158 {
159  const QString TEAM = tr("Team");
160  const QString PLAYER = tr("Player");
161  const QString SCORE = tr("Score");
162  const QString PING = tr("Ping");
163  const QString STATUS = tr("Status");
164 
165  QString teamHeader;
166  QString header = "<tr>";
167 
168  if (d->server->gameMode().isTeamGame())
169  {
170  teamHeader = "<td>" + TEAM + "</td>";
171  }
172 
173  header += teamHeader;
174  header += "<td>" + PLAYER + "</td>";
175  header += "<td align=\"right\">&nbsp;" + SCORE + "</td>";
176  header += "<td align=\"right\">&nbsp;" + PING + "</td>";
177  header += "<td>" + STATUS + "</td>";
178  header += "</tr>";
179  header += QString("<tr><td colspan=\"%1\"><hr width=\"100%\"></td></tr>").arg(d->numOfColumns);
180 
181  return header;
182 }
void inGamePlayersByTeams(PlayersByTeams &playersListMap) const
Divides players and bots to lists ordered by teams.
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
QString nameFormatted() const
Definition: player.cpp:113