KDEGames

kgame.h
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001 Martin Heni (kde at heni-online.de)
4  Copyright (C) 2001 Andreas Beckermann ([email protected])
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
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  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 
21 #ifndef __KGAME_H_
22 #define __KGAME_H_
23 
24 #include <QDataStream>
25 #include <QString>
26 #include <QList>
27 #include <QLoggingCategory>
28 
29 #include "kgamenetwork.h"
30 #include "libkdegamesprivate_export.h"
31 
32 Q_DECLARE_LOGGING_CATEGORY(GAMES_PRIVATE_KGAME)
33 
34 class KRandomSequence;
35 
36 class KPlayer;
37 class KGamePropertyBase;
39 class KGameSequence;
40 
41 class KGamePrivate;
42 
64 class KDEGAMESPRIVATE_EXPORT KGame : public KGameNetwork
65 {
66  Q_OBJECT
67 
68 public:
70 
93  {
94  PolicyUndefined = 0,
95  PolicyClean = 1,
96  PolicyDirty = 2,
97  PolicyLocal = 3
98  };
99 
105  explicit KGame(int cookie=42,QObject* parent=nullptr);
106 
110  ~KGame() override;
111 
115  void Debug() override;
116 
123  {
124  Init = 0,
125  Run = 1,
126  Pause = 2,
127  End = 3,
128  Abort = 4,
129  SystemPause = 5,
130  Intro = 6,
131  UserStatus = 7
132  };
133 
134  // Properties
140  KGamePlayerList *playerList();
141 
145  const KGamePlayerList *playerList() const;
146 
151  KGamePlayerList *inactivePlayerList();
152 
156  const KGamePlayerList *inactivePlayerList() const;
157 
163  KRandomSequence *random() const;
164 
169  KGameSequence *gameSequence() const;
170 
175  bool isRunning() const;
176 
177  // Player handling
183  KPlayer *findPlayer(quint32 id) const;
184 
194  void setGameSequence(KGameSequence* sequence);
195 
213  bool addPlayer(KPlayer* newplayer);
214 
221  //AB: TODO: make sendMessage to return if the message will be able to be
222  //sent, eg if a socket is connected, etc. If sendMessage returns false
223  //remove the player directly using systemRemovePlayer
224  bool removePlayer(KPlayer * player) { return removePlayer(player, 0); }
225 
230  void playerDeleted(KPlayer * player);
231 
235  bool activatePlayer(KPlayer *player);
236 
240  bool inactivatePlayer(KPlayer *player);
241 
248  void setMaxPlayers(uint maxnumber);
249 
254  int maxPlayers() const;
255 
262  void setMinPlayers(uint minnumber);
263 
268  uint minPlayers() const;
269 
274  uint playerCount() const;
275 
280  virtual KPlayer * nextPlayer(KPlayer *last,bool exclusive=true);
281 
282  // Input events
287  virtual bool sendPlayerInput(QDataStream &msg,KPlayer *player,quint32 sender=0);
288 
299  virtual bool systemPlayerInput(QDataStream &msg,KPlayer *player,quint32 sender=0);
300 
327  virtual KPlayer *createPlayer(int rtti,int io,bool isvirtual);
328 
329  // load/save
342  virtual bool load(QDataStream &stream,bool reset=true);
343 
352  virtual bool load(const QString& filename,bool reset=true);
353 
363  virtual bool save(QDataStream &stream,bool saveplayers=true);
364 
373  virtual bool save(const QString& filename,bool saveplayers=true);
374 
382  virtual bool reset();
383 
384 
385  // Game sequence
391  int gameStatus() const;
392 
398  void setGameStatus(int status);
399 
403  bool addProperty(KGamePropertyBase* data);
404 
408  bool sendPlayerProperty(int msgid, QDataStream& s, quint32 playerId);
409 
414  KGamePropertyBase* findProperty(int id) const;
415 
422  void setPolicy(GamePolicy p,bool recursive=true);
423 
427  GamePolicy policy() const;
428 
440  bool sendGroupMessage(const QByteArray& msg, int msgid, quint32 sender, const QString& group);
441  bool sendGroupMessage(const QDataStream &msg, int msgid, quint32 sender, const QString& group);
442  bool sendGroupMessage(int msg, int msgid, quint32 sender, const QString& group);
443  bool sendGroupMessage(const QString& msg, int msgid, quint32 sender, const QString& group);
444 
463  void networkTransmission(QDataStream &stream, int msgid, quint32 receiver, quint32 sender, quint32 clientID) override;
464 
468  KGamePropertyHandler* dataHandler() const;
469 
470 protected Q_SLOTS:
474  void sendProperty(int msgid, QDataStream& stream, bool* sent);
475 
479  void emitSignal(KGamePropertyBase *me);
480 
485  virtual void prepareNext();
486 
487 
492  void slotClientConnected(quint32 clientId);
493 
501  void slotClientDisconnected(quint32 clientId,bool broken);
502 
508  void slotServerDisconnected();
509 
510 Q_SIGNALS:
528  void signalReplacePlayerIO(KPlayer* player, bool* remove);
529 
544  void signalLoadPrePlayers(QDataStream &stream);
545 
553  void signalLoad(QDataStream &stream);
554 
568  void signalSavePrePlayers(QDataStream &stream);
569 
577  void signalSave(QDataStream &stream);
578 
590  void signalLoadError(QDataStream &stream,bool network,int cookie, bool &result);
591 
598  void signalNetworkData(int msgid,const QByteArray& buffer, quint32 receiver, quint32 sender);
599 
605  void signalMessageUpdate(int msgid,quint32 receiver,quint32 sender);
606 
617  void signalPlayerLeftGame(KPlayer *player);
618 
624  void signalPlayerJoinedGame(KPlayer *player);
625 
626 
631  void signalPropertyChanged(KGamePropertyBase *property, KGame *me);
632 
641  void signalGameOver(int status, KPlayer *current, KGame *me);
642 
654  void signalClientJoinedGame(quint32 clientid,KGame *me);
655 
673  void signalClientLeftGame(int clientID,int oldgamestatus,KGame *me);
674 
675 
676 protected:
704  virtual bool playerInput(QDataStream &msg,KPlayer *player)=0;
705 
706 
718  KPlayer *playerInputFinished(KPlayer *player);
719 
720 
754  virtual void newPlayersJoin(KGamePlayerList *oldplayer,
755  KGamePlayerList *newplayer,
756  QList<int> &inactivate) {
757  Q_UNUSED( oldplayer );
758  Q_UNUSED( newplayer );
759  Q_UNUSED( inactivate );
760  }
761 
770  void savePlayers(QDataStream &stream,KGamePlayerList *list=nullptr);
771 
782  void savePlayer(QDataStream& stream,KPlayer* player);
783 
792  KPlayer *loadPlayer(QDataStream& stream,bool isvirtual=false);
793 
794 
798  bool systemInactivatePlayer(KPlayer *player);
799 
803  bool systemActivatePlayer(KPlayer *player);
804 
822  bool systemAddPlayer(KPlayer* newplayer);
823 
831  void systemRemovePlayer(KPlayer* player,bool deleteit);
832 
843  virtual void negotiateNetworkGame(quint32 clientID);
844 
849  void syncRandom();
850 
851  void deletePlayers();
852  void deleteInactivePlayers();
853 
861  virtual int checkGameOver(KPlayer *player);
862 
876  virtual bool loadgame(QDataStream &stream, bool network, bool reset);
877 
887  virtual bool savegame(QDataStream &stream, bool network,bool saveplayers);
888 
889 private:
890  //AB: this is to hide the "receiver" parameter from the user. It shouldn't be
891  //used if possible (except for init).
900  //void addPlayer(KPlayer* newplayer, quint32 receiver);
901 
907  bool removePlayer(KPlayer * player, quint32 receiver);
908 
912  void setupGame(quint32 sender);
913 
917  void setupGameContinue(QDataStream& msg, quint32 sender);
918 
926  bool systemRemove(KPlayer* player,bool deleteit);
927 
928 
929 private:
930  KGamePrivate* const d;
931 };
932 
933 #endif
This class takes care of round or move management as well of the gameover condition.
Definition: kgamesequence.h:44
GameStatus
Game status - Use this to Control the game flow.
Definition: kgame.h:122
bool removePlayer(KPlayer *player)
Sends a message over the network, msgid=IdRemovePlayer.
Definition: kgame.h:224
A collection class for KGameProperty objects.
virtual void newPlayersJoin(KGamePlayerList *oldplayer, KGamePlayerList *newplayer, QList< int > &inactivate)
This virtual function can be overwritten for your own player management.
Definition: kgame.h:754
GamePolicy
The policy of the property.
Definition: kgame.h:92
Base class for a game player.
Definition: kplayer.h:69
Base class of KGameProperty.
Definition: kgameproperty.h:45
The KGameNetwork class is the KGame class with network support.
Definition: kgamenetwork.h:46
The main KDE game object.
Definition: kgame.h:64
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Thu Nov 26 2020 22:36:17 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.