summaryrefslogtreecommitdiff
path: root/src/chat-server
diff options
context:
space:
mode:
authorDavid Athay <ko2fan@gmail.com>2008-11-25 16:51:16 +0000
committerDavid Athay <ko2fan@gmail.com>2008-11-25 16:51:16 +0000
commit989af76dc2705ed371a5db3567c828fd309e5412 (patch)
tree206d1fe9908a85795567d0bcb617260cb1c82767 /src/chat-server
parent7f2f9c2a1e50fb3558418debad71a100770b888a (diff)
downloadmanaserv-989af76dc2705ed371a5db3567c828fd309e5412.tar.gz
manaserv-989af76dc2705ed371a5db3567c828fd309e5412.tar.bz2
manaserv-989af76dc2705ed371a5db3567c828fd309e5412.tar.xz
manaserv-989af76dc2705ed371a5db3567c828fd309e5412.zip
Separated out the handlers for guild and party
Diffstat (limited to 'src/chat-server')
-rw-r--r--src/chat-server/chathandler.cpp500
-rw-r--r--src/chat-server/chathandler.hpp10
-rw-r--r--src/chat-server/guildhandler.cpp371
-rw-r--r--src/chat-server/partyhandler.cpp197
4 files changed, 578 insertions, 500 deletions
diff --git a/src/chat-server/chathandler.cpp b/src/chat-server/chathandler.cpp
index e6e3452f..0fa39400 100644
--- a/src/chat-server/chathandler.cpp
+++ b/src/chat-server/chathandler.cpp
@@ -27,13 +27,10 @@
#include "defines.h"
#include "account-server/character.hpp"
#include "account-server/dalstorage.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"
-#include "chat-server/party.hpp"
#include "net/connectionhandler.hpp"
#include "net/messagein.hpp"
#include "net/messageout.hpp"
@@ -52,12 +49,6 @@ void registerChatClient(const std::string &token,
chatHandler->mTokenCollector.addPendingConnect(token, p);
}
-void updateInfo(ChatClient *client, int partyId)
-{
- Character *character = storage->getCharacter(client->characterName);
- GameServerHandler::sendPartyChange(character, partyId);
-}
-
ChatHandler::ChatHandler():
mTokenCollector(this)
{
@@ -565,14 +556,7 @@ ChatHandler::handleTopicChange(ChatClient &client, MessageIn &msg)
}
else
{
- Guild *guild = guildManager->findByName(channel->getName());
- if (guild)
- {
- if(guild->checkLeader(client.characterId))
- {
- chatChannelManager->setChannelTopic(channelId, topic);
- }
- }
+ guildChannelTopicChange(channel, client.characterId, topic);
}
}
@@ -587,226 +571,6 @@ 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
- Guild *guild = guildManager->createGuild(guildName, client.characterId);
- reply.writeByte(ERRMSG_OK);
- reply.writeString(guildName);
- reply.writeShort(guild->getId());
- reply.writeShort(guild->getUserPermissions(client.characterId));
-
- // Send autocreated channel id
- ChatChannel* channel = joinGuildChannel(guildName, client);
- reply.writeShort(channel->getId());
- }
- 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, and that they arent inviting themself,
- // and arent someone already in the guild
- if (guild->canInvite(client.characterId) &&
- (client.characterName != character) &&
- !guild->checkInGuild(invitedClient->characterId))
- {
- // 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(invitedClient->characterId);
- }
- 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);
- std::string guildName = msg.readString();
- bool error = true; // set true by default, and set false only if success
-
- // check guild exists and that member was invited
- // then add them as guild member
- // and remove from invite list
- Guild *guild = guildManager->findByName(guildName);
- if (guild)
- {
- if (guild->checkInvited(client.characterId))
- {
- // add user to guild
- guildManager->addGuildMember(guild, client.characterId);
- reply.writeByte(ERRMSG_OK);
- reply.writeString(guild->getName());
- reply.writeShort(guild->getId());
- reply.writeShort(guild->getUserPermissions(client.characterId));
-
- // have character join guild channel
- ChatChannel *channel = joinGuildChannel(guild->getName(), client);
- reply.writeShort(channel->getId());
- sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_NEW_PLAYER);
-
- // success! set error to false
- error = false;
- }
- }
-
- if (error)
- {
- 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)
- {
- // make sure the requestor is in the guild
- if (guild->checkInGuild(client.characterId))
- {
- reply.writeByte(ERRMSG_OK);
- reply.writeShort(guildId);
- std::list<GuildMember*> memberList = guild->getMembers();
- std::list<GuildMember*>::const_iterator itr_end = memberList.end();
- for(std::list<GuildMember*>::iterator itr = memberList.begin();
- itr != itr_end; ++itr)
- {
- Character *c = storage->getCharacter((*itr)->mId, NULL);
- std::string memberName = c->getName();
- reply.writeString(memberName);
- reply.writeByte(mPlayerMap.find(memberName) != mPlayerMap.end());
- }
- }
- }
- else
- {
- reply.writeByte(ERRMSG_FAILURE);
- }
-
- client.send(reply);
-}
-
-void
-ChatHandler::handleGuildMemberLevelChange(ChatClient &client, MessageIn &msg)
-{
- // get the guild, the user to change the permissions, and the new permission
- // check theyre valid, and then change them
- MessageOut reply(CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE);
- short guildId = msg.readShort();
- std::string user = msg.readString();
- short level = msg.readByte();
- Guild *guild = guildManager->findById(guildId);
- Character *c = storage->getCharacter(user);
-
- if (guild && c)
- {
- if (guildManager->changeMemberLevel(&client, guild, c->getDatabaseID(), level) == 0)
- {
- reply.writeByte(ERRMSG_OK);
- client.send(reply);
- }
- }
-
- 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.characterId))
- {
- reply.writeByte(ERRMSG_OK);
- reply.writeShort(guildId);
-
- // Check if they are the leader, and if so, remove the guild channel
- if (guild->checkLeader(client.characterId))
- {
- chatChannelManager->removeChannel(chatChannelManager->getChannelId(guild->getName()));
- }
- else
- {
- // guild manager checks if the member is the last in the guild
- // and removes the guild if so
- guildManager->removeGuildMember(guild, client.characterId);
- sendGuildListUpdate(guild->getName(), client.characterName, GUILD_EVENT_LEAVING_PLAYER);
- }
- }
- 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)
{
@@ -849,268 +613,6 @@ void ChatHandler::sendInChannel(ChatChannel *channel, MessageOut &msg)
}
}
-void ChatHandler::sendGuildInvite(const std::string &invitedName,
- const std::string &inviterName,
- const std::string &guildName)
-{
- MessageOut msg(CPMSG_GUILD_INVITED);
- msg.writeString(inviterName);
- msg.writeString(guildName);
-
- std::map<std::string, ChatClient*>::iterator itr = mPlayerMap.find(invitedName);
- if (itr == mPlayerMap.end())
- {
- itr->second->send(msg);
- }
-}
-
-void ChatHandler::sendGuildRejoin(ChatClient &client)
-{
- // Get list of guilds and check what rights they have.
- std::vector<Guild*> guilds = guildManager->getGuildsForPlayer(client.characterId);
- for (unsigned int i = 0; i != guilds.size(); ++i)
- {
- Guild *guild = guilds[i];
- short permissions;
- if (!guild)
- {
- return;
- }
- permissions = guild->getUserPermissions(client.characterId);
-
- std::string guildName = guild->getName();
-
- // Tell the client what guilds the character belongs to and their permissions
- MessageOut msg(CPMSG_GUILD_REJOIN);
- msg.writeString(guildName);
- msg.writeShort(guild->getId());
- msg.writeShort(permissions);
-
- // get channel id of guild channel
- ChatChannel *channel = joinGuildChannel(guildName, client);
-
- // send the channel id for the autojoined channel
- msg.writeShort(channel->getId());
- msg.writeString(channel->getAnnouncement());
-
- client.send(msg);
-
- sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_ONLINE_PLAYER);
-
- }
-}
-
-ChatChannel* ChatHandler::joinGuildChannel(const std::string &guildName, ChatClient &client)
-{
- // Automatically make the character join the guild chat channel
- ChatChannel *channel = chatChannelManager->getChannel(guildName);
- if (!channel)
- {
- // Channel doesnt exist so create it
- int channelId = chatChannelManager->createNewChannel(guildName,
- "Guild Channel", "", false);
- channel = chatChannelManager->getChannel(channelId);
- }
-
- // Add user to the channel
- if (channel->addUser(&client))
- {
- // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went
- // in the channel.
- warnUsersAboutPlayerEventInChat(channel, client.characterName,
- CHAT_EVENT_NEW_PLAYER);
- }
-
- return channel;
-}
-
-void ChatHandler::sendGuildListUpdate(const std::string &guildName,
- const std::string &characterName,
- char eventId)
-{
- Guild *guild = guildManager->findByName(guildName);
- if (guild)
- {
- MessageOut msg(CPMSG_GUILD_UPDATE_LIST);
-
- msg.writeShort(guild->getId());
- msg.writeString(characterName);
- msg.writeByte(eventId);
- std::map<std::string, ChatClient*>::const_iterator chr;
- std::list<GuildMember*> members = guild->getMembers();
-
- for (std::list<GuildMember*>::const_iterator itr = members.begin();
- itr != members.end(); ++itr)
- {
- Character *c = storage->getCharacter((*itr)->mId, NULL);
- chr = mPlayerMap.find(c->getName());
- if (chr != mPlayerMap.end())
- {
- chr->second->send(msg);
- }
- }
- }
-}
-
-bool ChatHandler::handlePartyJoin(const std::string &invited, const std::string &inviter)
-{
- MessageOut out(CPMSG_PARTY_INVITE_RESPONSE);
-
- // Get inviting client
- ChatClient *c1 = getClient(inviter);
- if (c1)
- {
- // if party doesnt exist, create it
- if (!c1->party)
- {
- c1->party = new Party();
- // tell game server to update info
- updateInfo(c1, c1->party->getId());
- }
-
- // add inviter to the party
- c1->party->addUser(inviter);
-
- // Get invited client
- ChatClient *c2 = getClient(invited);
- if (c2)
- {
- // add invited to the party
- c1->party->addUser(invited);
- c2->party = c1->party;
- // was successful so return success to inviter
- out.writeString(invited);
- out.writeByte(ERRMSG_OK);
- c1->send(out);
-
- // tell everyone a player joined
- informPartyMemberJoined(*c2);
-
- // tell game server to update info
- updateInfo(c2, c2->party->getId());
- return true;
- }
- }
-
- // there was an error, return false
- return false;
-
-}
-
-void ChatHandler::handlePartyInvite(ChatClient &client, MessageIn &msg)
-{
- //TODO: Handle errors
- MessageOut out(CPMSG_PARTY_INVITED);
-
- out.writeString(client.characterName);
-
- std::string invited = msg.readString();
- if (invited == client.characterName)
- {
- return;
- }
- if (invited != "")
- {
- // Get client and send it the invite
- ChatClient *c = getClient(invited);
- if (c)
- {
- // store the invite
- mPartyInvitedUsers.push_back(invited);
- c->send(out);
- }
- }
-}
-
-void ChatHandler::handlePartyAcceptInvite(ChatClient &client, MessageIn &msg)
-{
- MessageOut out(CPMSG_PARTY_ACCEPT_INVITE_RESPONSE);
-
- // Check that the player was invited
- std::vector<std::string>::iterator itr;
- itr = std::find(mPartyInvitedUsers.begin(), mPartyInvitedUsers.end(),
- client.characterName);
- if (itr != mPartyInvitedUsers.end())
- {
- // make them join the party
- if (handlePartyJoin(client.characterName, msg.readString()))
- {
- out.writeByte(ERRMSG_OK);
- mPartyInvitedUsers.erase(itr);
- }
- else
- {
- out.writeByte(ERRMSG_FAILURE);
- }
- }
- else
- {
- out.writeByte(ERRMSG_FAILURE);
- }
-
- client.send(out);
-}
-
-void ChatHandler::handlePartyQuit(ChatClient &client)
-{
- removeUserFromParty(client);
- MessageOut out(CPMSG_PARTY_QUIT_RESPONSE);
- out.writeByte(ERRMSG_OK);
- client.send(out);
-
- // tell game server to update info
- updateInfo(&client, 0);
-}
-
-void ChatHandler::removeUserFromParty(ChatClient &client)
-{
- if (client.party)
- {
- client.party->removeUser(client.characterName);
- informPartyMemberQuit(client);
-
- // if theres less than 1 member left, remove the party
- if (client.party->numUsers() < 1)
- {
- delete client.party;
- client.party = 0;
- }
- }
-}
-
-void ChatHandler::informPartyMemberQuit(ChatClient &client)
-{
- std::map<std::string, ChatClient*>::iterator itr;
- std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
-
- for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
- {
- if (itr->second->party == client.party)
- {
- MessageOut out(CPMSG_PARTY_MEMBER_LEFT);
- out.writeShort(client.characterId);
- itr->second->send(out);
- }
- }
-}
-
-void ChatHandler::informPartyMemberJoined(ChatClient &client)
-{
- std::map<std::string, ChatClient*>::iterator itr;
- std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
-
- for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
- {
- if (itr->second->party == client.party)
- {
- MessageOut out(CPMSG_PARTY_NEW_MEMBER);
- out.writeShort(client.characterId);
- out.writeString(client.characterName);
- itr->second->send(out);
- }
- }
-}
-
ChatClient* ChatHandler::getClient(const std::string &name)
{
std::map<std::string, ChatClient*>::iterator itr;
diff --git a/src/chat-server/chathandler.hpp b/src/chat-server/chathandler.hpp
index beeeb0d4..efd80320 100644
--- a/src/chat-server/chathandler.hpp
+++ b/src/chat-server/chathandler.hpp
@@ -22,7 +22,9 @@
#ifndef _TMWSERV_CHATHANDLER_H_
#define _TMWSERV_CHATHANDLER_H_
-#include <iosfwd>
+#include <map>
+#include <string>
+#include <vector>
#include "net/connectionhandler.hpp"
#include "utils/tokencollector.hpp"
@@ -322,6 +324,12 @@ class ChatHandler : public ConnectionHandler
ChatClient* getClient(const std::string &name);
/**
+ * Set the topic of a guild channel
+ */
+ void guildChannelTopicChange(ChatChannel *channel, int playerId,
+ const std::string &topic);
+
+ /**
* Container for pending clients and pending connections.
*/
TokenCollector<ChatHandler, ChatClient *, Pending *> mTokenCollector;
diff --git a/src/chat-server/guildhandler.cpp b/src/chat-server/guildhandler.cpp
new file mode 100644
index 00000000..d68746f7
--- /dev/null
+++ b/src/chat-server/guildhandler.cpp
@@ -0,0 +1,371 @@
+/*
+ * The Mana World Server
+ * Copyright 2008 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
+ */
+
+#include "chathandler.hpp"
+#include "chatchannel.hpp"
+#include "chatchannelmanager.hpp"
+#include "chatclient.hpp"
+#include "guild.hpp"
+#include "guildmanager.hpp"
+
+#include "../account-server/character.hpp"
+#include "../account-server/dalstorage.hpp"
+
+#include "../net/messagein.hpp"
+#include "../net/messageout.hpp"
+
+#include "../defines.h"
+
+void ChatHandler::sendGuildInvite(const std::string &invitedName,
+ const std::string &inviterName,
+ const std::string &guildName)
+{
+ MessageOut msg(CPMSG_GUILD_INVITED);
+ msg.writeString(inviterName);
+ msg.writeString(guildName);
+
+ std::map<std::string, ChatClient*>::iterator itr = mPlayerMap.find(invitedName);
+ if (itr == mPlayerMap.end())
+ {
+ itr->second->send(msg);
+ }
+}
+
+void ChatHandler::sendGuildRejoin(ChatClient &client)
+{
+ // Get list of guilds and check what rights they have.
+ std::vector<Guild*> guilds = guildManager->getGuildsForPlayer(client.characterId);
+ for (unsigned int i = 0; i != guilds.size(); ++i)
+ {
+ Guild *guild = guilds[i];
+ short permissions;
+ if (!guild)
+ {
+ return;
+ }
+ permissions = guild->getUserPermissions(client.characterId);
+
+ std::string guildName = guild->getName();
+
+ // Tell the client what guilds the character belongs to and their permissions
+ MessageOut msg(CPMSG_GUILD_REJOIN);
+ msg.writeString(guildName);
+ msg.writeShort(guild->getId());
+ msg.writeShort(permissions);
+
+ // get channel id of guild channel
+ ChatChannel *channel = joinGuildChannel(guildName, client);
+
+ // send the channel id for the autojoined channel
+ msg.writeShort(channel->getId());
+ msg.writeString(channel->getAnnouncement());
+
+ client.send(msg);
+
+ sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_ONLINE_PLAYER);
+
+ }
+}
+
+ChatChannel* ChatHandler::joinGuildChannel(const std::string &guildName, ChatClient &client)
+{
+ // Automatically make the character join the guild chat channel
+ ChatChannel *channel = chatChannelManager->getChannel(guildName);
+ if (!channel)
+ {
+ // Channel doesnt exist so create it
+ int channelId = chatChannelManager->createNewChannel(guildName,
+ "Guild Channel", "", false);
+ channel = chatChannelManager->getChannel(channelId);
+ }
+
+ // Add user to the channel
+ if (channel->addUser(&client))
+ {
+ // Send an CPMSG_UPDATE_CHANNEL to warn other clients a user went
+ // in the channel.
+ warnUsersAboutPlayerEventInChat(channel, client.characterName,
+ CHAT_EVENT_NEW_PLAYER);
+ }
+
+ return channel;
+}
+
+void ChatHandler::sendGuildListUpdate(const std::string &guildName,
+ const std::string &characterName,
+ char eventId)
+{
+ Guild *guild = guildManager->findByName(guildName);
+ if (guild)
+ {
+ MessageOut msg(CPMSG_GUILD_UPDATE_LIST);
+
+ msg.writeShort(guild->getId());
+ msg.writeString(characterName);
+ msg.writeByte(eventId);
+ std::map<std::string, ChatClient*>::const_iterator chr;
+ std::list<GuildMember*> members = guild->getMembers();
+
+ for (std::list<GuildMember*>::const_iterator itr = members.begin();
+ itr != members.end(); ++itr)
+ {
+ Character *c = storage->getCharacter((*itr)->mId, NULL);
+ chr = mPlayerMap.find(c->getName());
+ if (chr != mPlayerMap.end())
+ {
+ chr->second->send(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
+ Guild *guild = guildManager->createGuild(guildName, client.characterId);
+ reply.writeByte(ERRMSG_OK);
+ reply.writeString(guildName);
+ reply.writeShort(guild->getId());
+ reply.writeShort(guild->getUserPermissions(client.characterId));
+
+ // Send autocreated channel id
+ ChatChannel* channel = joinGuildChannel(guildName, client);
+ reply.writeShort(channel->getId());
+ }
+ 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, and that they arent inviting themself,
+ // and arent someone already in the guild
+ if (guild->canInvite(client.characterId) &&
+ (client.characterName != character) &&
+ !guild->checkInGuild(invitedClient->characterId))
+ {
+ // 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(invitedClient->characterId);
+ }
+ 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);
+ std::string guildName = msg.readString();
+ bool error = true; // set true by default, and set false only if success
+
+ // check guild exists and that member was invited
+ // then add them as guild member
+ // and remove from invite list
+ Guild *guild = guildManager->findByName(guildName);
+ if (guild)
+ {
+ if (guild->checkInvited(client.characterId))
+ {
+ // add user to guild
+ guildManager->addGuildMember(guild, client.characterId);
+ reply.writeByte(ERRMSG_OK);
+ reply.writeString(guild->getName());
+ reply.writeShort(guild->getId());
+ reply.writeShort(guild->getUserPermissions(client.characterId));
+
+ // have character join guild channel
+ ChatChannel *channel = joinGuildChannel(guild->getName(), client);
+ reply.writeShort(channel->getId());
+ sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_NEW_PLAYER);
+
+ // success! set error to false
+ error = false;
+ }
+ }
+
+ if (error)
+ {
+ 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)
+ {
+ // make sure the requestor is in the guild
+ if (guild->checkInGuild(client.characterId))
+ {
+ reply.writeByte(ERRMSG_OK);
+ reply.writeShort(guildId);
+ std::list<GuildMember*> memberList = guild->getMembers();
+ std::list<GuildMember*>::const_iterator itr_end = memberList.end();
+ for(std::list<GuildMember*>::iterator itr = memberList.begin();
+ itr != itr_end; ++itr)
+ {
+ Character *c = storage->getCharacter((*itr)->mId, NULL);
+ std::string memberName = c->getName();
+ reply.writeString(memberName);
+ reply.writeByte(mPlayerMap.find(memberName) != mPlayerMap.end());
+ }
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::handleGuildMemberLevelChange(ChatClient &client, MessageIn &msg)
+{
+ // get the guild, the user to change the permissions, and the new permission
+ // check theyre valid, and then change them
+ MessageOut reply(CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE);
+ short guildId = msg.readShort();
+ std::string user = msg.readString();
+ short level = msg.readByte();
+ Guild *guild = guildManager->findById(guildId);
+ Character *c = storage->getCharacter(user);
+
+ if (guild && c)
+ {
+ if (guildManager->changeMemberLevel(&client, guild, c->getDatabaseID(), level) == 0)
+ {
+ reply.writeByte(ERRMSG_OK);
+ client.send(reply);
+ }
+ }
+
+ 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.characterId))
+ {
+ reply.writeByte(ERRMSG_OK);
+ reply.writeShort(guildId);
+
+ // Check if they are the leader, and if so, remove the guild channel
+ if (guild->checkLeader(client.characterId))
+ {
+ chatChannelManager->removeChannel(chatChannelManager->getChannelId(guild->getName()));
+ }
+ else
+ {
+ // guild manager checks if the member is the last in the guild
+ // and removes the guild if so
+ guildManager->removeGuildMember(guild, client.characterId);
+ sendGuildListUpdate(guild->getName(), client.characterName, GUILD_EVENT_LEAVING_PLAYER);
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+ }
+ else
+ {
+ reply.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(reply);
+}
+
+void
+ChatHandler::guildChannelTopicChange(ChatChannel *channel, int playerId, const std::string &topic)
+{
+ Guild *guild = guildManager->findByName(channel->getName());
+ if (guild)
+ {
+ if(guild->checkLeader(playerId))
+ {
+ chatChannelManager->setChannelTopic(channel->getId(), topic);
+ }
+ }
+}
diff --git a/src/chat-server/partyhandler.cpp b/src/chat-server/partyhandler.cpp
new file mode 100644
index 00000000..22e24b64
--- /dev/null
+++ b/src/chat-server/partyhandler.cpp
@@ -0,0 +1,197 @@
+/*
+ * The Mana World Server
+ * Copyright 2008 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
+ */
+
+#include "chathandler.hpp"
+#include "chatclient.hpp"
+#include "party.hpp"
+
+#include "../account-server/dalstorage.hpp"
+#include "../account-server/serverhandler.hpp"
+
+#include "../net/messagein.hpp"
+#include "../net/messageout.hpp"
+
+#include "../defines.h"
+
+void updateInfo(ChatClient *client, int partyId)
+{
+ Character *character = storage->getCharacter(client->characterName);
+ GameServerHandler::sendPartyChange(character, partyId);
+}
+
+bool ChatHandler::handlePartyJoin(const std::string &invited, const std::string &inviter)
+{
+ MessageOut out(CPMSG_PARTY_INVITE_RESPONSE);
+
+ // Get inviting client
+ ChatClient *c1 = getClient(inviter);
+ if (c1)
+ {
+ // if party doesnt exist, create it
+ if (!c1->party)
+ {
+ c1->party = new Party();
+ // tell game server to update info
+ updateInfo(c1, c1->party->getId());
+ }
+
+ // add inviter to the party
+ c1->party->addUser(inviter);
+
+ // Get invited client
+ ChatClient *c2 = getClient(invited);
+ if (c2)
+ {
+ // add invited to the party
+ c1->party->addUser(invited);
+ c2->party = c1->party;
+ // was successful so return success to inviter
+ out.writeString(invited);
+ out.writeByte(ERRMSG_OK);
+ c1->send(out);
+
+ // tell everyone a player joined
+ informPartyMemberJoined(*c2);
+
+ // tell game server to update info
+ updateInfo(c2, c2->party->getId());
+ return true;
+ }
+ }
+
+ // there was an error, return false
+ return false;
+
+}
+
+void ChatHandler::handlePartyInvite(ChatClient &client, MessageIn &msg)
+{
+ //TODO: Handle errors
+ MessageOut out(CPMSG_PARTY_INVITED);
+
+ out.writeString(client.characterName);
+
+ std::string invited = msg.readString();
+ if (invited == client.characterName)
+ {
+ return;
+ }
+ if (invited != "")
+ {
+ // Get client and send it the invite
+ ChatClient *c = getClient(invited);
+ if (c)
+ {
+ // store the invite
+ mPartyInvitedUsers.push_back(invited);
+ c->send(out);
+ }
+ }
+}
+
+void ChatHandler::handlePartyAcceptInvite(ChatClient &client, MessageIn &msg)
+{
+ MessageOut out(CPMSG_PARTY_ACCEPT_INVITE_RESPONSE);
+
+ // Check that the player was invited
+ std::vector<std::string>::iterator itr;
+ itr = std::find(mPartyInvitedUsers.begin(), mPartyInvitedUsers.end(),
+ client.characterName);
+ if (itr != mPartyInvitedUsers.end())
+ {
+ // make them join the party
+ if (handlePartyJoin(client.characterName, msg.readString()))
+ {
+ out.writeByte(ERRMSG_OK);
+ mPartyInvitedUsers.erase(itr);
+ }
+ else
+ {
+ out.writeByte(ERRMSG_FAILURE);
+ }
+ }
+ else
+ {
+ out.writeByte(ERRMSG_FAILURE);
+ }
+
+ client.send(out);
+}
+
+void ChatHandler::handlePartyQuit(ChatClient &client)
+{
+ removeUserFromParty(client);
+ MessageOut out(CPMSG_PARTY_QUIT_RESPONSE);
+ out.writeByte(ERRMSG_OK);
+ client.send(out);
+
+ // tell game server to update info
+ updateInfo(&client, 0);
+}
+
+void ChatHandler::removeUserFromParty(ChatClient &client)
+{
+ if (client.party)
+ {
+ client.party->removeUser(client.characterName);
+ informPartyMemberQuit(client);
+
+ // if theres less than 1 member left, remove the party
+ if (client.party->numUsers() < 1)
+ {
+ delete client.party;
+ client.party = 0;
+ }
+ }
+}
+
+void ChatHandler::informPartyMemberQuit(ChatClient &client)
+{
+ std::map<std::string, ChatClient*>::iterator itr;
+ std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
+
+ for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
+ {
+ if (itr->second->party == client.party)
+ {
+ MessageOut out(CPMSG_PARTY_MEMBER_LEFT);
+ out.writeShort(client.characterId);
+ itr->second->send(out);
+ }
+ }
+}
+
+void ChatHandler::informPartyMemberJoined(ChatClient &client)
+{
+ std::map<std::string, ChatClient*>::iterator itr;
+ std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
+
+ for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
+ {
+ if (itr->second->party == client.party)
+ {
+ MessageOut out(CPMSG_PARTY_NEW_MEMBER);
+ out.writeShort(client.characterId);
+ out.writeString(client.characterName);
+ itr->second->send(out);
+ }
+ }
+}