Fixed possible crash
[getmangos.git] / src / game / ArenaTeamHandler.cpp
blob7cab8d085a172304d74b29c8c2393f8e7cd2c26b
1 /*
2 * Copyright (C) 2005-2008 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "WorldSession.h"
20 #include "WorldPacket.h"
21 #include "Log.h"
22 #include "Database/DatabaseEnv.h"
23 #include "Player.h"
24 #include "ObjectMgr.h"
25 #include "ArenaTeam.h"
26 #include "World.h"
27 #include "SocialMgr.h"
28 #include "Language.h"
30 void WorldSession::HandleInspectArenaStatsOpcode(WorldPacket & recv_data)
32 sLog.outDebug("MSG_INSPECT_ARENA_TEAMS");
33 //recv_data.hexlike();
35 CHECK_PACKET_SIZE(recv_data, 8);
37 uint64 guid;
38 recv_data >> guid;
39 sLog.outDebug("Inspect Arena stats " I64FMTD, guid);
41 if(Player *plr = objmgr.GetPlayer(guid))
43 for (uint8 i = 0; i < MAX_ARENA_SLOT; i++)
45 if(uint32 a_id = plr->GetArenaTeamId(i))
47 if(ArenaTeam *at = objmgr.GetArenaTeamById(a_id))
48 at->InspectStats(this, plr->GetGUID());
54 void WorldSession::HandleArenaTeamQueryOpcode(WorldPacket & recv_data)
56 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_QUERY" );
57 //recv_data.hexlike();
59 CHECK_PACKET_SIZE(recv_data, 4);
61 uint32 ArenaTeamId;
62 recv_data >> ArenaTeamId;
64 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
65 if(!arenateam) // arena team not found
66 return;
68 arenateam->Query(this);
69 arenateam->Stats(this);
72 void WorldSession::HandleArenaTeamRosterOpcode(WorldPacket & recv_data)
74 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_ROSTER" );
75 //recv_data.hexlike();
77 CHECK_PACKET_SIZE(recv_data, 4);
79 uint32 ArenaTeamId; // arena team id
80 recv_data >> ArenaTeamId;
82 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
83 if(!arenateam)
84 return;
86 arenateam->Roster(this);
89 void WorldSession::HandleArenaTeamAddMemberOpcode(WorldPacket & recv_data)
91 sLog.outDebug("CMSG_ARENA_TEAM_ADD_MEMBER");
92 //recv_data.hexlike();
94 CHECK_PACKET_SIZE(recv_data, 4+1);
96 uint32 ArenaTeamId; // arena team id
97 std::string Invitedname;
99 Player * player = NULL;
101 recv_data >> ArenaTeamId >> Invitedname;
103 if(!Invitedname.empty())
105 if(!normalizePlayerName(Invitedname))
106 return;
108 player = ObjectAccessor::Instance().FindPlayerByName(Invitedname.c_str());
111 if(!player)
113 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", Invitedname, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
114 return;
117 if(player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
119 //SendArenaTeamCommandResult(ARENA_TEAM_INVITE_SS,"",Invitedname,ARENA_TEAM_PLAYER_NOT_FOUND_S);
120 // can't find related opcode
121 SendNotification(LANG_HIS_ARENA_LEVEL_REQ_ERROR, player->GetName());
122 return;
125 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
126 if(!arenateam)
128 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM);
129 return;
132 // OK result but not send invite
133 if(player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
134 return;
136 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != GetPlayer()->GetTeam())
138 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_NOT_ALLIED);
139 return;
142 if(player->GetArenaTeamId(arenateam->GetSlot()))
144 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, player->GetName(), "", ERR_ALREADY_IN_ARENA_TEAM_S);
145 return;
148 if(player->GetArenaTeamIdInvited())
150 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, player->GetName(), "", ERR_ALREADY_INVITED_TO_ARENA_TEAM_S);
151 return;
154 if(arenateam->GetMembersSize() >= arenateam->GetType() * 2)
156 // should send an "arena team is full" or the likes message, I just don't know the proper values so... ERR_INTERNAL
157 // SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_INTERNAL);
158 SendNotification(LANG_YOUR_ARENA_TEAM_FULL, player->GetName());
159 return;
162 sLog.outDebug("Player %s Invited %s to Join his ArenaTeam", GetPlayer()->GetName(), Invitedname.c_str());
164 player->SetArenaTeamIdInvited(arenateam->GetId());
166 WorldPacket data(SMSG_ARENA_TEAM_INVITE, (8+10));
167 data << GetPlayer()->GetName();
168 data << arenateam->GetName();
169 player->GetSession()->SendPacket(&data);
171 sLog.outDebug("WORLD: Sent SMSG_ARENA_TEAM_INVITE");
174 void WorldSession::HandleArenaTeamInviteAcceptOpcode(WorldPacket & /*recv_data*/)
176 sLog.outDebug("CMSG_ARENA_TEAM_INVITE_ACCEPT"); // empty opcode
178 ArenaTeam *at = objmgr.GetArenaTeamById(_player->GetArenaTeamIdInvited());
179 if(!at)
181 // arena team not exist
182 return;
185 if(_player->GetArenaTeamId(at->GetSlot()))
187 // already in arena team that size
188 return;
191 // not let enemies sign petition
192 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && _player->GetTeam() != objmgr.GetPlayerTeamByGUID(at->GetCaptain()))
193 return;
195 if(!at->AddMember(_player->GetGUID()))
196 return;
198 // event
199 WorldPacket data;
200 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_JOIN_SS, 2, _player->GetName(), at->GetName(), "");
201 at->BroadcastPacket(&data);
204 void WorldSession::HandleArenaTeamInviteDeclineOpcode(WorldPacket & /*recv_data*/)
206 sLog.outDebug("CMSG_ARENA_TEAM_INVITE_DECLINE"); // empty opcode
208 _player->SetArenaTeamIdInvited(0); // no more invited
211 void WorldSession::HandleArenaTeamLeaveOpcode(WorldPacket & recv_data)
213 sLog.outDebug("CMSG_ARENA_TEAM_LEAVE");
214 //recv_data.hexlike();
216 CHECK_PACKET_SIZE(recv_data, 4);
218 uint32 ArenaTeamId; // arena team id
219 recv_data >> ArenaTeamId;
221 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
222 if(!at)
224 // send command result
225 return;
227 if(_player->GetGUID() == at->GetCaptain() && at->GetMembersSize() > 1)
229 // check for correctness
230 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
231 return;
233 // arena team has only one member (=captain)
234 if(_player->GetGUID() == at->GetCaptain())
236 at->Disband(this);
237 delete at;
238 return;
241 at->DelMember(_player->GetGUID());
243 // event
244 WorldPacket data;
245 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEAVE_SS, 2, _player->GetName(), at->GetName(), "");
246 at->BroadcastPacket(&data);
248 //SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, at->GetName(), "", 0);
251 void WorldSession::HandleArenaTeamDisbandOpcode(WorldPacket & recv_data)
253 sLog.outDebug("CMSG_ARENA_TEAM_DISBAND");
254 //recv_data.hexlike();
256 CHECK_PACKET_SIZE(recv_data, 4);
258 uint32 ArenaTeamId; // arena team id
259 recv_data >> ArenaTeamId;
261 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
262 if(!at)
264 // arena team not found
265 return;
268 if(at->GetCaptain() != _player->GetGUID())
270 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
271 return;
274 at->Disband(this);
275 delete at;
278 void WorldSession::HandleArenaTeamRemoveFromTeamOpcode(WorldPacket & recv_data)
280 sLog.outDebug("CMSG_ARENA_TEAM_REMOVE_FROM_TEAM");
281 //recv_data.hexlike();
283 CHECK_PACKET_SIZE(recv_data, 4+1);
285 uint32 ArenaTeamId;
286 std::string name;
288 recv_data >> ArenaTeamId;
289 recv_data >> name;
291 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
292 if(!at) // arena team not found
293 return;
295 if(at->GetCaptain() != _player->GetGUID())
297 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
298 return;
301 if(!normalizePlayerName(name))
302 return;
304 ArenaTeamMember* member = at->GetMember(name);
305 if(!member) // member not found
306 return;
308 if(at->GetCaptain() == member->guid)
310 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
311 return;
314 at->DelMember(member->guid);
316 // event
317 WorldPacket data;
318 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_REMOVE_SSS, 3, name, at->GetName(), _player->GetName());
319 at->BroadcastPacket(&data);
322 void WorldSession::HandleArenaTeamPromoteToCaptainOpcode(WorldPacket & recv_data)
324 sLog.outDebug("CMSG_ARENA_TEAM_PROMOTE_TO_CAPTAIN");
325 //recv_data.hexlike();
327 CHECK_PACKET_SIZE(recv_data, 4+1);
329 uint32 ArenaTeamId;
330 std::string name;
332 recv_data >> ArenaTeamId;
333 recv_data >> name;
335 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
336 if(!at) // arena team not found
337 return;
339 if(at->GetCaptain() != _player->GetGUID())
341 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
342 return;
345 if(!normalizePlayerName(name))
346 return;
348 ArenaTeamMember* member = at->GetMember(name);
349 if(!member) // member not found
350 return;
352 if(at->GetCaptain() == member->guid) // target player already captain
353 return;
355 at->SetCaptain(member->guid);
357 // event
358 WorldPacket data;
359 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEADER_CHANGED_SSS, 3, _player->GetName(), name, at->GetName());
360 at->BroadcastPacket(&data);
363 void WorldSession::SendArenaTeamCommandResult(uint32 unk1, std::string str1, std::string str2, uint32 unk3)
365 WorldPacket data(SMSG_ARENA_TEAM_COMMAND_RESULT, 4+str1.length()+1+str2.length()+1+4);
366 data << unk1;
367 data << str1;
368 data << str2;
369 data << unk3;
370 SendPacket(&data);
373 void WorldSession::BuildArenaTeamEventPacket(WorldPacket *data, uint8 eventid, uint8 str_count, std::string str1, std::string str2, std::string str3)
375 data->Initialize(SMSG_ARENA_TEAM_EVENT, 1+1+1);
376 *data << eventid;
377 *data << str_count;
378 switch(str_count)
380 case 1:
381 *data << str1;
382 break;
383 case 2:
384 *data << str1;
385 *data << str2;
386 break;
387 case 3:
388 *data << str1;
389 *data << str2;
390 *data << str3;
391 break;
392 default:
393 sLog.outError("Unhandled str_count %u in SendArenaTeamEvent()", str_count);
394 return;
398 void WorldSession::SendNotInArenaTeamPacket(uint8 type)
400 WorldPacket data(SMSG_ARENA_ERROR, 4+1); // 886 - You are not in a %uv%u arena team
401 uint32 unk = 0;
402 data << uint32(unk); // unk(0)
403 if(!unk)
404 data << uint8(type); // team type (2=2v2,3=3v3,5=5v5), can be used for custom types...
405 SendPacket(&data);
409 +ERR_ARENA_NO_TEAM_II "You are not in a %dv%d arena team"
411 +ERR_ARENA_TEAM_CREATE_S "%s created. To disband, use /teamdisband [2v2, 3v3, 5v5]."
412 +ERR_ARENA_TEAM_INVITE_SS "You have invited %s to join %s"
413 +ERR_ARENA_TEAM_QUIT_S "You are no longer a member of %s"
414 ERR_ARENA_TEAM_FOUNDER_S "Congratulations, you are a founding member of %s! To leave, use /teamquit [2v2, 3v3, 5v5]."
416 +ERR_ARENA_TEAM_INTERNAL "Internal arena team error"
417 +ERR_ALREADY_IN_ARENA_TEAM "You are already in an arena team of that size"
418 +ERR_ALREADY_IN_ARENA_TEAM_S "%s is already in an arena team of that size"
419 +ERR_INVITED_TO_ARENA_TEAM "You have already been invited into an arena team"
420 +ERR_ALREADY_INVITED_TO_ARENA_TEAM_S "%s has already been invited to an arena team"
421 +ERR_ARENA_TEAM_NAME_INVALID "That name contains invalid characters, please enter a new name"
422 +ERR_ARENA_TEAM_NAME_EXISTS_S "There is already an arena team named \"%s\""
423 +ERR_ARENA_TEAM_LEADER_LEAVE_S "You must promote a new team captain using /teamcaptain before leaving the team"
424 +ERR_ARENA_TEAM_PERMISSIONS "You don't have permission to do that"
425 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM "You are not in an arena team of that size"
426 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM_SS "%s is not in %s"
427 +ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S "\"%s\" not found"
428 +ERR_ARENA_TEAM_NOT_ALLIED "You cannot invite players from the opposing alliance"
430 +ERR_ARENA_TEAM_JOIN_SS "%s has joined %s"
431 +ERR_ARENA_TEAM_YOU_JOIN_S "You have joined %s. To leave, use /teamquit [2v2, 3v3, 5v5]."
433 +ERR_ARENA_TEAM_LEAVE_SS "%s has left %s"
435 +ERR_ARENA_TEAM_LEADER_IS_SS "%s is the captain of %s"
436 +ERR_ARENA_TEAM_LEADER_CHANGED_SSS "%s has made %s the new captain of %s"
438 +ERR_ARENA_TEAM_REMOVE_SSS "%s has been kicked out of %s by %s"
440 +ERR_ARENA_TEAM_DISBANDED_S "%s has disbanded %s"
442 ERR_ARENA_TEAM_TARGET_TOO_LOW_S "%s is not high enough level to join your team"
444 ERR_ARENA_TEAM_TOO_MANY_MEMBERS_S "%s is full"
446 ERR_ARENA_TEAM_LEVEL_TOO_LOW_I "You must be level %d to form an arena team"