Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed guild support

TODO:
 + Fix conflict between guild and normal channels
 + Fix being able to leave guild channel without leaving guild itself
  • Loading branch information...
commit 291b739bd562e28538fed953c822a58c59a761d5 1 parent 37adb26
@Ablu authored
View
1  docs/manaserv.xml.example
@@ -191,6 +191,7 @@
<option name="account_minPasswordLength" value="6" />
<option name="account_maxPasswordLength" value="25" />
<option name="account_maxCharacters" value="3" />
+ <option name="account_maxGuildsPerCharacter" value="1" />
<!-- end of accounts configuration **************************************** -->
View
6 src/account-server/storage.cpp
@@ -1478,9 +1478,9 @@ void Storage::setMemberRights(int guildId, int memberId, int rights)
{
std::ostringstream sql;
sql << "UPDATE " << GUILD_MEMBERS_TBL_NAME
- << " SET rights = '" << rights << "'"
- << " WHERE member_id = '" << memberId << "'"
- << " AND guild_id = '" << guildId << "'';";
+ << " SET rights = " << rights
+ << " WHERE member_id = " << memberId
+ << " AND guild_id = " << guildId << ";";
mDb->execSql(sql.str());
}
catch (const dal::DbSqlQueryExecFailure& e)
View
5 src/chat-server/chatclient.h
@@ -47,8 +47,9 @@ class ChatClient : public NetComputer
std::string characterName;
unsigned int characterId;
- std::vector< ChatChannel * > channels;
- Party* party;
+ std::vector<ChatChannel *> channels;
+ std::vector<Guild *> guilds;
+ Party *party;
unsigned char accountLevel;
std::map<ChatChannel*, std::string> userModes;
};
View
3  src/chat-server/chathandler.cpp
@@ -127,6 +127,9 @@ void ChatHandler::computerDisconnected(NetComputer *comp)
// Remove user from party
removeUserFromParty(*computer);
+ // Notify guilds about him leaving
+ guildManager->disconnectPlayer(computer);
+
// Remove the character from the player map
// need to do this after removing them from party
// as that uses the player map
View
5 src/chat-server/chathandler.h
@@ -25,7 +25,10 @@
#include <map>
#include <string>
+#include "chat-server/guild.h"
+
#include "net/connectionhandler.h"
+
#include "utils/tokencollector.h"
class ChatChannel;
@@ -106,7 +109,7 @@ class ChatHandler : public ConnectionHandler
/**
* Send information about a change in the guild list to guild members.
*/
- void sendGuildListUpdate(const std::string &guildName,
+ void sendGuildListUpdate(Guild *guild,
const std::string &characterName,
char eventId);
View
11 src/chat-server/guild.cpp
@@ -42,10 +42,8 @@ void Guild::addMember(int playerId, int permissions)
// add new guild member to guild
mMembers.push_back(member);
- if (checkInvited(playerId))
- {
- mInvited.remove(playerId);
- }
+ // remove from invited list if nessecary
+ mInvited.remove(playerId);
}
void Guild::removeMember(int playerId)
@@ -97,6 +95,11 @@ void Guild::addInvited(int playerId)
mInvited.push_back(playerId);
}
+void Guild::removeInvited(int playerId)
+{
+ mInvited.remove(playerId);
+}
+
bool Guild::checkInGuild(int playerId) const
{
return getMember(playerId) != 0;
View
5 src/chat-server/guild.h
@@ -83,6 +83,11 @@ class Guild
void addInvited(int playerId);
/**
+ * Remove a player from the invite list.
+ */
+ void removeInvited(int playerId);
+
+ /**
* Returns the name of the guild.
*/
std::string getName() const
View
161 src/chat-server/guildhandler.cpp
@@ -31,6 +31,7 @@
#include "net/messagein.h"
#include "net/messageout.h"
+#include "common/configuration.h"
#include "common/manaserv_protocol.h"
using namespace ManaServ;
@@ -53,15 +54,24 @@ void ChatHandler::sendGuildInvite(const std::string &invitedName,
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)
+ std::vector<Guild *> guilds =
+ guildManager->getGuildsForPlayer(client.characterId);
+
+ client.guilds = guilds;
+
+ for (std::vector<Guild *>::iterator it = guilds.begin(),
+ it_end = guilds.end(); it != it_end; ++it)
{
- const Guild *guild = guilds[i];
+ Guild *guild = *it;
+ // nothing to do if player is in no guild
+ if (!guild)
+ return;
const int permissions = guild->getUserPermissions(client.characterId);
const std::string guildName = guild->getName();
- // Tell the client what guilds the character belongs to and their permissions
+ // Tell the client what guilds the character belongs to
+ // and their permissions
MessageOut msg(CPMSG_GUILD_REJOIN);
msg.writeString(guildName);
msg.writeInt16(guild->getId());
@@ -76,7 +86,8 @@ void ChatHandler::sendGuildRejoin(ChatClient &client)
client.send(msg);
- sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_ONLINE_PLAYER);
+ sendGuildListUpdate(guild, client.characterName,
+ GUILD_EVENT_ONLINE_PLAYER);
}
}
@@ -104,30 +115,26 @@ ChatChannel *ChatHandler::joinGuildChannel(const std::string &guildName, ChatCli
return channel;
}
-void ChatHandler::sendGuildListUpdate(const std::string &guildName,
+void ChatHandler::sendGuildListUpdate(Guild *guild,
const std::string &characterName,
char eventId)
{
- Guild *guild = guildManager->findByName(guildName);
- if (guild)
- {
- MessageOut msg(CPMSG_GUILD_UPDATE_LIST);
+ MessageOut msg(CPMSG_GUILD_UPDATE_LIST);
- msg.writeInt16(guild->getId());
- msg.writeString(characterName);
- msg.writeInt8(eventId);
- std::map<std::string, ChatClient*>::const_iterator chr;
- std::list<GuildMember*> members = guild->getMembers();
+ msg.writeInt16(guild->getId());
+ msg.writeString(characterName);
+ msg.writeInt8(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)
+ 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())
{
- Character *c = storage->getCharacter((*itr)->mId, NULL);
- chr = mPlayerMap.find(c->getName());
- if (chr != mPlayerMap.end())
- {
- chr->second->send(msg);
- }
+ chr->second->send(msg);
}
}
}
@@ -140,8 +147,8 @@ void ChatHandler::handleGuildCreate(ChatClient &client, MessageIn &msg)
std::string guildName = msg.readString();
if (!guildManager->doesExist(guildName))
{
- // check the player hasnt already created a guild
- if (guildManager->alreadyOwner(client.characterId))
+ if ((int)client.guilds.size() >=
+ Configuration::getValue("account_maxGuildsPerCharacter", 1))
{
reply.writeInt8(ERRMSG_LIMIT_REACHED);
}
@@ -154,6 +161,8 @@ void ChatHandler::handleGuildCreate(ChatClient &client, MessageIn &msg)
reply.writeInt16(guild->getId());
reply.writeInt16(guild->getUserPermissions(client.characterId));
+ client.guilds.push_back(guild);
+
// Send autocreated channel id
ChatChannel* channel = joinGuildChannel(guildName, client);
reply.writeInt16(channel->getId());
@@ -186,20 +195,32 @@ void ChatHandler::handleGuildInvite(ChatClient &client, MessageIn &msg)
// and arent someone already in the guild
if (guild->canInvite(client.characterId) &&
(client.characterName != character) &&
- !guild->checkInGuild(invitedClient->characterId))
+ (guild->checkInGuild(client.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.writeInt16(guildId);
- invitedClient->send(invite);
- reply.writeInt8(ERRMSG_OK);
-
- // add member to list of invited members to the guild
- guild->addInvited(invitedClient->characterId);
+ if ((int)invitedClient->guilds.size() >=
+ Configuration::getValue("account_maxGuildsPerCharacter", 1))
+ {
+ reply.writeInt8(ERRMSG_LIMIT_REACHED);
+ }
+ else if (guild->checkInGuild(invitedClient->characterId))
+ {
+ reply.writeInt8(ERRMSG_ALREADY_MEMBER);
+ }
+ else
+ {
+ // 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.writeInt16(guildId);
+ invitedClient->send(invite);
+ reply.writeInt8(ERRMSG_OK);
+
+ // add member to list of invited members to the guild
+ guild->addInvited(invitedClient->characterId);
+ }
}
else
{
@@ -218,30 +239,40 @@ void ChatHandler::handleGuildAcceptInvite(ChatClient &client,
MessageIn &msg)
{
MessageOut reply(CPMSG_GUILD_ACCEPT_RESPONSE);
- std::string guildName = msg.readString();
+ const int guildID = msg.readInt16();
+ const bool accepted = msg.readInt8();
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 *guild = guildManager->findById(guildID))
{
if (guild->checkInvited(client.characterId))
{
- // add user to guild
- guildManager->addGuildMember(guild, client.characterId);
- reply.writeInt8(ERRMSG_OK);
- reply.writeString(guild->getName());
- reply.writeInt16(guild->getId());
- reply.writeInt16(guild->getUserPermissions(client.characterId));
-
- // have character join guild channel
- ChatChannel *channel = joinGuildChannel(guild->getName(), client);
- reply.writeInt16(channel->getId());
- sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_NEW_PLAYER);
-
- // success! set error to false
+ if (accepted)
+ {
+ // add user to guild
+ guildManager->addGuildMember(guild, client.characterId);
+ client.guilds.push_back(guild);
+ reply.writeInt8(ERRMSG_OK);
+ reply.writeString(guild->getName());
+ reply.writeInt16(guild->getId());
+ reply.writeInt16(guild->getUserPermissions(client.characterId));
+
+ // have character join guild channel
+ ChatChannel *channel = joinGuildChannel(guild->getName(),
+ client);
+ reply.writeInt16(channel->getId());
+ sendGuildListUpdate(guild, client.characterName,
+ GUILD_EVENT_NEW_PLAYER);
+
+ // success! set error to false
+ }
+ else
+ {
+ guild->removeInvited(client.characterId);
+ }
error = false;
}
}
@@ -347,28 +378,38 @@ void ChatHandler::handleGuildQuit(ChatClient &client, MessageIn &msg)
{
MessageOut reply(CPMSG_GUILD_QUIT_RESPONSE);
short guildId = msg.readInt16();
- 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 *guild = guildManager->findById(guildId))
{
if (guild->checkInGuild(client.characterId))
{
reply.writeInt8(ERRMSG_OK);
reply.writeInt16(guildId);
- // Check if there are no members left, remove the guild channel
- if (guild->memberCount() == 0)
+ // Check if there would be no members left, remove the guild channel
+ if (guild->memberCount() == 1)
{
- chatChannelManager->removeChannel(chatChannelManager->getChannelId(guild->getName()));
+ chatChannelManager->removeChannel(
+ chatChannelManager->getChannelId(guild->getName()));
}
// 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);
+ for (std::vector<Guild *>::iterator it = client.guilds.begin(),
+ it_end = client.guilds.end(); it != it_end; ++it)
+ {
+ if (*it == guild)
+ {
+ client.guilds.erase(it);
+ break;
+ }
+ }
+ sendGuildListUpdate(guild, client.characterName,
+ GUILD_EVENT_LEAVING_PLAYER);
}
else
{
View
38 src/chat-server/guildmanager.cpp
@@ -51,7 +51,6 @@ Guild* GuildManager::createGuild(const std::string &name, int playerId)
// Add guild, and add owner
mGuilds[guild->getId()] = guild;
- mOwners.push_back(playerId);
// put the owner in the guild
addGuildMember(guild, playerId);
@@ -67,7 +66,6 @@ Guild* GuildManager::createGuild(const std::string &name, int playerId)
void GuildManager::removeGuild(Guild *guild)
{
storage->removeGuild(guild);
- mOwners.remove(guild->getOwner());
mGuilds.erase(guild->getId());
delete guild;
}
@@ -87,8 +85,6 @@ void GuildManager::removeGuildMember(Guild *guild, int playerId)
// if theres no more members left delete the guild
if (guild->memberCount() == 0)
removeGuild(guild);
-
- mOwners.remove(playerId);
}
Guild *GuildManager::findById(short id) const
@@ -115,29 +111,26 @@ bool GuildManager::doesExist(const std::string &name) const
return findByName(name) != 0;
}
-std::vector<Guild*> GuildManager::getGuildsForPlayer(int playerId) const
+std::vector<Guild *> GuildManager::getGuildsForPlayer(int playerId) const
{
- std::vector<Guild*> guildList;
-
+ std::vector<Guild *> guilds;
for (std::map<int, Guild*>::const_iterator it = mGuilds.begin();
it != mGuilds.end(); ++it)
{
if (it->second->checkInGuild(playerId))
{
- guildList.push_back(it->second);
+ guilds.push_back(it->second);
}
}
- return guildList;
+ return guilds;
}
void GuildManager::disconnectPlayer(ChatClient *player)
{
- std::vector<Guild*> guildList = getGuildsForPlayer(player->characterId);
-
- for (std::vector<Guild*>::const_iterator it = guildList.begin();
- it != guildList.end(); ++it)
+ for (std::vector<Guild *>::iterator it = player->guilds.begin(),
+ it_end = player->guilds.end(); it != it_end; ++it)
{
- chatHandler->sendGuildListUpdate((*it)->getName(),
+ chatHandler->sendGuildListUpdate(*it,
player->characterName,
GUILD_EVENT_OFFLINE_PLAYER);
}
@@ -146,7 +139,7 @@ void GuildManager::disconnectPlayer(ChatClient *player)
int GuildManager::changeMemberLevel(ChatClient *player, Guild *guild,
int playerId, int level)
{
- if (guild->checkInGuild(player->characterId) && guild->checkInGuild(playerId))
+ if (guild->checkInGuild(playerId))
{
int playerLevel = guild->getUserPermissions(player->characterId);
@@ -161,21 +154,6 @@ int GuildManager::changeMemberLevel(ChatClient *player, Guild *guild,
return -1;
}
-bool GuildManager::alreadyOwner(int playerId) const
-{
- std::list<int>::const_iterator it = mOwners.begin();
- std::list<int>::const_iterator it_end = mOwners.end();
-
- while (it != it_end)
- {
- if (*it == playerId)
- return true;
- ++it;
- }
-
- return false;
-}
-
void GuildManager::setUserRights(Guild *guild, int playerId, int rights)
{
// Set and save the member rights
View
8 src/chat-server/guildmanager.h
@@ -80,7 +80,7 @@ class GuildManager
/**
* Return the guilds a character is in
*/
- std::vector<Guild*> getGuildsForPlayer(int playerId) const;
+ std::vector<Guild *> getGuildsForPlayer(int playerId) const;
/**
* Inform guild members that a player has disconnected.
@@ -97,18 +97,12 @@ class GuildManager
int playerId, int level);
/**
- * Check if the player already owns a guild
- */
- bool alreadyOwner(int playerId) const;
-
- /**
* Set user rights
*/
void setUserRights(Guild *guild, int playerId, int rights);
private:
std::map<int, Guild*> mGuilds;
- std::list<int> mOwners;
};
extern GuildManager *guildManager;
View
5 src/common/manaserv_protocol.h
@@ -180,7 +180,7 @@ enum {
CPMSG_GUILD_CREATE_RESPONSE = 0x0351, // B error, W guild, B rights, W channel
PCMSG_GUILD_INVITE = 0x0352, // W id, S name
CPMSG_GUILD_INVITE_RESPONSE = 0x0353, // B error
- PCMSG_GUILD_ACCEPT = 0x0354, // W id
+ PCMSG_GUILD_ACCEPT = 0x0354, // W id, B accepted (0 if false, 1 if true)
CPMSG_GUILD_ACCEPT_RESPONSE = 0x0355, // B error, W guild, B rights, W channel
PCMSG_GUILD_GET_MEMBERS = 0x0356, // W id
CPMSG_GUILD_GET_MEMBERS_RESPONSE = 0x0357, // S names, B online
@@ -283,7 +283,8 @@ enum {
ERRMSG_SERVER_FULL, // the server is overloaded
ERRMSG_TIME_OUT, // data failed to arrive in due time
ERRMSG_LIMIT_REACHED, // limit reached
- ERRMSG_ADMINISTRATIVE_LOGOFF // kicked by server administrator
+ ERRMSG_ADMINISTRATIVE_LOGOFF, // kicked by server administrator
+ ERRMSG_ALREADY_MEMBER // is already member of guild/party
};
// used in AGMSG_REGISTER_RESPONSE to show state of item db
Please sign in to comment.
Something went wrong with that request. Please try again.