player.cpp
1 //------------------------------------------------------------------------------
2 // player.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 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "player.h"
24 #include "strings.hpp"
25 
26 DClass<Player>
27 {
28 public:
29  QString name;
30  long score;
31  unsigned short ping;
32  bool spectator;
33  bool bot;
34  Player::PlayerTeam team;
35 };
36 
37 DPointered(Player)
38 
39 Player::Player()
40 {
41  d->score = 0;
42  d->ping = 0;
43  d->spectator = false;
44  d->bot = false;
45  d->team = TEAM_NONE;
46 }
47 
48 Player::Player(const QString &name, long score, unsigned long ping,
49  PlayerTeam team, bool spectator, bool bot)
50 {
51  d->name = name;
52  d->score = score;
53  d->ping = ping;
54  d->spectator = spectator;
55  d->bot = bot;
56  d->team = team;
57 }
58 
59 Player::Player(const Player &other)
60 {
61  d = other.d;
62 }
63 
64 Player &Player::operator=(const Player &other)
65 {
66  d = other.d;
67  return *this;
68 }
69 
70 Player::~Player()
71 {
72 }
73 
74 bool Player::isBot() const
75 {
76  return d->bot;
77 }
78 
79 bool Player::isSpectating() const
80 {
81  return d->spectator;
82 }
83 
84 bool Player::isTeamlessBot() const
85 {
86  return d->bot && d->team == TEAM_NONE;
87 }
88 
89 const QString &Player::name() const
90 {
91  return d->name;
92 }
93 
95 {
96  QString ret;
97  for (int i = 0; i < d->name.length(); ++i)
98  {
99  if (d->name[i] < 32 || d->name[i] > 126)
100  {
101  // Strip on \c.
102  // Removing the control characters is still a good idea though.
103  if (d->name[i] == ESCAPE_COLOR_CHAR)
104  {
105  // Find out what exactly needs to be removed. Either
106  // it will be just one character, a bracket-enclosed
107  // range or nothing if sequence is invalid.
108  int colorCodeIdx = i + 1;
109  bool range = false;
110  for (; colorCodeIdx < d->name.length(); ++colorCodeIdx)
111  {
112  QChar symbol = d->name[colorCodeIdx];
113  if (symbol == '[')
114  range = true;
115  else if ((range && symbol == ']') || !range)
116  break;
117  }
118  if (range && colorCodeIdx >= d->name.length())
119  ++i; // We didn't find range end.
120  else
121  i = colorCodeIdx;
122  }
123  continue;
124  }
125 
126  ret += d->name[i];
127  }
128  return ret;
129 }
130 
131 QString Player::nameFormatted() const
132 {
133  QString ret;
134  for (const auto &c : d->name)
135  {
136  // cut out bad characters
137  if ((c < 32 || c > 126) && c != ESCAPE_COLOR_CHAR)
138  continue;
139 
140  switch (c.toLatin1())
141  {
142  case '<':
143  ret += "&lt;";
144  break;
145 
146  case '>':
147  ret += "&gt;";
148  break;
149 
150  default:
151  ret += c;
152  break;
153  }
154  }
155 
156  return Strings::colorizeString(ret);
157 }
158 
159 unsigned long Player::ping() const
160 {
161  return d->ping;
162 }
163 
164 long Player::score() const
165 {
166  return d->score;
167 }
168 
169 Player::PlayerTeam Player::teamNum() const
170 {
171  return d->team;
172 }
173 
174 bool Player::operator==(const Player &other) const
175 {
176  return name().compare(other.name(), Qt::CaseInsensitive) == 0;
177 }
178 
180 
181 uint qHash(const Player &player)
182 {
183  return qHash(player.name());
184 }
QString nameColorTagsStripped() const
Definition: player.cpp:94
Data structure that holds information about players in a server.
Definition: player.h:37
static QString colorizeString(const QString &str, int def=4)
Definition: strings.cpp:50
QString nameFormatted() const
Definition: player.cpp:131