gameinfotip.cpp
1 //------------------------------------------------------------------------------
2 // gameinfotip.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library 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 GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; 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 "gameinfotip.h"
24 #include "serverapi/playerslist.h"
25 #include "serverapi/server.h"
27 
28 DClass<GameInfoTip>
29 {
30 public:
31  ServerCPtr server;
32 };
33 
34 DPointered(GameInfoTip)
35 
36 const QString GameInfoTip::UNLIMITED = QObject::tr("Unlimited");
37 
38 GameInfoTip::GameInfoTip(const ServerCPtr &server)
39 {
40  d->server = server;
41 }
42 
43 GameInfoTip::~GameInfoTip()
44 {
45 }
46 
47 QString GameInfoTip::generateHTML()
48 {
49  QString table = "<table>";
50  table += timelimitHTML();
51  table += scorelimitHTML();
52  table += teamScoresHTML();
53  table += playersHTML();
54  table += "</table>";
55 
56  return table;
57 }
58 
59 QString GameInfoTip::playersHTML()
60 {
61  const QString PLAYERS = tr("Players");
62 
63  const PlayersList &players = d->server->players();
64  int canJoin = d->server->maxPlayers() - players.numClients();
65  if (canJoin < 0)
66  {
67  canJoin = 0;
68  }
69 
70  QString html = "<tr><td>" + PLAYERS + ":&nbsp;</td><td>%1 / %2 (%3 can join)</td></tr>";
71  html = html.arg(players.numClients()).arg(d->server->numTotalSlots()).arg(canJoin);
72 
73  return html;
74 }
75 
76 QString GameInfoTip::limitHTML(QString limitName, QString valueArgsTemplate, int value)
77 {
78  QString row = "<tr><td>" + limitName + ":&nbsp;</td><td>" + valueArgsTemplate + "</td></tr>";
79 
80  if (value == 0)
81  {
82  row = row.arg(UNLIMITED);
83  }
84  else
85  {
86  row = row.arg(value);
87  }
88 
89  return row;
90 }
91 
92 QString GameInfoTip::scorelimitHTML()
93 {
94  const QString SCORELIMIT = tr("Scorelimit");
95  QString row = limitHTML(SCORELIMIT, "%1", d->server->scoreLimit());
96 
97  return row;
98 }
99 
100 QString GameInfoTip::teamScoresHTML()
101 {
102  QString teamScores;
103  if (d->server->gameMode().isTeamGame())
104  {
105  teamScores = "<tr><td colspan=\"2\">%1</td></tr>";
106  QString teams;
107  bool bPrependBar = false;
108  for (int i = 0; i < MAX_TEAMS; ++i)
109  {
110  if (d->server->players().numPlayersOnTeam(i) != 0)
111  {
112  if (bPrependBar)
113  {
114  teams += " | ";
115  }
116  teams += d->server->teamName(i) + ": " + QString::number(d->server->score(i));
117  bPrependBar = true;
118  }
119  }
120  teamScores = teamScores.arg(teams);
121  }
122 
123  return teamScores;
124 }
125 
126 QString GameInfoTip::timelimitHTML()
127 {
128  const QString TIMELIMIT = tr("Timelimit");
129  int timeLimit = d->server->timeLimit();
130  QString row = limitHTML(TIMELIMIT, "%1 %2", timeLimit);
131 
132  QString timeLeft = "";
133  if (timeLimit != 0)
134  {
135  timeLeft = tr("(%1 left)").arg(d->server->timeLeft());
136  }
137  row = row.arg(timeLeft);
138 
139  return row;
140 }
int numClients() const
Overall number of people and bots on the server.