00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "guildmanager.hpp"
00022 #include "guild.hpp"
00023 #include "defines.h"
00024 #include "account-server/dalstorage.hpp"
00025 #include "chat-server/chatclient.hpp"
00026 #include "chat-server/chathandler.hpp"
00027
00028 GuildManager::GuildManager()
00029 {
00030
00031 mGuilds = storage->getGuildList();
00032 }
00033
00034 GuildManager::~GuildManager()
00035 {
00036 for (std::list<Guild*>::iterator itr = mGuilds.begin();
00037 itr != mGuilds.end(); ++itr)
00038 {
00039 delete *itr;
00040 }
00041 mGuilds.clear();
00042 }
00043
00044 Guild* GuildManager::createGuild(const std::string &name, int playerId)
00045 {
00046 Guild *guild = new Guild(name);
00047
00048 storage->addGuild(guild);
00049
00050
00051 mGuilds.push_back(guild);
00052 mOwners.push_back(playerId);
00053
00054
00055 addGuildMember(guild, playerId);
00056
00057
00058 storage->setMemberRights(guild->getId(), playerId, GAL_OWNER);
00059
00060 guild->setOwner(playerId);
00061
00062 return guild;
00063 }
00064
00065 void GuildManager::removeGuild(Guild *guild)
00066 {
00067 if (!guild)
00068 return;
00069 storage->removeGuild(guild);
00070 mOwners.remove(guild->getOwner());
00071 mGuilds.remove(guild);
00072 delete guild;
00073 }
00074
00075 void GuildManager::addGuildMember(Guild *guild, int playerId)
00076 {
00077 if (!guild)
00078 return;
00079 storage->addGuildMember(guild->getId(), playerId);
00080 guild->addMember(playerId);
00081 }
00082
00083 void GuildManager::removeGuildMember(Guild *guild, int playerId)
00084 {
00085 if (!guild)
00086 return;
00087
00088
00089 storage->removeGuildMember(guild->getId(), playerId);
00090 guild->removeMember(playerId);
00091
00092
00093 if(guild->totalMembers() == 0)
00094 {
00095 removeGuild(guild);
00096 }
00097
00098
00099 std::list<int>::iterator itr = mOwners.begin();
00100 std::list<int>::iterator itr_end = mOwners.end();
00101 while (itr != itr_end)
00102 {
00103 if ((*itr) == playerId)
00104 mOwners.remove(playerId);
00105
00106 ++itr;
00107 }
00108 }
00109
00110 Guild *GuildManager::findById(short id)
00111 {
00112 Guild *guild;
00113 for (std::list<Guild*>::iterator itr = mGuilds.begin(),
00114 itr_end = mGuilds.end();
00115 itr != itr_end; ++itr)
00116 {
00117 guild = (*itr);
00118 if (guild->getId() == id)
00119 {
00120 return guild;
00121 }
00122 }
00123 return NULL;
00124 }
00125
00126 Guild *GuildManager::findByName(const std::string &name)
00127 {
00128 Guild *guild;
00129 for (std::list<Guild*>::iterator itr = mGuilds.begin(),
00130 itr_end = mGuilds.end();
00131 itr != itr_end; ++itr)
00132 {
00133 guild = (*itr);
00134 if (guild->getName() == name)
00135 {
00136 return guild;
00137 }
00138 }
00139 return NULL;
00140 }
00141
00142 bool GuildManager::doesExist(const std::string &name)
00143 {
00144 return findByName(name) != NULL;
00145 }
00146
00147 std::vector<Guild*> GuildManager::getGuildsForPlayer(int playerId)
00148 {
00149 std::vector<Guild*> guildList;
00150
00151 for (std::list<Guild*>::iterator itr = mGuilds.begin();
00152 itr != mGuilds.end(); ++itr)
00153 {
00154 if((*itr)->checkInGuild(playerId))
00155 {
00156 guildList.push_back((*itr));
00157 }
00158 }
00159 return guildList;
00160 }
00161
00162 void GuildManager::disconnectPlayer(ChatClient *player)
00163 {
00164 std::vector<Guild*> guildList = getGuildsForPlayer(player->characterId);
00165
00166 for (std::vector<Guild*>::const_iterator itr = guildList.begin();
00167 itr != guildList.end(); ++itr)
00168 {
00169 chatHandler->sendGuildListUpdate((*itr)->getName(),
00170 player->characterName,
00171 GUILD_EVENT_OFFLINE_PLAYER);
00172 }
00173 }
00174
00175 int GuildManager::changeMemberLevel(ChatClient *player, Guild *guild,
00176 int playerId, int level)
00177 {
00178 if (guild->checkInGuild(player->characterId) && guild->checkInGuild(playerId))
00179 {
00180 int playerLevel = guild->getUserPermissions(player->characterId);
00181
00182 if (playerLevel == GAL_OWNER)
00183 {
00184
00185 setUserRights(guild, playerId, level);
00186 return 0;
00187 }
00188 }
00189
00190 return -1;
00191 }
00192
00193 bool GuildManager::alreadyOwner(int playerId)
00194 {
00195 std::list<int>::iterator itr = mOwners.begin();
00196 std::list<int>::iterator itr_end = mOwners.end();
00197
00198 while (itr != itr_end)
00199 {
00200 if ((*itr) == playerId)
00201 {
00202 return true;
00203 }
00204 ++itr;
00205 }
00206
00207 return false;
00208 }
00209
00210 void GuildManager::setUserRights(Guild *guild, int playerId, int rights)
00211 {
00212
00213 storage->setMemberRights(guild->getId(), playerId, rights);
00214
00215
00216 guild->setUserPermissions(playerId, rights);
00217 }