diff options
-rw-r--r-- | ChangeLog | 14 | ||||
-rw-r--r-- | gameserver.cbp | 1 | ||||
-rw-r--r-- | src/account-server/dalstorage.cpp | 4 | ||||
-rw-r--r-- | src/account-server/dalstoragesql.hpp | 9 | ||||
-rw-r--r-- | src/chat-server/chatchannel.cpp | 11 | ||||
-rw-r--r-- | src/chat-server/chatchannel.hpp | 9 | ||||
-rw-r--r-- | src/chat-server/chatchannelmanager.cpp | 88 | ||||
-rw-r--r-- | src/chat-server/chatchannelmanager.hpp | 43 | ||||
-rw-r--r-- | src/chat-server/chathandler.cpp | 48 |
9 files changed, 121 insertions, 106 deletions
@@ -1,3 +1,17 @@ +2008-04-01 David Athay <ko2fan@gmail.com> + + * src/account-server/dalstorage.cpp, + src/account-server/dalstoragesql.hpp, + src/chat-server/chatchannelmanager.cpp, + src/chat-server/chatchannel.cpp, + src/chat-server/chatchannelmanager.hpp, + src/chat-server/chatchannel.hpp, src/chat-server/chathandler.cpp, + gameserver.cbp: Consolidated public and private channels into one, + which also includes guild channels. Channels now have a joinable + field, which is true for channels that are created by users, and + false for guild channels. Chatting in guild channels now works, and + non-guild members can no longer join guild channels. + 2008-03-31 Philipp Sehmisch <tmw@crushnet.org> * data/test.lua: Removed the +100 offset of NPC IDs (relict from diff --git a/gameserver.cbp b/gameserver.cbp index ce9334de..e7efd0dd 100644 --- a/gameserver.cbp +++ b/gameserver.cbp @@ -145,6 +145,7 @@ <Extensions> <code_completion /> <debugger /> + <envvars /> </Extensions> </Project> </CodeBlocks_project_file> diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp index 785e8943..c97aa487 100644 --- a/src/account-server/dalstorage.cpp +++ b/src/account-server/dalstorage.cpp @@ -697,13 +697,13 @@ DALStorage::updateChannels(std::map<unsigned short, ChatChannel>& channelList) sql.str(""); sql << "insert into " << CHANNELS_TBL_NAME - << " (id, name, announcement, password, privacy)" + << " (id, name, announcement, password, joinable)" << " values (" << i->first << ", \"" << i->second.getName() << "\", \"" << i->second.getAnnouncement() << "\", \"" << i->second.getPassword() << "\", \"" - << i->second.isPrivate() << "\");"; + << i->second.canJoin() << "\");"; LOG_DEBUG("Channel (" << i->first << ") saved: " << i->second.getName() diff --git a/src/account-server/dalstoragesql.hpp b/src/account-server/dalstoragesql.hpp index 16d0067e..ea7f620e 100644 --- a/src/account-server/dalstoragesql.hpp +++ b/src/account-server/dalstoragesql.hpp @@ -263,17 +263,20 @@ static char const *SQL_CHANNELS_TABLE = "id INTEGER PRIMARY KEY," "name VARCHAR(32) NOT NULL UNIQUE," "announcement VARCHAR(256) NOT NULL," - "password VARCHAR(32) NOT NULL" + "password VARCHAR(32) NOT NULL," + "joinable INTEGER NOT NULL" #elif defined (SQLITE_SUPPORT) "id INTEGER PRIMARY KEY," "name TEXT NOT NULL UNIQUE," "announcement TEXT NOT NULL," - "password TEXT NOT NULL" + "password TEXT NOT NULL," + "joinable INTEGER NOT NULL" #elif defined (POSTGRESQL_SUPPORT) "id SERIAL PRIMARY KEY," "name TEXT NOT NULL UNIQUE," "announcement TEXT NOT NULL," - "password TEXT NOT NULL" + "password TEXT NOT NULL," + "joinable INTEGER NOT NULL" #endif ");"; diff --git a/src/chat-server/chatchannel.cpp b/src/chat-server/chatchannel.cpp index 87c2a173..96de7335 100644 --- a/src/chat-server/chatchannel.cpp +++ b/src/chat-server/chatchannel.cpp @@ -29,11 +29,13 @@ ChatChannel::ChatChannel(int id, const std::string &name, const std::string &announcement, - const std::string &password): + const std::string &password, + bool joinable): mId(id), mName(name), mAnnouncement(announcement), - mPassword(password) + mPassword(password), + mJoinable(joinable) { } @@ -69,3 +71,8 @@ void ChatChannel::removeAllUsers() } mRegisteredUsers.clear(); } + +bool ChatChannel::canJoin() const +{ + return mJoinable; +} diff --git a/src/chat-server/chatchannel.hpp b/src/chat-server/chatchannel.hpp index d92f3c6a..423bf46f 100644 --- a/src/chat-server/chatchannel.hpp +++ b/src/chat-server/chatchannel.hpp @@ -56,7 +56,8 @@ class ChatChannel ChatChannel(int id, const std::string &name, const std::string &announcement = std::string(), - const std::string &password = std::string()); + const std::string &password = std::string(), + bool joinable = true); /** * Get the ID of the channel. @@ -131,11 +132,17 @@ class ChatChannel */ void removeAllUsers(); + /** + * Get whether a user can join this channel + */ + bool canJoin() const; + private: unsigned 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. */ + bool mJoinable; /**< Whether anyone can join. */ ChannelUsers mRegisteredUsers; /**< Users in this channel. */ }; diff --git a/src/chat-server/chatchannelmanager.cpp b/src/chat-server/chatchannelmanager.cpp index 11fa059f..36b8873b 100644 --- a/src/chat-server/chatchannelmanager.cpp +++ b/src/chat-server/chatchannelmanager.cpp @@ -29,7 +29,7 @@ #include "account-server/dalstorage.hpp" #include "chat-server/chatclient.hpp" -ChatChannelManager::ChatChannelManager() +ChatChannelManager::ChatChannelManager() : mNextChannelId(0) { // Load stored public chat channels from db mChatChannels = storage->getChannelList(); @@ -42,62 +42,20 @@ ChatChannelManager::~ChatChannelManager() } int -ChatChannelManager::registerPublicChannel(const std::string &channelName, +ChatChannelManager::createNewChannel(const std::string &channelName, const std::string &channelAnnouncement, - const std::string &channelPassword) + const std::string &channelPassword, + bool joinable) { - int channelId = 1; - for (ChatChannelIterator i = mChatChannels.begin(), - end = mChatChannels.end(); i != end; ++i) - { - // We seek the highest channelId in the public range - if (channelId <= i->first && - i->first < MAX_PUBLIC_CHANNELS_RANGE) - { - channelId = i->first + 1; - } - } - - // Too many channels registered - if (channelId >= MAX_PUBLIC_CHANNELS_RANGE) - return 0; + int channelId = nextUsable(); // Register channel mChatChannels.insert(std::make_pair(channelId, ChatChannel(channelId, channelName, channelAnnouncement, - channelPassword))); - return channelId; -} - - -int -ChatChannelManager::registerPrivateChannel(const std::string &channelName, - const std::string &channelAnnouncement, - const std::string &channelPassword) -{ - int channelId = MAX_PUBLIC_CHANNELS_RANGE; - - for (ChatChannelIterator i = mChatChannels.begin(), - end = mChatChannels.end(); i != end; ++i) - { - - // We seek the highest channelId in the private range - if (channelId <= i->first) - channelId = i->first + 1; - } - - // Too many channels registered - if (channelId >= MAX_PRIVATE_CHANNELS_RANGE) - return 0; - - // Register Channel - mChatChannels.insert(std::make_pair(channelId, - ChatChannel(channelId, - channelName, - channelAnnouncement, - channelPassword))); + channelPassword, + joinable))); return channelId; } @@ -107,6 +65,7 @@ bool ChatChannelManager::removeChannel(int channelId) if (i == mChatChannels.end()) return false; i->second.removeAllUsers(); mChatChannels.erase(i); + mChannelsNoLongerUsed.push_back(channelId); return true; } @@ -145,6 +104,20 @@ ChatChannel* ChatChannelManager::getChannel(int channelId) return NULL; } +ChatChannel* ChatChannelManager::getChannel(const std::string &name) +{ + ChatChannelIterator i_end = mChatChannels.end(); + for (ChatChannelIterator i = mChatChannels.begin(); i != i_end; ++i) + { + if (i->second.getName() == name) + { + return &(i->second); + } + } + + return NULL; +} + void ChatChannelManager::removeUserFromAllChannels(ChatClient *user) { // Local copy as they will be destroyed under our feet. @@ -161,3 +134,20 @@ bool ChatChannelManager::channelExists(int channelId) { return mChatChannels.find(channelId) != mChatChannels.end(); } + +int ChatChannelManager::nextUsable() +{ + int channelId = 0; + + if (mChannelsNoLongerUsed.size() > 0) + { + channelId = mChannelsNoLongerUsed[0]; + mChannelsNoLongerUsed.pop_front(); + } + else + { + channelId = ++mNextChannelId; + } + + return channelId; +} diff --git a/src/chat-server/chatchannelmanager.hpp b/src/chat-server/chatchannelmanager.hpp index 423e94ef..c6cbf5d7 100644 --- a/src/chat-server/chatchannelmanager.hpp +++ b/src/chat-server/chatchannelmanager.hpp @@ -26,6 +26,7 @@ #include <list> #include <map> +#include <deque> #include "chat-server/chatchannel.hpp" @@ -47,32 +48,14 @@ class ChatChannelManager ~ChatChannelManager(); /** - * Registers a public channel. Can fail if the maximum of public - * channels has already been reached or when a channel with the same - * name already exists. + * Create a new chat channel. * - * @return the ID of the registered channel, or 0 if the registering - * was unsuccessful. + * @return the ID of the registered channel */ - int registerPublicChannel(const std::string &channelName, + int createNewChannel(const std::string &channelName, const std::string &channelAnnouncement, - const std::string &channelPassword); - - /** - * Registers a private channel. Can fail if the maximum of private - * channels has already been reached or when a channel with the same - * name already exists. - * - * @todo <b>b_lindeijer:</b> 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. - */ - int registerPrivateChannel(const std::string &channelName, - const std::string &channelAnnouncement, - const std::string &channelPassword); + const std::string &channelPassword, + bool joinable); /** * Remove a channel. @@ -101,6 +84,13 @@ class ChatChannelManager ChatChannel* getChannel(int channelId); /** + * Returns the chat channel with the given channel name. + * + * @return The chat channel, or NULL when it doesn't exist. + */ + ChatChannel* getChannel(const std::string &name); + + /** * Remove a user from all channels. Used at logout. * * @see ChatChannel::removeUserFromChannel @@ -114,6 +104,11 @@ class ChatChannelManager */ bool channelExists(int channelId); + /** + * Get next usable channel ID + */ + int nextUsable(); + private: typedef std::map<unsigned short, ChatChannel> ChatChannels; typedef ChatChannels::iterator ChatChannelIterator; @@ -123,6 +118,8 @@ class ChatChannelManager * unique. */ ChatChannels mChatChannels; + int mNextChannelId; + std::deque<int> mChannelsNoLongerUsed; }; extern ChatChannelManager *chatChannelManager; diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp index 65e5ccab..d6a21ca9 100644 --- a/src/chat-server/chathandler.cpp +++ b/src/chat-server/chathandler.cpp @@ -347,21 +347,11 @@ ChatHandler::handleRegisterChannelMessage(ChatClient &client, MessageIn &msg) // We attempt to create a new channel short channelId; - // TODO: b_lindeijer: These methods should really be combined. - if (channelType) - { - channelId = chatChannelManager->registerPrivateChannel( - channelName, - channelAnnouncement, - channelPassword); - } - else - { - channelId = chatChannelManager->registerPublicChannel( + channelId = chatChannelManager->createNewChannel( channelName, channelAnnouncement, - channelPassword); - } + channelPassword, + true); if (channelId) { @@ -453,10 +443,9 @@ void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) std::string channelName = msg.readString(); std::string givenPassword = msg.readString(); - short channelId = chatChannelManager->getChannelId(channelName); - ChatChannel *channel = chatChannelManager->getChannel(channelId); + ChatChannel *channel = chatChannelManager->getChannel(channelName); - if (!channelId || !channel) + if (!channel) { reply.writeByte(ERRMSG_INVALID_ARGUMENT); } @@ -466,6 +455,10 @@ void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) // Incorrect password (should probably have its own return value) reply.writeByte(ERRMSG_INVALID_ARGUMENT); } + else if (!channel->canJoin()) + { + reply.writeByte(ERRMSG_INVALID_ARGUMENT); + } else { if (channel->addUser(&client)) @@ -473,7 +466,7 @@ void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg) 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.writeShort(channel->getId()); reply.writeString(channelName); reply.writeString(channel->getAnnouncement()); const ChatChannel::ChannelUsers &users = channel->getUserList(); @@ -596,8 +589,8 @@ ChatHandler::handleGuildCreation(ChatClient &client, MessageIn &msg) // Guild doesnt already exist so create it Guild *guild = guildManager->createGuild(guildName, client.characterName); reply.writeByte(ERRMSG_OK); - reply.writeShort(guild->getId()); reply.writeString(guildName); + reply.writeShort(guild->getId()); reply.writeByte(true); // Send autocreated channel id @@ -734,6 +727,7 @@ ChatHandler::handleGuildQuit(ChatClient &client, MessageIn &msg) if (guild->checkInGuild(client.characterName)) { reply.writeByte(ERRMSG_OK); + reply.writeShort(guildId); guild->removeMember(client.characterName); } else @@ -862,20 +856,20 @@ void ChatHandler::sendUserLeft(ChatChannel *channel, const std::string &name) int ChatHandler::joinGuildChannel(const std::string &guildName, ChatClient &client) { + int channelId = 0; // Automatically make the character join the guild chat channel - // COMMENT: I think it shouldn't need to go through channelId to - // get the channel pointer. Also need to change this to registering - // a generic channel, rather than public, once public and private - // are merged - short channelId = chatChannelManager->getChannelId(guildName); - ChatChannel *channel = chatChannelManager->getChannel(channelId); + ChatChannel *channel = chatChannelManager->getChannel(guildName); if (!channel) { // Channel doesnt exist so create it - channelId = chatChannelManager->registerPublicChannel(guildName, - "Guild Channel", ""); + channelId = chatChannelManager->createNewChannel(guildName, + "Guild Channel", "", false); channel = chatChannelManager->getChannel(channelId); } + else + { + channelId = channel->getId(); + } // Add user to the channel if (channel->addUser(&client)) @@ -885,4 +879,6 @@ int ChatHandler::joinGuildChannel(const std::string &guildName, ChatClient &clie warnUsersAboutPlayerEventInChat(channel, client.characterName, CHAT_EVENT_NEW_PLAYER); } + + return channelId; } |