• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdegames API Reference
  • KDE Home
  • Contact Us
 

libkdegames/libkdegamesprivate/kgame

  • sources
  • kde-4.14
  • kdegames
  • libkdegames
  • libkdegamesprivate
  • kgame
kgamechat.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the KDE games library
3  Copyright (C) 2001-2002 Andreas Beckermann (b_mann@gmx.de)
4  Copyright (C) 2001 Martin Heni (kde at heni-online.de)
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 "kgamechat.h"
22 #include "kgamechat.moc"
23 
24 #include "kgame.h"
25 #include "kplayer.h"
26 #include "kgameproperty.h"
27 #include "kgamemessage.h"
28 
29 #include <klocale.h>
30 #include <kdebug.h>
31 
32 #include <QMap>
33 
34 //FIXME:
35 #define FIRST_ID 2 // first id, that is free of use, aka not defined above
36 
37 class KGameChatPrivate
38 {
39 public:
40  KGameChatPrivate()
41  {
42  mFromPlayer = 0;
43  mGame = 0;
44 
45  mToMyGroup = -1;
46  }
47 
48  KGame* mGame;
49  KPlayer* mFromPlayer;
50  int mMessageId;
51 
52 
53  QMap<int, int> mSendId2PlayerId;
54  int mToMyGroup; // just as the above - but for the group, not for players
55 };
56 
57 KGameChat::KGameChat(KGame* g, int msgid, QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate)
58  : KChatBase(parent, model, delegate),
59  d( new KGameChatPrivate )
60 {
61  init(g, msgid);
62 }
63 
64 KGameChat::KGameChat(KGame* g, int msgid, KPlayer* fromPlayer, QWidget* parent, KChatBaseModel* model, KChatBaseItemDelegate* delegate)
65  : KChatBase(parent,model,delegate),
66  d( new KGameChatPrivate )
67 {
68  init(g, msgid);
69  setFromPlayer(fromPlayer);
70 }
71 
72 KGameChat::KGameChat(QWidget* parent)
73  : KChatBase(parent),
74  d( new KGameChatPrivate )
75 {
76  init(0, -1);
77 }
78 
79 KGameChat::~KGameChat()
80 {
81  kDebug(11001) ;
82  delete d;
83 }
84 
85 void KGameChat::init(KGame* g, int msgId)
86 {
87  kDebug(11001) ;
88  setMessageId(msgId);
89 
90  setKGame(g);
91 }
92 
93 void KGameChat::addMessage(int fromId, const QString& text)
94 {
95  if (!d->mGame) {
96  kWarning(11001) << "no KGame object has been set";
97  addMessage(i18n("Player %1", fromId), text);
98  } else {
99  KPlayer* p = d->mGame->findPlayer(fromId);
100  if (p) {
101  kDebug(11001) << "adding message of player" << p->name() << "id=" << fromId;
102  addMessage(p->name(), text);
103  } else {
104  kWarning(11001) << "Could not find player id" << fromId;
105  addMessage(i18nc("Unknown player", "Unknown"), text);
106  }
107  }
108 }
109 
110 void KGameChat::returnPressed(const QString& text)
111 {
112  if (!d->mFromPlayer) {
113  kWarning(11001) << ": You must set a player first!";
114  return;
115  }
116  if (!d->mGame) {
117  kWarning(11001) << ": You must set a game first!";
118  return;
119  }
120 
121  kDebug(11001) << "from:" << d->mFromPlayer->id() << "==" << d->mFromPlayer->name();
122 
123  int id = sendingEntry();
124 
125  if (isToGroupMessage(id)) {
126  // note: there is currently no support for other groups than the players
127  // group! It might be useful to send to other groups, too
128  QString group = d->mFromPlayer->group();
129  kDebug(11001) << "send to group" << group;
130  int sender = d->mFromPlayer->id();
131  d->mGame->sendGroupMessage(text, messageId(), sender, group);
132 
133  //TODO
134  //AB: this message is never received!! we need to connect to
135  //KPlayer::networkData!!!
136  //TODO
137 
138  } else {
139  int toPlayer = 0;
140  if (!isSendToAllMessage(id) && isToPlayerMessage(id)) {
141  toPlayer = playerId(id);
142  if (toPlayer == -1) {
143  kError(11001) << ": don't know that player "
144  << "- internal ERROR";
145  }
146  }
147  int receiver = toPlayer;
148  int sender = d->mFromPlayer->id();
149  d->mGame->sendMessage(text, messageId(), receiver, sender);
150  }
151 }
152 
153 void KGameChat::setMessageId(int msgid)
154 { d->mMessageId = msgid; }
155 
156 int KGameChat::messageId() const
157 { return d->mMessageId; }
158 
159 bool KGameChat::isSendToAllMessage(int id) const
160 { return (id == KChatBase::SendToAll); }
161 
162 bool KGameChat::isToGroupMessage(int id) const
163 { return (id == d->mToMyGroup); }
164 
165 bool KGameChat::isToPlayerMessage(int id) const
166 {
167 return d->mSendId2PlayerId.contains(id); }
168 
169 QString KGameChat::sendToPlayerEntry(const QString& name) const
170 { return i18n("Send to %1", name); }
171 
172 int KGameChat::playerId(int id) const
173 {
174  if (!isToPlayerMessage(id)) {
175  return -1;
176  }
177 
178  return d->mSendId2PlayerId[id];
179 }
180 
181 int KGameChat::sendingId(int playerId) const
182 {
183  QMap<int, int>::Iterator it;
184  for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
185  if (it.value() == playerId) {
186  return it.key();
187  }
188  }
189  return -1;
190 }
191 
192 QString KGameChat::fromName() const
193 { return d->mFromPlayer ? d->mFromPlayer->name() : QString(); }
194 
195 bool KGameChat::hasPlayer(int id) const
196 {
197  return (sendingId(id) != -1);
198 }
199 
200 void KGameChat::setFromPlayer(KPlayer* p)
201 {
202  if (!p) {
203  kError(11001) << ": NULL player";
204  removeSendingEntry(d->mToMyGroup);
205  d->mFromPlayer = 0;
206  return;
207  }
208  if (d->mFromPlayer) {
209  changeSendingEntry(p->group(), d->mToMyGroup);
210  } else {
211  if (d->mToMyGroup != -1) {
212  kWarning(11001) << "send to my group exists already - removing";
213  removeSendingEntry(d->mToMyGroup);
214  }
215  d->mToMyGroup = nextId();
216  addSendingEntry(i18n("Send to My Group (\"%1\")", p->group()), d->mToMyGroup);
217  }
218  d->mFromPlayer = p;
219  kDebug(11001) << "player=" << p;
220 }
221 
222 
223 void KGameChat::setKGame(KGame* g)
224 {
225  if (d->mGame) {
226  slotUnsetKGame();
227  }
228  kDebug(11001) << "game=" << g;
229  d->mGame = g;
230 
231  if (d->mGame) {
232  connect(d->mGame, SIGNAL(signalPlayerJoinedGame(KPlayer*)),
233  this, SLOT(slotAddPlayer(KPlayer*)));
234  connect(d->mGame, SIGNAL(signalPlayerLeftGame(KPlayer*)),
235  this, SLOT(slotRemovePlayer(KPlayer*)));
236  connect(d->mGame, SIGNAL(signalNetworkData(int,QByteArray,quint32,quint32)),
237  this, SLOT(slotReceiveMessage(int,QByteArray,quint32,quint32)));
238  connect(d->mGame, SIGNAL(destroyed()), this, SLOT(slotUnsetKGame()));
239 
240  QList<KPlayer*> playerList = *d->mGame->playerList();
241  for (int i = 0; i < playerList.count(); i++) {
242  slotAddPlayer(playerList.at(i));
243  }
244  }
245 }
246 
247 KGame* KGameChat::game() const
248 {
249  return d->mGame;
250 }
251 
252 KPlayer* KGameChat::fromPlayer() const
253 {
254  return d->mFromPlayer;
255 }
256 
257 void KGameChat::slotUnsetKGame()
258 {
259 //TODO: test this method!
260 
261  if (!d->mGame) {
262  return;
263  }
264  disconnect(d->mGame, 0, this, 0);
265  removeSendingEntry(d->mToMyGroup);
266  QMap<int, int>::Iterator it;
267  for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
268  removeSendingEntry(it.value());
269  }
270 }
271 
272 void KGameChat::slotAddPlayer(KPlayer* p)
273 {
274  if (!p) {
275  kError(11001) << ": cannot add NULL player";
276  return;
277  }
278  if (hasPlayer(p->id())) {
279  kError(11001) << ": player was added before";
280  return;
281  }
282 
283  int sendingId = nextId();
284  addSendingEntry(comboBoxItem(p->name()), sendingId);
285  d->mSendId2PlayerId.insert(sendingId, p->id());
286  connect(p, SIGNAL(signalPropertyChanged(KGamePropertyBase*,KPlayer*)),
287  this, SLOT(slotPropertyChanged(KGamePropertyBase*,KPlayer*)));
288  connect(p, SIGNAL(signalNetworkData(int,QByteArray,quint32,KPlayer*)),
289  this, SLOT(slotReceivePrivateMessage(int,QByteArray,quint32,KPlayer*)));
290 }
291 
292 void KGameChat::slotRemovePlayer(KPlayer* p)
293 {
294  if (!p) {
295  kError(11001) << ": NULL player";
296  return;
297  }
298  if (!hasPlayer(p->id())) {
299  kError(11001) << ": cannot remove non-existent player";
300  return;
301  }
302 
303  int id = sendingId(p->id());
304  removeSendingEntry(id);
305  p->disconnect(this);
306  d->mSendId2PlayerId.remove(id);
307 }
308 
309 void KGameChat::slotPropertyChanged(KGamePropertyBase* prop, KPlayer* player)
310 {
311  if (prop->id() == KGamePropertyBase::IdName) {
312 // kDebug(11001) << "new Name";
313  changeSendingEntry(player->name(), sendingId(player->id()));
314 /*
315  mCombo->changeItem(comboBoxItem(player->name()), index);
316  */
317  } else if (prop->id() == KGamePropertyBase::IdGroup) {
318  //TODO
319  }
320 }
321 
322 void KGameChat::slotReceivePrivateMessage(int msgid, const QByteArray& buffer, quint32 sender, KPlayer* me)
323 {
324  if (!me || me != fromPlayer()) {
325  kDebug() << "nope - not for us!";
326  return;
327  }
328  slotReceiveMessage(msgid, buffer, me->id(), sender);
329 }
330 
331 void KGameChat::slotReceiveMessage(int msgid, const QByteArray& buffer, quint32 , quint32 sender)
332 {
333  QDataStream msg(buffer);
334  if (msgid != messageId()) {
335  return;
336  }
337 
338  QString text;
339  msg >> text;
340 
341  addMessage(sender, text);
342 }
343 
KGameChat::sendToPlayerEntry
virtual QString sendToPlayerEntry(const QString &name) const
Definition: kgamechat.cpp:169
QWidget
KPlayer::id
quint32 id() const
Returns the id of the player.
Definition: kplayer.cpp:247
KGamePropertyBase::IdName
Definition: kgameproperty.h:54
KGameChat::addMessage
virtual void addMessage(const QString &fromName, const QString &text)
Definition: kgamechat.h:121
KGameChat::fromPlayer
KPlayer * fromPlayer() const
Definition: kgamechat.cpp:252
KGameChat::setKGame
void setKGame(KGame *g)
Set the KGame object for this chat widget.
Definition: kgamechat.cpp:223
KGameChat::isToGroupMessage
bool isToGroupMessage(int id) const
Used to indicate whether a message shall be sent to a group of players.
Definition: kgamechat.cpp:162
KGameChat::slotReceiveMessage
void slotReceiveMessage(int, const QByteArray &, quint32 receiver, quint32 sender)
Definition: kgamechat.cpp:331
kgame.h
KGameChat::messageId
int messageId() const
Definition: kgamechat.cpp:156
QByteArray
QDataStream
KGameChat::isToPlayerMessage
bool isToPlayerMessage(int id) const
Used to indicate whether the message shall be sent to a single player only.
Definition: kgamechat.cpp:165
QList::at
const T & at(int i) const
QMap< int, int >
KChatBase
KGameChat::slotUnsetKGame
void slotUnsetKGame()
Unsets a KGame object that has been set using setKGame before.
Definition: kgamechat.cpp:257
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
KGameChat::returnPressed
virtual void returnPressed(const QString &text)
Definition: kgamechat.cpp:110
KGameChat::setFromPlayer
void setFromPlayer(KPlayer *player)
This sets the fromPlayer to player.
Definition: kgamechat.cpp:200
KGameChat::sendingId
int sendingId(int playerId) const
Definition: kgamechat.cpp:181
QList::count
int count(const T &value) const
KPlayer::name
virtual const QString & name() const
Definition: kplayer.cpp:244
kgameproperty.h
KGameChat::fromName
virtual QString fromName() const
reimplemented from KChatBase
Definition: kgamechat.cpp:192
KPlayer::group
virtual const QString & group() const
Query the group the player belongs to.
Definition: kplayer.cpp:238
KGameChat::slotAddPlayer
void slotAddPlayer(KPlayer *)
Definition: kgamechat.cpp:272
QString
QList< KPlayer * >
kgamemessage.h
KPlayer
Base class for a game player.
Definition: kplayer.h:69
KGameChat::slotReceivePrivateMessage
void slotReceivePrivateMessage(int msgid, const QByteArray &buffer, quint32 sender, KPlayer *me)
Called when KPlayer::signalNetworkData is emitted.
Definition: kgamechat.cpp:322
KGamePropertyBase
Base class of KGameProperty.
Definition: kgameproperty.h:45
KGameChat::slotPropertyChanged
void slotPropertyChanged(KGamePropertyBase *, KPlayer *)
Definition: kgamechat.cpp:309
QMap::key
const Key key(const T &value) const
KGameChat::hasPlayer
bool hasPlayer(int id) const
Definition: kgamechat.cpp:195
kgamechat.h
KGameChat::game
KGame * game() const
Definition: kgamechat.cpp:247
KGameChat::setMessageId
void setMessageId(int msgid)
Change the message id of the chat widget.
Definition: kgamechat.cpp:153
KGameChat::playerId
int playerId(int id) const
Definition: kgamechat.cpp:172
KGame
The main KDE game object.
Definition: kgame.h:60
KGamePropertyBase::IdGroup
Definition: kgameproperty.h:50
kplayer.h
KGameChat::~KGameChat
virtual ~KGameChat()
Definition: kgamechat.cpp:79
KGameChat::isSendToAllMessage
bool isSendToAllMessage(int id) const
Definition: kgamechat.cpp:159
KGameChat::slotRemovePlayer
void slotRemovePlayer(KPlayer *)
Definition: kgamechat.cpp:292
KGameChat::KGameChat
KGameChat(KGame *game, int msgid, KPlayer *fromPlayer, QWidget *parent, KChatBaseModel *model=0, KChatBaseItemDelegate *delegate=0)
Construct a KGame chat widget on game that used msgid for the chat message.
Definition: kgamechat.cpp:64
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:241
QMap::value
const T value(const Key &key) const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:18:54 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdegames/libkdegamesprivate/kgame

Skip menu "libkdegames/libkdegamesprivate/kgame"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdegames API Reference

Skip menu "kdegames API Reference"
  • granatier
  • kapman
  • kblackbox
  • kgoldrunner
  • kigo
  • kmahjongg
  • KShisen
  • ksquares
  • libkdegames
  •   highscore
  •   libkdegamesprivate
  •     kgame
  • libkmahjongg
  • palapeli
  •   libpala

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal