KDEGames

kgamemessage.cpp
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 #include "kgamemessage.h"
22 
23 #include <KLocalizedString>
24 
25 #define MESSAGE_VERSION 2
26 
27 quint32 KGameMessage::createPlayerId(int oldplayerid,quint32 gameid)
28 {
29  int p;
30  p = oldplayerid & 0x3ff; // remove game id
31  p |= (gameid << 10);
32  return p;
33 }
34 
35 int KGameMessage::rawPlayerId(quint32 playerid)
36 {
37  return playerid & 0x03ff;
38 }
39 
40 quint32 KGameMessage::rawGameId(quint32 playerid)
41 {
42  return (playerid & 0xfc00) >> 10;
43 }
44 
45 bool KGameMessage::isPlayer(quint32 msgid)
46 {
47  if (msgid & 0xfc00) {
48  return true;
49  } else {
50  return false;
51  }
52 }
53 
54 bool KGameMessage::isGame(quint32 msgid)
55 {
56  return !isPlayer(msgid);
57 }
58 
59 
60 void KGameMessage::createHeader(QDataStream &msg,quint32 sender,quint32 receiver,int msgid)
61 {
62  msg << (qint16)sender << (qint16)receiver << (qint16)msgid;
63 }
64 
65 void KGameMessage::extractHeader(QDataStream &msg,quint32 &sender,quint32 &receiver,int &msgid)
66 {
67  qint16 d3,d4,d5;
68  msg >> d3 >> d4 >> d5;
69  sender=d3;receiver=d4;msgid=d5;
70 }
71 
72 void KGameMessage::createPropertyHeader(QDataStream &msg,int id)
73 {
74  msg << (qint16)id;
75 }
76 
77 void KGameMessage::extractPropertyHeader(QDataStream &msg,int &id)
78 {
79  qint16 d1;
80  msg >> d1;
81  id=d1;
82 }
83 
84 void KGameMessage::createPropertyCommand(QDataStream &msg,int cmdid,int pid,int cmd)
85 {
86  createPropertyHeader(msg,cmdid);
87  msg << (qint16)pid ;
88  msg << (qint8)cmd ;
89 }
90 
91 void KGameMessage::extractPropertyCommand(QDataStream &msg,int &pid,int &cmd)
92 {
93  qint16 d1;
94  qint8 d2;
95  msg >> d1 >> d2;
96  pid=d1;
97  cmd=d2;
98 }
99 
100 int KGameMessage::version()
101 {
102  return MESSAGE_VERSION;
103 }
104 
105 QString KGameMessage::messageId2Text(int msgid)
106 {
107 // this should contain all KGameMessage::GameMessageIds
108 // feel free to add missing ones, to remove obsolete one and even feel free to
109 // let it be ;-)
110  switch (msgid) {
111  case KGameMessage::IdSetupGame:
112  return i18n("Setup Game");
113  case KGameMessage::IdSetupGameContinue:
114  return i18n("Setup Game Continue");
115  case KGameMessage::IdGameLoad:
116  return i18n("Load Game");
117  case KGameMessage::IdGameConnected:
118  return i18n("Client game connected");
119  case KGameMessage::IdGameSetupDone:
120  return i18n("Game setup done");
121  case KGameMessage::IdSyncRandom:
122  return i18n("Synchronize Random");
123  case KGameMessage::IdDisconnect:
124  return i18n("Disconnect");
125  case KGameMessage::IdPlayerProperty:
126  return i18n("Player Property");
127  case KGameMessage::IdGameProperty:
128  return i18n("Game Property");
129  case KGameMessage::IdAddPlayer:
130  return i18n("Add Player");
131  case KGameMessage::IdRemovePlayer:
132  return i18n("Remove Player");
133  case KGameMessage::IdActivatePlayer:
134  return i18n("Activate Player");
135  case KGameMessage::IdInactivatePlayer:
136  return i18n("Inactivate Player");
137  case KGameMessage::IdTurn:
138  return i18n("Id Turn");
139  case KGameMessage::IdError:
140  return i18n("Error Message");
141  case KGameMessage::IdPlayerInput:
142  return i18n("Player Input");
143  case KGameMessage::IdIOAdded:
144  return i18n("An IO was added");
145  case KGameMessage::IdProcessQuery:
146  return i18n("Process Query");
147  case KGameMessage::IdPlayerId:
148  return i18n("Player ID");
149  case KGameMessage::IdUser: // IdUser must be unknown for use, too!
150  default:
151  return QString();
152  }
153 }
QString i18n(const char *text, const TYPE &arg...)
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Nov 30 2020 22:37:54 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.