[6837] Fixed typo in ArenaTeam::AddMember that sometimes prevented joining teams.
[getmangos.git] / src / game / ArenaTeam.cpp
blob88ed21ebf8de6c1e8dc7af013ff9dd81f669fcd6
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 "WorldPacket.h"
20 #include "ObjectMgr.h"
21 #include "ArenaTeam.h"
23 ArenaTeam::ArenaTeam()
25 Id = 0;
26 Type = 0;
27 Name = "";
28 CaptainGuid = 0;
29 BackgroundColor = 0; // background
30 EmblemStyle = 0; // icon
31 EmblemColor = 0; // icon color
32 BorderStyle = 0; // border
33 BorderColor = 0; // border color
34 stats.games = 0;
35 stats.played = 0;
36 stats.rank = 0;
37 stats.rating = 1500;
38 stats.wins = 0;
39 stats.wins2 = 0;
42 ArenaTeam::~ArenaTeam()
47 bool ArenaTeam::create(uint64 captainGuid, uint32 type, std::string ArenaTeamName)
49 if(!objmgr.GetPlayer(captainGuid)) // player not exist
50 return false;
51 if(objmgr.GetArenaTeamByName(ArenaTeamName)) // arena team with this name already exist
52 return false;
54 sLog.outDebug("GUILD: creating arena team %s to leader: %u", ArenaTeamName.c_str(), GUID_LOPART(CaptainGuid));
56 CaptainGuid = captainGuid;
57 Name = ArenaTeamName;
58 Type = type;
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);
77 return true;
80 bool ArenaTeam::AddMember(uint64 PlayerGuid)
82 std::string plName;
83 uint8 plClass;
85 // arena team is full (can't have more than type * 2 players!)
86 if(GetMembersSize() >= GetType() * 2)
87 return false;
89 Player *pl = objmgr.GetPlayer(PlayerGuid);
90 if(pl)
92 if(pl->GetArenaTeamId(GetSlot()))
94 sLog.outError("Arena::AddMember() : player already in this sized team");
95 return false;
98 plClass = (uint8)pl->getClass();
99 plName = pl->GetName();
101 else
103 // 0 1
104 QueryResult *result = CharacterDatabase.PQuery("SELECT name, class FROM characters WHERE guid='%u'", GUID_LOPART(PlayerGuid));
105 if(!result)
106 return false;
108 plName = (*result)[0].GetCppString();
109 plClass = (*result)[1].GetUInt8();
110 delete result;
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");
116 return false;
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));
136 if(pl)
138 pl->SetInArenaTeam(Id, GetSlot());
139 pl->SetArenaTeamIdInvited(0);
141 else
143 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + (GetSlot() * 6), Id, PlayerGuid);
146 // hide promote/remove buttons
147 if(CaptainGuid != PlayerGuid)
149 if(pl)
150 pl->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + 1 + (GetSlot() * 6), 1);
151 else
152 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + 1 + (GetSlot() * 6), 1, PlayerGuid);
154 return true;
157 bool ArenaTeam::LoadArenaTeamFromDB(uint32 ArenaTeamId)
159 LoadStatsFromDB(ArenaTeamId);
160 LoadMembersFromDB(ArenaTeamId);
162 // 0 1 2 3 4 5 6 7 8
163 QueryResult *result = CharacterDatabase.PQuery("SELECT arenateamid,name,captainguid,type,BackgroundColor,EmblemStyle,EmblemColor,BorderStyle,BorderColor FROM arena_team WHERE arenateamid = '%u'", ArenaTeamId);
165 if(!result)
166 return false;
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();
180 delete result;
182 return true;
185 void ArenaTeam::LoadStatsFromDB(uint32 ArenaTeamId)
187 // 0 1 2 3 4 5
188 QueryResult *result = CharacterDatabase.PQuery("SELECT rating,games,wins,played,wins2,rank FROM arena_team_stats WHERE arenateamid = '%u'", ArenaTeamId);
190 if(!result)
191 return;
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();
202 delete result;
205 void ArenaTeam::LoadMembersFromDB(uint32 ArenaTeamId)
207 Field *fields;
209 QueryResult *result = CharacterDatabase.PQuery("SELECT guid,played_week,wons_week,played_season,wons_season FROM arena_team_member WHERE arenateamid = '%u'", ArenaTeamId);
210 if(!result)
211 return;
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() );
225 delete result;
228 void ArenaTeam::LoadPlayerStats(ArenaTeamMember *member)
230 Field *fields;
232 QueryResult *result = CharacterDatabase.PQuery("SELECT name,class FROM characters WHERE guid = '%u'", GUID_LOPART(member->guid));
233 if(!result)
234 return;
235 fields = result->Fetch();
236 member->name = fields[0].GetCppString();
237 member->Class = fields[1].GetUInt8();
239 delete result;
242 void ArenaTeam::SetCaptain(uint64 guid)
244 // disable remove/promote buttons
245 Player *oldcaptain = objmgr.GetPlayer(GetCaptain());
246 if(oldcaptain)
247 oldcaptain->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + 1 + (GetSlot() * 6), 1);
248 else
249 Player::SetUInt32ValueInDB(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + 1 + (GetSlot() * 6), 1, GetCaptain());
251 // set new captain
252 CaptainGuid = guid;
254 // update database
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);
259 if(newcaptain)
260 newcaptain->SetUInt32Value(PLAYER_FIELD_ARENA_TEAM_INFO_1_1 + 1 + (GetSlot() * 6), 0);
261 else
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)
272 members.erase(itr);
273 break;
277 Player *player = objmgr.GetPlayer(guid);
278 if(player)
280 player->SetInArenaTeam(0, GetSlot());
281 player->GetSession()->SendArenaTeamCommandResult(ERR_ARENA_TEAM_QUIT_S, GetName(), "", 0);
283 else
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)
293 // event
294 WorldPacket data;
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;
302 uint32 i=0;
303 for(itr = members.begin(); itr != members.end(); itr++)
305 memberGuids[i] = itr->guid;
306 ++i;
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)
322 Player *pl = NULL;
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);
332 if(pl)
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?
346 else
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)
420 switch(stat_type)
422 case STAT_TYPE_RATING:
423 stats.rating = value;
424 CharacterDatabase.PExecute("UPDATE arena_team_stats SET rating = '%u' WHERE arenateamid = '%u'", value, GetId());
425 break;
426 case STAT_TYPE_GAMES:
427 stats.games = value;
428 CharacterDatabase.PExecute("UPDATE arena_team_stats SET games = '%u' WHERE arenateamid = '%u'", value, GetId());
429 break;
430 case STAT_TYPE_WINS:
431 stats.wins = value;
432 CharacterDatabase.PExecute("UPDATE arena_team_stats SET wins = '%u' WHERE arenateamid = '%u'", value, GetId());
433 break;
434 case STAT_TYPE_PLAYED:
435 stats.played = value;
436 CharacterDatabase.PExecute("UPDATE arena_team_stats SET played = '%u' WHERE arenateamid = '%u'", value, GetId());
437 break;
438 case STAT_TYPE_WINS2:
439 stats.wins2 = value;
440 CharacterDatabase.PExecute("UPDATE arena_team_stats SET wins2 = '%u' WHERE arenateamid = '%u'", value, GetId());
441 break;
442 case STAT_TYPE_RANK:
443 stats.rank = value;
444 CharacterDatabase.PExecute("UPDATE arena_team_stats SET rank = '%u' WHERE arenateamid = '%u'", value, GetId());
445 break;
446 default:
447 sLog.outDebug("unknown stat type in ArenaTeam::SetStats() %u", stat_type);
448 break;
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
461 return slot;
464 void ArenaTeam::BroadcastPacket(WorldPacket *packet)
466 for (MemberList::iterator itr = members.begin(); itr != members.end(); itr++)
468 Player *player = objmgr.GetPlayer(itr->guid);
469 if(player)
470 player->GetSession()->SendPacket(packet);
474 uint8 ArenaTeam::GetSlotByType( uint32 type )
476 switch(type)
478 case ARENA_TEAM_2v2: return 0;
479 case ARENA_TEAM_3v3: return 1;
480 case ARENA_TEAM_5v5: return 2;
481 default:
482 break;
484 return 0xFF;
487 bool ArenaTeam::HaveMember( uint64 guid ) const
489 for (MemberList::const_iterator itr = members.begin(); itr != members.end(); ++itr)
490 if(itr->guid==guid)
491 return true;
493 return false;
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
502 1418 - unk
503 1419 - unk
504 1420 - arena team id 3v3
505 1421 - 0=captain, 1=member
506 1422 - played this season
507 1423 - played this week
508 1424 - unk
509 1425 - unk
510 1426 - arena team id 5v5
511 1427 - 0=captain, 1=member
512 1428 - played this season
513 1429 - played this week
514 1430 - unk
515 1431 - unk