serverstructs.cpp
1 //------------------------------------------------------------------------------
2 // serverstructs.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) 2014 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #include "serverstructs.h"
24 
25 #include <QRegExp>
26 #include <QVector>
27 
28 
29 static QString coerceInternalName(const QString &name)
30 {
31  return name.toLower().remove(QRegExp("[^a-z0-9]"));
32 }
33 
34 
35 DClass<DMFlag>
36 {
37  public:
38  QString name;
39  unsigned value;
40  QString internalName;
41 
42  void setInternalName(const QString &name)
43  {
44  internalName = coerceInternalName(name);
45  }
46 };
47 
48 DPointered(DMFlag)
49 
50 DMFlag::DMFlag()
51 {
52  d->value = 0;
53 }
54 
55 DMFlag::DMFlag(const QString &internalName, unsigned value)
56 {
57  d->name = internalName;
58  d->setInternalName(internalName);
59  d->value = value;
60 }
61 
62 DMFlag::DMFlag(const QString &internalName, unsigned value, const QString &name)
63 {
64  d->name = name;
65  d->setInternalName(internalName);
66  d->value = value;
67 }
68 
69 DMFlag::~DMFlag()
70 {
71 }
72 
73 const QString &DMFlag::internalName() const
74 {
75  return d->internalName;
76 }
77 
78 bool DMFlag::isValid() const
79 {
80  return value() > 0;
81 }
82 
83 const QString &DMFlag::name() const
84 {
85  return d->name;
86 }
87 
88 unsigned DMFlag::value() const
89 {
90  return d->value;
91 }
92 
94 
95 DClass<DMFlagsSection>
96 {
97  public:
98  QString name;
99  QString internalName;
100  QVector<DMFlag> flags;
101 
102  void setInternalName(const QString &name)
103  {
104  internalName = coerceInternalName(name);
105  }
106 };
107 
108 DPointered(DMFlagsSection)
109 
110 DMFlagsSection::DMFlagsSection()
111 {
112 }
113 
114 DMFlagsSection::DMFlagsSection(const QString &internalName)
115 {
116  d->name = internalName;
117  d->setInternalName(internalName);
118 }
119 
120 DMFlagsSection::DMFlagsSection(const QString &internalName, const QString &name)
121 {
122  d->name = name;
123  d->setInternalName(internalName);
124 }
125 
126 DMFlagsSection::~DMFlagsSection()
127 {
128 }
129 
130 void DMFlagsSection::add(const DMFlag& flag)
131 {
132  d->flags << flag;
133 }
134 
136 {
137  unsigned result = 0;
138  foreach (const DMFlag& flag, d->flags)
139  {
140  result |= flag.value();
141  }
142  return result;
143 }
144 
146 {
147  DMFlagsSection copy = *this;
148  copy.d->flags.clear();
149  return copy;
150 }
151 
153 {
154  return d->flags.count();
155 }
156 
157 const QString &DMFlagsSection::internalName() const
158 {
159  return d->internalName;
160 }
161 
163 {
164  return count() == 0;
165 }
166 
167 const QString& DMFlagsSection::name() const
168 {
169  return d->name;
170 }
171 
172 const DMFlag& DMFlagsSection::operator[](int index) const
173 {
174  return d->flags[index];
175 }
176 
178 {
179  return d->flags[index];
180 }
181 
182 QList<DMFlagsSection> DMFlagsSection::removedBySection(
183  const QList<DMFlagsSection> &original,
184  const QList<DMFlagsSection> &removals)
185 {
186  QList<DMFlagsSection> copy;
187  foreach (const DMFlagsSection &section, original)
188  {
189  bool removed = false;
190  foreach (const DMFlagsSection &removal, removals)
191  {
192  if (section.internalName() == removal.internalName())
193  {
194  copy << section.removed(removal);
195  removed = true;
196  break;
197  }
198  }
199  if (!removed)
200  {
201  copy << section;
202  }
203  }
204  return copy;
205 }
206 
208 {
209  DMFlagsSection copy = *this;
210  foreach (const DMFlag &removal, removals.d->flags)
211  {
212  QMutableVectorIterator<DMFlag> i(copy.d->flags);
213  while (i.hasNext())
214  {
215  DMFlag &flag = i.next();
216  if (flag.value() == removal.value())
217  {
218  i.remove();
219  break;
220  }
221  }
222  }
223  return copy;
224 }
225 
227 
228 DClass<GameCVar>
229 {
230  public:
231  QString command;
232  QString name;
233  QVariant value;
234 };
235 
236 DPointered(GameCVar)
237 
238 GameCVar::GameCVar()
239 {
240 }
241 
242 GameCVar::GameCVar(const QString &name, const QString &command)
243 {
244  d->name = name;
245  d->command = command;
246 }
247 
248 GameCVar::GameCVar(const QString &name, const QString &command, const QVariant &value)
249 {
250  d->name = name;
251  d->command = command;
252  setValue(value);
253 }
254 
255 GameCVar::~GameCVar()
256 {
257 }
258 
259 const QString &GameCVar::command() const
260 {
261  return d->command;
262 }
263 
264 bool GameCVar::hasValue() const
265 {
266  return value().isValid();
267 }
268 
269 bool GameCVar::isValid() const
270 {
271  return !command().isEmpty();
272 }
273 
274 const QString &GameCVar::name() const
275 {
276  return d->name;
277 }
278 
279 void GameCVar::setValue(const QVariant& value)
280 {
281  d->value = value;
282 }
283 
284 const QVariant &GameCVar::value() const
285 {
286  return d->value;
287 }
288 
290 
291 DClass<GameCVarProvider>
292 {
293 };
294 
295 DPointeredNoCopy(GameCVarProvider)
296 
297 GameCVarProvider::GameCVarProvider()
298 {
299 }
300 
301 GameCVarProvider::~GameCVarProvider()
302 {
303 }
304 
305 QList<GameCVar> GameCVarProvider::get(const QVariant &context)
306 {
307  return QList<GameCVar>();
308 }
309 
311 
312 DClass<GameMode>
313 {
314  public:
315  int index;
316  QString name;
317  bool teamgame;
318 };
319 
320 DPointered(GameMode)
321 
323 {
324  d->index = -1;
325  d->teamgame = false;
326 }
327 
328 GameMode::GameMode(int index, const QString &name)
329 {
330  d->index = index;
331  d->name = name;
332  d->teamgame = false;
333 }
334 
335 GameMode::~GameMode()
336 {
337 }
338 
339 GameMode GameMode::ffaGame(int index, const QString &name)
340 {
341  GameMode result(index, name);
342  result.setTeamGame(false);
343  return result;
344 }
345 
346 int GameMode::index() const
347 {
348  return d->index;
349 }
350 
351 GameMode GameMode::mkCooperative()
352 {
353  return ffaGame(SGM_Cooperative, QObject::tr("Cooperative"));
354 }
355 
356 GameMode GameMode::mkDeathmatch()
357 {
358  return ffaGame(SGM_Deathmatch, QObject::tr("Deathmatch"));
359 }
360 
361 GameMode GameMode::mkTeamDeathmatch()
362 {
363  return teamGame(SGM_TeamDeathmatch, QObject::tr("Team DM"));
364 }
365 
366 GameMode GameMode::mkCaptureTheFlag()
367 {
368  return teamGame(SGM_CTF, QObject::tr("CTF"));
369 }
370 
371 GameMode GameMode::mkUnknown()
372 {
373  return ffaGame(SGM_Unknown, QObject::tr("Unknown"));
374 }
375 
376 const QString &GameMode::name() const
377 {
378  return d->name;
379 }
380 
382 {
383  return d->teamgame;
384 }
385 
386 bool GameMode::isValid() const
387 {
388  return !d->name.isEmpty();
389 }
390 
391 void GameMode::setTeamGame(bool b)
392 {
393  d->teamgame = b;
394 }
395 
396 GameMode GameMode::teamGame(int index, const QString &name)
397 {
398  GameMode result(index, name);
399  result.setTeamGame(true);
400  return result;
401 }
402 
404 
405 DClass<PWad>
406 {
407  public:
408  QString name;
409  bool optional;
410 };
411 
412 DPointered(PWad)
413 
414 PWad::PWad(const QString &name, bool optional)
415 {
416  d->name = name;
417  d->optional = optional;
418 }
419 
420 PWad::~PWad()
421 {
422 }
423 
424 bool PWad::isOptional() const
425 {
426  return d->optional;
427 }
428 
429 const QString& PWad::name() const
430 {
431  return d->name;
432 }
void add(const DMFlag &flag)
Append a new DMFlag to this section.
Creates GameCVar set.
const QString & name() const
Nice name to display to user in Create Game dialog and in other widgets.
const QString & name() const
User-friendly name to display for game mode.
const QString & internalName() const
static GameMode ffaGame(int index, const QString &name)
Construct a custom FFA game where players don&#39;t belong to any teams.
GameMode()
Constructs an invalid GameMode object.
void setValue(const QVariant &value)
Assign value() to this GameCVar.
PWAD hosted on a server.
bool isValid() const
&#39;Null&#39; objects are invalid.
A group of DMFlag objects that can be safely OR&#39;ed together to form a meaningful value.
A game setting that is a part of a group of settings that can be OR&#39;ed logically as a single integer...
Definition: serverstructs.h:58
const QVariant & value() const
Passed as the second argument, following command().
int count() const
Number of DMFlag objects inside the collection.
virtual QList< GameCVar > get(const QVariant &context)
Default implementation creates empty set.
bool isTeamGame() const
Is this GameMode based on rivaling teams?
bool hasValue() const
Is any value assigned to this GameCVar.
const QString & internalName() const
Uniquely identifiable name within its DMFlagsSection, ex. "Jump is allowed" or "jumpisallowed".
bool isValid() const
&#39;Null&#39; objects are invalid.
const DMFlag & operator[](int index) const
Access DMFlag at specific index with &#39;[]&#39; operator.
const QString & name() const
Server&#39;s name.
Definition: server.cpp:430
Game mode representation.
DMFlagsSection removed(const DMFlagsSection &removals) const
Returns a copy of this list with specified DMFlags removed.
unsigned combineValues() const
Logical OR of all DMFlag::value() results in this collection.
unsigned value() const
Bits that represent this flag (usually just a single &#39;1&#39; bit).
int index() const
Index, either a StandardGameMode or custom defined by plugin.
const QString & name() const
User-displayable name of this section, ex. "Compatibility flags".
bool isValid() const
Valid objects have value() greater than zero.
bool isOptional() const
Is this WAD required to join the server?
static GameMode teamGame(int index, const QString &name)
Game mode based on rivaling teams.
DMFlagsSection copyEmpty() const
Copies section maintaining its properties but removing all flags.
const QString & name() const
User-displayable, translateable name of the DMFlag, ex. "Jump is allowed".
bool isEmpty() const
Does this section contain any dmflag?
A general game setting or variable (like fraglimit).
const QString & command() const
Command-line argument that sets this GameCVar.
static QList< DMFlagsSection > removedBySection(const QList< DMFlagsSection > &original, const QList< DMFlagsSection > &removals)
Matches sections by internalName() and calls removed() on them.
const QString & name() const
File name of the WAD.