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 "WorldPacket.h"
20 #include "ObjectMgr.h"
21 #include "ArenaTeam.h"
23 ArenaTeam::ArenaTeam()
29 BackgroundColor
= 0; // background
30 EmblemStyle
= 0; // icon
31 EmblemColor
= 0; // icon color
32 BorderStyle
= 0; // border
33 BorderColor
= 0; // border color
42 ArenaTeam::~ArenaTeam()
47 bool ArenaTeam::create(uint64 captainGuid
, uint32 type
, std::string ArenaTeamName
)
49 if(!objmgr
.GetPlayer(captainGuid
)) // player not exist
51 if(objmgr
.GetArenaTeamByName(ArenaTeamName
)) // arena team with this name already exist
54 sLog
.outDebug("GUILD: creating arena team %s to leader: %u", ArenaTeamName
.c_str(), GUID_LOPART(CaptainGuid
));
56 CaptainGuid
= captainGuid
;
60 Id
= objmgr
.GenerateArenaTeamId();
62 // ArenaTeamName already assigned to ArenaTeam::name, use it to encode string for DB
63 CharacterDatabase
.escape_string(ArenaTeamName
);
65 CharacterDatabase
.BeginTransaction();
66 // CharacterDatabase.PExecute("DELETE FROM arena_team WHERE arenateamid='%u'", Id); - MAX(arenateam)+1 not exist
67 CharacterDatabase
.PExecute("DELETE FROM arena_team_member WHERE arenateamid='%u'", Id
);
68 CharacterDatabase
.PExecute("INSERT INTO arena_team (arenateamid,name,captainguid,type,BackgroundColor,EmblemStyle,EmblemColor,BorderStyle,BorderColor) "
69 "VALUES('%u','%s','%u','%u','%u','%u','%u','%u','%u')",
70 Id
, ArenaTeamName
.c_str(), GUID_LOPART(CaptainGuid
), Type
, BackgroundColor
,EmblemStyle
,EmblemColor
,BorderStyle
,BorderColor
);
71 CharacterDatabase
.PExecute("INSERT INTO arena_team_stats (arenateamid, rating, games, wins, played, wins2, rank) VALUES "
72 "('%u', '%u', '%u', '%u', '%u', '%u', '%u')", Id
,stats
.rating
,stats
.games
,stats
.wins
,stats
.played
,stats
.wins2
,stats
.rank
);
74 CharacterDatabase
.CommitTransaction();
76 AddMember(CaptainGuid
);
80 bool ArenaTeam::AddMember(uint64 PlayerGuid
)
85 // arena team is full (can't have more than type * 2 players!)
86 if(GetMembersSize() >= GetType() * 2)
89 Player
*pl
= objmgr
.GetPlayer(PlayerGuid
);
92 if(pl
->GetArenaTeamId(GetSlot()))
94 sLog
.outError("Arena::AddMember() : player already in this sized team");
98 plClass
= (uint8
)pl
->getClass();
99 plName
= pl
->GetName();
104 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT name, class FROM characters WHERE guid='%u'", GUID_LOPART(PlayerGuid
));
108 plName
= (*result
)[0].GetCppString();
109 plClass
= (*result
)[1].GetUInt8();
112 // check if player already in arenateam of that size
113 if(Player::GetArenaTeamIdFromDB(PlayerGuid
, GetType()) != 0)
115 sLog
.outError("Arena::AddMember() : player already in this sized team");
120 // remove all player signs from another petitions
121 // this will be prevent attempt joining player to many arenateams and corrupt arena team data integrity
122 Player::RemovePetitionsAndSigns(PlayerGuid
, GetType());
124 ArenaTeamMember newmember
;
125 newmember
.name
= plName
;
126 newmember
.guid
= PlayerGuid
;
127 newmember
.Class
= plClass
;
128 newmember
.played_season
= 0;
129 newmember
.played_week
= 0;
130 newmember
.wons_season
= 0;
131 newmember
.wons_week
= 0;
132 members
.push_back(newmember
);
134 CharacterDatabase
.PExecute("INSERT INTO arena_team_member (arenateamid,guid) VALUES ('%u', '%u')", Id
, GUID_LOPART(newmember
.guid
));
138 pl
->SetInArenaTeam(Id
, GetSlot());
139 pl
->SetArenaTeamIdInvited(0);
143 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ (GetSlot() * 6), Id
, PlayerGuid
);
146 // hide promote/remove buttons
147 if(CaptainGuid
!= PlayerGuid
)
150 pl
->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 1);
152 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 1, PlayerGuid
);
157 bool ArenaTeam::LoadArenaTeamFromDB(uint32 ArenaTeamId
)
159 LoadStatsFromDB(ArenaTeamId
);
160 LoadMembersFromDB(ArenaTeamId
);
163 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT arenateamid,name,captainguid,type,BackgroundColor,EmblemStyle,EmblemColor,BorderStyle,BorderColor FROM arena_team WHERE arenateamid = '%u'", ArenaTeamId
);
168 Field
*fields
= result
->Fetch();
170 Id
= fields
[0].GetUInt32();
171 Name
= fields
[1].GetCppString();
172 CaptainGuid
= MAKE_NEW_GUID(fields
[2].GetUInt32(), 0, HIGHGUID_PLAYER
);
173 Type
= fields
[3].GetUInt32();
174 BackgroundColor
= fields
[4].GetUInt32();
175 EmblemStyle
= fields
[5].GetUInt32();
176 EmblemColor
= fields
[6].GetUInt32();
177 BorderStyle
= fields
[7].GetUInt32();
178 BorderColor
= fields
[8].GetUInt32();
185 void ArenaTeam::LoadStatsFromDB(uint32 ArenaTeamId
)
188 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT rating,games,wins,played,wins2,rank FROM arena_team_stats WHERE arenateamid = '%u'", ArenaTeamId
);
193 Field
*fields
= result
->Fetch();
195 stats
.rating
= fields
[0].GetUInt32();
196 stats
.games
= fields
[1].GetUInt32();
197 stats
.wins
= fields
[2].GetUInt32();
198 stats
.played
= fields
[3].GetUInt32();
199 stats
.wins2
= fields
[4].GetUInt32();
200 stats
.rank
= fields
[5].GetUInt32();
205 void ArenaTeam::LoadMembersFromDB(uint32 ArenaTeamId
)
209 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT guid,played_week,wons_week,played_season,wons_season FROM arena_team_member WHERE arenateamid = '%u'", ArenaTeamId
);
215 fields
= result
->Fetch();
216 ArenaTeamMember newmember
;
217 newmember
.guid
= MAKE_NEW_GUID(fields
[0].GetUInt32(), 0, HIGHGUID_PLAYER
);
218 LoadPlayerStats(&newmember
);
219 newmember
.played_week
= fields
[1].GetUInt32();
220 newmember
.wons_week
= fields
[2].GetUInt32();
221 newmember
.played_season
= fields
[3].GetUInt32();
222 newmember
.wons_season
= fields
[4].GetUInt32();
223 members
.push_back(newmember
);
224 }while( result
->NextRow() );
228 void ArenaTeam::LoadPlayerStats(ArenaTeamMember
*member
)
232 QueryResult
*result
= CharacterDatabase
.PQuery("SELECT name,class FROM characters WHERE guid = '%u'", GUID_LOPART(member
->guid
));
235 fields
= result
->Fetch();
236 member
->name
= fields
[0].GetCppString();
237 member
->Class
= fields
[1].GetUInt8();
242 void ArenaTeam::SetCaptain(uint64 guid
)
244 // disable remove/promote buttons
245 Player
*oldcaptain
= objmgr
.GetPlayer(GetCaptain());
247 oldcaptain
->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 1);
249 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 1, GetCaptain());
255 CharacterDatabase
.PExecute("UPDATE arena_team SET captainguid = '%u' WHERE arenateamid = '%u'", GUID_LOPART(guid
), Id
);
257 // enable remove/promote buttons
258 Player
*newcaptain
= objmgr
.GetPlayer(guid
);
260 newcaptain
->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 0);
262 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ 1 + (GetSlot() * 6), 0, guid
);
265 void ArenaTeam::DelMember(uint64 guid
)
267 MemberList::iterator itr
;
268 for (itr
= members
.begin(); itr
!= members
.end(); itr
++)
270 if (itr
->guid
== guid
)
277 Player
*player
= objmgr
.GetPlayer(guid
);
280 player
->SetInArenaTeam(0, GetSlot());
281 player
->GetSession()->SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S
, GetName(), "", 0);
285 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1
+ (GetSlot() * 6), 0, guid
);
288 CharacterDatabase
.PExecute("DELETE FROM arena_team_member WHERE guid = '%u'", GUID_LOPART(guid
));
291 void ArenaTeam::Disband(WorldSession
*session
)
295 session
->BuildArenaTeamEventPacket(&data
, ERR_ARENA_TEAM_DISBANDED_S
, 2, session
->GetPlayerName(), GetName(), "");
296 BroadcastPacket(&data
);
298 uint32 count
= members
.size();
299 uint64
*memberGuids
= new uint64
[count
];
301 MemberList::iterator itr
;
303 for(itr
= members
.begin(); itr
!= members
.end(); itr
++)
305 memberGuids
[i
] = itr
->guid
;
309 for(uint32 j
= 0; j
< count
; j
++)
310 DelMember(memberGuids
[j
]);
311 delete[] memberGuids
;
313 CharacterDatabase
.BeginTransaction();
314 CharacterDatabase
.PExecute("DELETE FROM arena_team WHERE arenateamid = '%u'", Id
);
315 CharacterDatabase
.PExecute("DELETE FROM arena_team_stats WHERE arenateamid = '%u'", Id
);
316 CharacterDatabase
.CommitTransaction();
317 objmgr
.RemoveArenaTeam(this);
320 void ArenaTeam::Roster(WorldSession
*session
)
324 WorldPacket
data(SMSG_ARENA_TEAM_ROSTER
, 100);
325 data
<< uint32(GetId()); // arena team id
326 data
<< uint32(GetMembersSize()); // members count
327 data
<< uint32(GetType()); // arena team type?
329 for (MemberList::iterator itr
= members
.begin(); itr
!= members
.end(); ++itr
)
331 pl
= objmgr
.GetPlayer(itr
->guid
);
334 data
<< uint64(pl
->GetGUID()); // guid
335 data
<< uint8(1); // online flag
336 data
<< pl
->GetName(); // member name
337 data
<< uint32(itr
->guid
== GetCaptain() ? 0 : 1);// unknown
338 data
<< uint8(pl
->getLevel()); // unknown, probably level
339 data
<< uint8(pl
->getClass()); // class
340 data
<< uint32(itr
->played_week
); // played this week
341 data
<< uint32(itr
->wons_week
); // wins this week
342 data
<< uint32(itr
->played_season
); // played this season
343 data
<< uint32(itr
->wons_season
); // wins this season
344 data
<< uint32(0); // personal rating?
348 data
<< uint64(itr
->guid
); // guid
349 data
<< uint8(0); // online flag
350 data
<< itr
->name
; // member name
351 data
<< uint32(itr
->guid
== GetCaptain() ? 0 : 1);// unknown
352 data
<< uint8(0); // unknown, level?
353 data
<< uint8(itr
->Class
); // class
354 data
<< uint32(itr
->played_week
); // played this week
355 data
<< uint32(itr
->wons_week
); // wins this week
356 data
<< uint32(itr
->played_season
); // played this season
357 data
<< uint32(itr
->wons_season
); // wins this season
358 data
<< uint32(0); // personal rating?
361 session
->SendPacket(&data
);
362 sLog
.outDebug("WORLD: Sent SMSG_ARENA_TEAM_ROSTER");
365 void ArenaTeam::Query(WorldSession
*session
)
367 WorldPacket
data(SMSG_ARENA_TEAM_QUERY_RESPONSE
, 4*7+GetName().size()+1);
368 data
<< uint32(GetId()); // team id
369 data
<< GetName(); // team name
370 data
<< uint32(GetType()); // arena team type (2=2x2, 3=3x3 or 5=5x5)
371 data
<< uint32(BackgroundColor
); // background color
372 data
<< uint32(EmblemStyle
); // emblem style
373 data
<< uint32(EmblemColor
); // emblem color
374 data
<< uint32(BorderStyle
); // border style
375 data
<< uint32(BorderColor
); // border color
376 session
->SendPacket(&data
);
377 sLog
.outDebug("WORLD: Sent SMSG_ARENA_TEAM_QUERY_RESPONSE");
380 void ArenaTeam::Stats(WorldSession
*session
)
382 WorldPacket
data(SMSG_ARENA_TEAM_STATS
, 4*7);
383 data
<< uint32(GetId()); // arena team id
384 data
<< uint32(stats
.rating
); // rating
385 data
<< uint32(stats
.games
); // games
386 data
<< uint32(stats
.wins
); // wins
387 data
<< uint32(stats
.played
); // played
388 data
<< uint32(stats
.wins2
); // wins(again o_O)
389 data
<< uint32(stats
.rank
); // rank
390 session
->SendPacket(&data
);
393 void ArenaTeam::InspectStats(WorldSession
*session
, uint64 guid
)
395 WorldPacket
data(MSG_INSPECT_ARENA_TEAMS
, 8+1+4*6);
396 data
<< uint64(guid
); // player guid
397 data
<< uint8(GetSlot()); // slot (0...2)
398 data
<< uint32(GetId()); // arena team id
399 data
<< uint32(stats
.rating
); // rating
400 data
<< uint32(stats
.games
); // games
401 data
<< uint32(stats
.wins
); // wins
402 data
<< uint32(stats
.played
); // played (count of all games, that played...)
403 data
<< uint32(0); // 2.3.3 personal rating?
404 session
->SendPacket(&data
);
407 void ArenaTeam::SetEmblem(uint32 backgroundColor
, uint32 emblemStyle
, uint32 emblemColor
, uint32 borderStyle
, uint32 borderColor
)
409 BackgroundColor
= backgroundColor
;
410 EmblemStyle
= emblemStyle
;
411 EmblemColor
= emblemColor
;
412 BorderStyle
= borderStyle
;
413 BorderColor
= borderColor
;
415 CharacterDatabase
.PExecute("UPDATE arena_team SET BackgroundColor='%u', EmblemStyle='%u', EmblemColor='%u', BorderStyle='%u', BorderColor='%u' WHERE arenateamid='%u'", BackgroundColor
, EmblemStyle
, EmblemColor
, BorderStyle
, BorderColor
, Id
);
418 void ArenaTeam::SetStats(uint32 stat_type
, uint32 value
)
422 case STAT_TYPE_RATING
:
423 stats
.rating
= value
;
424 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET rating = '%u' WHERE arenateamid = '%u'", value
, GetId());
426 case STAT_TYPE_GAMES
:
428 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET games = '%u' WHERE arenateamid = '%u'", value
, GetId());
432 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET wins = '%u' WHERE arenateamid = '%u'", value
, GetId());
434 case STAT_TYPE_PLAYED
:
435 stats
.played
= value
;
436 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET played = '%u' WHERE arenateamid = '%u'", value
, GetId());
438 case STAT_TYPE_WINS2
:
440 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET wins2 = '%u' WHERE arenateamid = '%u'", value
, GetId());
444 CharacterDatabase
.PExecute("UPDATE arena_team_stats SET rank = '%u' WHERE arenateamid = '%u'", value
, GetId());
447 sLog
.outDebug("unknown stat type in ArenaTeam::SetStats() %u", stat_type
);
452 uint8
ArenaTeam::GetSlot() const
454 uint8 slot
= GetSlotByType(GetType());
455 if(slot
>= MAX_ARENA_SLOT
)
457 sLog
.outError("Unknown arena team type %u for arena team %u", uint32(GetType()), GetId());
458 return 0; // better return existed slot to prevent untelated data curruption
464 void ArenaTeam::BroadcastPacket(WorldPacket
*packet
)
466 for (MemberList::iterator itr
= members
.begin(); itr
!= members
.end(); itr
++)
468 Player
*player
= objmgr
.GetPlayer(itr
->guid
);
470 player
->GetSession()->SendPacket(packet
);
474 uint8
ArenaTeam::GetSlotByType( uint32 type
)
478 case ARENA_TEAM_2v2
: return 0;
479 case ARENA_TEAM_3v3
: return 1;
480 case ARENA_TEAM_5v5
: return 2;
487 bool ArenaTeam::HaveMember( uint64 guid
) const
489 for (MemberList::const_iterator itr
= members
.begin(); itr
!= members
.end(); ++itr
)
497 arenateam fields (id from 2.3.3 client):
498 1414 - arena team id 2v2
499 1415 - 0=captain, 1=member
500 1416 - played this season
501 1417 - played this week
504 1420 - arena team id 3v3
505 1421 - 0=captain, 1=member
506 1422 - played this season
507 1423 - played this week
510 1426 - arena team id 5v5
511 1427 - 0=captain, 1=member
512 1428 - played this season
513 1429 - played this week