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