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  const int canJoin = qMax(0, d->server->numFreeJoinSlots());
65  return "<tr><td>" + PLAYERS + ":&nbsp;</td><td>"
66  + tr("%1 / %2 (%3 can join)").arg(players.numClients()).arg(d->server->maxClients()).arg(canJoin)
67  + "</td></tr>";
68 }
69 
70 QString GameInfoTip::limitHTML(QString limitName, QString valueArgsTemplate, int value)
71 {
72  QString row = "<tr><td>" + limitName + ":&nbsp;</td><td>" + valueArgsTemplate + "</td></tr>";
73 
74  if (value == 0)
75  {
76  row = row.arg(UNLIMITED);
77  }
78  else
79  {
80  row = row.arg(value);
81  }
82 
83  return row;
84 }
85 
86 QString GameInfoTip::scorelimitHTML()
87 {
88  const QString SCORELIMIT = tr("Scorelimit");
89  QString row = limitHTML(SCORELIMIT, "%1", d->server->scoreLimit());
90 
91  return row;
92 }
93 
94 QString GameInfoTip::teamScoresHTML()
95 {
96  QString teamScores;
97  if (d->server->gameMode().isTeamGame())
98  {
99  teamScores = "<tr><td colspan=\"2\">%1</td></tr>";
100  QString teams;
101  bool bPrependBar = false;
102  for (int i = 0; i < MAX_TEAMS; ++i)
103  {
104  if (d->server->players().numPlayersOnTeam(i) != 0)
105  {
106  if (bPrependBar)
107  {
108  teams += " | ";
109  }
110  teams += d->server->teamName(i) + ": " + QString::number(d->server->score(i));
111  bPrependBar = true;
112  }
113  }
114  teamScores = teamScores.arg(teams);
115  }
116 
117  return teamScores;
118 }
119 
120 QString GameInfoTip::timelimitHTML()
121 {
122  const QString TIMELIMIT = tr("Timelimit");
123  int timeLimit = d->server->timeLimit();
124  QString row = limitHTML(TIMELIMIT, "%1 %2", timeLimit);
125 
126  QString timeLeft = "";
127  if (timeLimit != 0)
128  {
129  timeLeft = tr("(%1 left)").arg(d->server->timeLeft());
130  }
131  row = row.arg(timeLeft);
132 
133  return row;
134 }