server.h
1 //------------------------------------------------------------------------------
2 // server.h
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) 2009 Braden "Blzut3" Obrzut <admin@maniacsvault.net>
22 //------------------------------------------------------------------------------
23 
24 #ifndef __SERVER_H__
25 #define __SERVER_H__
26 
27 #include "dptr.h"
28 #include "global.h"
29 #include "serverapi/polymorphism.h"
30 #include "serverapi/serverptr.h"
31 
32 #include <QColor>
33 #include <QHostAddress>
34 #include <QHostInfo>
35 #include <QList>
36 #include <QMetaType>
37 #include <QObject>
38 #include <QPixmap>
39 #include <QString>
40 #include <QTime>
41 #include <QUdpSocket>
42 
43 class DMFlagsSection;
44 class EnginePlugin;
45 class ExeFile;
46 class GameCVar;
47 class GameHost;
48 class GameMode;
49 class GameClientRunner;
50 class PathFinder;
51 class Player;
52 class PlayersList;
53 class PWad;
54 class RConProtocol;
55 class TooltipGenerator;
56 
93 class MAIN_EXPORT Server : public QObject
94 {
95  Q_OBJECT
96 
97  friend class ServerPointer;
98 
99 public:
107  enum Response
108  {
153  };
154 
158  Server(const QHostAddress &address, unsigned short port);
159  virtual ~Server() override;
160 
161  // VIRTUALS
169  virtual ExeFile *clientExe();
177  virtual GameClientRunner *gameRunner();
183  virtual bool hasRcon() const { return false; }
187  virtual QList<GameCVar> modifiers() const;
195  virtual RConProtocol *rcon() { return nullptr; }
201  virtual QRgb teamColor(int team) const;
207  virtual QString teamName(int team) const;
214  virtual EnginePlugin *plugin() const = 0;
215 
225  virtual TooltipGenerator *tooltipGenerator() const;
226 
252  virtual PathFinder wadPathFinder();
253  // END OF VIRTUALS
254 
263  void addPlayer(const Player &player);
267  const QHostAddress &address() const;
271  QString addressWithPort() const;
272 
276  QStringList allWadNames() const;
277 
281  bool anyWadnameContains(const QString &text,
282  Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
283 
289  void clearPlayersList();
290 
294  QString customDetails();
295 
299  const QList<DMFlagsSection> &dmFlags() const;
303  const QString &email() const;
304 
312  QString engineName() const;
313 
317  const GameMode &gameMode() const;
321  const QString &gameVersion() const;
322 
335  QString hostName(bool forceAddress = false) const;
339  const QPixmap &icon() const;
340 
346  bool isCustom() const;
350  bool isEmpty() const;
354  bool isFull() const;
361  bool isKnown() const;
362 
366  bool isLockedAnywhere() const;
370  bool isLocked() const;
374  bool isLockedInGame() const;
375 
379  bool isRandomMapRotation() const;
383  bool isRefreshing() const;
384 
393  bool isSecure() const;
394 
398  bool isSpecial() const;
399 
403  bool isTestingServer() const;
404 
408  const QString &iwad() const;
409 
414  Response lastResponse() const;
422  void lookupHost();
423 
427  const QString &map() const;
431  const QStringList &mapList() const;
438  unsigned short maxClients() const;
442  unsigned short maxPlayers() const;
446  const QString &motd() const;
450  const QString &name() const;
454  int numFreeClientSlots() const;
458  int numFreeJoinSlots() const;
464  int numFreeSpectatorSlots() const;
469  int numTotalSlots() const
470  {
471  return maxPlayers() > maxClients() ? maxPlayers() : maxClients();
472  }
476  int numWads() const { return wads().size(); }
480  unsigned int ping() const;
484  const Player &player(int index) const;
488  const PlayersList &players() const;
492  unsigned short port() const;
493 
498  Response readRefreshQueryResponse(const QByteArray &data);
499 
503  void refreshStarts();
504 
508  void refreshStops(Response response);
509 
513  unsigned int score(int team = 0) const;
517  const QList<int> &scores() const;
523  unsigned int scoreLimit() const;
524 
534  QWeakPointer<Server> self() const;
535 
542  bool sendRefreshQuery(QUdpSocket *socket);
543 
548  void setCustom(bool custom);
552  void setEmail(const QString &mail);
556  void setGameMode(const GameMode &gameMode);
560  void setMap(const QString &name);
564  void setMapList(const QStringList &mapList);
568  void setMaxClients(unsigned short i);
572  void setMaxPlayers(unsigned short i);
576  void setMotd(const QString &message);
580  void setName(const QString &name);
584  void setPort(unsigned short i);
588  void setRandomMapRotation(bool b);
594  void setSelf(const QWeakPointer<Server> &self);
598  void setSkill(unsigned char newSkill);
602  void setWebSite(const QString &site);
603 
607  void setAdditionalWebSites(const QStringList &list);
608 
612  unsigned short timeLeft() const;
616  unsigned short timeLimit() const;
617 
630  qint64 timeMsSinceLastRefresh() const;
631 
639  unsigned char skill() const;
643  const PWad &wad(int index) const;
647  const QList<PWad> &wads() const;
651  const QString &webSite() const;
652 
656  const QStringList &additionalWebSites() const;
657 
661  QStringList allWebSites() const;
662 
666  bool isLan() const;
667  void setLan(bool b);
668 
669 signals:
676  void begunRefreshing(ServerPtr server);
688  void updated(ServerPtr server, int response);
689 
690 protected:
691  POLYMORPHIC_SETTER_DECLARE(QString, Server, customDetails, ())
692  QString customDetails_default();
693 
701  Response readRequest(const QByteArray &data);
702  POLYMORPHIC_SETTER_DECLARE(Response, Server, readRequest, (const QByteArray &data))
703 
709  QByteArray createSendRequest();
710  POLYMORPHIC_SETTER_DECLARE(QByteArray, Server, createSendRequest, ())
711 
715  void addWad(const PWad &wad);
719  void clearWads();
724  QList<int> &scoresMutable();
728  void setDmFlags(const QList<DMFlagsSection> &dmFlags);
732  void setGameVersion(const QString &version);
736  void setIwad(const QString &iwad);
740  void setLocked(bool locked);
744  void setLockedInGame(bool locked);
751  void setPing(unsigned int currentPing);
752 
759  void setPingIsSet(bool b);
760 
761  void setTestingServer(bool b);
765  void setTimeLeft(unsigned short timeLeft);
769  void setTimeLimit(unsigned short timeLimit);
773  void setScoreLimit(unsigned int scoreLimit);
777  void setSecure(bool bSecureServer);
778 
779 private:
780  Q_DISABLE_COPY(Server)
781 
782  static QString teamNames[];
783 
784  DPtr<Server> d;
785 
786  void clearDMFlags();
787 
788  QByteArray createSendRequest_default();
789  Response readRequest_default(const QByteArray &data);
790 
791  void setResponse(Response response);
792  void setScores(const QList<int> &scores);
793  void setWads(const QList<PWad> &wads);
794 
795 private slots:
796  void setHostName(QHostInfo host);
797 };
798 
799 Q_DECLARE_METATYPE(ServerPtr)
800 Q_DECLARE_METATYPE(ServerCPtr)
801 
802 #endif /* __SERVER_H__ */