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 short score, unsigned short ping, PlayerTeam team, bool spectator, bool bot)
49 {
50  d->name = name;
51  d->score = score;
52  d->ping = ping;
53  d->spectator = spectator;
54  d->bot = bot;
55  d->team = team;
56 }
57 
58 Player::Player(const Player& other)
59 {
60  d = other.d;
61 }
62 
63 Player& Player::operator=(const Player& other)
64 {
65  d = other.d;
66  return *this;
67 }
68 
69 Player::~Player()
70 {
71 }
72 
73 bool Player::isBot() const
74 {
75  return d->bot;
76 }
77 
78 bool Player::isSpectating() const
79 {
80  return d->spectator;
81 }
82 
83 bool Player::isTeamlessBot() const
84 {
85  return d->bot && d->team == TEAM_NONE;
86 }
87 
88 const QString& Player::name() const
89 {
90  return d->name;
91 }
92 
94 {
95  QString ret;
96  for (int i = 0; i < d->name.length(); ++i)
97  {
98  if (d->name[i] < 32 || d->name[i] > 126)
99  {
100  // Lets only remove the following character on \c.
101  // Removing the control characters is still a good idea though.
102  if(d->name[i] == ESCAPE_COLOR_CHAR)
103  ++i;
104  continue;
105  }
106 
107  ret += d->name[i];
108  }
109  return ret;
110 }
111 
112 QString Player::nameFormatted() const
113 {
114  QString ret;
115  for (int i = 0; i < d->name.length(); ++i)
116  {
117  // cut out bad characters
118  if ((d->name[i] < 32 || d->name[i] > 126) && d->name[i] != ESCAPE_COLOR_CHAR)
119  continue;
120 
121  switch (d->name[i].toAscii())
122  {
123  case '<':
124  ret += "&lt;";
125  break;
126 
127  case '>':
128  ret += "&gt;";
129  break;
130 
131  default:
132  ret += d->name[i];
133  break;
134  }
135  }
136 
137  return Strings::colorizeString(ret);
138 }
139 
140 unsigned short Player::ping() const
141 {
142  return d->ping;
143 }
144 
145 short Player::score() const
146 {
147  return d->score;
148 }
149 
150 Player::PlayerTeam Player::teamNum() const
151 {
152  return d->team;
153 }
154 
155 bool Player::operator==(const Player& other) const
156 {
157  return name().compare(other.name(), Qt::CaseInsensitive) == 0;
158 }
159 
161 
162 uint qHash(const Player& player)
163 {
164  return qHash(player.name());
165 }
QString nameColorTagsStripped() const
Definition: player.cpp:93
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:48
QString nameFormatted() const
Definition: player.cpp:112