21 #include "kgamechat.h" 22 #include "kgamechat.moc" 26 #include "kgameproperty.h" 27 #include "kgamemessage.h" 29 #include <tdelocale.h> 33 #include <tqintdict.h> 36 #define FIRST_ID 2 // first id, that is free of use, aka not defined above 38 class KGameChatPrivate
54 TQIntDict<KPlayer> mIndex2Player;
56 TQMap<int, int> mSendId2PlayerId;
76 KGameChat::~KGameChat()
78 kdDebug(11001) << k_funcinfo << endl;
82 void KGameChat::init(
KGame* g,
int msgId)
84 kdDebug(11001) << k_funcinfo << endl;
85 d =
new KGameChatPrivate;
91 void KGameChat::addMessage(
int fromId,
const TQString& text)
94 kdWarning(11001) <<
"no KGame object has been set" << endl;
95 addMessage(i18n(
"Player %1").arg(fromId), text);
97 KPlayer* p = d->mGame->findPlayer(fromId);
99 kdDebug(11001) <<
"adding message of player " << p->
name() <<
"id=" << fromId << endl;
100 addMessage(p->
name(), text);
102 kdWarning(11001) <<
"Could not find player id " << fromId << endl;
103 addMessage(i18n(
"Unknown"), text);
110 if (!d->mFromPlayer) {
111 kdWarning(11001) << k_funcinfo <<
": You must set a player first!" << endl;
115 kdWarning(11001) << k_funcinfo <<
": You must set a game first!" << endl;
119 kdDebug(11001) <<
"from: " << d->mFromPlayer->id() <<
"==" << d->mFromPlayer->name() << endl;
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);
140 if (toPlayer == -1) {
141 kdError(11001) << k_funcinfo <<
": don't know that player " 142 <<
"- internal ERROR" << endl;
145 int receiver = toPlayer;
146 int sender = d->mFromPlayer->id();
147 d->mGame->sendMessage(text,
messageId(), receiver, sender);
152 { d->mMessageId = msgid; }
155 {
return d->mMessageId; }
158 {
return (
id == KChatBase::SendToAll); }
161 {
return (
id == d->mToMyGroup); }
165 return d->mSendId2PlayerId.contains(
id); }
168 {
return i18n(
"Send to %1").arg(name); }
176 return d->mSendId2PlayerId[id];
181 TQMap<int, int>::Iterator it;
182 for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
191 {
return d->mFromPlayer ? d->mFromPlayer->name() : TQString(); }
201 kdError(11001) << k_funcinfo <<
": NULL player" << endl;
206 if (d->mFromPlayer) {
209 if (d->mToMyGroup != -1) {
210 kdWarning(11001) <<
"send to my group exists already - removing" << endl;
217 kdDebug(11001) << k_funcinfo <<
" player=" << p << endl;
226 kdDebug(11001) << k_funcinfo <<
" game=" << g << endl;
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()));
238 TQPtrList<KPlayer> playerList = *d->mGame->playerList();
239 for (
int unsigned i = 0; i < playerList.count(); i++) {
240 slotAddPlayer(playerList.at(i));
245 KGame* KGameChat::game()
const 250 KPlayer* KGameChat::fromPlayer()
const 252 return d->mFromPlayer;
262 disconnect(d->mGame, 0,
this, 0);
264 TQMap<int, int>::Iterator it;
265 for (it = d->mSendId2PlayerId.begin(); it != d->mSendId2PlayerId.end(); ++it) {
270 void KGameChat::slotAddPlayer(
KPlayer* p)
273 kdError(11001) << k_funcinfo <<
": cannot add NULL player" << endl;
277 kdError(11001) << k_funcinfo <<
": player was added before" << endl;
283 d->mSendId2PlayerId.insert(sendingId, p->
id());
286 connect(p, TQ_SIGNAL(signalNetworkData(
int,
const TQByteArray&, TQ_UINT32,
KPlayer*)),
290 void KGameChat::slotRemovePlayer(
KPlayer* p)
293 kdError(11001) << k_funcinfo <<
": NULL player" << endl;
297 kdError(11001) << k_funcinfo <<
": cannot remove non-existent player" << endl;
304 d->mSendId2PlayerId.remove(
id);
309 if (prop->
id() == KGamePropertyBase::IdName) {
315 }
else if (prop->
id() == KGamePropertyBase::IdGroup) {
322 if (!me || me != fromPlayer()) {
323 kdDebug() << k_funcinfo <<
"nope - not for us!" << endl;
326 slotReceiveMessage(msgid, buffer, me->
id(), sender);
329 void KGameChat::slotReceiveMessage(
int msgid,
const TQByteArray& buffer, TQ_UINT32 , TQ_UINT32 sender)
331 TQDataStream msg(buffer, IO_ReadOnly);
339 addMessage(sender, text);
virtual TQString sendToPlayerEntry(const TQString &name) const
int sendingId(int playerId) const
void setKGame(KGame *g)
Set the KGame object for this chat widget.
void removeSendingEntry(int id)
Removes the entry with the ID id from the combo box.
The base class for chat widgets.
void slotUnsetKGame()
Unsets a KGame object that has been set using setKGame before.
bool isToGroupMessage(int id) const
Used to indicate whether a message shall be sent to a group of players.
int playerId(int id) const
void setFromPlayer(KPlayer *player)
This sets the fromPlayer to player.
virtual const TQString & group() const
Query the group the player belongs to.
virtual const TQString & name() const
bool isSendToAllMessage(int id) const
TQ_UINT32 id() const
Returns the id of the player.
void slotReceivePrivateMessage(int msgid, const TQByteArray &buffer, TQ_UINT32 sender, KPlayer *me)
Called when KPlayer::signalNetworkData is emitted.
Base class for a game player.
void changeSendingEntry(const TQString &text, int id)
This changes a combo box entry.
Base class of KGameProperty.
bool addSendingEntry(const TQString &text, int id)
Adds a new entry in the combo box.
virtual TQString comboBoxItem(const TQString &name) const
Replace to customise the combo box.
virtual void returnPressed(const TQString &text)
This is called whenever the user pushed return ie wants to send a message.
void setMessageId(int msgid)
Change the message id of the chat widget.
The main KDE game object.
bool isToPlayerMessage(int id) const
Used to indicate whether the message shall be sent to a single player only.
KGameChat(KGame *game, int msgid, KPlayer *fromPlayer, TQWidget *parent)
Construct a KGame chat widget on game that used msgid for the chat message.
bool hasPlayer(int id) const
virtual const TQString & fromName() const
reimplemented from KChatBase