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  static const QString css = "<style>"
50  ".game-info-table th {"
51  " text-align: left;"
52  "}\n"
53  "</style>";
54 
55  QString table = R"(<table class="game-info-table">)";
56  table += timelimitHTML();
57  table += scorelimitHTML();
58  table += teamScoresHTML();
59  table += playersHTML();
60  table += "</table>";
61 
62  return css + table;
63 }
64 
65 QString GameInfoTip::playersHTML()
66 {
67  const QString PLAYERS = tr("Players");
68 
69  const PlayersList &players = d->server->players();
70  const int canJoin = qMax(0, d->server->numFreeJoinSlots());
71  return QString("<tr><th>%1:&nbsp;</th><td>").arg(PLAYERS)
72  + tr("%1 / %2 (%3 can join)").arg(players.numClients()).arg(d->server->maxClients()).arg(canJoin)
73  + "</td></tr>";
74 }
75 
76 QString GameInfoTip::limitHTML(QString limitName, QString valueArgsTemplate, int value)
77 {
78  QString row = QString("<tr><th>%1:&nbsp;</th><td>%2</td></tr>")
79  .arg(limitName.toHtmlEscaped())
80  .arg(valueArgsTemplate);
81 
82  if (value == 0)
83  {
84  row = row.arg(UNLIMITED);
85  }
86  else
87  {
88  row = row.arg(value);
89  }
90 
91  return row;
92 }
93 
94 QString GameInfoTip::scorelimitHTML()
95 {
96  const QString SCORELIMIT = tr("Scorelimit");
97  QString row = limitHTML(SCORELIMIT, "%1", d->server->scoreLimit());
98 
99  return row;
100 }
101 
102 QString GameInfoTip::teamScoresHTML()
103 {
104  QString teamScores;
105  if (d->server->gameMode().isTeamGame())
106  {
107  teamScores = "<tr><td colspan=\"2\">%1</td></tr>";
108  QString teams;
109  bool bPrependBar = false;
110  for (int i = 0; i < MAX_TEAMS; ++i)
111  {
112  if (d->server->players().numPlayersOnTeam(i) != 0)
113  {
114  if (bPrependBar)
115  {
116  teams += " | ";
117  }
118  teams += d->server->teamName(i).toHtmlEscaped()
119  + ": " + QString::number(d->server->score(i));
120  bPrependBar = true;
121  }
122  }
123  teamScores = teamScores.arg(teams);
124  }
125 
126  return teamScores;
127 }
128 
129 QString GameInfoTip::timelimitHTML()
130 {
131  const QString TIMELIMIT = tr("Timelimit");
132  int timeLimit = d->server->timeLimit();
133  QString row = limitHTML(TIMELIMIT, "%1 %2", timeLimit);
134 
135  QString timeLeft = "";
136  if (timeLimit != 0)
137  {
138  timeLeft = tr("(%1 left)").arg(d->server->timeLeft());
139  }
140  row = row.arg(timeLeft);
141 
142  return row;
143 }