File indexing completed on 2024-05-26 04:09:56

0001 /*
0002     This file is part of the KDE games library
0003     SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
0004     SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
0005 
0006     SPDX-License-Identifier: LGPL-2.0-only
0007 */
0008 
0009 #ifndef __KGAMEMESSAGE_H_
0010 #define __KGAMEMESSAGE_H_
0011 
0012 // own
0013 #include "kdegamesprivate_export.h"
0014 // Qt
0015 #include <QDataStream>
0016 
0017 /**
0018  * \class KGameMessage kgamemessage.h <KGame/KGameMessage>
0019  *
0020  * Namespace-like class for message-related static functions.
0021  */
0022 class KDEGAMESPRIVATE_EXPORT KGameMessage
0023 {
0024 public:
0025     /**
0026      * Creates a fully qualified player ID which contains the original
0027      * player id in the lower bits and the game number in the higher bits.
0028      * Do not rely on the exact bit positions as they are internal.
0029      *
0030      * See also @ref rawPlayerId and @ref rawGameId which are the inverse
0031      * operations
0032      *
0033      * @param player The player id - can include a gameid (will get removed)
0034      * @param game The game id (<64). 0 For broadcast.
0035      * @return The new player id
0036      */
0037     static quint32 createPlayerId(int player, quint32 game);
0038 
0039     /**
0040      * Returns the raw playerid, that is, a id which does not
0041      * contain the game number encoded in it. See also @ref createPlayerId which
0042      * is the inverse operation.
0043      *
0044      * @param playerid The player id
0045      * @return The raw player id
0046      */
0047     static int rawPlayerId(quint32 playerid);
0048 
0049     /**
0050      * Returns the raw game id, that is, the game id the player
0051      * belongs to. Se also @ref createPlayerId which is the inverse operation.
0052      *
0053      * @param playerid The player id
0054      * @return The raw game id
0055      */
0056     static quint32 rawGameId(quint32 playerid);
0057 
0058     /**
0059      * Checks whether a message receiver/sender is a player
0060      *
0061      * @param id The ID of the sender/receiver
0062      * @return true/false
0063      */
0064     static bool isPlayer(quint32 id);
0065 
0066     /**
0067      * Checks whether the sender/receiver of a message is a game
0068      *
0069      * @param id The ID of the sender/receiver
0070      * @return true/false
0071      */
0072     static bool isGame(quint32 id);
0073 
0074     /**
0075      * Creates a message header given cookie,sender,receiver,...
0076      *
0077      * Also puts "hidden" header into the stream which are used by KGameClient
0078      * (message length and magic cookie). If you don't need them remove them
0079      * with dropExternalHeader
0080      */
0081     static void createHeader(QDataStream &msg, quint32 sender, quint32 receiver, int msgid);
0082 
0083     /**
0084      * Retrieves the information like cookie,sender,receiver,... from a message header
0085      *
0086      * Note that it could be necessary to call dropExternalHeader first
0087      */
0088     static void extractHeader(QDataStream &msg, quint32 &sender, quint32 &receiver, int &msgid);
0089 
0090     /**
0091      * Creates a property header  given the property id
0092      */
0093     static void createPropertyHeader(QDataStream &msg, int id);
0094 
0095     /**
0096      * Retrieves the property id from a property message header
0097      */
0098     static void extractPropertyHeader(QDataStream &msg, int &id);
0099 
0100     /**
0101      * Creates a property header given the property id
0102      */
0103     static void createPropertyCommand(QDataStream &msg, int cmdid, int pid, int cmd);
0104 
0105     /**
0106      * Retrieves the property id from a property message header
0107      */
0108     static void extractPropertyCommand(QDataStream &msg, int &pid, int &cmd);
0109 
0110     /**
0111      * @return Version of the network library
0112      */
0113     static int version();
0114 
0115     /**
0116      * This function takes a @ref GameMessageIds as argument and returns a
0117      * suitable string for it. This string can't be used to identify a message
0118      * (as it is i18n'ed) but it can make debugging more easy.
0119      * @return Either a i18n'ed string (the name of the id) or QString() if
0120      * the msgid is unknown
0121      */
0122     static QString messageId2Text(int msgid);
0123 
0124     /**
0125      * Message Ids used inside @ref KGame.
0126      *
0127      * You can use your own custom message Id by adding @p IdUser to it.
0128      */
0129     // please document every new id with a short comment
0130     enum GameMessageIds {
0131         // game init, game load, disconnect, ...
0132         IdSetupGame = 1, // sent to a newly connected player
0133         IdSetupGameContinue = 2, // continue the setup
0134         IdGameLoad = 3, // load/save the game to the client
0135         IdGameConnected = 4, // Client successfully connected to master
0136         IdSyncRandom = 5, // new random seed set - sync games
0137         IdDisconnect = 6, // KGame object disconnects from game
0138         IdGameSetupDone = 7, // New game client is now operational
0139 
0140         // properties
0141         IdPlayerProperty = 20, // a player property changed
0142         IdGameProperty = 21, // a game property changed
0143 
0144         // player management
0145         IdAddPlayer = 30, // add a player
0146         IdRemovePlayer = 31, // the player will be removed
0147         IdActivatePlayer = 32, // Activate a player
0148         IdInactivatePlayer = 33, // Inactivate a player
0149         IdTurn = 34, // Turn to be prepared
0150 
0151         // to-be-categorized
0152         IdError = 100, // an error occurred
0153         IdPlayerInput = 101, // a player input occurred
0154         IdIOAdded = 102, // KGameIO got added to a player...init this IO
0155 
0156         // special ids for computer player
0157         IdProcessQuery = 220, // Process queries data (process only)
0158         IdPlayerId = 221, // PlayerId got changed (process only)
0159 
0160         IdUser = 256 // a user specified message
0161     };
0162 };
0163 
0164 #endif