Skip to content

Commit

Permalink
Implemented private messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
cathyjf committed Oct 7, 2010
1 parent 71160ac commit f4af248
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
41 changes: 39 additions & 2 deletions src/network/network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class InMessage {
USER_MESSAGE_REQUEST = 17,
CLIENT_ACTIVITY = 18,
CANCEL_QUEUE = 19,
CANCEL_BATTLE_ACTION = 20
CANCEL_BATTLE_ACTION = 20,
PRIVATE_MESSAGE = 21
};

InMessage() {
Expand Down Expand Up @@ -570,6 +571,18 @@ class InvalidTeamMessage : public OutMessage {
}
};

class PrivateMessage : public OutMessage {
public:
PrivateMessage(const string &user, const string &sender,
const string &message):
OutMessage(PRIVATE_MESSAGE) {
*this << user;
*this << sender;
*this << message;
finalise();
}
};

class MetagameQueue {
public:
typedef pair<ClientImplPtr, Pokemon::ARRAY> QUEUE_ENTRY;
Expand Down Expand Up @@ -1463,6 +1476,29 @@ class ClientImpl : public Client, public enable_shared_from_this<ClientImpl> {
}
}

/**
* string : target of private message
* string : content of private message
*/
void handlePrivateMessage(InMessage &msg) {
string target, content;
msg >> target >> content;

ClientImplPtr client = m_server->getClient(target);
if (!client) {
// The empty private message denotes that the target does not
// exist.
sendMessage(PrivateMessage(target, string(), string()));
return;
}
if (content.size() > 250) {
content.resize(250);
content += "(truncated)";
}
sendMessage(PrivateMessage(target, m_name, content));
client->sendMessage(PrivateMessage(m_name, target, content));
}

string m_name;
int m_id; // user id
bool m_authenticated;
Expand Down Expand Up @@ -1513,7 +1549,8 @@ const ClientImpl::MESSAGE_HANDLER ClientImpl::m_handlers[] = {
&ClientImpl::handlePersonalMessageRequest,
&ClientImpl::handleActivityMessage,
&ClientImpl::handleCancelQueue,
&ClientImpl::handleCancelBattleAction
&ClientImpl::handleCancelBattleAction,
&ClientImpl::handlePrivateMessage
};

const int ClientImpl::MESSAGE_COUNT =
Expand Down
3 changes: 2 additions & 1 deletion src/network/network.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ class OutMessage {
BATTLE_STATUS_CHANGE = 30,
CLAUSE_LIST = 31,
INVALID_TEAM = 32,
ERROR_MESSAGE = 33
ERROR_MESSAGE = 33,
PRIVATE_MESSAGE = 34
};

// variable size message
Expand Down

0 comments on commit f4af248

Please sign in to comment.