KGame Class Reference

The main KDE game object. More...

#include <kgame.h>

Inheritance diagram for KGame:

Inheritance graph
[legend]
Collaboration diagram for KGame:

Collaboration graph
[legend]
List of all members.

Public Types

typedef QPtrList< KPlayerKGamePlayerList
enum  GamePolicy { PolicyUndefined = 0, PolicyClean = 1, PolicyDirty = 2, PolicyLocal = 3 }
 The policy of the property. More...
enum  GameStatus {
  Init = 0, Run = 1, Pause = 2, End = 3,
  Abort = 4, SystemPause = 5, Intro = 6, UserStatus = 7
}
 Game status - Use this to Control the game flow. More...

Signals

void signalReplacePlayerIO (KPlayer *player, bool *remove)
 When a client disconnects from the game usually all players from that client are removed.
void signalLoadPrePlayers (QDataStream &stream)
 The game will be loaded from the given stream.
void signalLoad (QDataStream &stream)
 The game will be loaded from the given stream.
void signalSavePrePlayers (QDataStream &stream)
 The game will be saved to the given stream.
void signalSave (QDataStream &stream)
 The game will be saved to the given stream.
void signalLoadError (QDataStream &stream, bool network, int cookie, bool &result)
 Is emmited if a game with a different version cookie is loaded.
void signalNetworkData (int msgid, const QByteArray &buffer, Q_UINT32 receiver, Q_UINT32 sender)
 We got an user defined update message.
void signalMessageUpdate (int msgid, Q_UINT32 receiver, Q_UINT32 sender)
 We got an network message.
void signalPlayerLeftGame (KPlayer *player)
 a player left the game because of a broken connection or so!
void signalPlayerJoinedGame (KPlayer *player)
 a player joined the game
void signalPropertyChanged (KGamePropertyBase *property, KGame *me)
 This signal is emmited if a player property changes its value and the property is set to notify this change.
void signalGameOver (int status, KPlayer *current, KGame *me)
 Is emitted after a call to gameOver() returns a non zero return code.
void signalClientJoinedGame (Q_UINT32 clientid, KGame *me)
 Is emmited after a client is successfully connected to the game.
void signalClientLeftGame (int clientID, int oldgamestatus, KGame *me)
 This signal is emitted after a network partner left the game (either by a broken connection or voluntarily).

Public Member Functions

 KGame (int cookie=42, QObject *parent=0)
 Create a KGame object.
virtual ~KGame ()
 Destructs the game.
virtual void Debug ()
 Gives debug output of the game status.
KGamePlayerListplayerList ()
 Returns a list of all active players.
const KGamePlayerListplayerList () const
 The same as playerList but returns a const pointer.
KGamePlayerListinactivePlayerList ()
 Returns a list of all inactive players.
const KGamePlayerListinactivePlayerList () const
 The same as inactivePlayerList but returns a const pointer.
KRandomSequence * random () const
 Returns a pointer to the game's KRandomSequence.
KGameSequencegameSequence () const
bool isRunning () const
 Is the game running.
KPlayerfindPlayer (Q_UINT32 id) const
 Returns the player object for a given player id.
void setGameSequence (KGameSequence *sequence)
 Set a new KGameSequence to control player management.
void addPlayer (KPlayer *newplayer)
 Note that KPlayer::save must be implemented properly, as well as KPlayer::rtti This will only send a message to all clients.
bool removePlayer (KPlayer *player)
 Sends a message over the network, msgid=IdRemovePlayer.
void playerDeleted (KPlayer *player)
 Called by the destructor of KPlayer to remove itself from the game.
bool activatePlayer (KPlayer *player)
 sends activate player: internal use only?
bool inactivatePlayer (KPlayer *player)
 sends inactivate player: internal use only?
void setMaxPlayers (uint maxnumber)
 Set the maximal number of players.
int maxPlayers () const
 What is the maximal number of players?
void setMinPlayers (uint minnumber)
 Set the minimal number of players.
uint minPlayers () const
 What is the minimal number of players?
uint playerCount () const
 Returns how many players are plugged into the game.
virtual KPlayernextPlayer (KPlayer *last, bool exclusive=true)
virtual bool sendPlayerInput (QDataStream &msg, KPlayer *player, Q_UINT32 sender=0)
 Called by KPlayer to send a player input to the KMessageServer.
virtual bool systemPlayerInput (QDataStream &msg, KPlayer *player, Q_UINT32 sender=0)
 Called when a player input arrives from KMessageServer.
virtual KPlayercreatePlayer (int rtti, int io, bool isvirtual)
 This virtual function is called if the KGame needs to create a new player.
virtual bool load (QDataStream &stream, bool reset=true)
 Load a saved game, from file OR network.
virtual bool load (QString filename, bool reset=true)
 Same as above function but with different parameters.
virtual bool save (QDataStream &stream, bool saveplayers=true)
 Save a game to a file OR to network.
virtual bool save (QString filename, bool saveplayers=true)
 Same as above function but with different parameters.
virtual bool reset ()
 Resets the game, i.e.
int gameStatus () const
 returns the game status, ie running,pause,ended,.
void setGameStatus (int status)
 sets the game status
bool addProperty (KGamePropertyBase *data)
 docu: see KPlayer
bool sendPlayerProperty (int msgid, QDataStream &s, Q_UINT32 playerId)
 This is called by KPlayer::sendProperty only! Internal function!
KGamePropertyBasefindProperty (int id) const
 This function allows to find the pointer to a player property when you know it's id.
void setPolicy (GamePolicy p, bool recursive=true)
 Changes the consistency policy of a property.
GamePolicy policy () const
bool sendGroupMessage (const QByteArray &msg, int msgid, Q_UINT32 sender, const QString &group)
 See KGameNetwork::sendMessage.
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)
 This will either forward an incoming message to a specified player (see KPlayer::networkTransmission) or handle the message directly (e.g.
KGamePropertyHandlerdataHandler () const
 Returns a pointer to the KGame property handler.

Protected Slots

void sendProperty (int msgid, QDataStream &stream, bool *sent)
 Called by KGamePropertyHandler only! Internal function!
void emitSignal (KGamePropertyBase *me)
 Called by KGamePropertyHandler only! Internal function!
virtual void prepareNext ()
void slotClientConnected (Q_UINT32 clientId)
 Calls negotiateNetworkGame() See KGameNetwork::signalClientConnected.
void slotClientDisconnected (Q_UINT32 clientId, bool broken)
 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.
void slotServerDisconnected ()
 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.

Protected Member Functions

virtual bool playerInput (QDataStream &msg, KPlayer *player)=0
 A player input occurred.
KPlayerplayerInputFinished (KPlayer *player)
 Called after the player input is processed by the game.
virtual void newPlayersJoin (KGamePlayerList *oldplayer, KGamePlayerList *newplayer, QValueList< int > &inactivate)
 This virtual function can be overwritten for your own player management.
void savePlayers (QDataStream &stream, KGamePlayerList *list=0)
 Save the player list to a stream.
void savePlayer (QDataStream &stream, KPlayer *player)
 Prepare a player for being added.
KPlayerloadPlayer (QDataStream &stream, bool isvirtual =false)
 Load the player list from a stream.
bool systemInactivatePlayer (KPlayer *player)
 inactivates player.
bool systemActivatePlayer (KPlayer *player)
 activates player.
void systemAddPlayer (KPlayer *newplayer)
 Finally adds a player to the game and therefore to the list.
void systemRemovePlayer (KPlayer *player, bool deleteit)
 Removes a player from the game.
virtual void negotiateNetworkGame (Q_UINT32 clientID)
 This member function will transmit e.g.
void syncRandom ()
 syncronise the random numbers with all network clients not used by KGame - if it should be kept then as public method
void deletePlayers ()
void deleteInactivePlayers ()
virtual int checkGameOver (KPlayer *player)
virtual bool loadgame (QDataStream &stream, bool network, bool reset)
 Load a saved game, from file OR network.
virtual bool savegame (QDataStream &stream, bool network, bool saveplayers)
 Save a game, to file OR network.

Detailed Description

The main KDE game object.

The KGame class is the central game object. A game basically consists of following features:

Example:

 KGame *game=new KGame;

Author:
Martin Heni <martin@heni-online.de>

Definition at line 62 of file kgame.h.


Member Typedef Documentation

typedef QPtrList<KPlayer> KGame::KGamePlayerList
 

Definition at line 67 of file kgame.h.


Member Enumeration Documentation

enum KGame::GamePolicy
 

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.

Enumerator:
PolicyUndefined 
PolicyClean 
PolicyDirty 
PolicyLocal 

Definition at line 90 of file kgame.h.

enum KGame::GameStatus
 

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

Enumerator:
Init 
Run 
Pause 
End 
Abort 
SystemPause 
Intro 
UserStatus 

Definition at line 120 of file kgame.h.


Constructor & Destructor Documentation

KGame::KGame int  cookie = 42,
QObject *  parent = 0
 

Create a KGame object.

The cookie is used to identify your game in load/save and network operations. Change this between games.

Definition at line 84 of file kgame.cpp.

References emitSignal(), KGameMessage::IdGameProperty, KGamePropertyBase::IdGameStatus, KGamePropertyBase::IdMaxPlayer, KGamePropertyBase::IdMinPlayer, Init, KGamePrivate::mProperties, sendProperty(), setGameSequence(), KGameNetwork::signalClientConnected(), KGameNetwork::signalClientDisconnected(), KGameNetwork::signalConnectionBroken(), slotClientConnected(), slotClientDisconnected(), and slotServerDisconnected().

Here is the call graph for this function:

KGame::~KGame  )  [virtual]
 

Destructs the game.

Definition at line 120 of file kgame.cpp.

References KGamePrivate::mGameSequence, KGamePrivate::mRandom, and reset().

Here is the call graph for this function:


Member Function Documentation

bool KGame::activatePlayer KPlayer player  ) 
 

sends activate player: internal use only?

Definition at line 630 of file kgame.cpp.

References KPlayer::id(), KGameMessage::IdActivatePlayer, policy(), PolicyClean, PolicyDirty, PolicyLocal, KGameNetwork::sendSystemMessage(), and systemActivatePlayer().

Here is the call graph for this function:

void KGame::addPlayer KPlayer newplayer  ) 
 

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!

Parameters:
newplayer The player you want to add. KGame will send a message to all clients and add the player using systemAddPlayer

Definition at line 412 of file kgame.cpp.

References KGameMessage::createPlayerId(), KGameNetwork::gameId(), KPlayer::id(), maxPlayers(), KGamePrivate::mUniquePlayerNumber, playerCount(), and KPlayer::setId().

Referenced by KPlayer::KPlayer().

Here is the call graph for this function:

bool KGame::addProperty KGamePropertyBase data  ) 
 

docu: see KPlayer

Definition at line 1425 of file kgame.cpp.

References KGamePropertyHandler::addProperty(), and dataHandler().

Here is the call graph for this function:

int KGame::checkGameOver KPlayer player  )  [protected, virtual]
 

Deprecated:
Use KGameSequence instead.
Parameters:
player the player who made the last move
Returns:
anything else but 0 is considered as game over

Definition at line 790 of file kgame.cpp.

References KGameSequence::checkGameOver(), and gameSequence().

Referenced by playerInputFinished().

Here is the call graph for this function:

KPlayer * KGame::createPlayer int  rtti,
int  io,
bool  isvirtual
[virtual]
 

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;
    }

Parameters:
rtti is the type of the player (0 means default KPlayer)
io is the 'or'ed rtti of the KGameIO's
isvirtual true if player is virtual

Definition at line 349 of file kgame.cpp.

Referenced by loadPlayer().

KGamePropertyHandler * KGame::dataHandler  )  const
 

Returns a pointer to the KGame property handler.

Definition at line 689 of file kgame.cpp.

References KGamePrivate::mProperties.

Referenced by addProperty(), loadgame(), KGamePropertyBase::registerData(), savegame(), setPolicy(), and KGameDebugDialog::slotUpdateGameData().

void KGame::Debug  )  [virtual]
 

Gives debug output of the game status.

Reimplemented from KGameNetwork.

Definition at line 1241 of file kgame.cpp.

References KGameNetwork::Debug(), gameStatus(), maxPlayers(), KGamePrivate::mInactivePlayerList, KGamePrivate::mUniquePlayerNumber, and playerCount().

Here is the call graph for this function:

void KGame::deleteInactivePlayers  )  [protected]
 

Definition at line 151 of file kgame.cpp.

References KGamePrivate::mInactivePlayerList.

Referenced by reset().

void KGame::deletePlayers  )  [protected]
 

Definition at line 138 of file kgame.cpp.

References KGamePrivate::mPlayerList.

Referenced by reset().

void KGame::emitSignal KGamePropertyBase me  )  [protected, slot]
 

Called by KGamePropertyHandler only! Internal function!

Definition at line 1440 of file kgame.cpp.

References signalPropertyChanged().

Referenced by KGame().

KPlayer * KGame::findPlayer Q_UINT32  id  )  const
 

Returns the player object for a given player id.

Parameters:
id Player id
Returns:
player object

