00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "chathandler.hpp"
00023 #include "chatchannel.hpp"
00024 #include "chatchannelmanager.hpp"
00025 #include "chatclient.hpp"
00026 #include "guild.hpp"
00027 #include "guildmanager.hpp"
00028
00029 #include "../account-server/character.hpp"
00030 #include "../account-server/dalstorage.hpp"
00031
00032 #include "../net/messagein.hpp"
00033 #include "../net/messageout.hpp"
00034
00035 #include "../defines.h"
00036
00037 void ChatHandler::sendGuildInvite(const std::string &invitedName,
00038 const std::string &inviterName,
00039 const std::string &guildName)
00040 {
00041 MessageOut msg(CPMSG_GUILD_INVITED);
00042 msg.writeString(inviterName);
00043 msg.writeString(guildName);
00044
00045 std::map<std::string, ChatClient*>::iterator itr = mPlayerMap.find(invitedName);
00046 if (itr == mPlayerMap.end())
00047 {
00048 itr->second->send(msg);
00049 }
00050 }
00051
00052 void ChatHandler::sendGuildRejoin(ChatClient &client)
00053 {
00054
00055 std::vector<Guild*> guilds = guildManager->getGuildsForPlayer(client.characterId);
00056 for (unsigned int i = 0; i != guilds.size(); ++i)
00057 {
00058 Guild *guild = guilds[i];
00059 short permissions;
00060 if (!guild)
00061 {
00062 return;
00063 }
00064 permissions = guild->getUserPermissions(client.characterId);
00065
00066 std::string guildName = guild->getName();
00067
00068
00069 MessageOut msg(CPMSG_GUILD_REJOIN);
00070 msg.writeString(guildName);
00071 msg.writeShort(guild->getId());
00072 msg.writeShort(permissions);
00073
00074
00075 ChatChannel *channel = joinGuildChannel(guildName, client);
00076
00077
00078 msg.writeShort(channel->getId());
00079 msg.writeString(channel->getAnnouncement());
00080
00081 client.send(msg);
00082
00083 sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_ONLINE_PLAYER);
00084
00085 }
00086 }
00087
00088 ChatChannel* ChatHandler::joinGuildChannel(const std::string &guildName, ChatClient &client)
00089 {
00090
00091 ChatChannel *channel = chatChannelManager->getChannel(guildName);
00092 if (!channel)
00093 {
00094
00095 int channelId = chatChannelManager->createNewChannel(guildName,
00096 "Guild Channel", "", false);
00097 channel = chatChannelManager->getChannel(channelId);
00098 }
00099
00100
00101 if (channel->addUser(&client))
00102 {
00103
00104
00105 warnUsersAboutPlayerEventInChat(channel, client.characterName,
00106 CHAT_EVENT_NEW_PLAYER);
00107 }
00108
00109 return channel;
00110 }
00111
00112 void ChatHandler::sendGuildListUpdate(const std::string &guildName,
00113 const std::string &characterName,
00114 char eventId)
00115 {
00116 Guild *guild = guildManager->findByName(guildName);
00117 if (guild)
00118 {
00119 MessageOut msg(CPMSG_GUILD_UPDATE_LIST);
00120
00121 msg.writeShort(guild->getId());
00122 msg.writeString(characterName);
00123 msg.writeByte(eventId);
00124 std::map<std::string, ChatClient*>::const_iterator chr;
00125 std::list<GuildMember*> members = guild->getMembers();
00126
00127 for (std::list<GuildMember*>::const_iterator itr = members.begin();
00128 itr != members.end(); ++itr)
00129 {
00130 Character *c = storage->getCharacter((*itr)->mId, NULL);
00131 chr = mPlayerMap.find(c->getName());
00132 if (chr != mPlayerMap.end())
00133 {
00134 chr->second->send(msg);
00135 }
00136 }
00137 }
00138 }
00139
00140 void
00141 ChatHandler::handleGuildCreation(ChatClient &client, MessageIn &msg)
00142 {
00143 MessageOut reply(CPMSG_GUILD_CREATE_RESPONSE);
00144
00145
00146 std::string guildName = msg.readString();
00147 if (!guildManager->doesExist(guildName))
00148 {
00149
00150 if (guildManager->alreadyOwner(client.characterId))
00151 {
00152 reply.writeByte(ERRMSG_LIMIT_REACHED);
00153 }
00154 else
00155 {
00156
00157 Guild *guild = guildManager->createGuild(guildName, client.characterId);
00158 reply.writeByte(ERRMSG_OK);
00159 reply.writeString(guildName);
00160 reply.writeShort(guild->getId());
00161 reply.writeShort(guild->getUserPermissions(client.characterId));
00162
00163
00164 ChatChannel* channel = joinGuildChannel(guildName, client);
00165 reply.writeShort(channel->getId());
00166 }
00167 }
00168 else
00169 {
00170 reply.writeByte(ERRMSG_ALREADY_TAKEN);
00171 }
00172
00173 client.send(reply);
00174 }
00175
00176 void
00177 ChatHandler::handleGuildInvitation(ChatClient &client, MessageIn &msg)
00178 {
00179 MessageOut reply(CPMSG_GUILD_INVITE_RESPONSE);
00180 MessageOut invite(CPMSG_GUILD_INVITED);
00181
00182
00183 int guildId = msg.readShort();
00184 std::string character = msg.readString();
00185
00186
00187 ChatClient *invitedClient = mPlayerMap[character];
00188 Guild *guild = guildManager->findById(guildId);
00189
00190 if (invitedClient && guild)
00191 {
00192
00193
00194 if (guild->canInvite(client.characterId) &&
00195 (client.characterName != character) &&
00196 !guild->checkInGuild(invitedClient->characterId))
00197 {
00198
00199
00200 std::string senderName = client.characterName;
00201 std::string guildName = guild->getName();
00202 invite.writeString(senderName);
00203 invite.writeString(guildName);
00204 invite.writeShort(guildId);
00205 invitedClient->send(invite);
00206 reply.writeByte(ERRMSG_OK);
00207
00208
00209 guild->addInvited(invitedClient->characterId);
00210 }
00211 else
00212 {
00213 reply.writeByte(ERRMSG_FAILURE);
00214 }
00215 }
00216 else
00217 {
00218 reply.writeByte(ERRMSG_FAILURE);
00219 }
00220
00221 client.send(reply);
00222 }
00223
00224 void
00225 ChatHandler::handleGuildAcceptInvite(ChatClient &client, MessageIn &msg)
00226 {
00227 MessageOut reply(CPMSG_GUILD_ACCEPT_RESPONSE);
00228 std::string guildName = msg.readString();
00229 bool error = true;
00230
00231
00232
00233
00234 Guild *guild = guildManager->findByName(guildName);
00235 if (guild)
00236 {
00237 if (guild->checkInvited(client.characterId))
00238 {
00239
00240 guildManager->addGuildMember(guild, client.characterId);
00241 reply.writeByte(ERRMSG_OK);
00242 reply.writeString(guild->getName());
00243 reply.writeShort(guild->getId());
00244 reply.writeShort(guild->getUserPermissions(client.characterId));
00245
00246
00247 ChatChannel *channel = joinGuildChannel(guild->getName(), client);
00248 reply.writeShort(channel->getId());
00249 sendGuildListUpdate(guildName, client.characterName, GUILD_EVENT_NEW_PLAYER);
00250
00251
00252 error = false;
00253 }
00254 }
00255
00256 if (error)
00257 {
00258 reply.writeByte(ERRMSG_FAILURE);
00259 }
00260
00261 client.send(reply);
00262 }
00263
00264 void
00265 ChatHandler::handleGuildRetrieveMembers(ChatClient &client, MessageIn &msg)
00266 {
00267 MessageOut reply(CPMSG_GUILD_GET_MEMBERS_RESPONSE);
00268 short guildId = msg.readShort();
00269 Guild *guild = guildManager->findById(guildId);
00270
00271
00272
00273 if (guild)
00274 {
00275
00276 if (guild->checkInGuild(client.characterId))
00277 {
00278 reply.writeByte(ERRMSG_OK);
00279 reply.writeShort(guildId);
00280 std::list<GuildMember*> memberList = guild->getMembers();
00281 std::list<GuildMember*>::const_iterator itr_end = memberList.end();
00282 for(std::list<GuildMember*>::iterator itr = memberList.begin();
00283 itr != itr_end; ++itr)
00284 {
00285 Character *c = storage->getCharacter((*itr)->mId, NULL);
00286 std::string memberName = c->getName();
00287 reply.writeString(memberName);
00288 reply.writeByte(mPlayerMap.find(memberName) != mPlayerMap.end());
00289 }
00290 }
00291 }
00292 else
00293 {
00294 reply.writeByte(ERRMSG_FAILURE);
00295 }
00296
00297 client.send(reply);
00298 }
00299
00300 void
00301 ChatHandler::handleGuildMemberLevelChange(ChatClient &client, MessageIn &msg)
00302 {
00303
00304
00305 MessageOut reply(CPMSG_GUILD_PROMOTE_MEMBER_RESPONSE);
00306 short guildId = msg.readShort();
00307 std::string user = msg.readString();
00308 short level = msg.readByte();
00309 Guild *guild = guildManager->findById(guildId);
00310 Character *c = storage->getCharacter(user);
00311
00312 if (guild && c)
00313 {
00314 int rights = guild->getUserPermissions(c->getDatabaseID()) | level;
00315 if (guildManager->changeMemberLevel(&client, guild, c->getDatabaseID(), rights) == 0)
00316 {
00317 reply.writeByte(ERRMSG_OK);
00318 client.send(reply);
00319 }
00320 }
00321
00322 reply.writeByte(ERRMSG_FAILURE);
00323 client.send(reply);
00324 }
00325
00326 void ChatHandler::handleGuildMemberKick(ChatClient &client, MessageIn &msg)
00327 {
00328 MessageOut reply(CPMSG_GUILD_KICK_MEMBER_RESPONSE);
00329 short guildId = msg.readShort();
00330 std::string user = msg.readString();
00331
00332 Guild *guild = guildManager->findById(guildId);
00333 Character *c = storage->getCharacter(user);
00334
00335 if (guild && c)
00336 {
00337 if (guild->getUserPermissions(c->getDatabaseID()) & GAL_KICK)
00338 {
00339 reply.writeByte(ERRMSG_OK);
00340 }
00341 else
00342 {
00343 reply.writeByte(ERRMSG_INSUFFICIENT_RIGHTS);
00344 }
00345 }
00346 else
00347 {
00348 reply.writeByte(ERRMSG_INVALID_ARGUMENT);
00349 }
00350
00351 client.send(reply);
00352 }
00353
00354 void
00355 ChatHandler::handleGuildQuit(ChatClient &client, MessageIn &msg)
00356 {
00357 MessageOut reply(CPMSG_GUILD_QUIT_RESPONSE);
00358 short guildId = msg.readShort();
00359 Guild *guild = guildManager->findById(guildId);
00360
00361
00362
00363
00364 if (guild)
00365 {
00366 if (guild->checkInGuild(client.characterId))
00367 {
00368 reply.writeByte(ERRMSG_OK);
00369 reply.writeShort(guildId);
00370
00371
00372 if (guild->totalMembers() == 0)
00373 {
00374 chatChannelManager->removeChannel(chatChannelManager->getChannelId(guild->getName()));
00375 }
00376
00377
00378
00379 guildManager->removeGuildMember(guild, client.characterId);
00380 sendGuildListUpdate(guild->getName(), client.characterName, GUILD_EVENT_LEAVING_PLAYER);
00381 }
00382 else
00383 {
00384 reply.writeByte(ERRMSG_FAILURE);
00385 }
00386 }
00387 else
00388 {
00389 reply.writeByte(ERRMSG_FAILURE);
00390 }
00391
00392 client.send(reply);
00393 }
00394
00395 void
00396 ChatHandler::guildChannelTopicChange(ChatChannel *channel, int playerId, const std::string &topic)
00397 {
00398 Guild *guild = guildManager->findByName(channel->getName());
00399 if (guild)
00400 {
00401 if(guild->getUserPermissions(playerId) & GAL_TOPIC_CHANGE)
00402 {
00403 chatChannelManager->setChannelTopic(channel->getId(), topic);
00404 }
00405 }
00406 }