KGame Class Reference
#include <kgame.h>
Inheritance diagram for KGame:

Detailed Description
The main KDE game object.The KGame class is the central game object. A game basically consists of following features:
- Player handling (add, remove,...)
- Game status (end,start,pause,...)
- load/save
- Move (and message) handling
- nextPlayer and gameOver()
- Network connection (for KGameNetwork)
Example:
- Author:
- Martin Heni <martin@heni-online.de>
Definition at line 62 of file kgame.h.
Public Types | |
typedef QPtrList< KPlayer > | KGamePlayerList |
enum | GamePolicy { PolicyUndefined = 0, PolicyClean = 1, PolicyDirty = 2, PolicyLocal = 3 } |
enum | GameStatus { Init = 0, Run = 1, Pause = 2, End = 3, Abort = 4, SystemPause = 5, Intro = 6, UserStatus = 7 } |
Signals | |
void | signalReplacePlayerIO (KPlayer *player, bool *remove) |
void | signalLoadPrePlayers (QDataStream &stream) |
void | signalLoad (QDataStream &stream) |
void | signalSavePrePlayers (QDataStream &stream) |
void | signalSave (QDataStream &stream) |
void | signalLoadError (QDataStream &stream, bool network, int cookie, bool &result) |
void | signalNetworkData (int msgid, const QByteArray &buffer, Q_UINT32 receiver, Q_UINT32 sender) |
void | signalMessageUpdate (int msgid, Q_UINT32 receiver, Q_UINT32 sender) |
void | signalPlayerLeftGame (KPlayer *player) |
void | signalPlayerJoinedGame (KPlayer *player) |
void | signalPropertyChanged (KGamePropertyBase *property, KGame *me) |
void | signalGameOver (int status, KPlayer *current, KGame *me) |
void | signalClientJoinedGame (Q_UINT32 clientid, KGame *me) |
void | signalClientLeftGame (int clientID, int oldgamestatus, KGame *me) |
Public Member Functions | |
KGame (int cookie=42, QObject *parent=0) | |
virtual | ~KGame () |
virtual void | Debug () |
KGamePlayerList * | playerList () |
const KGamePlayerList * | playerList () const |
KGamePlayerList * | inactivePlayerList () |
const KGamePlayerList * | inactivePlayerList () const |
KRandomSequence * | random () const |
KGameSequence * | gameSequence () const |
bool | isRunning () const |
KPlayer * | findPlayer (Q_UINT32 id) const |
void | setGameSequence (KGameSequence *sequence) |
void | addPlayer (KPlayer *newplayer) |
bool | removePlayer (KPlayer *player) |
void | playerDeleted (KPlayer *player) |
bool | activatePlayer (KPlayer *player) |
bool | inactivatePlayer (KPlayer *player) |
void | setMaxPlayers (uint maxnumber) |
int | maxPlayers () const |
void | setMinPlayers (uint minnumber) |
uint | minPlayers () const |
uint | playerCount () const |
virtual KPlayer * | nextPlayer (KPlayer *last, bool exclusive=true) |
virtual bool | sendPlayerInput (QDataStream &msg, KPlayer *player, Q_UINT32 sender=0) |
virtual bool | systemPlayerInput (QDataStream &msg, KPlayer *player, Q_UINT32 sender=0) |
virtual KPlayer * | createPlayer (int rtti, int io, bool isvirtual) |
virtual bool | load (QDataStream &stream, bool reset=true) |
virtual bool | load (QString filename, bool reset=true) |
virtual bool | save (QDataStream &stream, bool saveplayers=true) |
virtual bool | save (QString filename, bool saveplayers=true) |
virtual bool | reset () |
int | gameStatus () const |
void | setGameStatus (int status) |
bool | addProperty (KGamePropertyBase *data) |
bool | sendPlayerProperty (int msgid, QDataStream &s, Q_UINT32 playerId) |
KGamePropertyBase * | findProperty (int id) const |
void | setPolicy (GamePolicy p, bool recursive=true) |
GamePolicy | policy () const |
bool | sendGroupMessage (const QByteArray &msg, int msgid, Q_UINT32 sender, const QString &group) |
bool | sendGroupMessage (const QDataStream &msg, int msgid, Q_UINT32 sender, const QString &group) |
bool | sendGroupMessage (int msg, int msgid, Q_UINT32 sender, const QString &group) |
bool | sendGroupMessage (const QString &msg, int msgid, Q_UINT32 sender, const QString &group) |
virtual void | networkTransmission (QDataStream &stream, int msgid, Q_UINT32 receiver, Q_UINT32 sender, Q_UINT32 clientID) |
KGamePropertyHandler * | dataHandler () const |
Protected Slots | |
void | sendProperty (int msgid, QDataStream &stream, bool *sent) |
void | emitSignal (KGamePropertyBase *me) |
virtual void | prepareNext () |
void | slotClientConnected (Q_UINT32 clientId) |
void | slotClientDisconnected (Q_UINT32 clientId, bool broken) |
void | slotServerDisconnected () |
Protected Member Functions | |
virtual bool | playerInput (QDataStream &msg, KPlayer *player)=0 |
KPlayer * | playerInputFinished (KPlayer *player) |
virtual void | newPlayersJoin (KGamePlayerList *oldplayer, KGamePlayerList *newplayer, QValueList< int > &inactivate) |
void | savePlayers (QDataStream &stream, KGamePlayerList *list=0) |
void | savePlayer (QDataStream &stream, KPlayer *player) |
KPlayer * | loadPlayer (QDataStream &stream, bool isvirtual =false) |
bool | systemInactivatePlayer (KPlayer *player) |
bool | systemActivatePlayer (KPlayer *player) |
void | systemAddPlayer (KPlayer *newplayer) |
void | systemRemovePlayer (KPlayer *player, bool deleteit) |
virtual void | negotiateNetworkGame (Q_UINT32 clientID) |
void | syncRandom () |
void | deletePlayers () |
void | deleteInactivePlayers () |
virtual int | checkGameOver (KPlayer *player) |
virtual bool | loadgame (QDataStream &stream, bool network, bool reset) |
virtual bool | savegame (QDataStream &stream, bool network, bool saveplayers) |
Member Enumeration Documentation
|
The policy of the property. This can be PolicyClean (setVale uses send), PolicyDirty (setValue uses changeValue) or PolicyLocal (setValue uses setLocal). A "clean" policy means that the property is always the same on every client. This is achieved by calling send which actually changes the value only when the message from the MessageServer is received. A "dirty" policy means that as soon as setValue is called the property is changed immediately. And additionally sent over network. This can sometimes lead to bugs as the other clients do not immediately have the same value. For more information see changeValue. PolicyLocal means that a KGameProperty behaves like ever "normal" variable. Whenever setValue is called (e.g. using "=") the value of the property is changes immediately without sending it over network. You might want to use this if you are sure that all clients set the property at the same time. |
|
Game status - Use this to Control the game flow. The KGame e.g. sets the status to Pause when you have less player than the minimum amount |
Constructor & Destructor Documentation
|
Create a KGame object. The cookie is used to identify your game in load/save and network operations. Change this between games. |
|
Destructs the game.
|
Member Function Documentation
|
sends activate player: internal use only?
|
|
Note that KPlayer::save must be implemented properly, as well as KPlayer::rtti This will only send a message to all clients. The player is _not_ added directly! See also playerInput which will be called as soon as the player really has been added. Note that an added player will first get into a "queue" and won't be in the game. It will be added to the game as soon as systemAddPlayer is called what will happen as soon as IdAddPlayer is received. Note: you probably want to connect to signalPlayerJoinedGame for further initialization!
|
|
docu: see KPlayer
|
|
|
|
This virtual function is called if the KGame needs to create a new player. This happens only over a network and with load/save. Doing nothing will create a default KPlayer. If you want to have your own player you have to create one with the given rtti here. Note: If your game uses a player class derived from KPlayer you MUST override this function and create your player here. Otherwise the game will crash. Example: KPlayer *MyGame::createPlayer(int rtti,int io,bool isvirtual) { KPlayer *player=new MyPlayer; if (!isvirtual) // network player ? { // Define something like this to add the IO modules createIO(player,(KGameIO::IOMode)io); } return player; }
|
|
Returns a pointer to the KGame property handler.
|
|
Gives debug output of the game status.
Reimplemented from KGameNetwork. |
|
Called by KGamePropertyHandler only! Internal function!
|
|
Returns the player object for a given player id.
|
|
This function allows to find the pointer to a player property when you know it's id.
|
|
|
|
returns the game status, ie running,pause,ended,. ..
|
|
sends inactivate player: internal use only?
|
|
The same as inactivePlayerList but returns a const pointer.
|
|
Returns a list of all inactive players.
|
|
Is the game running.
|
|
Same as above function but with different parameters.
|
|
Load a saved game, from file OR network. This function has to be overwritten or you need to connect to the load signal if you have game data other than KGameProperty. For file load you should reset() the game before any load attempt to make sure you load into an clear state.
|
|
Load a saved game, from file OR network. Internal. Warning: loadgame must not rely that all players all already activated. Actually the network will activate a player AFTER the loadgame only. This is not true anymore. But be careful anyway.
|
|
Load the player list from a stream. Used for network game and load/save. Can be overwritten if you know what you are doing
|
|
What is the maximal number of players?
|
|
What is the minimal number of players?
|
|
This member function will transmit e.g. all players to that client, as well as all properties of these players (at least if they have been added by KPlayer::addProperty) so that the client will finally have the same status as the master. You want to overwrite this function if you expand KGame by any properties which have to be known by all clients. Only the ADMIN is allowed to call this.
|
|
This will either forward an incoming message to a specified player (see KPlayer::networkTransmission) or handle the message directly (e.g. if msgif==IdRemovePlayer it will remove the (in the stream) specified player). If both is not possible (i.e. the message is user specified data) the signal signalNetworkData is emitted. This emits signalMessageUpdate before doing anything with the message. You can use this signal when you want to be notified about an update/change.
Implements KGameNetwork. |
|
This virtual function can be overwritten for your own player management. It is called when a new game connects to an existing network game or to the network master. In case you do not want all players of both games to be present in the new network game, you can deactivate players here. This is of particular importance if you have a game with fixed number of player like e.g. chess. A network connect needs to disable one player of each game to make sense. Not overwriting this function will activate a default behaviour which will deactivate players until the maxPlayers() numebr is reached according to the KPlayer::networkPriority() value. Players with a low value will be kicked out first. With equal priority players of the new client will leave first. This means, not setting this value and not overwriting this function will never allow a chess game to add client players!!! On the other hand setting one player of each game to a networkPriorty of say 10, already does most of the work for you. The parameters of this function are the playerlist of the network game, which is playerList(). The second argument is the player list of the new client who wants to join and the third argument serves as return parameter. All player ID's which are written into this list will be removed from the created game. You do this by an inactivate.append(player->id());
|
|
|
|
Returns how many players are plugged into the game.
|
|
Called by the destructor of KPlayer to remove itself from the game.
|
|
A player input occurred. This is the most important function as the given message will contain the current move made by the given player. Note that you HAVE to overwrite this function. Otherwise your game makes no sense at all. Generally you have to return TRUE in this function. Only then the game sequence is proceeded by calling playerInputFinished which in turn will check for game over or the next player However, if you have a delayed move, because you e.g. move a card or a piece you want to return FALSE to pause the game sequence and then manually call playerInputFinished to resume it. Example: bool MyClass::playerInput(QDataStream &msg,KPlayer *player) { Q_INT32 move; msg >> move; kdDebug() << " Player " << player->id() << " moved to " << move << endl; return true; }
|
|
Called after the player input is processed by the game. Here the checks for game over and nextPlayer (in the case of turn base games) are processed. Call this manually if you have a delayed move, i.e. your playerInput function returns FALSE. If it returns true you need not do anything here.
|
|
The same as playerList but returns a const pointer.
|
|
Returns a list of all active players.
|
|
|
|
|
|
Returns a pointer to the game's KRandomSequence. This sequence is identical for all network players!
|
|
Sends a message over the network, msgid=IdRemovePlayer. As soon as this message is received by networkTransmission systemRemovePlayer is called and the player is removed. |
|
Resets the game, i.e. puts it into a state where everything can be started from, e.g. a load game Right now it does only need to delete all players
|
|
Same as above function but with different parameters.
|
|
Save a game to a file OR to network. Otherwise the same as the load function
|
|
Save a game, to file OR network. Internal.
|
|
Prepare a player for being added. Put all data about a player into the stream so that it can be sent to the KGameCommunicationServer using addPlayer (e.g.) This function ensures that the code for adding a player is the same in addPlayer as well as in negotiateNetworkGame
|
|
Save the player list to a stream. Used for network game and load/save. Can be overwritten if you know what you are doing
|
|
See KGameNetwork::sendMessage. Send a network message msg with a given message ID msgid to all players of a given group (see KPlayer::group)
|
|
Called by KPlayer to send a player input to the KMessageServer.
|
|
This is called by KPlayer::sendProperty only! Internal function!
|
|
Called by KGamePropertyHandler only! Internal function!
|
|
Set a new KGameSequence to control player management. By default KGame uses a normal KGameSequence object. You might want to subclass that and provide your own object. The previous sequence will get deleted.
|
|
sets the game status
|
|
Set the maximal number of players. After this is reached no more players can be added. You must be ADMIN to call this (
|
|
Set the minimal number of players. A game can not be started with less player resp. is paused when already running. You must be ADMIN to call this (see isAdmin)!
|
|
Changes the consistency policy of a property. The GamePolicy is one of PolicyClean (default), PolicyDirty or PolicyLocal. It is up to you to decide how you want to work. |
|
Is emmited after a client is successfully connected to the game. The client id is the id of the new game client. An easy way to check whether that's us is if (clientid==gameid()) .. // we joined else ... // someone joined the game
|
|
This signal is emitted after a network partner left the game (either by a broken connection or voluntarily). All changes to the network players have already be done. If there are not enough players left, the game might have been paused. To check this you get the old gamestatus before the disconnection as argument here. The id of the client who left the game allows to distinguish who left the game. If it is 0, the server disconnected and you were a client which has been switched back to local play. You can use this signal to, e.g. set some menues back to local player when they were network before.
|
|
Is emitted after a call to gameOver() returns a non zero return code. This code is forwarded to this signal as 'status'.
|
|
The game will be loaded from the given stream. Load from here the data which is NOT a game or player property. It is not necessary to use this signal for a full property game.
|
|
Is emmited if a game with a different version cookie is loaded. Normally this should result in an error. But maybe you do support loading of older game versions. Here would be a good place to do a conversion.
|
|
The game will be loaded from the given stream. Load from here the data which is NOT a game or player property. It is not necessary to use this signal for a full property game. This signal is emitted before the players are loaded by KGame. See also signalLoad You must load exactly the same data from the stream that you have saved in signalSavePrePlayers. Otherwise player loading will not work anymore.
|
|
We got an network message. this can be used to notify us that something changed. What changed can be seen in the message id. Whether this is the best possible method to do this is unclear... |
|
We got an user defined update message. This is usually done by a sendData in a inherited KGame Object which defines its own methods and has to syncronise them over the network. Reaction to this is usually a call to a KGame function. |
|
a player joined the game
|
|
a player left the game because of a broken connection or so! Note that when this signal is emitted the player is not part of playerList anymore but the pointer is still valid. You should do some final cleanups here since the player is usually deleted after the signal is emitted.
|
|
This signal is emmited if a player property changes its value and the property is set to notify this change.
|
|
When a client disconnects from the game usually all players from that client are removed. But if you use completely the KGame structure you probably don't want this. You just want to replace the KGameIO of the (human) player by a computer KGameIO. So this player continues game but is from this point on controlled by the computer. You achieve this by connecting to this signal. It is emitted as soon as a client disconnects on all other clients. Make sure to add a new KGameIO only once! you might want to use isAdmin for this. If you added a new KGameIO set *remove=false otherwise the player is completely removed.
|
|
The game will be saved to the given stream. Fill this with data which is NOT a game or player property. It is not necessary to use this signal for a full property game.
|
|
The game will be saved to the given stream. Fill this with data which is NOT a game or player property. It is not necessary to use this signal for a full property game. This signal is emitted before the players are saved by KGame. See also signalSave If you can choose between signalSavePrePlayers and signalSave then better use signalSave
|
|
Calls negotiateNetworkGame() See KGameNetwork::signalClientConnected.
|
|
This slot is called whenever the connection to a client is lost (ie the signal KGameNetwork::signalClientDisconnected is emitted) and will remove the players from that client.
|
|
This slot is called whenever the connection to the server is lost (ie the signal KGameNetwork::signalConnectionBroken is emitted) and will switch to local game mode.
|
|
syncronise the random numbers with all network clients not used by KGame - if it should be kept then as public method
|
|
activates player. Use activatePlayer instead! |
|
Finally adds a player to the game and therefore to the list.
|
|
inactivates player. Use inactivatePlayer instead! |
|
Called when a player input arrives from KMessageServer. Calls prepareNext (using QTimer::singleShot) if gameOver() returns 0. This function should normally not be used outside KGame. It could be made non-virtual,protected in a later version. At the moment it is a virtual function to give you more control over KGame. For documentation see playerInput. |
|
Removes a player from the game. Use removePlayer to send KGameMessage::IdRemovePlayer. As soon as this Id is received systemRemovePlayer is called and the player is removed directly. |
The documentation for this class was generated from the following files: