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 "chatclient.hpp"
00024 #include "party.hpp"
00025
00026 #include "../account-server/dalstorage.hpp"
00027 #include "../account-server/serverhandler.hpp"
00028
00029 #include "../net/messagein.hpp"
00030 #include "../net/messageout.hpp"
00031
00032 #include "../defines.h"
00033
00034 #include <algorithm>
00035
00036 void updateInfo(ChatClient *client, int partyId)
00037 {
00038 Character *character = storage->getCharacter(client->characterName);
00039 GameServerHandler::sendPartyChange(character, partyId);
00040 }
00041
00042 bool ChatHandler::handlePartyJoin(const std::string &invited, const std::string &inviter)
00043 {
00044 MessageOut out(CPMSG_PARTY_INVITE_RESPONSE);
00045
00046
00047 ChatClient *c1 = getClient(inviter);
00048 if (c1)
00049 {
00050
00051 if (!c1->party)
00052 {
00053 c1->party = new Party();
00054
00055 updateInfo(c1, c1->party->getId());
00056 }
00057
00058
00059 c1->party->addUser(inviter);
00060
00061
00062 ChatClient *c2 = getClient(invited);
00063 if (c2)
00064 {
00065
00066 c1->party->addUser(invited);
00067 c2->party = c1->party;
00068
00069 out.writeString(invited);
00070 out.writeByte(ERRMSG_OK);
00071 c1->send(out);
00072
00073
00074 informPartyMemberJoined(*c2);
00075
00076
00077 updateInfo(c2, c2->party->getId());
00078 return true;
00079 }
00080 }
00081
00082
00083 return false;
00084
00085 }
00086
00087 void ChatHandler::handlePartyInvite(ChatClient &client, MessageIn &msg)
00088 {
00089
00090 MessageOut out(CPMSG_PARTY_INVITED);
00091
00092 out.writeString(client.characterName);
00093
00094 std::string invited = msg.readString();
00095 if (invited == client.characterName)
00096 {
00097 return;
00098 }
00099 if (invited != "")
00100 {
00101
00102 ChatClient *c = getClient(invited);
00103 if (c)
00104 {
00105 ++client.numInvites;
00106
00107
00108
00109
00110
00111 PartyInvite invite;
00112 invite.mInvited = invited;
00113 invite.mInviter = client.characterName;
00114 if (client.party)
00115 invite.mPartyId = client.party->getId();
00116 else
00117 invite.mPartyId = 0;
00118
00119 mPartyInvitedUsers.push_back(invite);
00120
00121 c->send(out);
00122 }
00123 }
00124 }
00125
00126 void ChatHandler::handlePartyAcceptInvite(ChatClient &client, MessageIn &msg)
00127 {
00128 MessageOut out(CPMSG_PARTY_ACCEPT_INVITE_RESPONSE);
00129
00130 std::string inviter = msg.readString();
00131
00132
00133 std::vector<PartyInvite>::iterator itr, itr_end;
00134 itr = mPartyInvitedUsers.begin();
00135 itr_end = mPartyInvitedUsers.end();
00136
00137 bool found = false;
00138
00139 while (itr != itr_end)
00140 {
00141 if ((*itr).mInvited == client.characterName &&
00142 (*itr).mInviter == inviter)
00143 {
00144
00145 if (handlePartyJoin(client.characterName, inviter))
00146 {
00147 out.writeByte(ERRMSG_OK);
00148 mPartyInvitedUsers.erase(itr);
00149 found = true;
00150 break;
00151 }
00152 }
00153
00154 ++itr;
00155 }
00156
00157 if (!found)
00158 {
00159 out.writeByte(ERRMSG_FAILURE);
00160 }
00161
00162 client.send(out);
00163 }
00164
00165 void ChatHandler::handlePartyQuit(ChatClient &client)
00166 {
00167 removeUserFromParty(client);
00168 MessageOut out(CPMSG_PARTY_QUIT_RESPONSE);
00169 out.writeByte(ERRMSG_OK);
00170 client.send(out);
00171
00172
00173 updateInfo(&client, 0);
00174 }
00175
00176 void ChatHandler::handlePartyRejection(ChatClient &client, MessageIn &msg)
00177 {
00178 MessageOut out(CPMSG_PARTY_REJECTED);
00179
00180 std::string inviter = msg.readString();
00181
00182
00183 std::vector<PartyInvite>::iterator itr, itr_end;
00184
00185 itr = mPartyInvitedUsers.begin();
00186 itr_end = mPartyInvitedUsers.end();
00187 bool found = false;
00188
00189 while (itr != itr_end)
00190 {
00191
00192 if ((*itr).mInvited == client.characterName &&
00193 (*itr).mInviter == inviter)
00194 {
00195
00196 mPartyInvitedUsers.erase(itr);
00197 found = true;
00198 break;
00199 }
00200
00201 ++itr;
00202 }
00203
00204 if (!found)
00205 {
00206 out.writeByte(ERRMSG_FAILURE);
00207 }
00208
00209
00210 ChatClient *inviterClient = getClient(inviter);
00211
00212 inviterClient->send(out);
00213 }
00214
00215 void ChatHandler::removeUserFromParty(ChatClient &client)
00216 {
00217 if (client.party)
00218 {
00219 client.party->removeUser(client.characterName);
00220 informPartyMemberQuit(client);
00221
00222
00223 if (client.party->numUsers() < 1)
00224 {
00225 delete client.party;
00226 client.party = 0;
00227 }
00228 }
00229 }
00230
00231 void ChatHandler::informPartyMemberQuit(ChatClient &client)
00232 {
00233 std::map<std::string, ChatClient*>::iterator itr;
00234 std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
00235
00236 for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
00237 {
00238 if (itr->second->party == client.party)
00239 {
00240 MessageOut out(CPMSG_PARTY_MEMBER_LEFT);
00241 out.writeShort(client.characterId);
00242 itr->second->send(out);
00243 }
00244 }
00245 }
00246
00247 void ChatHandler::informPartyMemberJoined(ChatClient &client)
00248 {
00249 std::map<std::string, ChatClient*>::iterator itr;
00250 std::map<std::string, ChatClient*>::const_iterator itr_end = mPlayerMap.end();
00251
00252 for (itr = mPlayerMap.begin(); itr != itr_end; ++itr)
00253 {
00254 if (itr->second->party == client.party)
00255 {
00256 MessageOut out(CPMSG_PARTY_NEW_MEMBER);
00257 out.writeShort(client.characterId);
00258 out.writeString(client.characterName);
00259 itr->second->send(out);
00260 }
00261 }
00262 }