summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorDavid Athay <ko2fan@gmail.com>2008-04-01 17:10:01 +0000
committerDavid Athay <ko2fan@gmail.com>2008-04-01 17:10:01 +0000
commit7d05004c8264eeee8aa1e9a5f0bc6a1126bc793a (patch)
tree15c075dd93f7099ad5e1e791f68f38aad758082d /src
parent7e9bb1451b05480f30be2edfd0b19e7b1e487932 (diff)
downloadmanaserv-7d05004c8264eeee8aa1e9a5f0bc6a1126bc793a.tar.gz
manaserv-7d05004c8264eeee8aa1e9a5f0bc6a1126bc793a.tar.xz
manaserv-7d05004c8264eeee8aa1e9a5f0bc6a1126bc793a.zip
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
Diffstat (limited to 'src')
-rw-r--r--src/account-server/dalstorage.cpp4
-rw-r--r--src/account-server/dalstoragesql.hpp9
-rw-r--r--src/chat-server/chatchannel.cpp11
-rw-r--r--src/chat-server/chatchannel.hpp9
-rw-r--r--src/chat-server/chatchannelmanager.cpp88
-rw-r--r--src/chat-server/chatchannelmanager.hpp43
-rw-r--r--src/chat-server/chathandler.cpp48
7 files changed, 106 insertions, 106 deletions
diff --git a/src/account-server/dalstorage.cpp b/src/account-server/dalstorage.cpp
index 785e894..c97aa48 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 16d0067..ea7f620 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 87c2a17..96de733 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 d92f3c6..423bf46 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 11fa059..36b8873 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 423e94e..c6cbf5d 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 65e5cca..d6a21ca 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;
}