player.cpp
1 //------------------------------------------------------------------------------
2 // player.cpp
3 //------------------------------------------------------------------------------
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program 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
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; 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 "Blzut3" <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 #include "player.h"
24 #include "strings.h"
25 
26 DClass<Player>
27 {
28  public:
29  QString name;
30  short 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, unsigned 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  // Lets only remove the following character on \c.
102  // Removing the control characters is still a good idea though.
103  if(d->name[i] == ESCAPE_COLOR_CHAR)
104  ++i;
105  continue;
106  }
107 
108  ret += d->name[i];
109  }
110  return ret;
111 }
112 
113 QString Player::nameFormatted() const
114 {
115  QString ret;
116  for (int i = 0; i < d->name.length(); ++i)
117  {
118  // cut out bad characters
119  if ((d->name[i] < 32 || d->name[i] > 126) && d->name[i] != ESCAPE_COLOR_CHAR)
120  continue;
121 
122  switch (d->name[i].toLatin1())
123  {
124  case '<':
125  ret += "&lt;";
126  break;
127 
128  case '>':
129  ret += "&gt;";
130  break;
131 
132  default:
133  ret += d->name[i];
134  break;
135  }
136  }
137 
138  return Strings::colorizeString(ret);
139 }
140 
141 unsigned long Player::ping() const
142 {
143  return d->ping;
144 }
145 
146 long Player::score() const
147 {
148  return d->score;
149 }
150 
151 Player::PlayerTeam Player::teamNum() const
152 {
153  return d->team;
154 }
155 
156 bool Player::operator==(const Player& other) const
157 {
158  return name().compare(other.name(), Qt::CaseInsensitive) == 0;
159 }
160 
162 
163 uint qHash(const Player& player)
164 {
165  return qHash(player.name());
166 }
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:113