KDEGames

kgamemessage.cpp
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
4 SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kgamemessage.h"
10
11// KF
12#include <KLocalizedString>
13
14#define MESSAGE_VERSION 2
15
16quint32 KGameMessage::createPlayerId(int oldplayerid, quint32 gameid)
17{
18 int p;
19 p = oldplayerid & 0x3ff; // remove game id
20 p |= (gameid << 10);
21 return p;
22}
23
24int KGameMessage::rawPlayerId(quint32 playerid)
25{
26 return playerid & 0x03ff;
27}
28
29quint32 KGameMessage::rawGameId(quint32 playerid)
30{
31 return (playerid & 0xfc00) >> 10;
32}
33
34bool KGameMessage::isPlayer(quint32 msgid)
35{
36 if (msgid & 0xfc00) {
37 return true;
38 } else {
39 return false;
40 }
41}
42
43bool KGameMessage::isGame(quint32 msgid)
44{
45 return !isPlayer(msgid);
46}
47
48void KGameMessage::createHeader(QDataStream &msg, quint32 sender, quint32 receiver, int msgid)
49{
50 msg << (qint16)sender << (qint16)receiver << (qint16)msgid;
51}
52
53void KGameMessage::extractHeader(QDataStream &msg, quint32 &sender, quint32 &receiver, int &msgid)
54{
55 qint16 d3, d4, d5;
56 msg >> d3 >> d4 >> d5;
57 sender = d3;
58 receiver = d4;
59 msgid = d5;
60}
61
63{
64 msg << (qint16)id;
65}
66
68{
69 qint16 d1;
70 msg >> d1;
71 id = d1;
72}
73
74void KGameMessage::createPropertyCommand(QDataStream &msg, int cmdid, int pid, int cmd)
75{
76 createPropertyHeader(msg, cmdid);
77 msg << (qint16)pid;
78 msg << (qint8)cmd;
79}
80
82{
83 qint16 d1;
84 qint8 d2;
85 msg >> d1 >> d2;
86 pid = d1;
87 cmd = d2;
88}
89
91{
92 return MESSAGE_VERSION;
93}
94
96{
97 // this should contain all KGameMessage::GameMessageIds
98 // feel free to add missing ones, to remove obsolete one and even feel free to
99 // let it be ;-)
100 switch (msgid) {
101 case KGameMessage::IdSetupGame:
102 return i18n("Setup Game");
103 case KGameMessage::IdSetupGameContinue:
104 return i18n("Setup Game Continue");
105 case KGameMessage::IdGameLoad:
106 return i18n("Load Game");
107 case KGameMessage::IdGameConnected:
108 return i18n("Client game connected");
109 case KGameMessage::IdGameSetupDone:
110 return i18n("Game setup done");
111 case KGameMessage::IdSyncRandom:
112 return i18n("Synchronize Random");
113 case KGameMessage::IdDisconnect:
114 return i18n("Disconnect");
115 case KGameMessage::IdPlayerProperty:
116 return i18n("Player Property");
117 case KGameMessage::IdGameProperty:
118 return i18n("Game Property");
119 case KGameMessage::IdAddPlayer:
120 return i18n("Add Player");
121 case KGameMessage::IdRemovePlayer:
122 return i18n("Remove Player");
123 case KGameMessage::IdActivatePlayer:
124 return i18n("Activate Player");
125 case KGameMessage::IdInactivatePlayer:
126 return i18n("Inactivate Player");
127 case KGameMessage::IdTurn:
128 return i18n("Id Turn");
129 case KGameMessage::IdError:
130 return i18n("Error Message");
131 case KGameMessage::IdPlayerInput:
132 return i18n("Player Input");
133 case KGameMessage::IdIOAdded:
134 return i18n("An IO was added");
135 case KGameMessage::IdProcessQuery:
136 return i18n("Process Query");
137 case KGameMessage::IdPlayerId:
138 return i18n("Player ID");
139 case KGameMessage::IdUser: // IdUser must be unknown for use, too!
140 default:
141 return QString();
142 }
143}
static void extractPropertyHeader(QDataStream &msg, int &id)
Retrieves the property id from a property message header.
static bool isPlayer(quint32 id)
Checks whether a message receiver/sender is a player.
static void extractHeader(QDataStream &msg, quint32 &sender, quint32 &receiver, int &msgid)
Retrieves the information like cookie,sender,receiver,... from a message header.
static void extractPropertyCommand(QDataStream &msg, int &pid, int &cmd)
Retrieves the property id from a property message header.
static void createHeader(QDataStream &msg, quint32 sender, quint32 receiver, int msgid)
Creates a message header given cookie,sender,receiver,...
static int rawPlayerId(quint32 playerid)
Returns the raw playerid, that is, a id which does not contain the game number encoded in it.
static QString messageId2Text(int msgid)
This function takes a GameMessageIds as argument and returns a suitable string for it.
static int version()
static bool isGame(quint32 id)
Checks whether the sender/receiver of a message is a game.
static void createPropertyHeader(QDataStream &msg, int id)
Creates a property header given the property id.
static void createPropertyCommand(QDataStream &msg, int cmdid, int pid, int cmd)
Creates a property header given the property id.
static quint32 rawGameId(quint32 playerid)
Returns the raw game id, that is, the game id the player belongs to.
static quint32 createPlayerId(int player, quint32 game)
Creates a fully qualified player ID which contains the original player id in the lower bits and the g...
QString i18n(const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:13:43 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.