Just a few renames.
[AHbot.git] / src / game / ArenaTeamHandler.cpp
blobf042f25517003edfd315733240d2d768ebee2e70
1 /*
2 * Copyright (C) 2005-2009 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"
29 void WorldSession::HandleInspectArenaTeamsOpcode(WorldPacket & recv_data)
31 sLog.outDebug("MSG_INSPECT_ARENA_TEAMS");
33 CHECK_PACKET_SIZE(recv_data, 8);
35 uint64 guid;
36 recv_data >> guid;
37 sLog.outDebug("Inspect Arena stats " I64FMTD, guid);
39 if(Player *plr = objmgr.GetPlayer(guid))
41 for (uint8 i = 0; i < MAX_ARENA_SLOT; ++i)
43 if(uint32 a_id = plr->GetArenaTeamId(i))
45 if(ArenaTeam *at = objmgr.GetArenaTeamById(a_id))
46 at->InspectStats(this, plr->GetGUID());
52 void WorldSession::HandleArenaTeamQueryOpcode(WorldPacket & recv_data)
54 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_QUERY" );
56 CHECK_PACKET_SIZE(recv_data, 4);
58 uint32 ArenaTeamId;
59 recv_data >> ArenaTeamId;
61 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
62 if(!arenateam) // arena team not found
63 return;
65 arenateam->Query(this);
66 arenateam->Stats(this);
69 void WorldSession::HandleArenaTeamRosterOpcode(WorldPacket & recv_data)
71 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_ROSTER" );
73 CHECK_PACKET_SIZE(recv_data, 4);
75 uint32 ArenaTeamId; // arena team id
76 recv_data >> ArenaTeamId;
78 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
79 if(!arenateam)
80 return;
82 arenateam->Roster(this);
85 void WorldSession::HandleArenaTeamInviteOpcode(WorldPacket & recv_data)
87 sLog.outDebug("CMSG_ARENA_TEAM_INVITE");
89 CHECK_PACKET_SIZE(recv_data, 4+1);
91 uint32 ArenaTeamId; // arena team id
92 std::string Invitedname;
94 Player * player = NULL;
96 recv_data >> ArenaTeamId >> Invitedname;
98 if(!Invitedname.empty())
100 if(!normalizePlayerName(Invitedname))
101 return;
103 player = ObjectAccessor::Instance().FindPlayerByName(Invitedname.c_str());
106 if(!player)
108 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", Invitedname, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
109 return;
112 if(player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
114 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", player->GetName(), ERR_ARENA_TEAM_PLAYER_TO_LOW);
115 return;
118 ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
119 if(!arenateam)
121 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM);
122 return;
125 // OK result but not send invite
126 if(player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
127 return;
129 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != GetPlayer()->GetTeam())
131 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_NOT_ALLIED);
132 return;
135 if(player->GetArenaTeamId(arenateam->GetSlot()))
137 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", player->GetName(), ERR_ALREADY_IN_ARENA_TEAM_S);
138 return;
141 if(player->GetArenaTeamIdInvited())
143 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", player->GetName(), ERR_ALREADY_INVITED_TO_ARENA_TEAM_S);
144 return;
147 if(arenateam->GetMembersSize() >= arenateam->GetType() * 2)
149 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,arenateam->GetName(),"",ERR_ARENA_TEAM_FULL);
150 return;
153 sLog.outDebug("Player %s Invited %s to Join his ArenaTeam", GetPlayer()->GetName(), Invitedname.c_str());
155 player->SetArenaTeamIdInvited(arenateam->GetId());
157 WorldPacket data(SMSG_ARENA_TEAM_INVITE, (8+10));
158 data << GetPlayer()->GetName();
159 data << arenateam->GetName();
160 player->GetSession()->SendPacket(&data);
162 sLog.outDebug("WORLD: Sent SMSG_ARENA_TEAM_INVITE");
165 void WorldSession::HandleArenaTeamAcceptOpcode(WorldPacket & /*recv_data*/)
167 sLog.outDebug("CMSG_ARENA_TEAM_ACCEPT"); // empty opcode
169 ArenaTeam *at = objmgr.GetArenaTeamById(_player->GetArenaTeamIdInvited());
170 if(!at)
171 return;
173 if(_player->GetArenaTeamId(at->GetSlot()))
175 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ALREADY_IN_ARENA_TEAM); // already in arena team that size
176 return;
179 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && _player->GetTeam() != objmgr.GetPlayerTeamByGUID(at->GetCaptain()))
181 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ARENA_TEAM_NOT_ALLIED);// not let enemies sign petition
182 return;
185 if(!at->AddMember(_player->GetGUID()))
187 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ARENA_TEAM_INTERNAL);// arena team not found
188 return;
191 // event
192 WorldPacket data;
193 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_JOIN_SS, 2, _player->GetName(), at->GetName(), "");
194 at->BroadcastPacket(&data);
197 void WorldSession::HandleArenaTeamDeclineOpcode(WorldPacket & /*recv_data*/)
199 sLog.outDebug("CMSG_ARENA_TEAM_DECLINE"); // empty opcode
201 _player->SetArenaTeamIdInvited(0); // no more invited
204 void WorldSession::HandleArenaTeamLeaveOpcode(WorldPacket & recv_data)
206 sLog.outDebug("CMSG_ARENA_TEAM_LEAVE");
208 CHECK_PACKET_SIZE(recv_data, 4);
210 uint32 ArenaTeamId; // arena team id
211 recv_data >> ArenaTeamId;
213 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
214 if(!at)
215 return;
216 if(_player->GetGUID() == at->GetCaptain() && at->GetMembersSize() > 1)
218 // check for correctness
219 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
220 return;
222 // arena team has only one member (=captain)
223 if(_player->GetGUID() == at->GetCaptain())
225 at->Disband(this);
226 delete at;
227 return;
230 at->DelMember(_player->GetGUID());
232 // event
233 WorldPacket data;
234 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEAVE_SS, 2, _player->GetName(), at->GetName(), "");
235 at->BroadcastPacket(&data);
237 //send you are no longer member of team
238 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, at->GetName(), "", 0);
241 void WorldSession::HandleArenaTeamDisbandOpcode(WorldPacket & recv_data)
243 sLog.outDebug("CMSG_ARENA_TEAM_DISBAND");
245 CHECK_PACKET_SIZE(recv_data, 4);
247 uint32 ArenaTeamId; // arena team id
248 recv_data >> ArenaTeamId;
250 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
251 if(!at)
252 return;
254 if(at->GetCaptain() != _player->GetGUID())
255 return;
257 if (at->IsFighting())
258 return;
260 at->Disband(this);
261 delete at;
264 void WorldSession::HandleArenaTeamRemoveOpcode(WorldPacket & recv_data)
266 sLog.outDebug("CMSG_ARENA_TEAM_REMOVE");
268 CHECK_PACKET_SIZE(recv_data, 4+1);
270 uint32 ArenaTeamId;
271 std::string name;
273 recv_data >> ArenaTeamId;
274 recv_data >> name;
276 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
277 if(!at) // arena team not found
278 return;
280 if(at->GetCaptain() != _player->GetGUID())
282 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
283 return;
286 if(!normalizePlayerName(name))
287 return;
289 ArenaTeamMember* member = at->GetMember(name);
290 if(!member) // member not found
292 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", name, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
293 return;
296 if(at->GetCaptain() == member->guid)
298 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
299 return;
302 at->DelMember(member->guid);
304 // event
305 WorldPacket data;
306 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_REMOVE_SSS, 3, name, at->GetName(), _player->GetName());
307 at->BroadcastPacket(&data);
310 void WorldSession::HandleArenaTeamLeaderOpcode(WorldPacket & recv_data)
312 sLog.outDebug("CMSG_ARENA_TEAM_LEADER");
314 CHECK_PACKET_SIZE(recv_data, 4+1);
316 uint32 ArenaTeamId;
317 std::string name;
319 recv_data >> ArenaTeamId;
320 recv_data >> name;
322 ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
323 if(!at) // arena team not found
324 return;
326 if(at->GetCaptain() != _player->GetGUID())
328 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
329 return;
332 if(!normalizePlayerName(name))
333 return;
335 ArenaTeamMember* member = at->GetMember(name);
336 if(!member) // member not found
338 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", name, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
339 return;
342 if(at->GetCaptain() == member->guid) // target player already captain
343 return;
345 at->SetCaptain(member->guid);
347 // event
348 WorldPacket data;
349 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEADER_CHANGED_SSS, 3, _player->GetName(), name, at->GetName());
350 at->BroadcastPacket(&data);
353 void WorldSession::SendArenaTeamCommandResult(uint32 team_action, const std::string& team, const std::string& player, uint32 error_id)
355 WorldPacket data(SMSG_ARENA_TEAM_COMMAND_RESULT, 4+team.length()+1+player.length()+1+4);
356 data << team_action;
357 data << team;
358 data << player;
359 data << error_id;
360 SendPacket(&data);
363 void WorldSession::BuildArenaTeamEventPacket(WorldPacket *data, uint8 eventid, uint8 str_count, const std::string& str1, const std::string& str2, const std::string& str3)
365 data->Initialize(SMSG_ARENA_TEAM_EVENT, 1+1+1);
366 *data << eventid;
367 *data << str_count;
368 switch(str_count)
370 case 1:
371 *data << str1;
372 break;
373 case 2:
374 *data << str1;
375 *data << str2;
376 break;
377 case 3:
378 *data << str1;
379 *data << str2;
380 *data << str3;
381 break;
382 default:
383 sLog.outError("Unhandled str_count %u in SendArenaTeamEvent()", str_count);
384 return;
388 void WorldSession::SendNotInArenaTeamPacket(uint8 type)
390 WorldPacket data(SMSG_ARENA_ERROR, 4+1); // 886 - You are not in a %uv%u arena team
391 uint32 unk = 0;
392 data << uint32(unk); // unk(0)
393 if(!unk)
394 data << uint8(type); // team type (2=2v2,3=3v3,5=5v5), can be used for custom types...
395 SendPacket(&data);
399 +ERR_ARENA_NO_TEAM_II "You are not in a %dv%d arena team"
401 +ERR_ARENA_TEAM_CREATE_S "%s created. To disband, use /teamdisband [2v2, 3v3, 5v5]."
402 +ERR_ARENA_TEAM_INVITE_SS "You have invited %s to join %s"
403 +ERR_ARENA_TEAM_QUIT_S "You are no longer a member of %s"
404 ERR_ARENA_TEAM_FOUNDER_S "Congratulations, you are a founding member of %s! To leave, use /teamquit [2v2, 3v3, 5v5]."
406 +ERR_ARENA_TEAM_INTERNAL "Internal arena team error"
407 +ERR_ALREADY_IN_ARENA_TEAM "You are already in an arena team of that size"
408 +ERR_ALREADY_IN_ARENA_TEAM_S "%s is already in an arena team of that size"
409 +ERR_INVITED_TO_ARENA_TEAM "You have already been invited into an arena team"
410 +ERR_ALREADY_INVITED_TO_ARENA_TEAM_S "%s has already been invited to an arena team"
411 +ERR_ARENA_TEAM_NAME_INVALID "That name contains invalid characters, please enter a new name"
412 +ERR_ARENA_TEAM_NAME_EXISTS_S "There is already an arena team named \"%s\""
413 +ERR_ARENA_TEAM_LEADER_LEAVE_S "You must promote a new team captain using /teamcaptain before leaving the team"
414 +ERR_ARENA_TEAM_PERMISSIONS "You don't have permission to do that"
415 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM "You are not in an arena team of that size"
416 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM_SS "%s is not in %s"
417 +ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S "\"%s\" not found"
418 +ERR_ARENA_TEAM_NOT_ALLIED "You cannot invite players from the opposing alliance"
420 +ERR_ARENA_TEAM_JOIN_SS "%s has joined %s"
421 +ERR_ARENA_TEAM_YOU_JOIN_S "You have joined %s. To leave, use /teamquit [2v2, 3v3, 5v5]."
423 +ERR_ARENA_TEAM_LEAVE_SS "%s has left %s"
425 +ERR_ARENA_TEAM_LEADER_IS_SS "%s is the captain of %s"
426 +ERR_ARENA_TEAM_LEADER_CHANGED_SSS "%s has made %s the new captain of %s"
428 +ERR_ARENA_TEAM_REMOVE_SSS "%s has been kicked out of %s by %s"
430 +ERR_ARENA_TEAM_DISBANDED_S "%s has disbanded %s"
432 ERR_ARENA_TEAM_TARGET_TOO_LOW_S "%s is not high enough level to join your team"
434 ERR_ARENA_TEAM_TOO_MANY_MEMBERS_S "%s is full"
436 ERR_ARENA_TEAM_LEVEL_TOO_LOW_I "You must be level %d to form an arena team"