Definition at line 386 of file kgame.cpp.

References KGamePrivate::mPlayerList.

Referenced by KGameChat::addMessage(), loadPlayer(), networkTransmission(), KGameDebugDialog::removePlayer(), and KGameDebugDialog::slotUpdatePlayerData().

KGamePropertyBase * KGame::findProperty int  id  )  const
 

This function allows to find the pointer to a player property when you know it's id.

Definition at line 1445 of file kgame.cpp.

References KGamePropertyHandler::find(), and KGamePrivate::mProperties.

Here is the call graph for this function:

KGameSequence * KGame::gameSequence  )  const
 

Returns:
The KGameSequence object that is currently in use.
See also:
setGameSequence

Definition at line 809 of file kgame.cpp.

References KGamePrivate::mGameSequence.

Referenced by checkGameOver(), loadgame(), nextPlayer(), playerInputFinished(), and prepareNext().

int KGame::gameStatus  )  const
 

returns the game status, ie running,pause,ended,.

..

Returns:
game status

Definition at line 683 of file kgame.cpp.

References KGamePrivate::mGameStatus, and KGameProperty< type >::value().

Referenced by Debug(), slotClientDisconnected(), slotServerDisconnected(), KGameDebugDialog::slotUpdateGameData(), and systemRemovePlayer().

Here is the call graph for this function:

bool KGame::inactivatePlayer KPlayer player  ) 
 

sends inactivate player: internal use only?

Definition at line 582 of file kgame.cpp.

References KPlayer::id(), KGameMessage::IdInactivatePlayer, policy(), PolicyClean, PolicyDirty, PolicyLocal, KGameNetwork::sendSystemMessage(), and systemInactivatePlayer().

Here is the call graph for this function:

const KGame::KGamePlayerList * KGame::inactivePlayerList  )  const
 

The same as inactivePlayerList but returns a const pointer.

Definition at line 696 of file kgame.cpp.

References KGamePrivate::mInactivePlayerList.

KGame::KGamePlayerList * KGame::inactivePlayerList  ) 
 

Returns a list of all inactive players.

Returns:
the list of players

Definition at line 693 of file kgame.cpp.

References KGamePrivate::mInactivePlayerList.

bool KGame::isRunning  )  const
 

Is the game running.

Returns:
true/false

Definition at line 686 of file kgame.cpp.

References KGamePrivate::mGameStatus, Run, and KGameProperty< type >::value().

Referenced by sendPlayerInput(), KGameDebugDialog::slotUpdateGameData(), and systemPlayerInput().

Here is the call graph for this function:

bool KGame::load QString  filename,
bool  reset = true
[virtual]
 

Same as above function but with different parameters.

Parameters:
filename - the filename of the file to be opened
reset - shall the game be reset before loading
Returns:
true?

Definition at line 162 of file kgame.cpp.

bool KGame::load QDataStream &  stream,
bool  reset = true
[virtual]
 

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.

Parameters:
stream a data stream where you can stream the game from
reset - shall the game be reset before loading
Returns:
true?

Definition at line 179 of file kgame.cpp.

References loadgame().

Here is the call graph for this function:

bool KGame::loadgame QDataStream &  stream,
bool  network,
bool  reset
[protected, virtual]
 

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.

Parameters:
stream a data stream where you can stream the game from
network is it a network call -> make players virtual
reset shall the game be reset before loading
Returns:
true?

Definition at line 182 of file kgame.cpp.

References KGameNetwork::cookie(), KPlayer::dataHandler(), dataHandler(), gameSequence(), KGamePropertyHandler::lockDirectEmit(), KGamePrivate::mRandom, KGamePrivate::mUniquePlayerNumber, playerList(), reset(), KGameSequence::setCurrentPlayer(), and signalLoadError().

Referenced by load().

Here is the call graph for this function:

KPlayer * KGame::loadPlayer QDataStream &  stream,
bool  isvirtual = false
[protected]
 

Load the player list from a stream.

Used for network game and load/save. Can be overwritten if you know what you are doing

Parameters:
stream is the stream to save the player to
isvirtual will set the virtual flag true/false

Definition at line 354 of file kgame.cpp.

References createPlayer(), findPlayer(), KPlayer::load(), and KPlayer::setVirtual().

Here is the call graph for this function:

int KGame::maxPlayers  )  const
 

What is the maximal number of players?

Returns:
maximal number of players

Definition at line 677 of file kgame.cpp.

References KGamePrivate::mMaxPlayer, and KGameProperty< type >::value().

Referenced by addPlayer(), Debug(), and KGameDebugDialog::slotUpdateGameData().

Here is the call graph for this function:

uint KGame::minPlayers  )  const
 

What is the minimal number of players?

Returns:
minimal number of players

Definition at line 674 of file kgame.cpp.

References KGamePrivate::mMinPlayer, and KGameProperty< type >::value().

Referenced by setGameStatus(), KGameDebugDialog::slotUpdateGameData(), and systemRemovePlayer().

Here is the call graph for this function:

void KGame::negotiateNetworkGame Q_UINT32  clientID  )  [protected, virtual]
 

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.

Parameters:
clientID The ID of the message client which has connected

Definition at line 1376 of file kgame.cpp.

References KGameNetwork::cookie(), KGameMessage::IdSetupGame, KGameNetwork::isAdmin(), KGameNetwork::sendSystemMessage(), and KGameMessage::version().

Referenced by slotClientConnected().

Here is the call graph for this function:

void KGame::networkTransmission QDataStream &  stream,
int  msgid,
Q_UINT32  receiver,
Q_UINT32  sender,
Q_UINT32  clientID
[virtual]
 

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.

Parameters:
msgid Specifies the kind of the message. See messages.txt for further information
stream The message that is being sent
receiver The is of the player this message is for. 0 For broadcast.
sender 
clientID the client from which we received the transmission - hardly used

Implements KGameNetwork.

Definition at line 843 of file kgame.cpp.

References findPlayer(), KPlayer::isActive(), KGameMessage::isPlayer(), KPlayer::networkTransmission(), and signalMessageUpdate().

Here is the call graph for this function:

virtual void KGame::newPlayersJoin KGamePlayerList oldplayer,
KGamePlayerList newplayer,
QValueList< int > &  inactivate
[inline, protected, virtual]
 

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());

Parameters:
oldplayer - the list of the network players
newplayer - the list of the client players
inactivate - the value list of ids to be deactivated

Definition at line 753 of file kgame.h.

KPlayer * KGame::nextPlayer KPlayer last,
bool  exclusive = true
[virtual]
 

Deprecated:
Use KGameSequence::nextPlayer instead

Definition at line 823 of file kgame.cpp.

References gameSequence(), and KGameSequence::nextPlayer().

Referenced by prepareNext().

Here is the call graph for this function:

uint KGame::playerCount  )  const
 

Returns how many players are plugged into the game.

Returns:
number of players

Definition at line 680 of file kgame.cpp.

References KGamePrivate::mPlayerList.

Referenced by addPlayer(), Debug(), setGameStatus(), KGameDebugDialog::slotUpdateGameData(), and systemRemovePlayer().

void KGame::playerDeleted KPlayer player  ) 
 

Called by the destructor of KPlayer to remove itself from the game.

Definition at line 492 of file kgame.cpp.

References KPlayer::id(), KGameMessage::IdRemovePlayer, KPlayer::isVirtual(), policy(), PolicyClean, PolicyDirty, PolicyLocal, KGameNetwork::sendSystemMessage(), and systemRemovePlayer().

Referenced by KPlayer::~KPlayer().

Here is the call graph for this function:

virtual bool KGame::playerInput QDataStream &  msg,
KPlayer player
[protected, pure virtual]
 

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;
 }

Parameters:
msg the move message
player the player who did the move
Returns:
true - input ready, false: input manual

Referenced by systemPlayerInput().

KPlayer * KGame::playerInputFinished KPlayer player  )  [protected]
 

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.

Returns:
the current player

Definition at line 758 of file kgame.cpp.

References KPlayer::asyncInput(), checkGameOver(), End, gameSequence(), KPlayer::id(), prepareNext(), KGameSequence::setCurrentPlayer(), setGameStatus(), KPlayer::setTurn(), and signalGameOver().

Referenced by systemPlayerInput().

Here is the call graph for this function:

const KGame::KGamePlayerList * KGame::playerList  )  const
 

The same as playerList but returns a const pointer.

Definition at line 702 of file kgame.cpp.

References KGamePrivate::mPlayerList.

KGame::KGamePlayerList * KGame::playerList  ) 
 

Returns a list of all active players.

Returns:
the list of players

Definition at line 699 of file kgame.cpp.

References KGamePrivate::mPlayerList.

