diff options
author | Roderic Morris <roderic@ccs.neu.edu> | 2008-06-03 16:29:11 +0000 |
---|---|---|
committer | Roderic Morris <roderic@ccs.neu.edu> | 2008-06-03 16:29:11 +0000 |
commit | 0fdbf1d62c1add8800ffc7171a1911e1e243ac2a (patch) | |
tree | c22e6d658d908d3050cfe3dd5970f356e28aa465 /src | |
parent | d4e8401e55c7bc3f5c8545b66167e8c3bf3cd380 (diff) | |
download | mana-0fdbf1d62c1add8800ffc7171a1911e1e243ac2a.tar.gz mana-0fdbf1d62c1add8800ffc7171a1911e1e243ac2a.tar.bz2 mana-0fdbf1d62c1add8800ffc7171a1911e1e243ac2a.tar.xz mana-0fdbf1d62c1add8800ffc7171a1911e1e243ac2a.zip |
channel announcements and leave / enter messages, chat code refactoring
Diffstat (limited to 'src')
-rw-r--r-- | src/channel.cpp | 38 | ||||
-rw-r--r-- | src/channel.h | 72 | ||||
-rw-r--r-- | src/gui/chat.cpp | 96 | ||||
-rw-r--r-- | src/gui/chat.h | 71 | ||||
-rw-r--r-- | src/net/chathandler.cpp | 311 | ||||
-rw-r--r-- | src/net/chathandler.h | 56 | ||||
-rw-r--r-- | src/net/guildhandler.cpp | 8 | ||||
-rw-r--r-- | src/net/protocol.h | 17 |
8 files changed, 357 insertions, 312 deletions
diff --git a/src/channel.cpp b/src/channel.cpp index 3204b3b2..2bebcf51 100644 --- a/src/channel.cpp +++ b/src/channel.cpp @@ -21,35 +21,33 @@ * $Id$ */ +#include <algorithm> #include "channel.h" -Channel::Channel(short id) : -mID(id) +Channel::Channel(short id, + const std::string &name, + const std::string &announcement) : + mId(id), + mName(name), + mAnnouncement(announcement) { } -std::string Channel::getName() const +void Channel::addUser(const std::string &user) { - return mName; + // Check if the user already exists in the channel + ChannelUsers::const_iterator i = mUserList.begin(), + i_end = mUserList.end(); + if (std::find(i, i_end, user) != i_end) return; + mUserList.push_back(user); } -void Channel::setName(const std::string &channelName) +void Channel::removeUser(const std::string &user) { - mName = channelName; -} - -int Channel::getUserListSize() const -{ - return userList.size(); -} - -std::string Channel::getUser(unsigned int id) const -{ - if(id <= userList.size()) - { - return userList[id]; - } - return ""; + ChannelUsers::iterator i_end = mUserList.end(), + i = std::find(mUserList.begin(), i_end, user); + if (i == i_end) return; + mUserList.erase(i); } diff --git a/src/channel.h b/src/channel.h index ea6789ee..9f7557c1 100644 --- a/src/channel.h +++ b/src/channel.h @@ -27,15 +27,69 @@ class Channel { public: - Channel(short id); - std::string getName() const; - void setName(const std::string &channelName); - int getId() const { return mID; } - int getUserListSize() const; - std::string getUser(unsigned int i) const; + + typedef std::vector<std::string> ChannelUsers; + + /** + * Constructor. + * + * @param id the id associated with the channel. + * @param name the name of the channel. + * @param announcement a welcome message. + */ + Channel(short id, + const std::string &name, + const std::string &announcement = std::string()); + + /** + * Get the id associated witht his channel + */ + int getId() const { return mId; } + + /** + * Get this channel's name + */ + const std::string& getName() const + { return mName; } + + /** + * Get the announcement message for this channel + */ + const std::string& getAnnouncement() const + { return mAnnouncement; } + + /** + * Get the list of users in this channel + */ + const ChannelUsers& getUserList() const + { return mUserList; } + + /** + * Sets the name of the channel. + */ + void setName(const std::string &channelName) + { mName = channelName; } + + /** + * Sets the announcement string of the channel. + */ + void setAnnouncement(const std::string &channelAnnouncement) + { mAnnouncement = channelAnnouncement; } + + /** + * Adds a user to this channel. + */ + void addUser(const std::string &user); + + /** + * Removes a user from the channel. + */ + void removeUser(const std::string &user); + private: - typedef std::vector<std::string> Users; + + unsigned short mId; std::string mName; - short mID; - Users userList; + std::string mAnnouncement; + ChannelUsers mUserList; }; diff --git a/src/gui/chat.cpp b/src/gui/chat.cpp index 3748b31c..e7b82621 100644 --- a/src/gui/chat.cpp +++ b/src/gui/chat.cpp @@ -203,14 +203,6 @@ ChatWindow::chatLog(std::string line, int own, const std::string &channelName) scroll->logic(); } -#if 0 -void -ChatWindow::chatLog(CHATSKILL act) -{ - chatLog(const_msg(act), BY_SERVER); -} -#endif - void ChatWindow::action(const gcn::ActionEvent &event) { @@ -385,94 +377,6 @@ void ChatWindow::chatSend(std::string const &nick, std::string const &msg, } } -#if 0 -std::string -ChatWindow::const_msg(CHATSKILL act) -{ - std::string msg; - if (act.success == SKILL_FAILED && act.skill == SKILL_BASIC) { - switch (act.bskill) { - case BSKILL_TRADE : - msg = "Trade failed!"; - break; - case BSKILL_EMOTE : - msg = "Emote failed!"; - break; - case BSKILL_SIT : - msg = "Sit failed!"; - break; - case BSKILL_CREATECHAT : - msg = "Chat creating failed!"; - break; - case BSKILL_JOINPARTY : - msg = "Could not join party!"; - break; - case BSKILL_SHOUT : - msg = "Cannot shout!"; - break; - } - - switch (act.reason) { - case RFAIL_SKILLDEP : - msg += " You have not yet reached a high enough lvl!"; - break; - case RFAIL_INSUFHP : - msg += " Insufficient HP!"; - break; - case RFAIL_INSUFSP : - msg += " Insufficient SP!"; - break; - case RFAIL_NOMEMO : - msg += " You have no memos!"; - break; - case RFAIL_SKILLDELAY : - msg += " You cannot do that right now!"; - break; - case RFAIL_ZENY : - msg += " Seems you need more Zeny... ;-)"; - break; - case RFAIL_WEAPON : - msg += " You cannot use this skill with that kind of weapon!"; - break; - case RFAIL_REDGEM : - msg += " You need another red gem!"; - break; - case RFAIL_BLUEGEM : - msg += " You need another blue gem!"; - break; - case RFAIL_OVERWEIGHT : - msg += " You're carrying to much to do this!"; - break; - default : - msg += " Huh? What's that?"; - break; - } - } else { - switch(act.skill) { - case SKILL_WARP : - msg = "Warp failed..."; - break; - case SKILL_STEAL : - msg = "Could not steal anything..."; - break; - case SKILL_ENVENOM : - msg = "Poison had no effect..."; - break; - } - } - - return msg; -} -#endif - -void -ChatWindow::addChannel(short channelId, const std::string &channelName) -{ - Channel *channel = new Channel(channelId); - channel->setName(channelName); - channelManager->addChannel(channel); -} - void ChatWindow::removeChannel(short channelId) { diff --git a/src/gui/chat.h b/src/gui/chat.h index 26e30374..148feb7e 100644 --- a/src/gui/chat.h +++ b/src/gui/chat.h @@ -49,57 +49,6 @@ enum BY_LOGGER }; -#if 0 -/** - * gets in between usernick and message text depending on - * message type - */ -#define CAT_NORMAL ": " -#define CAT_IS "" -#define CAT_WHISPER " says: " - -/** job dependend identifiers (?) */ -#define SKILL_BASIC 0x0001 -#define SKILL_WARP 0x001b -#define SKILL_STEAL 0x0032 -#define SKILL_ENVENOM 0x0034 - -/** basic skills identifiers */ -#define BSKILL_TRADE 0x0000 -#define BSKILL_EMOTE 0x0001 -#define BSKILL_SIT 0x0002 -#define BSKILL_CREATECHAT 0x0003 -#define BSKILL_JOINPARTY 0x0004 -#define BSKILL_SHOUT 0x0005 -#define BSKILL_PK 0x0006 // ?? -#define BSKILL_SETALLIGN 0x0007 // ?? - -/** reasons why action failed */ -#define RFAIL_SKILLDEP 0x00 -#define RFAIL_INSUFHP 0x01 -#define RFAIL_INSUFSP 0x02 -#define RFAIL_NOMEMO 0x03 -#define RFAIL_SKILLDELAY 0x04 -#define RFAIL_ZENY 0x05 -#define RFAIL_WEAPON 0x06 -#define RFAIL_REDGEM 0x07 -#define RFAIL_BLUEGEM 0x08 -#define RFAIL_OVERWEIGHT 0x09 -#define RFAIL_GENERIC 0x0a - -/** should always be zero if failed */ -#define SKILL_FAILED 0x00 - -struct CHATSKILL -{ - short skill; - short bskill; - short unused; - char success; - char reason; -}; -#endif - /** * The chat window. * @@ -126,7 +75,7 @@ class ChatWindow : public Window, */ void widgetResized(const gcn::Event &event); - /* + /** * Adds a line of text to our message list. Parameters: * * @param line Text message. @@ -134,13 +83,6 @@ class ChatWindow : public Window, */ void chatLog(std::string line, int own, const std::string &channelName = "General"); -#if 0 - /* - * Calls original chat_log() after processing the packet. - */ - void chatLog(CHATSKILL); -#endif - /** * Performs action. */ @@ -156,7 +98,7 @@ class ChatWindow : public Window, */ bool isFocused(); - /* + /** * Determines whether to send a command or an ordinary message, then * contructs packets & sends them. * @@ -183,10 +125,6 @@ class ChatWindow : public Window, void chatSend(std::string const &nick, std::string const &msg, std::string const &channelName); - /** Called to add the channel to the channel manager */ - void - addChannel(short channel, const std::string &channelName); - /** Called to remove the channel from the channel manager */ void removeChannel(short channelId); @@ -251,11 +189,6 @@ class ChatWindow : public Window, ScrollArea *scroll; }; -#if 0 - /** Constructs failed messages for actions */ - std::string const_msg(CHATSKILL);*/ -#endif - /** Tabbed area for holding each channel. */ TabbedArea *mChatTabs; diff --git a/src/net/chathandler.cpp b/src/net/chathandler.cpp index 824d6e8c..aa090d64 100644 --- a/src/net/chathandler.cpp +++ b/src/net/chathandler.cpp @@ -33,6 +33,8 @@ #include "../being.h" #include "../beingmanager.h" #include "../game.h" +#include "../channel.h" +#include "../channelmanager.h" #include "../gui/chat.h" #include "../gui/guildwindow.h" @@ -53,6 +55,7 @@ ChatHandler::ChatHandler() CPMSG_PRIVMSG, CPMSG_QUIT_CHANNEL_RESPONSE, CPMSG_LIST_CHANNELUSERS_RESPONSE, + CPMSG_CHANNEL_EVENT, 0 }; handledMessages = _messages; @@ -60,146 +63,224 @@ ChatHandler::ChatHandler() void ChatHandler::handleMessage(MessageIn &msg) { - Being *being; - std::string chatMsg; - short channelId; - short id; - std::string userNick; - std::string channelName; - short error = -1; - switch (msg.getId()) { case GPMSG_SAY: - id = msg.readInt16(); - chatMsg = msg.readString(); - if (id == 0) - { - chatWindow->chatLog(chatMsg, BY_SERVER); - break; - } - being = beingManager->findBeing(id); - if (being) - { - chatWindow->chatLog(being->getName() + " : " + chatMsg, being == player_node ? BY_PLAYER : BY_OTHER); - being->setSpeech(chatMsg, SPEECH_TIME); - } - else - { - chatWindow->chatLog("Unknown : " + chatMsg, BY_OTHER); - } + handleGameChatMessage(msg); break; + case CPMSG_REGISTER_CHANNEL_RESPONSE: - error = msg.readInt8(); - if(error == ERRMSG_OK) - { - channelId = msg.readInt16(); - std::string channelName = msg.readString(); - chatWindow->chatLog("Registered Channel " + channelName, BY_SERVER); - chatWindow->addChannel(channelId, channelName); - chatWindow->createNewChannelTab(channelName); - } - else - { - if (error == ERRMSG_INVALID_ARGUMENT) - { - chatWindow->chatLog("Error registering channel - Invalid Channel Name given", BY_SERVER); - } - else - { - chatWindow->chatLog("Error registering channel", BY_SERVER); - } - } + handleRegisterChannelResponse(msg); break; + case CPMSG_ENTER_CHANNEL_RESPONSE: - if(msg.readInt8() == ERRMSG_OK) - { - channelId = msg.readInt16(); - channelName = msg.readString(); - std::string announcement = msg.readString(); - std::vector<std::string> userList; - std::string user; - while(msg.getUnreadLength()) - { - user = msg.readString(); - if (user == "") - break; - userList.push_back(user); - } - chatWindow->addChannel(channelId, channelName); - chatWindow->createNewChannelTab(channelName); - chatWindow->chatLog(announcement, BY_SERVER, channelName); - } - else - { - chatWindow->chatLog("Error joining channel", BY_SERVER); - } + handleEnterChannelResponse(msg); break; case CPMSG_LIST_CHANNELS_RESPONSE: - chatWindow->chatLog("Listing Channels", BY_SERVER); - while(msg.getUnreadLength()) - { - channelName = msg.readString(); - if (channelName == "") - break; - std::ostringstream numUsers; - numUsers << msg.readInt16(); - if(channelName != "") - { - channelName += " - "; - channelName += numUsers.str(); - chatWindow->chatLog(channelName, BY_SERVER); - } - } - chatWindow->chatLog("End of channel list", BY_SERVER); + handleListChannelsResponse(msg); break; case CPMSG_PRIVMSG: - userNick = msg.readString(); - chatMsg = msg.readString(); - - if (!chatWindow->tabExists(userNick)) - { - chatWindow->createNewChannelTab(userNick); - - } - chatWindow->chatLog(userNick + ": " + chatMsg, BY_OTHER, userNick); + handlePrivateMessage(msg); break; case CPMSG_ANNOUNCEMENT: - chatMsg = msg.readString(); - chatWindow->chatLog(chatMsg, BY_GM); + handleAnnouncement(msg); break; case CPMSG_PUBMSG: - channelId = msg.readInt16(); - userNick = msg.readString(); - chatMsg = msg.readString(); - - chatWindow->sendToChannel(channelId, userNick, chatMsg); + handleChatMessage(msg); break; case CPMSG_QUIT_CHANNEL_RESPONSE: - if(msg.readInt8() == ERRMSG_OK) - { - channelId = msg.readInt16(); - // remove the chat tab - chatWindow->removeChannel(channelId); - } + handleQuitChannelResponse(msg); break; case CPMSG_LIST_CHANNELUSERS_RESPONSE: - channelName = msg.readString(); - while(msg.getUnreadLength()) - { - userNick = msg.readString(); - if (userNick == "") - { - break; - } - guildWindow->setOnline(channelName, userNick, true); - } + handleListChannelUsersResponse(msg); + break; + + case CPMSG_CHANNEL_EVENT: + handleChannelEvent(msg); + } +} + +void ChatHandler::handleGameChatMessage(MessageIn &msg) +{ + short id = msg.readInt16(); + std::string chatMsg = msg.readString(); + + if (id == 0) + { + chatWindow->chatLog(chatMsg, BY_SERVER); + return; + } + + Being *being = beingManager->findBeing(id); + + if (being) + { + chatWindow->chatLog(being->getName() + " : " + chatMsg, + being == player_node ? BY_PLAYER : BY_OTHER); + being->setSpeech(chatMsg, SPEECH_TIME); + } + else + { + chatWindow->chatLog("Unknown : " + chatMsg, BY_OTHER); + } +} + +void ChatHandler::handleRegisterChannelResponse(MessageIn &msg) +{ + char error = msg.readInt8(); + if(error == ERRMSG_OK) + { + short channelId = msg.readInt16(); + std::string channelName = msg.readString(); + std::string channelAnnouncement = msg.readString(); + chatWindow->chatLog("Registered Channel " + channelName, BY_SERVER); + channelManager->addChannel(new Channel(channelId, + channelName, + channelAnnouncement)); + chatWindow->createNewChannelTab(channelName); + } + else + { + if (error == ERRMSG_INVALID_ARGUMENT) + { + chatWindow->chatLog("Error registering channel - Invalid Channel Name given", BY_SERVER); + } + else + { + chatWindow->chatLog("Error registering channel", BY_SERVER); + } + } +} + +void ChatHandler::handleEnterChannelResponse(MessageIn &msg) +{ + if(msg.readInt8() == ERRMSG_OK) + { + short channelId = msg.readInt16(); + std::string channelName = msg.readString(); + std::string announcement = msg.readString(); + Channel *channel = new Channel(channelId, channelName, announcement); + std::string user; + while(msg.getUnreadLength()) + { + user = msg.readString(); + if (user == "") + return; + channel->addUser(user); + } + channelManager->addChannel(channel); + chatWindow->createNewChannelTab(channelName); + chatWindow->chatLog(announcement, BY_SERVER, channelName); + } + else + { + chatWindow->chatLog("Error joining channel", BY_SERVER); + } +} + +void ChatHandler::handleListChannelsResponse(MessageIn &msg) +{ + chatWindow->chatLog("Listing Channels", BY_SERVER); + while(msg.getUnreadLength()) + { + std::string channelName = msg.readString(); + if (channelName == "") + return; + std::ostringstream numUsers; + numUsers << msg.readInt16(); + if(channelName != "") + { + channelName += " - "; + channelName += numUsers.str(); + chatWindow->chatLog(channelName, BY_SERVER); + } + } + chatWindow->chatLog("End of channel list", BY_SERVER); +} + +void ChatHandler::handlePrivateMessage(MessageIn &msg) +{ + std::string userNick = msg.readString(); + std::string chatMsg = msg.readString(); + + if (!chatWindow->tabExists(userNick)) + { + chatWindow->createNewChannelTab(userNick); + + } + chatWindow->chatLog(userNick + ": " + chatMsg, BY_OTHER, userNick); +} + +void ChatHandler::handleAnnouncement(MessageIn &msg) +{ + std::string chatMsg = msg.readString(); + chatWindow->chatLog(chatMsg, BY_GM); +} + +void ChatHandler::handleChatMessage(MessageIn &msg) +{ + short channelId = msg.readInt16(); + std::string userNick = msg.readString(); + std::string chatMsg = msg.readString(); + + chatWindow->sendToChannel(channelId, userNick, chatMsg); +} + +void ChatHandler::handleQuitChannelResponse(MessageIn &msg) +{ + if(msg.readInt8() == ERRMSG_OK) + { + short channelId = msg.readInt16(); + // remove the chat tab + chatWindow->removeChannel(channelId); + } +} + +void ChatHandler::handleListChannelUsersResponse(MessageIn &msg) +{ + std::string channelName = msg.readString(); + std::string userNick; + while(msg.getUnreadLength()) + { + userNick = msg.readString(); + if (userNick == "") + { break; + } + guildWindow->setOnline(channelName, userNick, true); } } + +void ChatHandler::handleChannelEvent(MessageIn &msg) +{ + short channelId = msg.readInt16(); + char eventId = msg.readInt8(); + std::string line = msg.readString(); + Channel *channel = channelManager->findById(channelId); + + if(channel) + { + switch(eventId) + { + case CHAT_EVENT_NEW_PLAYER: + line += " entered the channel."; + break; + + case CHAT_EVENT_LEAVING_PLAYER: + line += " left the channel."; + break; + + default: + line = "Unknown channel event."; + } + + chatWindow->chatLog(line, BY_SERVER, channel->getName()); + } +} + diff --git a/src/net/chathandler.h b/src/net/chathandler.h index e9db3575..874998d9 100644 --- a/src/net/chathandler.h +++ b/src/net/chathandler.h @@ -30,8 +30,62 @@ class ChatHandler : public MessageHandler { public: ChatHandler(); - + + /** + * Handle the given message appropriately. + */ void handleMessage(MessageIn &msg); + + private: + /** + * Handle chat messages sent from the game server. + */ + void handleGameChatMessage(MessageIn &msg); + + /** + * Handle channel registration responses. + */ + void handleRegisterChannelResponse(MessageIn &msg); + + /** + * Handle channel entry responses. + */ + void handleEnterChannelResponse(MessageIn &msg); + + /** + * Handle list channels responses. + */ + void handleListChannelsResponse(MessageIn &msg); + + /** + * Handle private messages. + */ + void handlePrivateMessage(MessageIn &msg); + + /** + * Handle announcements. + */ + void handleAnnouncement(MessageIn &msg); + + /** + * Handle chat messages. + */ + void handleChatMessage(MessageIn &msg); + + /** + * Handle quit channel responses. + */ + void handleQuitChannelResponse(MessageIn &msg); + + /** + * Handle list channel users responses. + */ + void handleListChannelUsersResponse(MessageIn &msg); + + /** + * Handle channel events. + */ + void handleChannelEvent(MessageIn &msg); }; #endif diff --git a/src/net/guildhandler.cpp b/src/net/guildhandler.cpp index 459c5ac4..10da5075 100644 --- a/src/net/guildhandler.cpp +++ b/src/net/guildhandler.cpp @@ -35,6 +35,8 @@ #include "../guild.h" #include "../log.h" #include "../localplayer.h" +#include "../channel.h" +#include "../channelmanager.h" GuildHandler::GuildHandler() { @@ -181,6 +183,7 @@ void GuildHandler::joinedGuild(MessageIn &msg) short guildId = msg.readInt16(); bool leader = msg.readInt8(); short channelId = msg.readInt16(); + std::string announcement = msg.readString(); // Add guild to player and create new guild tab Guild *guild = player_node->addGuild(guildId, leader); @@ -190,7 +193,8 @@ void GuildHandler::joinedGuild(MessageIn &msg) // Automatically create the guild channel // COMMENT: Should this go here?? - chatWindow->addChannel(channelId, guildName); + Channel *channel = new Channel(channelId, guildName, announcement); + channelManager->addChannel(channel); chatWindow->createNewChannelTab(guildName); - chatWindow->chatLog("Guild Channel", BY_SERVER, guildName); + chatWindow->chatLog(announcement, BY_SERVER, guildName); } diff --git a/src/net/protocol.h b/src/net/protocol.h index d2d5937d..87577942 100644 --- a/src/net/protocol.h +++ b/src/net/protocol.h @@ -252,4 +252,21 @@ enum { MOVING_DESTINATION = 2 }; +// Email change specific return values +enum { + EMAILCHG_EXISTS_EMAIL = 0x40 +}; + +// Chat errors return values +enum { + CHAT_USING_BAD_WORDS = 0x40, + CHAT_UNHANDLED_COMMAND +}; + +// Chat channels event values +enum { + CHAT_EVENT_NEW_PLAYER = 0, + CHAT_EVENT_LEAVING_PLAYER +}; + #endif |