• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

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

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.9.4
This website is maintained by Timothy Pearson.