Referenced by loadgame(), KGameSequence::nextPlayer(), savegame(), savePlayers(), KGameDialogConnectionConfig::setKGame(), KGameDebugDialog::setKGame(), KGameChat::setKGame(), and KPlayer::setTurn().

KGame::GamePolicy KGame::policy  )  const
 

Returns:
The default policy of the property

Definition at line 1448 of file kgame.cpp.

References KGamePrivate::mPolicy.

Referenced by activatePlayer(), inactivatePlayer(), playerDeleted(), and savegame().

void KGame::prepareNext  )  [protected, virtual, slot]
 

Deprecated:
Use KGameSequence::prepareNext() instead

Definition at line 814 of file kgame.cpp.

References gameSequence(), and nextPlayer().

Referenced by playerInputFinished().

KRandomSequence * KGame::random  )  const
 

Returns a pointer to the game's KRandomSequence.

This sequence is identical for all network players!

Returns:
KRandomSequence pointer

Definition at line 705 of file kgame.cpp.

References KGamePrivate::mRandom.

bool KGame::removePlayer KPlayer player  )  [inline]
 

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.

Definition at line 222 of file kgame.h.

Referenced by KGameDialogConnectionConfig::slotKickPlayerOut().

bool KGame::reset  )  [virtual]
 

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

Returns:
true on success

Definition at line 131 of file kgame.cpp.

References deleteInactivePlayers(), and deletePlayers().

Referenced by loadgame(), and ~KGame().

Here is the call graph for this function:

bool KGame::save QString  filename,
bool  saveplayers = true
[virtual]
 

Same as above function but with different parameters.

Parameters:
filename the filename of the file to be saved
saveplayers If true then all players wil be saved too
Returns:
true?

Definition at line 267 of file kgame.cpp.

bool KGame::save QDataStream &  stream,
bool  saveplayers = true
[virtual]
 

Save a game to a file OR to network.

Otherwise the same as the load function

Parameters:
stream a data stream to load the game from
saveplayers If true then all players wil be saved too
Returns:
true?

Definition at line 284 of file kgame.cpp.

References savegame().

Here is the call graph for this function:

bool KGame::savegame QDataStream &  stream,
bool  network,
bool  saveplayers
[protected, virtual]
 

Save a game, to file OR network.

Internal.

Parameters:
stream a data stream where you can stream the game from
network is it a call from the network or from a file (unused but informative)
saveplayers shall the players be saved too (should be TRUE)
Returns:
true?

Definition at line 287 of file kgame.cpp.

References KGameNetwork::cookie(), dataHandler(), KGAME_LOAD_COOKIE, KGamePrivate::mRandom, KGamePrivate::mUniquePlayerNumber, playerList(), policy(), KGamePropertyHandler::save(), savePlayers(), signalSave(), and signalSavePrePlayers().

Referenced by save().

Here is the call graph for this function:

void KGame::savePlayer QDataStream &  stream,
KPlayer player
[protected]
 

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

Parameters:
stream is the stream to add the player
player The player to add

Definition at line 323 of file kgame.cpp.

References KPlayer::calcIOValue(), KPlayer::id(), KPlayer::rtti(), and KPlayer::save().

Referenced by savePlayers().

Here is the call graph for this function:

void KGame::savePlayers QDataStream &  stream,
KGamePlayerList list = 0
[protected]
 

Save the player list to a stream.

Used for network game and load/save. Can be overwritten if you know what you are doing

Parameters:
stream is the stream to save the player ot
list the optional list is the player list to be saved, default is playerList()

Definition at line 332 of file kgame.cpp.

References playerList(), and savePlayer().

Referenced by savegame().

Here is the call graph for this function:

bool KGame::sendGroupMessage const QString &  msg,
int  msgid,
Q_UINT32  sender,
const QString &  group
 

Definition at line 1417 of file kgame.cpp.

References sendGroupMessage().

Here is the call graph for this function:

bool KGame::sendGroupMessage int  msg,
int  msgid,
Q_UINT32  sender,
const QString &  group
 

bool KGame::sendGroupMessage const QDataStream &  msg,
int  msgid,
Q_UINT32  sender,
const QString &  group
 

Definition at line 1414 of file kgame.cpp.

References sendGroupMessage().

Here is the call graph for this function:

bool KGame::sendGroupMessage const QByteArray &  msg,
int  msgid,
Q_UINT32  sender,
const QString &  group
 

See KGameNetwork::sendMessage.

Send a network message msg with a given message ID msgid to all players of a given group (see KPlayer::group)

Parameters:
msg the message which will be send. See messages.txt for contents
msgid an id for this message
sender the id of the sender
group the group of the receivers
Returns:
true if worked

Definition at line 1399 of file kgame.cpp.

References KPlayer::group(), KPlayer::id(), KGamePrivate::mPlayerList, and KGameNetwork::sendMessage().

Referenced by KGameChat::returnPressed(), and sendGroupMessage().

Here is the call graph for this function:

bool KGame::sendPlayerInput QDataStream &  msg,
KPlayer player,
Q_UINT32  sender = 0
[virtual]
 

Called by KPlayer to send a player input to the KMessageServer.

Definition at line 709 of file kgame.cpp.

References KPlayer::id(), KGameMessage::IdPlayerInput, isRunning(), and KGameNetwork::sendSystemMessage().

Referenced by KPlayer::forwardInput().

Here is the call graph for this function:

bool KGame::sendPlayerProperty int  msgid,
QDataStream &  s,
Q_UINT32  playerId
 

This is called by KPlayer::sendProperty only! Internal function!

Definition at line 1428 of file kgame.cpp.

References KGameNetwork::sendSystemMessage().

Referenced by KPlayer::sendProperty().

Here is the call graph for this function:

void KGame::sendProperty int  msgid,
QDataStream &  stream,
bool *  sent
[protected, slot]
 

Called by KGamePropertyHandler only! Internal function!

Definition at line 1431 of file kgame.cpp.

References KGameNetwork::sendSystemMessage().

Referenced by KGame().

void KGame::setGameSequence KGameSequence sequence  ) 
 

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.

Parameters:
sequence The new game sequence object. KGame takes ownership and will delete it on destruction!

Definition at line 799 of file kgame.cpp.

References KGamePrivate::mGameSequence, and KGameSequence::setGame().

Referenced by KGame().

Here is the call graph for this function:

void KGame::setGameStatus int  status  ) 
 

sets the game status

Parameters:
status the new status

Definition at line 832 of file kgame.cpp.

References KGamePrivate::mGameStatus, minPlayers(), Pause, playerCount(), and Run.

Referenced by playerInputFinished(), and systemRemovePlayer().

Here is the call graph for this function:

void KGame::setMaxPlayers uint  maxnumber  ) 
 

Set the maximal number of players.

After this is reached no more players can be added. You must be ADMIN to call this (

See also:
isAdmin).
Parameters:
maxnumber maximal number of players

Definition at line 668 of file kgame.cpp.

References KGameProperty< type >::changeValue(), KGameNetwork::isAdmin(), and KGamePrivate::mMaxPlayer.

Here is the call graph for this function:

void KGame::setMinPlayers uint  minnumber  ) 
 

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)!

Parameters:
minnumber minimal number of players

Definition at line 671 of file kgame.cpp.

References KGameProperty< type >::changeValue(), KGameNetwork::isAdmin(), and KGamePrivate::mMinPlayer.

Here is the call graph for this function:

void KGame::setPolicy GamePolicy  p,
bool  recursive = true
 

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.

Definition at line 1452 of file kgame.cpp.

References dataHandler(), KGamePrivate::mInactivePlayerList, KGamePrivate::mPlayerList, KGamePrivate::mPolicy, and KGamePropertyHandler::setPolicy().

Here is the call graph for this function:

void KGame::signalClientJoinedGame Q_UINT32  clientid,
KGame me
[signal]
 

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
Parameters:
clientid - The id of the new client
me - our game pointer

void KGame::signalClientLeftGame int  clientID,
int  oldgamestatus,
KGame me
[signal]
 

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.

Parameters:
clientID - 0:server left, otherwise the client who left
oldgamestatus - the gamestatus before the loss
me - our game pointer

void KGame::signalGameOver int  status,
KPlayer current,
KGame me
[signal]
 

Is emitted after a call to gameOver() returns a non zero return code.

This code is forwarded to this signal as 'status'.

Parameters:
status the return code of gameOver()
current the player who did the last move
me a pointer to the KGame object

Referenced by playerInputFinished().

void KGame::signalLoad QDataStream &  stream  )  [signal]
 

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.

Parameters:
stream the load stream

void KGame::signalLoadError QDataStream &  stream,
bool  network,
int  cookie,
bool &  result
[signal]
 

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.

Parameters:
stream - the load stream
network - true if this is a network connect. False for load game
cookie - the saved cookie. It differs from KGame::cookie()
result - set this to true if you managed to load the game

