diff options
author | Yohann Ferreira <bertram@cegetel.net> | 2006-01-18 00:09:21 +0000 |
---|---|---|
committer | Yohann Ferreira <bertram@cegetel.net> | 2006-01-18 00:09:21 +0000 |
commit | 2c1461de62e85772143fd58b7a13f1a8a964c930 (patch) | |
tree | ce793467c5ddaba927260b223108b8f1cb81bd06 /src | |
parent | 4871897d9f2b2155bcc71c5dafd925983edf8127 (diff) | |
download | manaserv-2c1461de62e85772143fd58b7a13f1a8a964c930.tar.gz manaserv-2c1461de62e85772143fd58b7a13f1a8a964c930.tar.bz2 manaserv-2c1461de62e85772143fd58b7a13f1a8a964c930.tar.xz manaserv-2c1461de62e85772143fd58b7a13f1a8a964c930.zip |
Chat channeling commit part 2.
Diffstat (limited to 'src')
-rw-r--r-- | src/chatchannel.cpp | 63 | ||||
-rw-r--r-- | src/chatchannel.h | 51 | ||||
-rw-r--r-- | src/chatchannelmanager.cpp | 125 | ||||
-rw-r--r-- | src/chatchannelmanager.h | 52 | ||||
-rw-r--r-- | src/dalstorage.cpp | 34 | ||||
-rw-r--r-- | src/dalstorage.h | 4 | ||||
-rw-r--r-- | src/dalstoragesql.h | 14 | ||||
-rw-r--r-- | src/defines.h | 6 | ||||
-rw-r--r-- | src/storage.h | 5 |
9 files changed, 237 insertions, 117 deletions
diff --git a/src/chatchannel.cpp b/src/chatchannel.cpp index 8e02b868..8f5e22a8 100644 --- a/src/chatchannel.cpp +++ b/src/chatchannel.cpp @@ -23,16 +23,12 @@ #include "chatchannel.h" -ChatChannel::ChatChannel(): - mChannelId(0), - mChannelName("") -{ - mRegisteredUsers.clear(); -} - -ChatChannel::ChatChannel(short channelId, std::string channelName): - mChannelId(channelId), - mChannelName(channelName) +ChatChannel::ChatChannel(const std::string channelName, + const std::string channelAnnouncement = "", + const std::string channelPassword = ""): + mChannelName(channelName), + mChannelAnnouncement(channelAnnouncement), + mChannelPassword(channelPassword) { mRegisteredUsers.clear(); } @@ -43,54 +39,69 @@ ChatChannel::~ChatChannel() } -const std::string -ChatChannel::getName() +const std::string& +ChatChannel::getName() const { return mChannelName; } +const std::string& +ChatChannel::getAnnouncement() const +{ + return mChannelAnnouncement; +} + +const std::string& +ChatChannel::getPassword() const +{ + return mChannelPassword; +} void -ChatChannel::setName(std::string channelName) +ChatChannel::setName(const std::string channelName) { mChannelName = channelName; } - -const short -ChatChannel::getChannelId() +void +ChatChannel::setAnnouncement(const std::string channelAnnouncement) { - return mChannelId; + mChannelAnnouncement = channelAnnouncement; } +void +ChatChannel::setPassword(const std::string channelPassword) +{ + mChannelPassword = channelPassword; +} -const std::list<std::string> -ChatChannel::getUserList() +std::vector<tmwserv::BeingPtr> +ChatChannel::getUserList() const { return mRegisteredUsers; } bool -ChatChannel::addUserInChannel(std::string playerName) +ChatChannel::addUserInChannel(tmwserv::BeingPtr beingPtr) { // Check if the user already exists in the channel - for (std::list<std::string>::iterator i = mRegisteredUsers.begin(); i != mRegisteredUsers.end();) + for (std::vector<tmwserv::BeingPtr>::iterator i = mRegisteredUsers.begin(); i != mRegisteredUsers.end();) { - if ( *i == playerName ) return false; + if ( i->get() == beingPtr.get() ) return false; ++i; } - mRegisteredUsers.push_back(playerName); + mRegisteredUsers.push_back(beingPtr); return true; } bool -ChatChannel::removeUserFromChannel(std::string playerName) +ChatChannel::removeUserFromChannel(tmwserv::BeingPtr beingPtr) { - for (std::list<std::string>::iterator i = mRegisteredUsers.begin(); i != mRegisteredUsers.end();) + for (std::vector<tmwserv::BeingPtr>::iterator i = mRegisteredUsers.begin(); i != mRegisteredUsers.end();) { - if ( *i == playerName ) + if ( i->get() == beingPtr.get() ) { mRegisteredUsers.erase(i); return true; diff --git a/src/chatchannel.h b/src/chatchannel.h index 0b35ee9f..4baf0933 100644 --- a/src/chatchannel.h +++ b/src/chatchannel.h @@ -24,9 +24,11 @@ #ifndef _TMWSERV_CHATCHANNEL_H_ #define _TMWSERV_CHATCHANNEL_H_ -#include <list> +#include <vector> #include <string> +#include "being.h" + class ChatChannel { public: @@ -34,9 +36,9 @@ class ChatChannel { /** * Constructors */ - ChatChannel(); - - ChatChannel(short channelId, std::string channelName); + ChatChannel(const std::string channelName, + const std::string ChannelAnnouncement, + const std::string ChannelPassword); /** * Destructor @@ -46,49 +48,68 @@ class ChatChannel { /** * Get the name of the channel */ - const std::string getName(); + const std::string& getName() const; + + /** + * Get the Announcement string of the channel + */ + const std::string& getAnnouncement() const; + + /** + * Get the password of the channel + */ + const std::string& getPassword() const; /** * Set the name of the channel */ - void setName(std::string channelName); + void setName(const std::string channelName); /** - * Get the id of the channel + * Set the Announcement string of the channel */ - const short getChannelId(); + void setAnnouncement(const std::string channelAnnouncement); + + /** + * Set the password of the channel + */ + void setPassword(const std::string channelPassword); /** * Get the list of the users registered in the channel */ - const std::list<std::string> getUserList(); + std::vector<tmwserv::BeingPtr> getUserList() const; /** * Add a user in the channel */ - bool addUserInChannel(std::string playerName); + bool addUserInChannel(tmwserv::BeingPtr beingPtr); /** * Remove a user from the channel. */ - bool removeUserFromChannel(std::string playerName); + bool removeUserFromChannel(tmwserv::BeingPtr beingPtr); private: + /** + * The Channel's name + */ + std::string mChannelName; /** - * The channel id which must be unique. + * The Channel's name */ - short mChannelId; + std::string mChannelAnnouncement; /** * The Channel's name */ - std::string mChannelName; + std::string mChannelPassword; /** * The registered user list */ - std::list<std::string> mRegisteredUsers; + std::vector<tmwserv::BeingPtr> mRegisteredUsers; }; diff --git a/src/chatchannelmanager.cpp b/src/chatchannelmanager.cpp index f5c9707f..f7f0a181 100644 --- a/src/chatchannelmanager.cpp +++ b/src/chatchannelmanager.cpp @@ -30,76 +30,64 @@ ChatChannelManager::ChatChannelManager() { //Load stored public chat channels from db tmwserv::Storage &store = tmwserv::Storage::instance("tmw"); - std::map<short, std::string> channelList = store.getChannelList(); - - for (std::map<short, std::string>::iterator i = channelList.begin(); i != channelList.end();) - { - mChatChannels.push_back(ChatChannel(i->first,i->second)); - ++i; - } + mChatChannels = store.getChannelList(); } ChatChannelManager::~ChatChannelManager() { tmwserv::Storage &store = tmwserv::Storage::instance("tmw"); - std::map<short, std::string> channelList; - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) - { - channelList.insert(std::make_pair(i->getChannelId(), i->getName())); - ++i; - } - store.updateChannels(channelList); + store.updateChannels(mChatChannels); mChatChannels.clear(); } short -ChatChannelManager::registerPublicChannel(std::string channelName) +ChatChannelManager::registerPublicChannel(const std::string& channelName) { short channelId = 1; - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getName() == channelName ) return 0; + if ( i->second.getName() == channelName ) return 0; // We seek the highest channelId in the public range - if ( (channelId <= i->getChannelId()) && (i->getChannelId() < 1000) ) - channelId = i->getChannelId() + 1; + if ( (channelId <= i->first) && (i->first < (signed)MAX_PRIVATE_CHANNELS_RANGE) ) + channelId = i->first + 1; ++i; } // Too much channels registered - if ( channelId >= 1000 ) return 0; + if ( channelId >= (signed)MAX_PRIVATE_CHANNELS_RANGE ) return 0; // Register Channel - mChatChannels.push_back(ChatChannel(channelId,channelName)); + mChatChannels.insert(std::make_pair(channelId,ChatChannel(channelName, "", ""))); return channelId; } short -ChatChannelManager::registerPrivateChannel(std::string channelName) +ChatChannelManager::registerPrivateChannel(const std::string& channelName) { - short channelId = 1000; - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + short channelId = MAX_PRIVATE_CHANNELS_RANGE; + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getName() == channelName ) return 0; + if ( i->second.getName() == channelName ) return 0; // We seek the highest channelId in the private range - if ( (channelId <= i->getChannelId()) && (i->getChannelId() >= 1000) ) - channelId = i->getChannelId() + 1; + if ( (channelId <= i->first) && (i->first >= (signed)MAX_PRIVATE_CHANNELS_RANGE) ) + channelId = i->first + 1; ++i; } // Too much channels registered - if ( channelId >= 10000 ) return 0; + if ( channelId >= (signed)MAX_PUBLIC_CHANNELS_RANGE ) return 0; // Register Channel - mChatChannels.push_back(ChatChannel(channelId,channelName)); + mChatChannels.insert(std::make_pair(channelId, ChatChannel(channelName, "", ""))); return channelId; } bool -ChatChannelManager::removeChannel(short channelId) +ChatChannelManager::removeChannel(const short channelId) { - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getChannelId() == channelId ) + if ( i->first == channelId ) { mChatChannels.erase(i); i++; @@ -112,37 +100,70 @@ ChatChannelManager::removeChannel(short channelId) short -ChatChannelManager::getChannelId(std::string channelName) +ChatChannelManager::getChannelId(const std::string& channelName) { - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getName() == channelName ) return i->getChannelId(); + if ( i->second.getName() == channelName ) return i->first; ++i; } return 0; } -std::string -ChatChannelManager::getChannelName(short channelId) +const std::string +ChatChannelManager::getChannelName(const short channelId) { - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getChannelId() == channelId ) return i->getName(); + if ( i->first == channelId ) return i->second.getName(); ++i; } return ""; } +const std::string +ChatChannelManager::getChannelAnnouncement(const short channelId) +{ + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + { + if ( i->first == channelId ) return i->second.getAnnouncement(); + ++i; + } + return ""; +} + +const std::string +ChatChannelManager::getChannelPassword(const short channelId) +{ + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + { + if ( i->first == channelId ) return i->second.getPassword(); + ++i; + } + return ""; +} + +const ChatChannel +ChatChannelManager::getChannel(const short channelId) +{ + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + { + if ( i->first == channelId ) return i->second; + ++i; + } + return ChatChannel("", "", ""); +} + bool -ChatChannelManager::addUserInChannel(std::string playerName, short channelId) +ChatChannelManager::addUserInChannel(tmwserv::BeingPtr beingPtr, const short channelId) { - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getChannelId() == channelId ) + if ( i->first == channelId ) { - return i->addUserInChannel(playerName); + return i->second.addUserInChannel(beingPtr); } ++i; } @@ -151,15 +172,25 @@ ChatChannelManager::addUserInChannel(std::string playerName, short channelId) bool -ChatChannelManager::removeUserFromChannel(std::string playerName, short channelId) +ChatChannelManager::removeUserFromChannel(tmwserv::BeingPtr beingPtr, const short channelId) { - for (std::list<ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) { - if ( i->getChannelId() == channelId ) + if ( i->first == channelId ) { - return i->removeUserFromChannel(playerName); + return i->second.removeUserFromChannel(beingPtr); } ++i; } return false; } + +void +ChatChannelManager::removeUserFromEveryChannels(tmwserv::BeingPtr beingPtr) +{ + for (std::map<short, ChatChannel>::iterator i = mChatChannels.begin(); i != mChatChannels.end();) + { + i->second.removeUserFromChannel(beingPtr); + ++i; + } +} diff --git a/src/chatchannelmanager.h b/src/chatchannelmanager.h index 5c417698..668b1401 100644 --- a/src/chatchannelmanager.h +++ b/src/chatchannelmanager.h @@ -24,6 +24,8 @@ #ifndef _TMWSERV_CHATCHANNELHANDLER_H_ #define _TMWSERV_CHATCHANNELHANDLER_H_ +#include <map> + #include "chatchannel.h" class ChatChannelManager { @@ -46,7 +48,7 @@ public: * @return the number of the channel registered. * 0 if the registering was unsuccessful. */ - short registerPublicChannel(std::string channelName); + short registerPublicChannel(const std::string& channelName); /** * Add a private channel. @@ -54,12 +56,12 @@ public: * @return the number of the channel registered. * 0 if the registering was unsuccessful. */ - short registerPrivateChannel(std::string channelName); + short registerPrivateChannel(const std::string& channelName); /** * Remove a channel. */ - bool removeChannel(short channelId); + bool removeChannel(const short channelId); /** * get the id of a channel from its name. @@ -67,31 +69,63 @@ public: * @return the id of the channel * 0 if it was unsuccessful. */ - short getChannelId(std::string channelName); + short getChannelId(const std::string& channelName); /** * get the name of a channel from its id. * * @return the name of the channel */ - std::string getChannelName(short channelId); + const std::string getChannelName(const short channelId); + + /** + * get the announcement string of a channel from its id. + * + * @return the announcement string of the channel + */ + const std::string + ChatChannelManager::getChannelAnnouncement(const short channelId); + + /** + * get the password of a channel from its id. + * + * @return the password of the channel + */ + const std::string + ChatChannelManager::getChannelPassword(const short channelId); + + /** + * get the ChatChannel object from its id. + * + * @return the ChatChannel object + */ + const ChatChannel + ChatChannelManager::getChannel(const short channelId); /** - * Add a user in the channel + * Add a user in a channel */ - bool addUserInChannel(std::string playerName, short channelId); + bool addUserInChannel(tmwserv::BeingPtr beingPtr, const short channelId); /** * Remove a user from a channel. */ - bool removeUserFromChannel(std::string playerName, short channelId); + bool removeUserFromChannel(tmwserv::BeingPtr beingPtr, const short channelId); + + /** + * Remove a user from every channels. + * Used at logout. + */ + void removeUserFromEveryChannels(tmwserv::BeingPtr beingPtr); private: /** * The list keeping all the chat channels. + * + * The channel id must be unique. */ - std::list<ChatChannel> mChatChannels; + std::map<short, ChatChannel> mChatChannels; }; diff --git a/src/dalstorage.cpp b/src/dalstorage.cpp index d8351b0c..22b3d74a 100644 --- a/src/dalstorage.cpp +++ b/src/dalstorage.cpp @@ -21,7 +21,6 @@ */ #include <sstream> -#include <vector> #include "configuration.h" #include "dalstorage.h" @@ -503,7 +502,7 @@ DALStorage::getMapNameFromId(const unsigned int mapId) const dal::RecordSet& mapInfo = mDb->execSql(sql.str()); - // If the map return is empty then we have no choice but to return false. + // If the map return is empty then we have no choice but to return None. if (mapInfo.isEmpty()) { return "None"; } @@ -519,7 +518,7 @@ DALStorage::getMapNameFromId(const unsigned int mapId) return "None"; } -const std::map<short, std::string> +std::map<short, ChatChannel> DALStorage::getChannelList() { // If not opened already @@ -530,11 +529,11 @@ DALStorage::getChannelList() string_to<short> toShort; // The formatted datas - std::map<short, std::string> channels; + std::map<short, ChatChannel> channels; try { std::stringstream sql; - sql << "select id, name from "; + sql << "select id, name, announcement, password from "; sql << CHANNELS_TBL_NAME; sql << ";"; @@ -547,7 +546,13 @@ DALStorage::getChannelList() for ( unsigned int i = 0; i < channelInfo.rows(); ++i) { - channels.insert(std::make_pair(toShort(channelInfo(0,0)),std::string(channelInfo(0,1)))); + channels.insert(std::make_pair(toShort(channelInfo(0,0)), + ChatChannel(channelInfo(0,1), + channelInfo(0,2), + channelInfo(0,3)))); + + LOG_DEBUG("Channel loaded: " << channelInfo(0,1) + << ":" << channelInfo(0,2), 5) } return channels; @@ -561,7 +566,7 @@ DALStorage::getChannelList() } void -DALStorage::updateChannels(std::map<short, std::string> channelList) +DALStorage::updateChannels(std::map<short, ChatChannel>& channelList) { // If not opened already open(); @@ -575,19 +580,24 @@ DALStorage::updateChannels(std::map<short, std::string> channelList) mDb->execSql(sql.str()); - for ( std::map<short, std::string>::iterator i = channelList.begin(); + for ( std::map<short, ChatChannel>::iterator i = channelList.begin(); i != channelList.end();) { - // insert registered channel if id < MAX_PUBLIC_CHANNELS; - if ( i->first < /*MAX_PUBLIC_CHANNELS*/ 1000 ) + // insert registered channel if id < MAX_PUBLIC_CHANNELS_RANGE; + if ( i->first < (signed)MAX_PUBLIC_CHANNELS_RANGE ) { sql.str(""); sql << "insert into " << CHANNELS_TBL_NAME - << " (id, name)" + << " (id, name, announcement, password)" << " values (" << i->first << ", '" - << i->second << "');"; + << i->second.getName() << "', '" + << i->second.getAnnouncement() << "', '" + << i->second.getPassword() << "');"; + + LOG_DEBUG("Channel saved: " << i->second.getName() + << ":" << i->second.getAnnouncement(), 5) mDb->execSql(sql.str()); } diff --git a/src/dalstorage.h b/src/dalstorage.h index 0867ef4c..f633527d 100644 --- a/src/dalstorage.h +++ b/src/dalstorage.h @@ -123,7 +123,7 @@ class DALStorage: public Storage * Gives the list of opened public channels registered in database * @return a map of the public channels */ - const std::map<short, std::string> + std::map<short, ChatChannel> getChannelList(); /** @@ -131,7 +131,7 @@ class DALStorage: public Storage * to the one in db. */ void - updateChannels(std::map<short, std::string> channelList); + updateChannels(std::map<short, ChatChannel>& channelList); /** * Save changes to the database permanently. diff --git a/src/dalstoragesql.h b/src/dalstoragesql.h index 84e1a57f..fb214420 100644 --- a/src/dalstoragesql.h +++ b/src/dalstoragesql.h @@ -324,14 +324,20 @@ const std::string CHANNELS_TBL_NAME("tmw_channels"); const std::string SQL_CHANNELS_TABLE( "CREATE TABLE tmw_channels (" #if defined (MYSQL_SUPPORT) - "id INTEGER PRIMARY KEY," - "name VARCHAR(32) NOT NULL UNIQUE" + "id INTEGER PRIMARY KEY," + "name VARCHAR(32) NOT NULL UNIQUE," + "announcement VARCHAR(256)," + "password VARCHAR(32)" #elif defined (SQLITE_SUPPORT) "id INTEGER PRIMARY KEY," - "name TEXT NOT NULL UNIQUE" + "name TEXT NOT NULL UNIQUE," + "announcement TEXT," + "password TEXT" #elif defined (POSTGRESQL_SUPPORT) "id SERIAL PRIMARY KEY," - "name TEXT NOT NULL UNIQUE" + "name TEXT NOT NULL UNIQUE," + "announcement TEXT," + "password TEXT" #endif ");" ); diff --git a/src/defines.h b/src/defines.h index f76dba41..41ac64a8 100644 --- a/src/defines.h +++ b/src/defines.h @@ -56,6 +56,12 @@ typedef enum { // Network related const unsigned int MAX_CLIENTS = 1024, +/** + * N.B: Private channels can't have an id less + * than MAX_PUBLIC_CHANNELS_RANGE. + */ + MAX_PUBLIC_CHANNELS_RANGE = 1000, + MAX_PRIVATE_CHANNELS_RANGE = 10000, // Registering related MIN_LOGIN_LENGTH = 4, diff --git a/src/storage.h b/src/storage.h index 8006a128..23e58312 100644 --- a/src/storage.h +++ b/src/storage.h @@ -29,6 +29,7 @@ #include <list> #include "account.h" +#include "chatchannel.h" namespace tmwserv @@ -317,7 +318,7 @@ class Storage * Gives the list of opened public channels registered in database * @return a map of the public channels */ - virtual const std::map<short, std::string> + virtual std::map<short, ChatChannel> getChannelList() = 0; /** @@ -325,7 +326,7 @@ class Storage * to the one in db. */ virtual void - updateChannels(std::map<short, std::string> channelList) = 0; + updateChannels(std::map<short, ChatChannel>& channelList) = 0; /** |