[9232] Replace list bool fields with exclusive true values by subtype field in Creature.
[getmangos.git] / src / game / ArenaTeamHandler.cpp
blob93543cc6fe480478a14b10eb396ec4443c7212b1
1 /*
2 * Copyright (C) 2005-2010 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 uint64 guid;
34 recv_data >> guid;
35 sLog.outDebug("Inspect Arena stats (GUID: %u TypeId: %u)", GUID_LOPART(guid),GuidHigh2TypeId(GUID_HIPART(guid)));
37 if(Player *plr = sObjectMgr.GetPlayer(guid))
39 for (uint8 i = 0; i < MAX_ARENA_SLOT; ++i)
41 if(uint32 a_id = plr->GetArenaTeamId(i))
43 if(ArenaTeam *at = sObjectMgr.GetArenaTeamById(a_id))
44 at->InspectStats(this, plr->GetGUID());
50 void WorldSession::HandleArenaTeamQueryOpcode(WorldPacket & recv_data)
52 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_QUERY" );
54 uint32 ArenaTeamId;
55 recv_data >> ArenaTeamId;
57 if(ArenaTeam *arenateam = sObjectMgr.GetArenaTeamById(ArenaTeamId))
59 arenateam->Query(this);
60 arenateam->Stats(this);
64 void WorldSession::HandleArenaTeamRosterOpcode(WorldPacket & recv_data)
66 sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_ROSTER" );
68 uint32 ArenaTeamId; // arena team id
69 recv_data >> ArenaTeamId;
71 if(ArenaTeam *arenateam = sObjectMgr.GetArenaTeamById(ArenaTeamId))
72 arenateam->Roster(this);
75 void WorldSession::HandleArenaTeamInviteOpcode(WorldPacket & recv_data)
77 sLog.outDebug("CMSG_ARENA_TEAM_INVITE");
79 uint32 ArenaTeamId; // arena team id
80 std::string Invitedname;
82 Player * player = NULL;
84 recv_data >> ArenaTeamId >> Invitedname;
86 if(!Invitedname.empty())
88 if(!normalizePlayerName(Invitedname))
89 return;
91 player = ObjectAccessor::FindPlayerByName(Invitedname.c_str());
94 if(!player)
96 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", Invitedname, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
97 return;
100 if(player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
102 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", player->GetName(), ERR_ARENA_TEAM_PLAYER_TO_LOW);
103 return;
106 ArenaTeam *arenateam = sObjectMgr.GetArenaTeamById(ArenaTeamId);
107 if(!arenateam)
109 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM);
110 return;
113 // OK result but not send invite
114 if(player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
115 return;
117 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != GetPlayer()->GetTeam())
119 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_NOT_ALLIED);
120 return;
123 if(player->GetArenaTeamId(arenateam->GetSlot()))
125 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", player->GetName(), ERR_ALREADY_IN_ARENA_TEAM_S);
126 return;
129 if(player->GetArenaTeamIdInvited())
131 SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", player->GetName(), ERR_ALREADY_INVITED_TO_ARENA_TEAM_S);
132 return;
135 if(arenateam->GetMembersSize() >= arenateam->GetType() * 2)
137 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,arenateam->GetName(),"",ERR_ARENA_TEAM_FULL);
138 return;
141 sLog.outDebug("Player %s Invited %s to Join his ArenaTeam", GetPlayer()->GetName(), Invitedname.c_str());
143 player->SetArenaTeamIdInvited(arenateam->GetId());
145 WorldPacket data(SMSG_ARENA_TEAM_INVITE, (8+10));
146 data << GetPlayer()->GetName();
147 data << arenateam->GetName();
148 player->GetSession()->SendPacket(&data);
150 sLog.outDebug("WORLD: Sent SMSG_ARENA_TEAM_INVITE");
153 void WorldSession::HandleArenaTeamAcceptOpcode(WorldPacket & /*recv_data*/)
155 sLog.outDebug("CMSG_ARENA_TEAM_ACCEPT"); // empty opcode
157 ArenaTeam *at = sObjectMgr.GetArenaTeamById(_player->GetArenaTeamIdInvited());
158 if(!at)
159 return;
161 if(_player->GetArenaTeamId(at->GetSlot()))
163 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ALREADY_IN_ARENA_TEAM); // already in arena team that size
164 return;
167 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && _player->GetTeam() != sObjectMgr.GetPlayerTeamByGUID(at->GetCaptain()))
169 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ARENA_TEAM_NOT_ALLIED);// not let enemies sign petition
170 return;
173 if(!at->AddMember(_player->GetGUID()))
175 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S,"","",ERR_ARENA_TEAM_INTERNAL);// arena team not found
176 return;
179 // event
180 WorldPacket data;
181 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_JOIN_SS, 2, _player->GetName(), at->GetName(), "");
182 at->BroadcastPacket(&data);
185 void WorldSession::HandleArenaTeamDeclineOpcode(WorldPacket & /*recv_data*/)
187 sLog.outDebug("CMSG_ARENA_TEAM_DECLINE"); // empty opcode
189 _player->SetArenaTeamIdInvited(0); // no more invited
192 void WorldSession::HandleArenaTeamLeaveOpcode(WorldPacket & recv_data)
194 sLog.outDebug("CMSG_ARENA_TEAM_LEAVE");
196 uint32 ArenaTeamId; // arena team id
197 recv_data >> ArenaTeamId;
199 ArenaTeam *at = sObjectMgr.GetArenaTeamById(ArenaTeamId);
200 if(!at)
201 return;
203 if(_player->GetGUID() == at->GetCaptain() && at->GetMembersSize() > 1)
205 // check for correctness
206 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
207 return;
210 // arena team has only one member (=captain)
211 if(_player->GetGUID() == at->GetCaptain())
213 at->Disband(this);
214 delete at;
215 return;
218 at->DelMember(_player->GetGUID());
220 // event
221 WorldPacket data;
222 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEAVE_SS, 2, _player->GetName(), at->GetName(), "");
223 at->BroadcastPacket(&data);
225 //send you are no longer member of team
226 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, at->GetName(), "", 0);
229 void WorldSession::HandleArenaTeamDisbandOpcode(WorldPacket & recv_data)
231 sLog.outDebug("CMSG_ARENA_TEAM_DISBAND");
233 uint32 ArenaTeamId; // arena team id
234 recv_data >> ArenaTeamId;
236 if(ArenaTeam *at = sObjectMgr.GetArenaTeamById(ArenaTeamId))
238 if(at->GetCaptain() != _player->GetGUID())
239 return;
241 if(at->IsFighting())
242 return;
244 at->Disband(this);
245 delete at;
249 void WorldSession::HandleArenaTeamRemoveOpcode(WorldPacket & recv_data)
251 sLog.outDebug("CMSG_ARENA_TEAM_REMOVE");
253 uint32 ArenaTeamId;
254 std::string name;
256 recv_data >> ArenaTeamId;
257 recv_data >> name;
259 ArenaTeam *at = sObjectMgr.GetArenaTeamById(ArenaTeamId);
260 if(!at) // arena team not found
261 return;
263 if(at->GetCaptain() != _player->GetGUID())
265 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
266 return;
269 if(!normalizePlayerName(name))
270 return;
272 ArenaTeamMember* member = at->GetMember(name);
273 if(!member) // member not found
275 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", name, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
276 return;
279 if(at->GetCaptain() == member->guid)
281 SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
282 return;
285 at->DelMember(member->guid);
287 // event
288 WorldPacket data;
289 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_REMOVE_SSS, 3, name, at->GetName(), _player->GetName());
290 at->BroadcastPacket(&data);
293 void WorldSession::HandleArenaTeamLeaderOpcode(WorldPacket & recv_data)
295 sLog.outDebug("CMSG_ARENA_TEAM_LEADER");
297 uint32 ArenaTeamId;
298 std::string name;
300 recv_data >> ArenaTeamId;
301 recv_data >> name;
303 ArenaTeam *at = sObjectMgr.GetArenaTeamById(ArenaTeamId);
304 if(!at) // arena team not found
305 return;
307 if(at->GetCaptain() != _player->GetGUID())
309 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
310 return;
313 if(!normalizePlayerName(name))
314 return;
316 ArenaTeamMember* member = at->GetMember(name);
317 if(!member) // member not found
319 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", name, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
320 return;
323 if(at->GetCaptain() == member->guid) // target player already captain
324 return;
326 at->SetCaptain(member->guid);
328 // event
329 WorldPacket data;
330 BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEADER_CHANGED_SSS, 3, _player->GetName(), name, at->GetName());
331 at->BroadcastPacket(&data);
334 void WorldSession::SendArenaTeamCommandResult(uint32 team_action, const std::string& team, const std::string& player, uint32 error_id)
336 WorldPacket data(SMSG_ARENA_TEAM_COMMAND_RESULT, 4+team.length()+1+player.length()+1+4);
337 data << team_action;
338 data << team;
339 data << player;
340 data << error_id;
341 SendPacket(&data);
344 void WorldSession::BuildArenaTeamEventPacket(WorldPacket *data, uint8 eventid, uint8 str_count, const std::string& str1, const std::string& str2, const std::string& str3)
346 data->Initialize(SMSG_ARENA_TEAM_EVENT, 1+1+1);
347 *data << eventid;
348 *data << str_count;
349 switch(str_count)
351 case 1:
352 *data << str1;
353 break;
354 case 2:
355 *data << str1;
356 *data << str2;
357 break;
358 case 3:
359 *data << str1;
360 *data << str2;
361 *data << str3;
362 break;
363 default:
364 sLog.outError("Unhandled str_count %u in SendArenaTeamEvent()", str_count);
365 return;
369 void WorldSession::SendNotInArenaTeamPacket(uint8 type)
371 WorldPacket data(SMSG_ARENA_ERROR, 4+1); // 886 - You are not in a %uv%u arena team
372 uint32 unk = 0;
373 data << uint32(unk); // unk(0)
374 if(!unk)
375 data << uint8(type); // team type (2=2v2,3=3v3,5=5v5), can be used for custom types...
376 SendPacket(&data);
380 +ERR_ARENA_NO_TEAM_II "You are not in a %dv%d arena team"
382 +ERR_ARENA_TEAM_CREATE_S "%s created. To disband, use /teamdisband [2v2, 3v3, 5v5]."
383 +ERR_ARENA_TEAM_INVITE_SS "You have invited %s to join %s"
384 +ERR_ARENA_TEAM_QUIT_S "You are no longer a member of %s"
385 ERR_ARENA_TEAM_FOUNDER_S "Congratulations, you are a founding member of %s! To leave, use /teamquit [2v2, 3v3, 5v5]."
387 +ERR_ARENA_TEAM_INTERNAL "Internal arena team error"
388 +ERR_ALREADY_IN_ARENA_TEAM "You are already in an arena team of that size"
389 +ERR_ALREADY_IN_ARENA_TEAM_S "%s is already in an arena team of that size"
390 +ERR_INVITED_TO_ARENA_TEAM "You have already been invited into an arena team"
391 +ERR_ALREADY_INVITED_TO_ARENA_TEAM_S "%s has already been invited to an arena team"
392 +ERR_ARENA_TEAM_NAME_INVALID "That name contains invalid characters, please enter a new name"
393 +ERR_ARENA_TEAM_NAME_EXISTS_S "There is already an arena team named \"%s\""
394 +ERR_ARENA_TEAM_LEADER_LEAVE_S "You must promote a new team captain using /teamcaptain before leaving the team"
395 +ERR_ARENA_TEAM_PERMISSIONS "You don't have permission to do that"
396 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM "You are not in an arena team of that size"
397 +ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM_SS "%s is not in %s"
398 +ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S "\"%s\" not found"
399 +ERR_ARENA_TEAM_NOT_ALLIED "You cannot invite players from the opposing alliance"
401 +ERR_ARENA_TEAM_JOIN_SS "%s has joined %s"
402 +ERR_ARENA_TEAM_YOU_JOIN_S "You have joined %s. To leave, use /teamquit [2v2, 3v3, 5v5]."
404 +ERR_ARENA_TEAM_LEAVE_SS "%s has left %s"
406 +ERR_ARENA_TEAM_LEADER_IS_SS "%s is the captain of %s"
407 +ERR_ARENA_TEAM_LEADER_CHANGED_SSS "%s has made %s the new captain of %s"
409 +ERR_ARENA_TEAM_REMOVE_SSS "%s has been kicked out of %s by %s"
411 +ERR_ARENA_TEAM_DISBANDED_S "%s has disbanded %s"
413 ERR_ARENA_TEAM_TARGET_TOO_LOW_S "%s is not high enough level to join your team"
415 ERR_ARENA_TEAM_TOO_MANY_MEMBERS_S "%s is full"
417 ERR_ARENA_TEAM_LEVEL_TOO_LOW_I "You must be level %d to form an arena team"