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"
32 
33 #include <QColor>
34 #include <QHostAddress>
35 #include <QHostInfo>
36 #include <QList>
37 #include <QMetaType>
38 #include <QObject>
39 #include <QPixmap>
40 #include <QString>
41 #include <QTime>
42 #include <QUdpSocket>
43 
44 class DMFlagsSection;
45 class EnginePlugin;
46 class ExeFile;
47 class GameCVar;
48 class GameHost;
49 class GameClientRunner;
50 class PathFinder;
51 class Player;
52 class PlayersList;
53 class RConProtocol;
54 class TooltipGenerator;
55 
92 class MAIN_EXPORT Server : public QObject
93 {
94  Q_OBJECT
95 
96  friend class ServerPointer;
97 
98 public:
106  enum Response
107  {
152  };
153 
157  Server(const QHostAddress &address, unsigned short port);
158  virtual ~Server() override;
159 
160  // VIRTUALS
168  virtual ExeFile *clientExe();
176  virtual GameClientRunner *gameRunner();
182  virtual bool hasRcon() const { return false; }
186  virtual QList<GameCVar> modifiers() const;
194  virtual RConProtocol *rcon() { return nullptr; }
200  virtual QRgb teamColor(int team) const;
206  virtual QString teamName(int team) const;
213  virtual EnginePlugin *plugin() const = 0;
214 
224  virtual TooltipGenerator *tooltipGenerator() const;
225 
251  virtual PathFinder wadPathFinder();
252  // END OF VIRTUALS
253 
262  void addPlayer(const Player &player);
266  const QHostAddress &address() const;
270  QString addressWithPort() const;
271 
275  QStringList allWadNames() const;
276 
280  bool anyWadnameContains(const QString &text,
281  Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
282 
288  void clearPlayersList();
289 
293  QString customDetails();
294 
295  const QString &country() const;
296 
300  const QList<DMFlagsSection> &dmFlags() const;
304  const QString &email() const;
305 
313  QString engineName() const;
314 
318  const GameMode &gameMode() const;
322  const QString &gameVersion() const;
323 
336  QString hostName(bool forceAddress = false) const;
340  const QPixmap &icon() const;
341 
347  bool isCustom() const;
351  bool isEmpty() const;
355  bool isFull() const;
362  bool isKnown() const;
363 
367  bool isLockedAnywhere() const;
371  bool isLocked() const;
375  bool isLockedInGame() const;
376 
380  bool isRandomMapRotation() const;
384  bool isRefreshing() const;
385 
394  bool isSecure() const;
395 
399  bool isSpecial() const;
400 
404  bool isTestingServer() const;
405 
409  const QString &iwad() const;
410 
415  Response lastResponse() const;
423  void lookupHost();
424 
428  const QString &map() const;
432  const QStringList &mapList() const;
439  unsigned short maxClients() const;
443  unsigned short maxPlayers() const;
447  const QString &motd() const;
451  const QString &name() const;
455  int numFreeClientSlots() const;
459  int numFreeJoinSlots() const;
465  int numFreeSpectatorSlots() const;
470  int numTotalSlots() const
471  {
472  return maxPlayers() > maxClients() ? maxPlayers() : maxClients();
473  }
477  int numWads() const { return wads().size(); }
481  unsigned int ping() const;
485  const Player &player(int index) const;
489  const PlayersList &players() const;
493  unsigned short port() const;
494 
499  Response readRefreshQueryResponse(const QByteArray &data);
500 
504  void refreshStarts();
505 
509  void refreshStops(Response response);
510 
514  unsigned int score(int team = 0) const;
518  const QList<int> &scores() const;
524  unsigned int scoreLimit() const;
525 
535  QWeakPointer<Server> self() const;
536 
543  bool sendRefreshQuery(QUdpSocket *socket);
544 
558  void setCountry(const QString &country);
563  void setCustom(bool custom);
567  void setEmail(const QString &mail);
571  void setGameMode(const GameMode &gameMode);
575  void setMap(const QString &name);
579  void setMapList(const QStringList &mapList);
583  void setMaxClients(unsigned short i);
587  void setMaxPlayers(unsigned short i);
591  void setMotd(const QString &message);
595  void setName(const QString &name);
599  void setPort(unsigned short i);
603  void setRandomMapRotation(bool b);
609  void setSelf(const QWeakPointer<Server> &self);
613  void setSkill(unsigned char newSkill);
617  void setWebSite(const QString &site);
618 
622  void setAdditionalWebSites(const QStringList &list);
623 
627  unsigned short timeLeft() const;
631  unsigned short timeLimit() const;
632 
645  qint64 timeMsSinceLastRefresh() const;
646 
654  unsigned char skill() const;
658  const PWad &wad(int index) const;
662  const QList<PWad> &wads() const;
666  const QString &webSite() const;
667 
671  const QStringList &additionalWebSites() const;
672 
676  QStringList allWebSites() const;
677 
681  bool isLan() const;
682  void setLan(bool b);
683 
684 signals:
691  void begunRefreshing(ServerPtr server);
703  void updated(ServerPtr server, int response);
704 
705 protected:
706  POLYMORPHIC_SETTER_DECLARE(QString, Server, customDetails, ())
707  QString customDetails_default();
708 
716  Response readRequest(const QByteArray &data);
717  POLYMORPHIC_SETTER_DECLARE(Response, Server, readRequest, (const QByteArray &data))
718 
724  QByteArray createSendRequest();
725  POLYMORPHIC_SETTER_DECLARE(QByteArray, Server, createSendRequest, ())
726 
730  void addWad(const PWad &wad);
734  void clearWads();
739  QList<int> &scoresMutable();
743  void setDmFlags(const QList<DMFlagsSection> &dmFlags);
747  void setGameVersion(const QString &version);
751  void setIwad(const QString &iwad);
755  void setLocked(bool locked);
759  void setLockedInGame(bool locked);
766  void setPing(unsigned int currentPing);
767 
774  void setPingIsSet(bool b);
775 
776  void setTestingServer(bool b);
780  void setTimeLeft(unsigned short timeLeft);
784  void setTimeLimit(unsigned short timeLimit);
788  void setScoreLimit(unsigned int scoreLimit);
792  void setSecure(bool bSecureServer);
793 
794 private:
795  Q_DISABLE_COPY(Server)
796 
797  static QString teamNames[];
798 
799  DPtr<Server> d;
800 
801  void clearDMFlags();
802 
803  QByteArray createSendRequest_default();
804  Response readRequest_default(const QByteArray &data);
805 
806  void setResponse(Response response);
807  void setScores(const QList<int> &scores);
808  void setWads(const QList<PWad> &wads);
809 
810 private slots:
811  void setHostName(QHostInfo host);
812 };
813 
814 Q_DECLARE_METATYPE(ServerPtr)
815 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
816 // Qt6 normalizes smart pointer meta types so we only need the non-const version
817 Q_DECLARE_METATYPE(ServerCPtr)
818 #endif
819 
820 #endif /* __SERVER_H__ */