summaryrefslogtreecommitdiff
path: root/src/chat-server
diff options
context:
space:
mode:
authorDavid Athay <ko2fan@gmail.com>2008-02-28 12:33:19 +0000
committerDavid Athay <ko2fan@gmail.com>2008-02-28 12:33:19 +0000
commit49c153eec0129fcb7afb651f1407a493e51134f6 (patch)
treeb48a211c4b6c9866e2bf12a860fd775379e235a0 /src/chat-server
parent2c6abfdf3ccfca0650def767bf024f8496928a83 (diff)
downloadmanaserv-49c153eec0129fcb7afb651f1407a493e51134f6.tar.gz
manaserv-49c153eec0129fcb7afb651f1407a493e51134f6.tar.bz2
manaserv-49c153eec0129fcb7afb651f1407a493e51134f6.tar.xz
manaserv-49c153eec0129fcb7afb651f1407a493e51134f6.zip
Work in Progress commit of guilds.
Diffstat (limited to 'src/chat-server')
-rw-r--r--src/chat-server/chatchannelmanager.cpp16
-rw-r--r--src/chat-server/chathandler.cpp268
-rw-r--r--src/chat-server/chathandler.hpp48
-rw-r--r--src/chat-server/guild.cpp78
-rw-r--r--src/chat-server/guild.hpp117
-rw-r--r--src/chat-server/guildmanager.cpp124
-rw-r--r--src/chat-server/guildmanager.hpp95
7 files changed, 632 insertions, 114 deletions
diff --git a/src/chat-server/chatchannelmanager.cpp b/src/chat-server/chatchannelmanager.cpp
index fe4a48d9..11fa059f 100644
--- a/src/chat-server/chatchannelmanager.cpp
+++ b/src/chat-server/chatchannelmanager.cpp
@@ -46,17 +46,10 @@ ChatChannelManager::registerPublicChannel(const std::string &channelName,
const std::string &channelAnnouncement,
const std::string &channelPassword)
{
- /* FIXME: This code is ill-designed. If the highest ID is already in use,
- then it is impossible to create new channels, even if there are some
- unused IDs. */
int channelId = 1;
for (ChatChannelIterator i = mChatChannels.begin(),
end = mChatChannels.end(); i != end; ++i)
{
- // 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 < MAX_PUBLIC_CHANNELS_RANGE)
@@ -65,7 +58,7 @@ ChatChannelManager::registerPublicChannel(const std::string &channelName,
}
}
- // Too much channels registered
+ // Too many channels registered
if (channelId >= MAX_PUBLIC_CHANNELS_RANGE)
return 0;
@@ -84,21 +77,20 @@ ChatChannelManager::registerPrivateChannel(const std::string &channelName,
const std::string &channelAnnouncement,
const std::string &channelPassword)
{
- // FIXME: see above.
int channelId = MAX_PUBLIC_CHANNELS_RANGE;
for (ChatChannelIterator i = mChatChannels.begin(),
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 >= MAX_PRIVATE_CHANNELS_RANGE) return 0;
+ // Too many channels registered
+ if (channelId >= MAX_PRIVATE_CHANNELS_RANGE)
+ return 0;
// Register Channel
mChatChannels.insert(std::make_pair(channelId,
diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp
index 0cc91acc..d4db3a74 100644
--- a/src/chat-server/chathandler.cpp
+++ b/src/chat-server/chathandler.cpp
@@ -24,9 +24,8 @@
#include <list>
#include "defines.h"
-#include "account-server/guild.hpp"
-#include "account-server/guildmanager.hpp"
-#include "account-server/serverhandler.hpp"
+#include "chat-server/guild.hpp"
+#include "chat-server/guildmanager.hpp"
#include "chat-server/chatchannelmanager.hpp"
#include "chat-server/chatclient.hpp"
#include "chat-server/chathandler.hpp"
@@ -162,6 +161,26 @@ void ChatHandler::processMessage(NetComputer *comp, MessageIn &message)
case PCMSG_DISCONNECT:
handleDisconnectMessage(computer, message);
break;
+
+ case PCMSG_GUILD_CREATE:
+ handleGuildCreation(computer, message);
+ break;
+
+ case PCMSG_GUILD_INVITE:
+ handleGuildInvitation(computer, message);
+ break;
+
+ case PCMSG_GUILD_ACCEPT:
+ handleGuildAcceptInvite(computer, message);
+ break;
+
+ case PCMSG_GUILD_GET_MEMBERS:
+ handleGuildRetrieveMembers(computer, message);
+ break;
+
+ case PCMSG_GUILD_QUIT:
+ handleGuildQuit(computer, message);
+ break;
default:
LOG_WARN("ChatHandler::processMessage, Invalid message type"
@@ -312,13 +331,11 @@ ChatHandler::handleRegisterChannelMessage(ChatClient &client, MessageIn &msg)
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
-#if 0
else if (guildManager->doesExist(channelName))
{
// Channel already exists
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
-#endif
else
{
// We attempt to create a new channel
@@ -386,28 +403,6 @@ ChatHandler::handleUnregisterChannelMessage(ChatClient &client, MessageIn &msg)
else
reply.writeByte(ERRMSG_FAILURE);
}
-/* The chat server should not access directly to the objects of the account
- server, so that they can be splitted later, if needed. */
-#if 0
- else if (guildManager->doesExist(channelName))
- {
- Guild *guild = guildManager->findByName(channelName);
- if (guild->checkLeader(character.get()))
- {
- // TODO: b_lindeijer: I think it would be better if guild
- // channels were removed in response to a guild being
- // removed, as opposed to removing a guild because its
- // channel disappears.
- chatChannelManager->removeChannel(channelId);
- guildManager->removeGuild(guild->getId());
- reply.writeByte(ERRMSG_OK);
- }
- else
- {
- reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
- }
- }
-#endif
else
{
reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
@@ -455,14 +450,6 @@ void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg)
short channelId = chatChannelManager->getChannelId(channelName);
ChatChannel *channel = chatChannelManager->getChannel(channelId);
-#if 0
- // TODO: b_lindeijer: Currently, the client has to join its guild channels
- // explicitly by sending 'enter channel' messages. This should be
- // changed to implicitly joining relevant guild channels right after
- // login.
- Guild *guild = guildManager->findByName(channelName);
-#endif
-
if (!channelId || !channel)
{
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
@@ -473,25 +460,10 @@ void ChatHandler::handleEnterChannelMessage(ChatClient &client, MessageIn &msg)
// Incorrect password (should probably have its own return value)
reply.writeByte(ERRMSG_INVALID_ARGUMENT);
}
-#if 0
- else if (guild && !guild->checkInGuild(client.characterName))
- {
- // Player tried to join a guild channel of a guild he's not a member of
- reply.writeByte(ERRMSG_INVALID_ARGUMENT);
- }
-#endif
else
{
if (channel->addUser(&client))
{
-#if 0
- // In the case of a guild, send user joined message.
- if (guild)
- {
- sendUserJoined(channel, client.characterName);
- }
-#endif
-
reply.writeByte(ERRMSG_OK);
// The user entered the channel, now give him the channel
// id, the announcement string and the user list.
@@ -547,20 +519,6 @@ ChatHandler::handleQuitChannelMessage(ChatClient &client, MessageIn &msg)
warnUsersAboutPlayerEventInChat(channel,
client.characterName,
CHAT_EVENT_LEAVING_PLAYER);
-
-#if 0
- // 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);
- }
-#endif
}
client.send(reply);
@@ -592,15 +550,10 @@ ChatHandler::handleListChannelUsersMessage(ChatClient &client, MessageIn &msg)
{
MessageOut reply(CPMSG_LIST_CHANNELUSERS_RESPONSE);
- // TODO: b_lindeijer: Since it only makes sense to ask for the list of
- // users in a channel you're in, this message should really take
- // a channel id instead.
- std::string channelName = msg.readString();
-
- int channelId = chatChannelManager->getChannelId(channelName);
+ int channelId = msg.readLong();
ChatChannel *channel = chatChannelManager->getChannel(channelId);
- reply.writeString(channelName);
+ reply.writeLong(channelId);
if (channel)
{
@@ -626,6 +579,162 @@ ChatHandler::handleDisconnectMessage(ChatClient &client, MessageIn &msg)
}
void
+ChatHandler::handleGuildCreation(ChatClient &client, MessageIn &msg)
+{
+ MessageOut reply(CPMSG_GUILD_CREATE_RESPONSE);
+
+ // Check if guild already exists and if so, return error
+ std::string guildName = msg.readString();
+ if (!guildManager->doesExist(guildName))
+ {
+ // Guild doesnt already exist so create it
+ guildManager->createGuild(guildName, client.characterName);
+ reply.writeByte(ERRMSG_OK);
+ Guild *guild = guildManager->findByName(guildName);
+ reply.writeShort(guild->getId());
+ reply.writeString(guildName);
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_ALREADY_TAKEN);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::handleGuildInvitation(ChatClient &client, MessageIn &msg)
+{
+ MessageOut reply(CPMSG_GUILD_INVITE_RESPONSE);
+ MessageOut invite(CPMSG_GUILD_INVITED);
+
+ // send an invitation from sender to character to join guild
+ int guildId = msg.readShort();
+ std::string character = msg.readString();
+
+ // get the chat client and the guild
+ ChatClient *invitedClient = mPlayerMap[character];
+ Guild *guild = guildManager->findById(guildId);
+
+ if (invitedClient && guild)
+ {
+ // check permissions of inviter
+ if (guild->checkLeader(client.characterName))
+ {
+ // send the name of the inviter and the name of the guild
+ // that the character has been invited to join
+ std::string senderName = client.characterName;
+ std::string guildName = guild->getName();
+ invite.writeString(senderName);
+ invite.writeString(guildName);
+ invite.writeShort(guildId);
+ invitedClient->send(invite);
+ reply.writeByte(ERRMSG_OK);
+
+ // add member to list of invited members to the guild
+ guild->addInvited(character);
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::handleGuildAcceptInvite(ChatClient &client, MessageIn &msg)
+{
+ MessageOut reply(CPMSG_GUILD_ACCEPT_RESPONSE);
+ short guildId = msg.readShort();
+
+ // check guild exists and that member was invited
+ // then add them as guild member
+ // and remove from invite list
+ Guild *guild = guildManager->findById(guildId);
+ if (guild)
+ {
+ if (guild->checkInvited(client.characterName))
+ {
+ guild->addMember(client.characterName);
+ reply.writeByte(ERRMSG_OK);
+ reply.writeShort(guild->getId());
+ reply.writeString(guild->getName());
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::handleGuildRetrieveMembers(ChatClient &client, MessageIn &msg)
+{
+ MessageOut reply(CPMSG_GUILD_GET_MEMBERS_RESPONSE);
+ short guildId = msg.readShort();
+ Guild *guild = guildManager->findById(guildId);
+
+ // check for valid guild
+ // write a list of member names that belong to the guild
+ if (guild)
+ {
+ reply.writeByte(ERRMSG_OK);
+ for(int i = 0; i < guild->totalMembers(); ++i)
+ {
+ reply.writeString(guild->getMember(i));
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::handleGuildQuit(ChatClient &client, MessageIn &msg)
+{
+ MessageOut reply(CPMSG_GUILD_QUIT_RESPONSE);
+ short guildId = msg.readShort();
+ Guild *guild = guildManager->findById(guildId);
+
+ // check for valid guild
+ // check the member is in the guild
+ // remove the member from the guild
+ if (guild)
+ {
+ if (guild->checkInGuild(client.characterName))
+ {
+ reply.writeByte(ERRMSG_OK);
+ guild->removeMember(client.characterName);
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
ChatHandler::sayToPlayer(ChatClient &computer, const std::string &playerName,
const std::string &text)
{
@@ -668,23 +777,6 @@ void ChatHandler::sendInChannel(ChatChannel *channel, MessageOut &msg)
}
}
-void ChatHandler::sendGuildEnterChannel(const MessageOut &msg,
- const std::string &name)
-{
- // TODO: b_lindeijer: This method is just an inefficient way to send a
- // message to a player with a certain name. Would be good to get
- // rid of it.
- for (NetComputers::iterator i = clients.begin(), i_end = clients.end();
- i != i_end; ++i)
- {
- if (static_cast< ChatClient * >(*i)->characterName == name)
- {
- (*i)->send(msg);
- break;
- }
- }
-}
-
void ChatHandler::sendGuildInvite(const std::string &invitedName,
const std::string &inviterName,
const std::string &guildName)
diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp
index 1b44772d..30f83b94 100644
--- a/src/chat-server/chathandler.hpp
+++ b/src/chat-server/chathandler.hpp
@@ -51,6 +51,11 @@ class ChatHandler : public ConnectionHandler
std::string character;
unsigned char level;
};
+
+ /**
+ * Map the chat clients to the characters name
+ */
+ std::map<std::string, ChatClient*> mPlayerMap;
public:
@@ -72,20 +77,6 @@ class ChatHandler : public ConnectionHandler
char eventId);
/**
- * Send chat and guild info to chat client, so that they can join the
- * correct channels.
- */
- void sendGuildEnterChannel(const MessageOut &msg,
- const std::string &name);
-
- /**
- * Send guild invite.
- */
- void sendGuildInvite(const std::string &invitedName,
- const std::string &inviterName,
- const std::string &guildName);
-
- /**
* Called by TokenCollector when a client wrongly connected.
*/
void deletePendingClient(ChatClient *);
@@ -120,6 +111,20 @@ class ChatHandler : public ConnectionHandler
* Send messages for each guild the character belongs to.
*/
void sendGuildRejoin(ChatClient &computer);
+
+ /**
+ * Send chat and guild info to chat client, so that they can join the
+ * correct channels.
+ */
+ void sendGuildEnterChannel(const MessageOut &msg,
+ const std::string &name);
+
+ /**
+ * Send guild invite.
+ */
+ void sendGuildInvite(const std::string &invitedName,
+ const std::string &inviterName,
+ const std::string &guildName);
private:
/**
@@ -156,6 +161,21 @@ class ChatHandler : public ConnectionHandler
void
handleDisconnectMessage(ChatClient &client, MessageIn &msg);
+
+ void
+ handleGuildCreation(ChatClient &client, MessageIn &msg);
+
+ void
+ handleGuildInvitation(ChatClient &client, MessageIn &msg);
+
+ void
+ handleGuildAcceptInvite(ChatClient &client, MessageIn &msg);
+
+ void
+ handleGuildRetrieveMembers(ChatClient &client, MessageIn &msg);
+
+ void
+ handleGuildQuit(ChatClient &client, MessageIn &msg);
/**
* Tell the player to be more polite.
diff --git a/src/chat-server/guild.cpp b/src/chat-server/guild.cpp
new file mode 100644
index 00000000..95b09c43
--- /dev/null
+++ b/src/chat-server/guild.cpp
@@ -0,0 +1,78 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: guild.cpp 3549 2007-08-30 16:20:33Z gmelquio $
+ */
+#include "guild.hpp"
+
+
+Guild::Guild(const std::string &name) :
+mName(name)
+{
+}
+
+Guild::~Guild()
+{
+}
+
+void Guild::addMember(const std::string &playerName)
+{
+ mMembers.push_back(playerName);
+}
+
+void Guild::removeMember(const std::string &playerName)
+{
+ mMembers.remove(playerName);
+}
+
+bool Guild::checkLeader(const std::string &playerName)
+{
+ std::string leaderName = mMembers.front();
+ return leaderName == playerName;
+}
+
+bool Guild::checkInvited(const std::string &playerName)
+{
+ return std::find(mInvited.begin(), mInvited.end(), playerName) != mInvited.end();
+}
+
+void Guild::addInvited(const std::string &playerName)
+{
+ mInvited.push_back(playerName);
+}
+
+const std::string& Guild::getMember(int i) const
+{
+ int x = 0;
+ for (guildMembers::const_iterator itr = mMembers.begin();
+ itr != mMembers.end();
+ ++itr, ++x)
+ {
+ if (x == i)
+ {
+ return (*itr);
+ }
+ }
+ return NULL;
+}
+
+bool Guild::checkInGuild(const std::string &playerName)
+{
+ return std::find(mMembers.begin(), mMembers.end(), playerName) != mMembers.end();
+}
diff --git a/src/chat-server/guild.hpp b/src/chat-server/guild.hpp
new file mode 100644
index 00000000..dd54d8cc
--- /dev/null
+++ b/src/chat-server/guild.hpp
@@ -0,0 +1,117 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: guild.hpp 3549 2007-08-30 16:20:33Z gmelquio $
+ */
+
+#ifndef _TMWSERV_CHATSERVER_GUILD_H_
+#define _TMWSERV_CHATSERVER_GUILD_H_
+
+#include <string>
+#include <list>
+
+/**
+ * A guild and its members.
+ */
+class Guild
+{
+ public:
+ typedef std::list<std::string> guildMembers;
+
+ /**
+ * Constructor.
+ */
+ Guild(const std::string &name);
+
+ /**
+ * Destructor.
+ */
+ ~Guild();
+
+ /**
+ * Add a member to the guild.
+ */
+ void addMember(const std::string &playerName);
+
+ /**
+ * Remove a member from the guild.
+ */
+ void removeMember(const std::string &playerName);
+
+ /**
+ * Check player is the leader of the guild.
+ */
+ bool checkLeader(const std::string &playerName);
+
+ /**
+ * Set the ID of the guild.
+ */
+ void setId(int id)
+ { mId = id; }
+
+ /**
+ * Check if player has been invited to the guild.
+ */
+ bool checkInvited(const std::string &playerName);
+
+ /**
+ * Add a player to the invite list.
+ */
+ void addInvited(const std::string &playerName);
+
+ /**
+ * Returns the name of the guild.
+ */
+ const std::string& getName() const
+ { return mName; }
+
+ /**
+ * Returns the ID of the guild.
+ */
+ int getId() const
+ { return mId; }
+
+ /**
+ * Returns the total number of members in the guild.
+ */
+ int totalMembers() const
+ { return mMembers.size(); }
+
+ /**
+ * Get a member in the guild.
+ */
+ const std::string& getMember(int i) const;
+
+ /**
+ * Find member by name.
+ */
+ bool checkInGuild(const std::string &playerName);
+
+ /**
+ * Return the ID of the guild leader.
+ */
+
+ private:
+ short mId;
+ std::string mName;
+ std::list<std::string> mMembers;
+ std::list<std::string> mInvited;
+};
+
+#endif
diff --git a/src/chat-server/guildmanager.cpp b/src/chat-server/guildmanager.cpp
new file mode 100644
index 00000000..fc3580f1
--- /dev/null
+++ b/src/chat-server/guildmanager.cpp
@@ -0,0 +1,124 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: guildmanager.cpp 3549 2007-08-30 16:20:33Z gmelquio $
+ */
+
+#include "guildmanager.hpp"
+#include "guild.hpp"
+#include "account-server/dalstorage.hpp"
+
+GuildManager::GuildManager()
+{
+ // Load stored guilds from db
+ mGuilds = storage->getGuildList();
+}
+
+GuildManager::~GuildManager()
+{
+ for (std::list<Guild*>::iterator itr = mGuilds.begin();
+ itr != mGuilds.end(); ++itr)
+ {
+ delete *itr;
+ }
+ mGuilds.clear();
+}
+
+short GuildManager::createGuild(const std::string &name, const std::string &playerName)
+{
+ Guild *guild = new Guild(name);
+ // Add guild to db
+ storage->addGuild(guild);
+
+ // Make sure to add guild to mGuilds before searching for it to add the
+ // player
+ mGuilds.push_back(guild);
+ addGuildMember(guild->getId(), playerName);
+
+ return guild->getId();
+}
+
+void GuildManager::removeGuild(short guildId)
+{
+ Guild *guild = findById(guildId);
+ if (!guild)
+ return;
+ storage->removeGuild(guild);
+ mGuilds.remove(guild);
+ delete guild;
+}
+
+void GuildManager::addGuildMember(short guildId, const std::string &playerName)
+{
+ Guild *guild = findById(guildId);
+ if (!guild)
+ return;
+ storage->addGuildMember(guildId, playerName);
+ guild->addMember(playerName);
+}
+
+void GuildManager::removeGuildMember(short guildId, const std::string &playerName)
+{
+ Guild *guild = findById(guildId);
+ if (!guild)
+ return;
+ storage->removeGuildMember(guildId, playerName);
+ guild->removeMember(playerName);
+ if(guild->totalMembers() == 0)
+ {
+ removeGuild(guildId);
+ }
+}
+
+Guild *GuildManager::findById(short id)
+{
+ Guild *guild;
+ for (std::list<Guild*>::iterator itr = mGuilds.begin(),
+ itr_end = mGuilds.end();
+ itr != itr_end; ++itr)
+ {
+ guild = (*itr);
+ if (guild->getId() == id)
+ {
+ return guild;
+ }
+ }
+ return NULL;
+}
+
+Guild *GuildManager::findByName(const std::string &name)
+{
+ Guild *guild;
+ for (std::list<Guild*>::iterator itr = mGuilds.begin(),
+ itr_end = mGuilds.end();
+ itr != itr_end; ++itr)
+ {
+ guild = (*itr);
+ if (guild->getName() == name)
+ {
+ return guild;
+ }
+ }
+ return NULL;
+}
+
+bool GuildManager::doesExist(const std::string &name)
+{
+ return findByName(name) != NULL;
+}
diff --git a/src/chat-server/guildmanager.hpp b/src/chat-server/guildmanager.hpp
new file mode 100644
index 00000000..594a2991
--- /dev/null
+++ b/src/chat-server/guildmanager.hpp
@@ -0,0 +1,95 @@
+/*
+ * The Mana World Server
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or any later version.
+ *
+ * The Mana World is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
+ * more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with The Mana World; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ *
+ * $Id: guildmanager.hpp 3549 2007-08-30 16:20:33Z gmelquio $
+ */
+#ifndef TMW_CHATSERVER_GUILDMANAGER_H
+#define TMW_CHATSERVER_GUILDMANAGER_H
+
+#include <list>
+#include <string>
+
+class Guild;
+
+/**
+ * Guild manager takes care of creating, removing and modifying guilds.
+ */
+class GuildManager
+{
+ public:
+ /**
+ * Constructor.
+ */
+ GuildManager();
+
+ /**
+ * Destructor.
+ */
+ ~GuildManager();
+
+ /**
+ * Creates a guild.
+ */
+ short createGuild(const std::string &name, const std::string &playerName);
+
+ /**
+ * Removes a guild.
+ */
+ void removeGuild(short guildId);
+
+ /**
+ * Adds a member to a guild.
+ */
+ void addGuildMember(short guildId, const std::string &playerName);
+
+ /**
+ * Removes a member from a guild.
+ */
+ void removeGuildMember(short guildId, const std::string &playerName);
+
+ /**
+ * Returns the guild with the given id. O(n)
+ *
+ * @todo <b>b_lindeijer:</b> Since this method is used so often, its
+ * efficiency should be improved, probably by storing the guilds
+ * in a map<int,Guild*> instead of list<Guild*>.
+ *
+ * @return the guild with the given id, or NULL if it doesn't exist
+ */
+ Guild *findById(short id);
+
+ /**
+ * Returns the guild with the given name. O(n)
+ *
+ * @return the guild with the given name, or NULL if it doesn't exist
+ */
+ Guild *findByName(const std::string &name);
+
+ /**
+ * Returns whether a guild exists.
+ */
+ bool doesExist(const std::string &name);
+
+ private:
+ std::list<Guild*> mGuilds;
+};
+
+extern GuildManager *guildManager;
+
+#endif