root/trunk/src/game/ArenaTeamHandler.cpp @ 143

Revision 102, 13.7 kB (checked in by yumileroy, 17 years ago)

[svn] Fixed copyright notices to comply with GPL.

Original author: w12x
Date: 2008-10-23 03:29:52-05:00

Line 
1/*
2 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
3 *
4 * Copyright (C) 2008 Trinity <http://www.trinitycore.org/>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include "WorldSession.h"
22#include "WorldPacket.h"
23#include "Log.h"
24#include "Database/DatabaseEnv.h"
25#include "Player.h"
26#include "ObjectMgr.h"
27#include "ArenaTeam.h"
28#include "World.h"
29#include "SocialMgr.h"
30#include "Language.h"
31
32void WorldSession::HandleInspectArenaStatsOpcode(WorldPacket & recv_data)
33{
34    sLog.outDebug("MSG_INSPECT_ARENA_TEAMS");
35    //recv_data.hexlike();
36
37    CHECK_PACKET_SIZE(recv_data, 8);
38
39    uint64 guid;
40    recv_data >> guid;
41    sLog.outDebug("Inspect Arena stats " I64FMTD, guid);
42
43    if(Player *plr = objmgr.GetPlayer(guid))
44    {
45        for (uint8 i = 0; i < MAX_ARENA_SLOT; i++)
46        {
47            if(uint32 a_id = plr->GetArenaTeamId(i))
48            {
49                if(ArenaTeam *at = objmgr.GetArenaTeamById(a_id))
50                    at->InspectStats(this, plr->GetGUID());
51            }
52        }
53    }
54}
55
56void WorldSession::HandleArenaTeamQueryOpcode(WorldPacket & recv_data)
57{
58    sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_QUERY" );
59    //recv_data.hexlike();
60
61    CHECK_PACKET_SIZE(recv_data, 4);
62
63    uint32 ArenaTeamId;
64    recv_data >> ArenaTeamId;
65
66    ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
67    if(!arenateam)                                          // arena team not found
68        return;
69
70    arenateam->Query(this);
71    arenateam->Stats(this);
72}
73
74void WorldSession::HandleArenaTeamRosterOpcode(WorldPacket & recv_data)
75{
76    sLog.outDebug( "WORLD: Received CMSG_ARENA_TEAM_ROSTER" );
77    //recv_data.hexlike();
78
79    CHECK_PACKET_SIZE(recv_data, 4);
80
81    uint32 ArenaTeamId;                                     // arena team id
82    recv_data >> ArenaTeamId;
83
84    ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
85    if(!arenateam)
86        return;
87
88    arenateam->Roster(this);
89}
90
91void WorldSession::HandleArenaTeamAddMemberOpcode(WorldPacket & recv_data)
92{
93    sLog.outDebug("CMSG_ARENA_TEAM_ADD_MEMBER");
94    //recv_data.hexlike();
95
96    CHECK_PACKET_SIZE(recv_data, 4+1);
97
98    uint32 ArenaTeamId;                                     // arena team id
99    std::string Invitedname;
100
101    Player * player = NULL;
102
103    recv_data >> ArenaTeamId >> Invitedname;
104
105    if(!Invitedname.empty())
106    {
107        if(!normalizePlayerName(Invitedname))
108            return;
109
110        player = ObjectAccessor::Instance().FindPlayerByName(Invitedname.c_str());
111    }
112
113    if(!player)
114    {
115        SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", Invitedname, ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S);
116        return;
117    }
118
119    if(player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
120    {
121        //SendArenaTeamCommandResult(ARENA_TEAM_INVITE_SS,"",Invitedname,ARENA_TEAM_PLAYER_NOT_FOUND_S);
122                                                            // can't find related opcode
123        SendNotification(LANG_HIS_ARENA_LEVEL_REQ_ERROR, player->GetName());
124        return;
125    }
126
127    ArenaTeam *arenateam = objmgr.GetArenaTeamById(ArenaTeamId);
128    if(!arenateam)
129    {
130        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM);
131        return;
132    }
133
134    // OK result but not send invite
135    if(player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
136        return;
137
138    if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && player->GetTeam() != GetPlayer()->GetTeam())
139    {
140        SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_NOT_ALLIED);
141        return;
142    }
143
144    if(player->GetArenaTeamId(arenateam->GetSlot()))
145    {
146        SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, player->GetName(), "", ERR_ALREADY_IN_ARENA_TEAM_S);
147        return;
148    }
149
150    if(player->GetArenaTeamIdInvited())
151    {
152        SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, player->GetName(), "", ERR_ALREADY_INVITED_TO_ARENA_TEAM_S);
153        return;
154    }
155
156    if(arenateam->GetMembersSize() >= arenateam->GetType() * 2)
157    {
158        // should send an "arena team is full" or the likes message, I just don't know the proper values so... ERR_INTERNAL
159//        SendArenaTeamCommandResult(ERR_ARENA_TEAM_INVITE_SS, "", "", ERR_ARENA_TEAM_INTERNAL);
160        SendNotification(LANG_YOUR_ARENA_TEAM_FULL, player->GetName());
161        return;
162    }
163
164    sLog.outDebug("Player %s Invited %s to Join his ArenaTeam", GetPlayer()->GetName(), Invitedname.c_str());
165
166    player->SetArenaTeamIdInvited(arenateam->GetId());
167
168    WorldPacket data(SMSG_ARENA_TEAM_INVITE, (8+10));
169    data << GetPlayer()->GetName();
170    data << arenateam->GetName();
171    player->GetSession()->SendPacket(&data);
172
173    sLog.outDebug("WORLD: Sent SMSG_ARENA_TEAM_INVITE");
174}
175
176void WorldSession::HandleArenaTeamInviteAcceptOpcode(WorldPacket & /*recv_data*/)
177{
178    sLog.outDebug("CMSG_ARENA_TEAM_INVITE_ACCEPT");         // empty opcode
179
180    ArenaTeam *at = objmgr.GetArenaTeamById(_player->GetArenaTeamIdInvited());
181    if(!at)
182    {
183        // arena team not exist
184        return;
185    }
186
187    if(_player->GetArenaTeamId(at->GetSlot()))
188    {
189        // already in arena team that size
190        return;
191    }
192
193    // not let enemies sign petition
194    if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && _player->GetTeam() != objmgr.GetPlayerTeamByGUID(at->GetCaptain()))
195        return;
196
197    if(!at->AddMember(_player->GetGUID()))
198        return;
199
200    // event
201    WorldPacket data;
202    BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_JOIN_SS, 2, _player->GetName(), at->GetName(), "");
203    at->BroadcastPacket(&data);
204}
205
206void WorldSession::HandleArenaTeamInviteDeclineOpcode(WorldPacket & /*recv_data*/)
207{
208    sLog.outDebug("CMSG_ARENA_TEAM_INVITE_DECLINE");        // empty opcode
209
210    _player->SetArenaTeamIdInvited(0);                      // no more invited
211}
212
213void WorldSession::HandleArenaTeamLeaveOpcode(WorldPacket & recv_data)
214{
215    sLog.outDebug("CMSG_ARENA_TEAM_LEAVE");
216    //recv_data.hexlike();
217
218    CHECK_PACKET_SIZE(recv_data, 4);
219
220    uint32 ArenaTeamId;                                     // arena team id
221    recv_data >> ArenaTeamId;
222
223    ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
224    if(!at)
225    {
226        // send command result
227        return;
228    }
229    if(_player->GetGUID() == at->GetCaptain() && at->GetMembersSize() > 1)
230    {
231        // check for correctness
232        SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
233        return;
234    }
235    // arena team has only one member (=captain)
236    if(_player->GetGUID() == at->GetCaptain())
237    {
238        at->Disband(this);
239        delete at;
240        return;
241    }
242
243    at->DelMember(_player->GetGUID());
244
245    // event
246    WorldPacket data;
247    BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEAVE_SS, 2, _player->GetName(), at->GetName(), "");
248    at->BroadcastPacket(&data);
249
250    //SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, at->GetName(), "", 0);
251}
252
253void WorldSession::HandleArenaTeamDisbandOpcode(WorldPacket & recv_data)
254{
255    sLog.outDebug("CMSG_ARENA_TEAM_DISBAND");
256    //recv_data.hexlike();
257
258    CHECK_PACKET_SIZE(recv_data, 4);
259
260    uint32 ArenaTeamId;                                     // arena team id
261    recv_data >> ArenaTeamId;
262
263    ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
264    if(!at)
265    {
266        // arena team not found
267        return;
268    }
269
270    if(at->GetCaptain() != _player->GetGUID())
271    {
272        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
273        return;
274    }
275
276    at->Disband(this);
277    delete at;
278}
279
280void WorldSession::HandleArenaTeamRemoveFromTeamOpcode(WorldPacket & recv_data)
281{
282    sLog.outDebug("CMSG_ARENA_TEAM_REMOVE_FROM_TEAM");
283    //recv_data.hexlike();
284
285    CHECK_PACKET_SIZE(recv_data, 4+1);
286
287    uint32 ArenaTeamId;
288    std::string name;
289
290    recv_data >> ArenaTeamId;
291    recv_data >> name;
292
293    ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
294    if(!at)
295    {
296        // arena team not found
297        return;
298    }
299
300    uint64 guid = objmgr.GetPlayerGUIDByName(name);
301    if(!guid)
302    {
303        // player guid not found
304        return;
305    }
306
307    if(at->GetCaptain() == guid)
308    {
309        // unsure
310        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
311        return;
312    }
313
314    if(at->GetCaptain() != _player->GetGUID())
315    {
316        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
317        return;
318    }
319
320    if(at->GetCaptain() == guid)
321    {
322        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_LEADER_LEAVE_S);
323        return;
324    }
325
326    at->DelMember(guid);
327
328    // event
329    WorldPacket data;
330    BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_REMOVE_SSS, 3, name, at->GetName(), _player->GetName());
331    at->BroadcastPacket(&data);
332}
333
334void WorldSession::HandleArenaTeamPromoteToCaptainOpcode(WorldPacket & recv_data)
335{
336    sLog.outDebug("CMSG_ARENA_TEAM_PROMOTE_TO_CAPTAIN");
337    //recv_data.hexlike();
338
339    CHECK_PACKET_SIZE(recv_data, 4+1);
340
341    uint32 ArenaTeamId;
342    std::string name;
343
344    recv_data >> ArenaTeamId;
345    recv_data >> name;
346
347    ArenaTeam *at = objmgr.GetArenaTeamById(ArenaTeamId);
348    if(!at)
349    {
350        // arena team not found
351        return;
352    }
353
354    uint64 guid = objmgr.GetPlayerGUIDByName(name);
355    if(!guid)
356    {
357        // player guid not found
358        return;
359    }
360
361    if(at->GetCaptain() == guid)
362    {
363        // target player already captain
364        return;
365    }
366
367    if(at->GetCaptain() != _player->GetGUID())
368    {
369        SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, "", "", ERR_ARENA_TEAM_PERMISSIONS);
370        return;
371    }
372
373    at->SetCaptain(guid);
374
375    // event
376    WorldPacket data;
377    BuildArenaTeamEventPacket(&data, ERR_ARENA_TEAM_LEADER_CHANGED_SSS, 3, _player->GetName(), name, at->GetName());
378    at->BroadcastPacket(&data);
379}
380
381void WorldSession::SendArenaTeamCommandResult(uint32 unk1, std::string str1, std::string str2, uint32 unk3)
382{
383    WorldPacket data(SMSG_ARENA_TEAM_COMMAND_RESULT, 4+str1.length()+1+str2.length()+1+4);
384    data << unk1;
385    data << str1;
386    data << str2;
387    data << unk3;
388    SendPacket(&data);
389}
390
391void WorldSession::BuildArenaTeamEventPacket(WorldPacket *data, uint8 eventid, uint8 str_count, std::string str1, std::string str2, std::string str3)
392{
393    data->Initialize(SMSG_ARENA_TEAM_EVENT, 1+1+1);
394    *data << eventid;
395    *data << str_count;
396    switch(str_count)
397    {
398        case 1:
399            *data << str1;
400            break;
401        case 2:
402            *data << str1;
403            *data << str2;
404            break;
405        case 3:
406            *data << str1;
407            *data << str2;
408            *data << str3;
409            break;
410        default:
411            sLog.outError("Unhandled str_count %u in SendArenaTeamEvent()", str_count);
412            return;
413    }
414}
415
416void WorldSession::SendNotInArenaTeamPacket(uint8 type)
417{
418    WorldPacket data(SMSG_ARENA_ERROR, 4+1);                // 886 - You are not in a %uv%u arena team
419    uint32 unk = 0;
420    data << uint32(unk);                                    // unk(0)
421    if(!unk)
422        data << uint8(type);                                // team type (2=2v2,3=3v3,5=5v5), can be used for custom types...
423    SendPacket(&data);
424}
425
426/*
427+ERR_ARENA_NO_TEAM_II "You are not in a %dv%d arena team"
428
429+ERR_ARENA_TEAM_CREATE_S "%s created.  To disband, use /teamdisband [2v2, 3v3, 5v5]."
430+ERR_ARENA_TEAM_INVITE_SS "You have invited %s to join %s"
431+ERR_ARENA_TEAM_QUIT_S "You are no longer a member of %s"
432ERR_ARENA_TEAM_FOUNDER_S "Congratulations, you are a founding member of %s!  To leave, use /teamquit [2v2, 3v3, 5v5]."
433
434+ERR_ARENA_TEAM_INTERNAL "Internal arena team error"
435+ERR_ALREADY_IN_ARENA_TEAM "You are already in an arena team of that size"
436+ERR_ALREADY_IN_ARENA_TEAM_S "%s is already in an arena team of that size"
437+ERR_INVITED_TO_ARENA_TEAM "You have already been invited into an arena team"
438+ERR_ALREADY_INVITED_TO_ARENA_TEAM_S "%s has already been invited to an arena team"
439+ERR_ARENA_TEAM_NAME_INVALID "That name contains invalid characters, please enter a new name"
440+ERR_ARENA_TEAM_NAME_EXISTS_S "There is already an arena team named \"%s\""
441+ERR_ARENA_TEAM_LEADER_LEAVE_S "You must promote a new team captain using /teamcaptain before leaving the team"
442+ERR_ARENA_TEAM_PERMISSIONS "You don't have permission to do that"
443+ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM "You are not in an arena team of that size"
444+ERR_ARENA_TEAM_PLAYER_NOT_IN_TEAM_SS "%s is not in %s"
445+ERR_ARENA_TEAM_PLAYER_NOT_FOUND_S "\"%s\" not found"
446+ERR_ARENA_TEAM_NOT_ALLIED "You cannot invite players from the opposing alliance"
447
448+ERR_ARENA_TEAM_JOIN_SS "%s has joined %s"
449+ERR_ARENA_TEAM_YOU_JOIN_S "You have joined %s.  To leave, use /teamquit [2v2, 3v3, 5v5]."
450
451+ERR_ARENA_TEAM_LEAVE_SS "%s has left %s"
452
453+ERR_ARENA_TEAM_LEADER_IS_SS "%s is the captain of %s"
454+ERR_ARENA_TEAM_LEADER_CHANGED_SSS "%s has made %s the new captain of %s"
455
456+ERR_ARENA_TEAM_REMOVE_SSS "%s has been kicked out of %s by %s"
457
458+ERR_ARENA_TEAM_DISBANDED_S "%s has disbanded %s"
459
460ERR_ARENA_TEAM_TARGET_TOO_LOW_S "%s is not high enough level to join your team"
461
462ERR_ARENA_TEAM_TOO_MANY_MEMBERS_S "%s is full"
463
464ERR_ARENA_TEAM_LEVEL_TOO_LOW_I "You must be level %d to form an arena team"
465*/
Note: See TracBrowser for help on using the browser.