Referenced by loadgame().

void KGame::signalLoadPrePlayers QDataStream &  stream  )  [signal]
 

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.

Parameters:
stream the load stream

void KGame::signalMessageUpdate int  msgid,
Q_UINT32  receiver,
Q_UINT32  sender
[signal]
 

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...

Referenced by networkTransmission().

void KGame::signalNetworkData int  msgid,
const QByteArray &  buffer,
Q_UINT32  receiver,
Q_UINT32  sender
[signal]
 

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.

void KGame::signalPlayerJoinedGame KPlayer player  )  [signal]
 

a player joined the game

Parameters:
player the player who joined the game

void KGame::signalPlayerLeftGame KPlayer player  )  [signal]
 

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.

Parameters:
player the player who left the game

void KGame::signalPropertyChanged KGamePropertyBase property,
KGame me
[signal]
 

This signal is emmited if a player property changes its value and the property is set to notify this change.

Referenced by emitSignal().

void KGame::signalReplacePlayerIO KPlayer player,
bool *  remove
[signal]
 

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.

Parameters:
player The player that is about to be removed. Add your new KGameIO here - but only on one client!
remove Set this to FALSE if you don't want this player to be removed completely.

void KGame::signalSave QDataStream &  stream  )  [signal]
 

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.

Parameters:
stream the save stream

Referenced by savegame().

void KGame::signalSavePrePlayers QDataStream &  stream  )  [signal]
 

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

Parameters:
stream the save stream

Referenced by savegame().

void KGame::slotClientConnected Q_UINT32  clientId  )  [protected, slot]
 

Calls negotiateNetworkGame() See KGameNetwork::signalClientConnected.

Definition at line 1254 of file kgame.cpp.

References KGameNetwork::isAdmin(), and negotiateNetworkGame().

Referenced by KGame().

void KGame::slotClientDisconnected Q_UINT32  clientId,
bool  broken
[protected, slot]
 

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.

Parameters:
clientId The client the connection has been lost to
broken (ignore this - not used)

Definition at line 1325 of file kgame.cpp.

References gameStatus(), KPlayer::id(), and KGamePrivate::mPlayerList.

Referenced by KGame().

void KGame::slotServerDisconnected  )  [protected, slot]
 

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.

Definition at line 1262 of file kgame.cpp.

References KGameNetwork::gameId(), gameStatus(), KPlayer::id(), KGamePrivate::mInactivePlayerList, and KGamePrivate::mPlayerList.

Referenced by KGame().

void KGame::syncRandom  )  [protected]
 

syncronise the random numbers with all network clients not used by KGame - if it should be kept then as public method

Definition at line 1234 of file kgame.cpp.

References KGameMessage::IdSyncRandom, KGamePrivate::mRandom, and KGameNetwork::sendSystemMessage().

Here is the call graph for this function:

bool KGame::systemActivatePlayer KPlayer player  )  [protected]
 

activates player.

Use activatePlayer instead!

Definition at line 648 of file kgame.cpp.

References KPlayer::isActive().

Referenced by activatePlayer().

Here is the call graph for this function:

void KGame::systemAddPlayer KPlayer newplayer  )  [protected]
 

Finally adds a player to the game and therefore to the list.

Definition at line 460 of file kgame.cpp.

References KPlayer::id().

Here is the call graph for this function:

bool KGame::systemInactivatePlayer KPlayer player  )  [protected]
 

inactivates player.

Use inactivatePlayer instead!

Definition at line 602 of file kgame.cpp.

References KPlayer::isActive().

Referenced by inactivatePlayer().

Here is the call graph for this function:

bool KGame::systemPlayerInput QDataStream &  msg,
KPlayer player,
Q_UINT32  sender = 0
[virtual]
 

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.

Definition at line 727 of file kgame.cpp.

References KPlayer::asyncInput(), isRunning(), playerInput(), playerInputFinished(), and KPlayer::setTurn().

Referenced by KPlayer::forwardInput().

Here is the call graph for this function:

void KGame::systemRemovePlayer KPlayer player,
bool  deleteit
[protected]
 

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.

Definition at line 532 of file kgame.cpp.

References gameStatus(), KPlayer::id(), minPlayers(), Pause, playerCount(), Run, and setGameStatus().

Referenced by playerDeleted().

Here is the call graph for this function:


The documentation for this class was generated from the following files:
Generated on Wed Aug 23 18:05:01 2006 for libkdegames by  doxygen 1.4.6