From 6ba73b56b72bf53d4976fd1423490e682542b6d2 Mon Sep 17 00:00:00 2001 From: Bjørn Lindeijer Date: Wed, 25 Jul 2007 00:56:06 +0000 Subject: Removed a host of unnecessary methods from ChatChannelManager. --- src/account-server/dalstorage.cpp | 21 ++-- src/account-server/serverhandler.cpp | 39 +++--- src/chat-server/chatchannel.cpp | 10 +- src/chat-server/chatchannel.hpp | 32 +++-- src/chat-server/chatchannelmanager.cpp | 133 ++++++-------------- src/chat-server/chatchannelmanager.hpp | 95 ++------------- src/chat-server/chathandler.cpp | 216 ++++++++++++++++----------------- src/chat-server/chathandler.hpp | 33 ++++- src/game-server/map.hpp | 4 +- 9 files changed, 243 insertions(+), 340 deletions(-) (limited to 'src') diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 01b6ea6d..a4506846 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -718,16 +718,19 @@ DALStorage::getChannelList() return channels; } - for ( unsigned int i = 0; i < channelInfo.rows(); ++i) + for (unsigned int i = 0; i < channelInfo.rows(); ++i) { - channels.insert(std::make_pair(toShort(channelInfo(i,0)), - ChatChannel(channelInfo(i,1), - channelInfo(i,2), - channelInfo(i,3), - toBool(channelInfo(i,4))))); - - LOG_DEBUG("Channel (" << channelInfo(i,0) << ") loaded: " << channelInfo(i,1) - << ": " << channelInfo(i,2)); + short channelId = toShort(channelInfo(i, 0)); + channels.insert( + std::make_pair(channelId, + ChatChannel(channelId, + channelInfo(i, 1), + channelInfo(i, 2), + channelInfo(i, 3), + toBool(channelInfo(i, 4))))); + + LOG_DEBUG("Channel (" << channelId << ") loaded: " + << channelInfo(i, 1) << ": " << channelInfo(i, 2)); } return channels; diff --git a/src/account-server/serverhandler.cpp b/src/account-server/serverhandler.cpp index ad6bb446..5ef3403d 100644 --- a/src/account-server/serverhandler.cpp +++ b/src/account-server/serverhandler.cpp @@ -364,31 +364,35 @@ void ServerHandler::processMessage(NetComputer *comp, MessageIn &msg) comp->send(result); } -void ServerHandler::enterChannel(const std::string &name, CharacterData *player) +void ServerHandler::enterChannel(const std::string &name, + CharacterData *player) { MessageOut result(CPMSG_ENTER_CHANNEL_RESPONSE); + short channelId = chatChannelManager->getChannelId(name); - if (!chatChannelManager->channelExists(channelId)) + ChatChannel *channel = chatChannelManager->getChannel(channelId); + + if (!channel) { // Channel doesn't exist yet so create one - channelId = chatChannelManager->registerPrivateChannel( - name, - "Guild Channel", - ""); + channelId = chatChannelManager->registerPrivateChannel(name, + "Guild Channel", + ""); + channel = chatChannelManager->getChannel(channelId); } - if (chatChannelManager->addUserInChannel(player->getName(), channelId)) + if (channel && channel->addUser(player->getName())) { result.writeByte(ERRMSG_OK); - // The user entered the channel, now give him the channel id, the announcement string - // and the user list. + // The user entered the channel, now give him the channel id, the + // announcement string and the user list. result.writeShort(channelId); result.writeString(name); - result.writeString(chatChannelManager->getChannelAnnouncement(channelId)); - std::vector< std::string > const &userList = - chatChannelManager->getUserListInChannel(channelId); - for (std::vector< std::string >::const_iterator i = userList.begin(), + result.writeString(channel->getAnnouncement()); + const ChatChannel::ChannelUsers &userList = channel->getUserList(); + + for (ChatChannel::ChannelUsers::const_iterator i = userList.begin(), i_end = userList.end(); i != i_end; ++i) { @@ -397,16 +401,17 @@ void ServerHandler::enterChannel(const std::string &name, CharacterData *player) // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went // in the channel. - chatHandler->warnUsersAboutPlayerEventInChat(channelId, - player->getName(), - CHAT_EVENT_NEW_PLAYER); + chatHandler->warnUsersAboutPlayerEventInChat(channel, + player->getName(), + CHAT_EVENT_NEW_PLAYER); } chatHandler->sendGuildEnterChannel(result, player->getName()); } -void ServerHandler::sendInvite(const std::string &invitedName, const std::string &inviterName, +void ServerHandler::sendInvite(const std::string &invitedName, + const std::string &inviterName, const std::string &guildName) { // TODO: Separate account and chat server diff --git a/src/chat-server/chatchannel.cpp b/src/chat-server/chatchannel.cpp index bf8bce50..e8afda13 100644 --- a/src/chat-server/chatchannel.cpp +++ b/src/chat-server/chatchannel.cpp @@ -23,10 +23,12 @@ #include "chat-server/chatchannel.hpp" -ChatChannel::ChatChannel(const std::string &name, +ChatChannel::ChatChannel(short id, + const std::string &name, const std::string &announcement, const std::string &password, bool privacy): + mId(id), mName(name), mAnnouncement(announcement), mPassword(password), @@ -62,7 +64,7 @@ ChatChannel::setPassword(const std::string &password) mPassword = password; } -bool ChatChannel::addUserInChannel(std::string const &user) +bool ChatChannel::addUser(const std::string &user) { // Check if the user already exists in the channel ChannelUsers::const_iterator i = mRegisteredUsers.begin(), @@ -72,7 +74,7 @@ bool ChatChannel::addUserInChannel(std::string const &user) return true; } -bool ChatChannel::removeUserFromChannel(std::string const &user) +bool ChatChannel::removeUser(const std::string &user) { ChannelUsers::iterator i_end = mRegisteredUsers.end(), i = std::find(mRegisteredUsers.begin(), i_end, user); @@ -81,7 +83,7 @@ bool ChatChannel::removeUserFromChannel(std::string const &user) return true; } -void ChatChannel::removeAllUsersFromChannel() +void ChatChannel::removeAllUsers() { mRegisteredUsers.clear(); } diff --git a/src/chat-server/chatchannel.hpp b/src/chat-server/chatchannel.hpp index fd2bff35..44914324 100644 --- a/src/chat-server/chatchannel.hpp +++ b/src/chat-server/chatchannel.hpp @@ -35,10 +35,10 @@ * that no user joins the channel twice and checking that a user who leaves * actually existed in the channel. * - * TODO: b_lindeijer: It would be nicer when some more logic could be placed - * in this class to remove some weight from the ChatHandler. Referencing - * ChatClient instances would also be nicer than to store only the names - * of the characters. + * @todo b_lindeijer: It would be nicer when some more logic could be + * placed in this class to remove some weight from the ChatHandler. + * Referencing ChatClient instances would also be nicer than to store + * only the names of the characters. */ class ChatChannel { @@ -48,20 +48,27 @@ class ChatChannel /** * Constructor. * - * TODO: b_lindeijer: I would say a channel can be defined as private - * when a non-empty password is set, in which case we can get - * rid of the privacy parameter. + * @todo b_lindeijer: I would say a channel can be defined as + * private when a non-empty password is set, in which case we can + * get rid of the privacy parameter. * * @param name the name of the channel. * @param announcement a welcome message. * @param password password (for private channels). * @param privacy whether this channel is private. */ - ChatChannel(const std::string &name, + ChatChannel(short id, + const std::string &name, const std::string &announcement = "", const std::string &password = "", bool privacy = false); + /** + * Get the ID of the channel. + */ + short getId() const + { return mId; } + /** * Get the name of the channel. */ @@ -108,25 +115,26 @@ class ChatChannel { return mRegisteredUsers; } /** - * Adds a user in the channel. + * Adds a user to the channel. * * @return whether the user was successfully added */ - bool addUserInChannel(std::string const &); + bool addUser(const std::string &name); /** * Removes a user from the channel. * * @return whether the user was successfully removed */ - bool removeUserFromChannel(std::string const &); + bool removeUser(const std::string &name); /** * Empties a channel from its users (admin included). */ - void removeAllUsersFromChannel(); + void removeAllUsers(); private: + short mId; /**< The ID of the channel. */ std::string mName; /**< The name of the channel. */ std::string mAnnouncement; /**< Welcome message. */ std::string mPassword; /**< The channel password. */ diff --git a/src/chat-server/chatchannelmanager.cpp b/src/chat-server/chatchannelmanager.cpp index 77f1c397..dbae5516 100644 --- a/src/chat-server/chatchannelmanager.cpp +++ b/src/chat-server/chatchannelmanager.cpp @@ -50,17 +50,29 @@ ChatChannelManager::registerPublicChannel(const std::string &channelName, for (ChatChannelIterator i = mChatChannels.begin(), end = mChatChannels.end(); i != end; ++i) { - if (i->second.getName() == channelName) return 0; + // Don't allow channels with the same name + if (i->second.getName() == channelName) + return 0; + // We seek the highest channelId in the public range - if (channelId <= i->first && i->first < (signed)MAX_PUBLIC_CHANNELS_RANGE) + if (channelId <= i->first && + i->first < (signed) MAX_PUBLIC_CHANNELS_RANGE) + { channelId = i->first + 1; + } } - // Too much channels registered - if (channelId >= (signed) MAX_PUBLIC_CHANNELS_RANGE) return 0; - // Register Channel - mChatChannels.insert(std::make_pair(channelId, ChatChannel(channelName, - channelAnnouncement, channelPassword, false))); + // Too much channels registered + if (channelId >= (signed) MAX_PUBLIC_CHANNELS_RANGE) + return 0; + + // Register channel + mChatChannels.insert(std::make_pair(channelId, + ChatChannel(channelId, + channelName, + channelAnnouncement, + channelPassword, + false))); return channelId; } @@ -71,20 +83,27 @@ ChatChannelManager::registerPrivateChannel(const std::string &channelName, const std::string &channelPassword) { short channelId = MAX_PUBLIC_CHANNELS_RANGE; + for (ChatChannelIterator i = mChatChannels.begin(), - end = mChatChannels.end(); i != end; ++i) + end = mChatChannels.end(); i != end; ++i) { if (i->second.getName() == channelName) return 0; + // We seek the highest channelId in the private range if (channelId <= i->first) channelId = i->first + 1; } + // Too much channels registered if (channelId >= (signed) MAX_PRIVATE_CHANNELS_RANGE) return 0; // Register Channel - mChatChannels.insert(std::make_pair(channelId, ChatChannel(channelName, - channelAnnouncement, channelPassword, true))); + mChatChannels.insert(std::make_pair(channelId, + ChatChannel(channelId, + channelName, + channelAnnouncement, + channelPassword, + true))); return channelId; } @@ -92,15 +111,14 @@ bool ChatChannelManager::removeChannel(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); if (i == mChatChannels.end()) return false; - i->second.removeAllUsersFromChannel(); + i->second.removeAllUsers(); mChatChannels.erase(i); return true; } -std::list -ChatChannelManager::getPublicChannelNames() +std::list ChatChannelManager::getPublicChannels() { - std::list channels; + std::list channels; for (ChatChannels::const_iterator i = mChatChannels.begin(), i_end = mChatChannels.end(); @@ -108,20 +126,13 @@ ChatChannelManager::getPublicChannelNames() { if (!i->second.isPrivate()) { - channels.push_back(i->second.getName()); + channels.push_back(&i->second); } } return channels; } -short ChatChannelManager::getNumberOfChannelUsers(const std::string &channelName) -{ - ChatChannel channel = _getChannel(getChannelId(channelName)); - short size = channel.getUserList().size(); - return size; -} - short ChatChannelManager::getChannelId(std::string const &channelName) { for (ChatChannels::const_iterator i = mChatChannels.begin(), @@ -133,90 +144,24 @@ short ChatChannelManager::getChannelId(std::string const &channelName) return 0; } -std::string ChatChannelManager::getChannelName(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getName() : std::string(); -} - -std::string ChatChannelManager::getChannelAnnouncement(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getAnnouncement() : ""; -} - -std::string ChatChannelManager::getChannelPassword(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.getPassword() : ""; -} - -bool ChatChannelManager::getChannelPrivacy(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - return (i != mChatChannels.end()) ? i->second.isPrivate() : true; -} - -bool ChatChannelManager::setChannelAnnouncement(short channelId, std::string const &channelAnnouncement) +ChatChannel* ChatChannelManager::getChannel(short channelId) { ChatChannelIterator i = mChatChannels.find(channelId); - if (i == mChatChannels.end()) return false; - i->second.setAnnouncement(channelAnnouncement); - return true; + if (i != mChatChannels.end()) return &i->second; + return NULL; } -bool ChatChannelManager::setChannelPassword(short channelId, std::string const &channelPassword) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - if (i == mChatChannels.end()) return false; - i->second.setPassword(channelPassword); - return true; -} - -ChatChannel ChatChannelManager::_getChannel(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - if (i != mChatChannels.end()) return i->second; - return ChatChannel("", "", "", true); -} - - -bool ChatChannelManager::addUserInChannel(std::string const &user, short channelId) -{ - std::map::iterator i = mChatChannels.find(channelId); - if (i == mChatChannels.end()) return false; - return i->second.addUserInChannel(user); -} - - -bool ChatChannelManager::removeUserFromChannel(std::string const &user, short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - if (i == mChatChannels.end()) return false; - return i->second.removeUserFromChannel(user); -} - -void ChatChannelManager::removeUserFromAllChannels(std::string const &user) +void ChatChannelManager::removeUserFromAllChannels(const std::string &user) { for (ChatChannelIterator i = mChatChannels.begin(), i_end = mChatChannels.end(); i != i_end; ++i) { - i->second.removeUserFromChannel(user); + i->second.removeUser(user); } } -std::vector const & -ChatChannelManager::getUserListInChannel(short channelId) -{ - ChatChannelIterator i = mChatChannels.find(channelId); - if (i != mChatChannels.end()) return i->second.getUserList(); - static std::vector< std::string > emptyList; - return emptyList; -} - bool ChatChannelManager::channelExists(short channelId) { - ChatChannelIterator i = mChatChannels.find(channelId); - return i != mChatChannels.end(); + return mChatChannels.find(channelId) != mChatChannels.end(); } diff --git a/src/chat-server/chatchannelmanager.hpp b/src/chat-server/chatchannelmanager.hpp index ecc12525..eeca3d1e 100644 --- a/src/chat-server/chatchannelmanager.hpp +++ b/src/chat-server/chatchannelmanager.hpp @@ -62,9 +62,9 @@ class ChatChannelManager * channels has already been reached or when a channel with the same * name already exists. * - * TODO: b_lindeijer: Pretty much the same as registering public - * channel. Maybe they should be merged and private/public - * should be passed as a boolean? + * @todo b_lindeijer: Pretty much the same as registering public + * channel. Maybe they should be merged and private/public should + * be passed as a boolean? * * @return the ID of the registered channel, or 0 if the registering * was unsuccessful. @@ -79,19 +79,11 @@ class ChatChannelManager bool removeChannel(short channelId); /** - * Returns a list containing the names of all public channels. + * Returns a list containing all public channels. * - * @return a list of public channel names + * @return a list of all public channels */ - std::list getPublicChannelNames(); - - /** - * Get the number of users that have joined a channel. - * - * @param channelName the name of the channel - * @return the number of users in the channel - */ - short getNumberOfChannelUsers(const std::string &channelName); + std::list getPublicChannels(); /** * Get the id of a channel from its name. @@ -101,84 +93,19 @@ class ChatChannelManager short getChannelId(const std::string &channelName); /** - * Get the name of a channel from its id. - * - * @return the name of the channel - * @deprecated Use ChatChannel::getName instead - */ - std::string getChannelName(short channelId); - - /** - * Get the announcement string of a channel from its id. - * - * @return the announcement string of the channel - * @deprecated Use ChatChannel::getAnnouncement instead - */ - std::string getChannelAnnouncement(short channelId); - - /** - * Set the announcement string of a channel from its id. - * - * @return whether the channel exists - * @deprecated Use ChatChannel::setAnnouncement instead - */ - bool setChannelAnnouncement(short channelId, - std::string const &channelAnnouncement); - - /** - * Set the password of a channel by its id. - * - * @return whether the channel exists - * @deprecated Use ChatChannel::setPassword instead - */ - bool setChannelPassword(short channelId, - const std::string &channelPassword); - - /** - * Get the password of a channel from its id. - * - * @return the password of the channel - * @deprecated Use ChatChannel::getPassword instead - */ - std::string getChannelPassword(short channelId); - - /** - * Get the privacy of the channel from its id. + * Returns the chat channel with the given channel ID. * - * @return the privacy of the channel - * @deprecated Use ChatChannel::isPrivate instead + * @return The chat channel, or NULL when it doesn't exist. */ - bool getChannelPrivacy(short channelId); - - /** - * Get the ChatChannel object from its id. - * TODO: If we have a channel object, why not use that to set - * announcement, password, private status, add/remove users, etc? - * - * @return the ChatChannel object - */ - ChatChannel _getChannel(short channelId); - - /** - * Add a user in a channel. - */ - bool addUserInChannel(std::string const &, short channelId); - - /** - * Remove a user from a channel. - */ - bool removeUserFromChannel(std::string const &, short channelId); + ChatChannel* getChannel(short channelId); /** * Remove a user from all channels. Used at logout. + * + * @see ChatChannel::removeUserFromChannel */ void removeUserFromAllChannels(std::string const &userName); - /** - * Get the list of the users registered in a channel. - */ - std::vector const &getUserListInChannel(short channelId); - /** * Returns whether a channel exists. * diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp index 21cebac0..8d64f223 100644 --- a/src/chat-server/chathandler.cpp +++ b/src/chat-server/chathandler.cpp @@ -243,18 +243,20 @@ ChatHandler::handleChatMessage(ChatClient &client, MessageIn &msg) return; } - short channel = msg.readShort(); - - LOG_DEBUG(client.characterName << " says in channel " << channel << ": " - << text); + short channelId = msg.readShort(); + ChatChannel *channel = chatChannelManager->getChannel(channelId); - MessageOut result(CPMSG_PUBMSG); - result.writeShort(channel); - result.writeString(client.characterName); - result.writeString(text); + if (channel) + { + LOG_DEBUG(client.characterName << " says in channel " << channelId + << ": " << text); - // Send the message to the players registered in the channel. - sendInChannel(channel, result); + MessageOut result(CPMSG_PUBMSG); + result.writeShort(channelId); + result.writeString(client.characterName); + result.writeString(text); + sendInChannel(channel, result); + } } void @@ -377,8 +379,8 @@ ChatHandler::handleRegisterChannelMessage(ChatClient &client, MessageIn &msg) // user registering a private channel is the only one to be able to // update the password and the announcement in it and also to // remove it. - chatChannelManager->addUserInChannel(client.characterName, - channelId); + ChatChannel *channel = chatChannelManager->getChannel(channelId); + channel->addUser(client.characterName); reply.writeByte(ERRMSG_OK); reply.writeShort(channelId); @@ -399,10 +401,10 @@ ChatHandler::handleUnregisterChannelMessage(ChatClient &client, MessageIn &msg) MessageOut reply(CPMSG_UNREGISTER_CHANNEL_RESPONSE); short channelId = msg.readShort(); + ChatChannel *channel = chatChannelManager->getChannel(channelId); - if (!chatChannelManager->channelExists(channelId)) + if (!channel) { - // Channel doesn't exist reply.writeByte(ERRMSG_INVALID_ARGUMENT); } else if (channelId < (signed) MAX_PUBLIC_CHANNELS_RANGE) @@ -412,12 +414,12 @@ ChatHandler::handleUnregisterChannelMessage(ChatClient &client, MessageIn &msg) // Get character based on name CharacterPtr character = serverHandler->getCharacter(client.characterName); - std::string channelName = chatChannelManager->getChannelName(channelId); + const std::string &channelName = channel->getName(); if (client.accountLevel == AL_ADMIN || client.accountLevel == AL_GM) { warnUsersAboutPlayerEventInChat( - channelId, "", CHAT_EVENT_LEAVING_PLAYER); + channel, "", CHAT_EVENT_LEAVING_PLAYER); if (chatChannelManager->removeChannel(channelId)) reply.writeByte(ERRMSG_OK); else @@ -451,16 +453,21 @@ ChatHandler::handleUnregisterChannelMessage(ChatClient &client, MessageIn &msg) // Private channel // We first see if the user is the admin (first user) of the channel - const std::vector &userList = - chatChannelManager->getUserListInChannel(channelId); - std::vector::const_iterator i = userList.begin(); - // If it's actually the private channel's admin - if (*i == client.characterName) + const ChatChannel::ChannelUsers &userList = channel->getUserList(); + ChatChannel::ChannelUsers::const_iterator i = userList.begin(); + + if (*i != client.characterName) + { + reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); + } + else { // Make every user quit the channel warnUsersAboutPlayerEventInChat( - channelId, "", CHAT_EVENT_LEAVING_PLAYER); - if (chatChannelManager->removeChannel(channelId)) { + channel, "", CHAT_EVENT_LEAVING_PLAYER); + + if (chatChannelManager->removeChannel(channelId)) + { reply.writeByte(ERRMSG_OK); } else @@ -468,16 +475,12 @@ ChatHandler::handleUnregisterChannelMessage(ChatClient &client, MessageIn &msg) reply.writeByte(ERRMSG_FAILURE); } } - else - { - reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS); - } } client.send(reply); } -void + void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) { MessageOut reply(CPMSG_ENTER_CHANNEL_RESPONSE); @@ -486,8 +489,7 @@ ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) std::string givenPassword = msg.readString(); short channelId = chatChannelManager->getChannelId(channelName); - std::string channelPassword = - chatChannelManager->getChannelPassword(channelId); + ChatChannel *channel = chatChannelManager->getChannel(channelId); // TODO: b_lindeijer: Currently, the client has to join its guild channels // explicitly by sending 'enter channel' messages. This should be @@ -495,11 +497,12 @@ ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) // login. Guild *guild = guildManager->findByName(channelName); - if (!channelId || !chatChannelManager->channelExists(channelId)) + if (!channelId || !channel) { reply.writeByte(ERRMSG_INVALID_ARGUMENT); } - else if (!channelPassword.empty() && channelPassword != givenPassword) + else if (!channel->getPassword().empty() && + channel->getPassword() != givenPassword) { // Incorrect password (should probably have its own return value) reply.writeByte(ERRMSG_INVALID_ARGUMENT); @@ -511,34 +514,31 @@ ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) } else { - // In the case of a guild, send user joined message. - if (guild) + if (channel->addUser(client.characterName)) { - sendUserJoined(channelId, client.characterName); - } + // In the case of a guild, send user joined message. + if (guild) + { + sendUserJoined(channel, client.characterName); + } - if (chatChannelManager->addUserInChannel(client.characterName, - channelId)) - { reply.writeByte(ERRMSG_OK); // The user entered the channel, now give him the channel // id, the announcement string and the user list. reply.writeShort(channelId); reply.writeString(channelName); - reply.writeString( - chatChannelManager->getChannelAnnouncement(channelId)); - const std::vector &userList = - chatChannelManager->getUserListInChannel(channelId); + reply.writeString(channel->getAnnouncement()); + const ChatChannel::ChannelUsers &users = channel->getUserList(); - for (std::vector::const_iterator i = userList.begin(), - i_end = userList.end(); + for (ChatChannel::ChannelUsers::const_iterator i = users.begin(), + i_end = users.end(); i != i_end; ++i) { reply.writeString(*i); } // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went // in the channel. - warnUsersAboutPlayerEventInChat(channelId, + warnUsersAboutPlayerEventInChat(channel, client.characterName, CHAT_EVENT_NEW_PLAYER); } @@ -557,42 +557,38 @@ ChatHandler::handleQuitChannelMessage(ChatClient &client, MessageIn &msg) MessageOut reply(CPMSG_QUIT_CHANNEL_RESPONSE); short channelId = msg.readShort(); + ChatChannel *channel = chatChannelManager->getChannel(channelId); - if (channelId != 0 && chatChannelManager->channelExists(channelId)) + if (channelId == 0 || !channel) { - if (chatChannelManager->removeUserFromChannel(client.characterName, - channelId)) - { - reply.writeByte(ERRMSG_OK); - reply.writeShort(channelId); - - // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user left - // the channel. - warnUsersAboutPlayerEventInChat(channelId, - client.characterName, - CHAT_EVENT_LEAVING_PLAYER); - - // TODO: b_lindeijer: Clients aren't supposed to quit guild - // channels explicitly, this should rather happen - // implicitly. See similar note at handling 'enter channel' - // messages. - std::string channelName = - chatChannelManager->getChannelName(channelId); - - if (guildManager->doesExist(channelName)) - { - // Send a user left message - sendUserLeft(channelId, client.characterName); - } - } - else - { - reply.writeByte(ERRMSG_FAILURE); - } + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } + else if (!channel->removeUser(client.characterName)) + { + reply.writeByte(ERRMSG_FAILURE); } else { - reply.writeByte(ERRMSG_INVALID_ARGUMENT); + reply.writeByte(ERRMSG_OK); + reply.writeShort(channelId); + + // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user left + // the channel. + warnUsersAboutPlayerEventInChat(channel, + client.characterName, + CHAT_EVENT_LEAVING_PLAYER); + + // TODO: b_lindeijer: Clients aren't supposed to quit guild + // channels explicitly, this should rather happen + // implicitly. See similar note at handling 'enter channel' + // messages. + const std::string &channelName = channel->getName(); + + if (guildManager->doesExist(channelName)) + { + // Send a user left message + sendUserLeft(channel, client.characterName); + } } client.send(reply); @@ -603,15 +599,15 @@ ChatHandler::handleListChannelsMessage(ChatClient &client, MessageIn &msg) { MessageOut reply(CPMSG_LIST_CHANNELS_RESPONSE); - std::list publicChannels = - chatChannelManager->getPublicChannelNames(); + std::list channels = + chatChannelManager->getPublicChannels(); - for (std::list::iterator i = publicChannels.begin(), - i_end = publicChannels.end(); + for (std::list::iterator i = channels.begin(), + i_end = channels.end(); i != i_end; ++i) { - const std::string &name = *i; - short users = chatChannelManager->getNumberOfChannelUsers(name); + const std::string &name = (*i)->getName(); + short users = (*i)->getUserList().size(); reply.writeString(name); reply.writeShort(users); } @@ -629,18 +625,20 @@ ChatHandler::handleListChannelUsersMessage(ChatClient &client, MessageIn &msg) // a channel id instead. std::string channelName = msg.readString(); - reply.writeString(channelName); - - std::vector channelUsers = - chatChannelManager->getUserListInChannel( - chatChannelManager->getChannelId(channelName)); + int channelId = chatChannelManager->getChannelId(channelName); + ChatChannel *channel = chatChannelManager->getChannel(channelId); - // TODO: b_lindeijer: This method should check whether the channel exists. + reply.writeString(channelName); - // Add a user at a time - for (unsigned int i = 0; i < channelUsers.size(); ++i) + if (channel) { - reply.writeString(channelUsers[i]); + const ChatChannel::ChannelUsers &channelUsers = channel->getUserList(); + + // Add a user at a time + for (unsigned int i = 0; i < channelUsers.size(); ++i) + { + reply.writeString(channelUsers[i]); + } } client.send(reply); @@ -676,26 +674,20 @@ ChatHandler::sayToPlayer(ChatClient &computer, const std::string &playerName, } } -void ChatHandler::warnUsersAboutPlayerEventInChat(short channelId, +void ChatHandler::warnUsersAboutPlayerEventInChat(ChatChannel *channel, const std::string &userName, char eventId) { - MessageOut result; - result.writeShort(CPMSG_CHANNEL_EVENT); - result.writeShort(channelId); - result.writeByte(eventId); - result.writeString(userName); - sendInChannel(channelId, result); + MessageOut msg(CPMSG_CHANNEL_EVENT); + msg.writeShort(channel->getId()); + msg.writeByte(eventId); + msg.writeString(userName); + sendInChannel(channel, msg); } -void ChatHandler::sendInChannel(short channelId, MessageOut &msg) +void ChatHandler::sendInChannel(ChatChannel *channel, MessageOut &msg) { - // TODO: b_lindeijer: Instead of looping through the channel users for each - // connected client, it would be much better to directly associate - // the connected clients with the channel. - - const std::vector &users = - chatChannelManager->getUserListInChannel(channelId); + const ChatChannel::ChannelUsers &users = channel->getUserList(); for (NetComputers::iterator i = clients.begin(), i_end = clients.end(); i != i_end; ++i) @@ -776,18 +768,18 @@ void ChatHandler::sendGuildRejoin(ChatClient &client) } } -void ChatHandler::sendUserJoined(short channelId, const std::string &name) +void ChatHandler::sendUserJoined(ChatChannel *channel, const std::string &name) { MessageOut msg(CPMSG_USERJOINED); - msg.writeShort(channelId); + msg.writeShort(channel->getId()); msg.writeString(name); - sendInChannel(channelId, msg); + sendInChannel(channel, msg); } -void ChatHandler::sendUserLeft(short channelId, const std::string &name) +void ChatHandler::sendUserLeft(ChatChannel *channel, const std::string &name) { MessageOut msg(CPMSG_USERLEFT); - msg.writeShort(channelId); + msg.writeShort(channel->getId()); msg.writeString(name); - sendInChannel(channelId, msg); + sendInChannel(channel, msg); } diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp index 6e8bf29a..72f0ef31 100644 --- a/src/chat-server/chathandler.hpp +++ b/src/chat-server/chathandler.hpp @@ -35,7 +35,8 @@ class ChatClient; * as well as guild chat. The only form of chat not handled by this server is * local chat, which is handled by the game server. * - * TODO: Extend with handling of team chat once teams are implemented. + * @todo b_lindeijer: Extend this class with handling of team chat once + * teams are implemented. */ class ChatHandler : public ConnectionHandler { @@ -55,8 +56,11 @@ class ChatHandler : public ConnectionHandler /** * Tell a list of users about an event in a chatchannel about a player. + * + * @param channel the channel to send the message in, must not be NULL + * @param userName the name of the player the event applies to */ - void warnUsersAboutPlayerEventInChat(short channelId, + void warnUsersAboutPlayerEventInChat(ChatChannel *channel, const std::string &userName, char eventId); @@ -143,9 +147,20 @@ class ChatHandler : public ConnectionHandler const std::string &text); /** - * Send packet to every client in a registered channel. + * Sends a message to every client in a registered channel. O(c*u), + * where c is the amount of connected clients and u the + * number of users in the given channel. + * + * @param channel the channel to send the message in, must not be NULL + * @param msg the message to be sent + * + * @todo b_lindeijer: Currently this method is looping through + * the channel users for each connected client in order to + * determine whether it should receive the message. It would be + * much better to directly associate the connected clients with + * the channel. */ - void sendInChannel(short channelId, MessageOut &); + void sendInChannel(ChatChannel *channel, MessageOut &msg); /** * Removes outdated pending logins. These are connected clients that @@ -155,13 +170,19 @@ class ChatHandler : public ConnectionHandler /** * Send user joined message. + * + * @param channel the channel to send the message in, must not be NULL + * @param name the name of the user who joined */ - void sendUserJoined(short channelId, const std::string &name); + void sendUserJoined(ChatChannel *channel, const std::string &name); /** * Send user left message. + * + * @param channel the channel to send the message in, must not be NULL + * @param name the name of the user who left */ - void sendUserLeft(short channelId, const std::string &name); + void sendUserLeft(ChatChannel *channel, const std::string &name); }; /** diff --git a/src/game-server/map.hpp b/src/game-server/map.hpp index b0840b2c..000913c5 100644 --- a/src/game-server/map.hpp +++ b/src/game-server/map.hpp @@ -171,8 +171,8 @@ class Map /** * Find a path from one location to the next. */ - std::list - findPath(int startX, int startY, int destX, int destY, int maxCost = 20); + std::list findPath(int startX, int startY, + int destX, int destY, int maxCost = 20); private: int width, height; -- cgit v1.2.3-60-g2f50