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