More fixes for wrong format arg/value pairs.
[auctionmangos.git] / src / game / PetitionsHandler.cpp
blob1064bd23fe82b3a3a29b577130ab9271e2fa2a9f
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 "Common.h"
20 #include "Language.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "World.h"
24 #include "ObjectMgr.h"
25 #include "Log.h"
26 #include "Opcodes.h"
27 #include "Guild.h"
28 #include "ArenaTeam.h"
29 #include "MapManager.h"
30 #include "GossipDef.h"
31 #include "SocialMgr.h"
33 /*enum PetitionType // dbc data
35 PETITION_TYPE_GUILD = 1,
36 PETITION_TYPE_ARENA_TEAM = 3
37 };*/
39 // Charters ID in item_template
40 #define GUILD_CHARTER 5863
41 #define GUILD_CHARTER_COST 1000 // 10 S
42 #define ARENA_TEAM_CHARTER_2v2 23560
43 #define ARENA_TEAM_CHARTER_2v2_COST 800000 // 80 G
44 #define ARENA_TEAM_CHARTER_3v3 23561
45 #define ARENA_TEAM_CHARTER_3v3_COST 1200000 // 120 G
46 #define ARENA_TEAM_CHARTER_5v5 23562
47 #define ARENA_TEAM_CHARTER_5v5_COST 2000000 // 200 G
49 void WorldSession::HandlePetitionBuyOpcode(WorldPacket & recv_data)
51 CHECK_PACKET_SIZE(recv_data, 8+8+4+1+5*8+2+1+4+4);
53 sLog.outDebug("Received opcode CMSG_PETITION_BUY");
54 //recv_data.hexlike();
56 uint64 guidNPC;
57 uint64 unk1, unk3, unk4, unk5, unk6, unk7;
58 uint32 unk2;
59 std::string name;
60 uint16 unk8;
61 uint8 unk9;
62 uint32 unk10; // selected index
63 uint32 unk11;
64 recv_data >> guidNPC; // NPC GUID
65 recv_data >> unk1; // 0
66 recv_data >> unk2; // 0
67 recv_data >> name; // name
69 // recheck
70 CHECK_PACKET_SIZE(recv_data, 8+8+4+(name.size()+1)+5*8+2+1+4+4);
72 recv_data >> unk3; // 0
73 recv_data >> unk4; // 0
74 recv_data >> unk5; // 0
75 recv_data >> unk6; // 0
76 recv_data >> unk7; // 0
77 recv_data >> unk8; // 0
78 recv_data >> unk9; // 0
79 recv_data >> unk10; // index
80 recv_data >> unk11; // 0
81 sLog.outDebug("Petitioner with GUID %u tried sell petition: name %s", GUID_LOPART(guidNPC), name.c_str());
83 // prevent cheating
84 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guidNPC,UNIT_NPC_FLAG_PETITIONER);
85 if (!pCreature)
87 sLog.outDebug("WORLD: HandlePetitionBuyOpcode - Unit (GUID: %u) not found or you can't interact with him.", GUID_LOPART(guidNPC));
88 return;
91 // remove fake death
92 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
93 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
95 uint32 charterid = 0;
96 uint32 cost = 0;
97 uint32 type = 0;
98 if(pCreature->isTabardDesigner())
100 // if tabard designer, then trying to buy a guild charter.
101 // do not let if already in guild.
102 if(_player->GetGuildId())
103 return;
105 charterid = GUILD_CHARTER;
106 cost = GUILD_CHARTER_COST;
107 type = 9;
109 else
111 // TODO: find correct opcode
112 if(_player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
114 SendNotification(LANG_ARENA_ONE_TOOLOW, 70);
115 return;
118 for(uint8 i = 0; i < MAX_ARENA_SLOT; i++)
120 if(_player->GetArenaTeamId(i) && (i == (unk10-1)))
122 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ALREADY_IN_ARENA_TEAM);
123 return;
126 switch(unk10)
128 case 1:
129 charterid = ARENA_TEAM_CHARTER_2v2;
130 cost = ARENA_TEAM_CHARTER_2v2_COST;
131 type = 2; // 2v2
132 break;
133 case 2:
134 charterid = ARENA_TEAM_CHARTER_3v3;
135 cost = ARENA_TEAM_CHARTER_3v3_COST;
136 type = 3; // 3v3
137 break;
138 case 3:
139 charterid = ARENA_TEAM_CHARTER_5v5;
140 cost = ARENA_TEAM_CHARTER_5v5_COST;
141 type = 5; // 5v5
142 break;
143 default:
144 sLog.outDebug("unknown selection at buy petition: %u", unk10);
145 return;
149 if(type == 9)
151 if(objmgr.GetGuildByName(name))
153 SendGuildCommandResult(GUILD_CREATE_S, name, GUILD_NAME_EXISTS);
154 return;
156 if(objmgr.IsReservedName(name))
158 SendGuildCommandResult(GUILD_CREATE_S, name, GUILD_NAME_INVALID);
159 return;
161 if(!ObjectMgr::IsValidCharterName(name))
163 SendGuildCommandResult(GUILD_CREATE_S, name, GUILD_NAME_INVALID);
164 return;
167 else
169 if(objmgr.GetArenaTeamByName(name))
171 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_EXISTS_S);
172 return;
174 if(objmgr.IsReservedName(name))
176 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_INVALID);
177 return;
179 if(!ObjectMgr::IsValidCharterName(name))
181 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_INVALID);
182 return;
186 ItemPrototype const *pProto = objmgr.GetItemPrototype(charterid);
187 if(!pProto)
189 _player->SendBuyError(BUY_ERR_CANT_FIND_ITEM, NULL, charterid, 0);
190 return;
193 if(_player->GetMoney() < cost)
194 { //player hasn't got enough money
195 _player->SendBuyError(BUY_ERR_NOT_ENOUGHT_MONEY, pCreature, charterid, 0);
196 return;
199 ItemPosCountVec dest;
200 uint8 msg = _player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, charterid, pProto->BuyCount );
201 if(msg != EQUIP_ERR_OK)
203 _player->SendBuyError(msg, pCreature, charterid, 0);
204 return;
207 _player->ModifyMoney(-(int32)cost);
208 Item *charter = _player->StoreNewItem(dest, charterid, true);
209 if(!charter)
210 return;
212 charter->SetUInt32Value(ITEM_FIELD_ENCHANTMENT, charter->GetGUIDLow());
213 // ITEM_FIELD_ENCHANTMENT is guild/arenateam id
214 // ITEM_FIELD_ENCHANTMENT+1 is current signatures count (showed on item)
215 charter->SetState(ITEM_CHANGED, _player);
216 _player->SendNewItem(charter, 1, true, false);
218 // a petition is invalid, if both the owner and the type matches
219 QueryResult *result = CharacterDatabase.PQuery("SELECT petitionguid FROM petition WHERE ownerguid = '%u' AND type = '%u'", _player->GetGUIDLow(), type);
221 std::ostringstream ssInvalidPetitionGUIDs;
223 if (result)
228 Field *fields = result->Fetch();
229 ssInvalidPetitionGUIDs << "'" << fields[0].GetUInt32() << "' , ";
230 } while (result->NextRow());
232 delete result;
235 // delete petitions with the same guid as this one
236 ssInvalidPetitionGUIDs << "'" << charter->GetGUIDLow() << "'";
238 sLog.outDebug("Invalid petition GUIDs: %s", ssInvalidPetitionGUIDs.str().c_str());
239 CharacterDatabase.escape_string(name);
240 CharacterDatabase.BeginTransaction();
241 CharacterDatabase.PExecute("DELETE FROM petition WHERE petitionguid IN ( %s )", ssInvalidPetitionGUIDs.str().c_str());
242 CharacterDatabase.PExecute("DELETE FROM petition_sign WHERE petitionguid IN ( %s )", ssInvalidPetitionGUIDs.str().c_str());
243 CharacterDatabase.PExecute("INSERT INTO petition (ownerguid, petitionguid, name, type) VALUES ('%u', '%u', '%s', '%u')",
244 _player->GetGUIDLow(), charter->GetGUIDLow(), name.c_str(), type);
245 CharacterDatabase.CommitTransaction();
248 void WorldSession::HandlePetitionShowSignOpcode(WorldPacket & recv_data)
250 CHECK_PACKET_SIZE(recv_data, 8);
252 // ok
253 sLog.outDebug("Received opcode CMSG_PETITION_SHOW_SIGNATURES");
254 //recv_data.hexlike();
256 uint8 signs = 0;
257 uint64 petitionguid;
258 recv_data >> petitionguid; // petition guid
260 // solve (possible) some strange compile problems with explicit use GUID_LOPART(petitionguid) at some GCC versions (wrong code optimization in compiler?)
261 uint32 petitionguid_low = GUID_LOPART(petitionguid);
263 QueryResult *result = CharacterDatabase.PQuery("SELECT petitionguid, type FROM petition WHERE petitionguid = '%u'", petitionguid_low);
264 if(!result)
266 sLog.outError("any petition on server...");
267 return;
269 Field *fields = result->Fetch();
270 uint32 type = fields[1].GetUInt32();
271 delete result;
272 // if guild petition and has guild => error, return;
273 if(type==9 && _player->GetGuildId())
274 return;
276 result = CharacterDatabase.PQuery("SELECT playerguid FROM petition_sign WHERE petitionguid = '%u'", petitionguid_low);
278 // result==NULL also correct in case no sign yet
279 if(result)
280 signs = result->GetRowCount();
282 sLog.outDebug("CMSG_PETITION_SHOW_SIGNATURES petition entry: '%u'", petitionguid_low);
284 WorldPacket data(SMSG_PETITION_SHOW_SIGNATURES, (8+8+4+1+signs*12));
285 data << petitionguid; // petition guid
286 data << _player->GetGUID(); // owner guid
287 data << petitionguid_low; // guild guid (in mangos always same as GUID_LOPART(petitionguid)
288 data << signs; // sign's count
290 for(uint8 i = 1; i <= signs; i++)
292 Field *fields = result->Fetch();
293 uint64 plguid = fields[0].GetUInt64();
295 data << plguid; // Player GUID
296 data << (uint32)0; // there 0 ...
298 result->NextRow();
300 delete result;
301 SendPacket(&data);
304 void WorldSession::HandlePetitionQueryOpcode(WorldPacket & recv_data)
306 CHECK_PACKET_SIZE(recv_data, 4+8);
308 sLog.outDebug("Received opcode CMSG_PETITION_QUERY"); // ok
309 //recv_data.hexlike();
311 uint32 guildguid;
312 uint64 petitionguid;
313 recv_data >> guildguid; // in mangos always same as GUID_LOPART(petitionguid)
314 recv_data >> petitionguid; // petition guid
315 sLog.outDebug("CMSG_PETITION_QUERY Petition GUID %u Guild GUID %u", GUID_LOPART(petitionguid), guildguid);
317 SendPetitionQueryOpcode(petitionguid);
320 void WorldSession::SendPetitionQueryOpcode(uint64 petitionguid)
322 uint64 ownerguid = 0;
323 uint32 type;
324 std::string name = "NO_NAME_FOR_GUID";
325 uint8 signs = 0;
327 QueryResult *result = CharacterDatabase.PQuery(
328 "SELECT ownerguid, name, "
329 " (SELECT COUNT(playerguid) FROM petition_sign WHERE petition_sign.petitionguid = '%u') AS signs "
330 "FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid), GUID_LOPART(petitionguid));
332 if(result)
334 Field* fields = result->Fetch();
335 ownerguid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER);
336 name = fields[1].GetCppString();
337 signs = fields[2].GetUInt8();
338 delete result;
340 else
342 sLog.outDebug("CMSG_PETITION_QUERY failed for petition (GUID: %u)", GUID_LOPART(petitionguid));
343 return;
346 QueryResult *result2 = CharacterDatabase.PQuery("SELECT type FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
348 if(result2)
350 Field* fields = result2->Fetch();
351 type = fields[0].GetUInt32();
352 delete result2;
354 else
356 sLog.outDebug("CMSG_PETITION_QUERY failed for petition (GUID: %u)", GUID_LOPART(petitionguid));
357 return;
360 WorldPacket data(SMSG_PETITION_QUERY_RESPONSE, (4+8+name.size()+1+1+4*13));
361 data << GUID_LOPART(petitionguid); // guild/team guid (in mangos always same as GUID_LOPART(petition guid)
362 data << ownerguid; // charter owner guid
363 data << name; // name (guild/arena team)
364 data << uint8(0); // 1
365 if(type == 9)
367 data << uint32(9);
368 data << uint32(9);
369 data << uint32(0); // bypass client - side limitation, a different value is needed here for each petition
371 else
373 data << type-1;
374 data << type-1;
375 data << type; // bypass client - side limitation, a different value is needed here for each petition
377 data << uint32(0); // 5
378 data << uint32(0); // 6
379 data << uint32(0); // 7
380 data << uint32(0); // 8
381 data << uint16(0); // 9 2 bytes field
382 data << uint32(0); // 10
383 data << uint32(0); // 11
384 data << uint32(0); // 13 count of next strings?
385 data << uint32(0); // 14
386 if(type == 9)
387 data << uint32(0); // 15 0 - guild, 1 - arena team
388 else
389 data << uint32(1);
390 SendPacket(&data);
393 void WorldSession::HandlePetitionRenameOpcode(WorldPacket & recv_data)
395 CHECK_PACKET_SIZE(recv_data, 8+1);
397 sLog.outDebug("Received opcode MSG_PETITION_RENAME"); // ok
398 //recv_data.hexlike();
400 uint64 petitionguid;
401 uint32 type;
402 std::string newname;
404 recv_data >> petitionguid; // guid
405 recv_data >> newname; // new name
407 Item *item = _player->GetItemByGuid(petitionguid);
408 if(!item)
409 return;
411 QueryResult *result2 = CharacterDatabase.PQuery("SELECT type FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
413 if(result2)
415 Field* fields = result2->Fetch();
416 type = fields[0].GetUInt32();
417 delete result2;
419 else
421 sLog.outDebug("CMSG_PETITION_QUERY failed for petition (GUID: %u)", GUID_LOPART(petitionguid));
422 return;
425 if(type == 9)
427 if(objmgr.GetGuildByName(newname))
429 SendGuildCommandResult(GUILD_CREATE_S, newname, GUILD_NAME_EXISTS);
430 return;
432 if(objmgr.IsReservedName(newname))
434 SendGuildCommandResult(GUILD_CREATE_S, newname, GUILD_NAME_INVALID);
435 return;
437 if(!ObjectMgr::IsValidCharterName(newname))
439 SendGuildCommandResult(GUILD_CREATE_S, newname, GUILD_NAME_INVALID);
440 return;
443 else
445 if(objmgr.GetArenaTeamByName(newname))
447 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, newname, "", ERR_ARENA_TEAM_NAME_EXISTS_S);
448 return;
450 if(objmgr.IsReservedName(newname))
452 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, newname, "", ERR_ARENA_TEAM_NAME_INVALID);
453 return;
455 if(!ObjectMgr::IsValidCharterName(newname))
457 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, newname, "", ERR_ARENA_TEAM_NAME_INVALID);
458 return;
462 std::string db_newname = newname;
463 CharacterDatabase.escape_string(db_newname);
464 CharacterDatabase.PExecute("UPDATE petition SET name = '%s' WHERE petitionguid = '%u'",
465 db_newname.c_str(), GUID_LOPART(petitionguid));
467 sLog.outDebug("Petition (GUID: %u) renamed to '%s'", GUID_LOPART(petitionguid), newname.c_str());
468 WorldPacket data(MSG_PETITION_RENAME, (8+newname.size()+1));
469 data << petitionguid;
470 data << newname;
471 SendPacket(&data);
474 void WorldSession::HandlePetitionSignOpcode(WorldPacket & recv_data)
476 CHECK_PACKET_SIZE(recv_data, 8+1);
478 sLog.outDebug("Received opcode CMSG_PETITION_SIGN"); // ok
479 //recv_data.hexlike();
481 Field *fields;
482 uint64 petitionguid;
483 uint32 type;
484 uint8 unk;
485 uint64 ownerguid;
486 recv_data >> petitionguid; // petition guid
487 recv_data >> unk;
489 uint8 signs = 0;
491 QueryResult *result = CharacterDatabase.PQuery(
492 "SELECT ownerguid, "
493 " (SELECT COUNT(playerguid) FROM petition_sign WHERE petition_sign.petitionguid = '%u') AS signs "
494 "FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid), GUID_LOPART(petitionguid));
496 if(!result)
498 sLog.outError("any petition on server...");
499 return;
502 fields = result->Fetch();
503 ownerguid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER);
504 signs = fields[1].GetUInt8();
506 delete result;
508 uint32 plguidlo = _player->GetGUIDLow();
509 if(GUID_LOPART(ownerguid) == plguidlo)
510 return;
512 // not let enemies sign guild charter
513 if(!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && GetPlayer()->GetTeam() != objmgr.GetPlayerTeamByGUID(ownerguid))
514 return;
516 QueryResult *result2 = CharacterDatabase.PQuery("SELECT type FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
518 if(result2)
520 Field* fields = result2->Fetch();
521 type = fields[0].GetUInt32();
522 delete result2;
524 else
526 sLog.outDebug("CMSG_PETITION_QUERY failed for petition (GUID: %u)", GUID_LOPART(petitionguid));
527 return;
530 if(type != 9 && _player->getLevel() < sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
532 // player is too low level to join an arena team
533 SendNotification(LANG_YOUR_ARENA_LEVEL_REQ_ERROR,sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL));
534 return;
537 signs += 1;
538 if(signs > type) // client signs maximum
539 return;
541 //client doesn't allow to sign petition two times by one character, but not check sign by another character from same account
542 //not allow sign another player from already sign player account
543 result = CharacterDatabase.PQuery("SELECT playerguid FROM petition_sign WHERE player_account = '%u' AND petitionguid = '%u'", GetAccountId(), GUID_LOPART(petitionguid));
545 if(result)
547 delete result;
548 WorldPacket data(SMSG_PETITION_SIGN_RESULTS, (8+8+4));
549 data << petitionguid;
550 data << _player->GetGUID();
551 data << (uint32)PETITION_SIGN_ALREADY_SIGNED;
553 // close at signer side
554 SendPacket(&data);
556 // update for owner if online
557 if(Player *owner = objmgr.GetPlayer(ownerguid))
558 owner->GetSession()->SendPacket(&data);
559 return;
562 CharacterDatabase.PExecute("INSERT INTO petition_sign (ownerguid,petitionguid, playerguid, player_account) VALUES ('%u', '%u', '%u','%u')", GUID_LOPART(ownerguid),GUID_LOPART(petitionguid), plguidlo,GetAccountId());
564 sLog.outDebug("PETITION SIGN: GUID %u by player: %s (GUID: %u Account: %u)", GUID_LOPART(petitionguid), _player->GetName(),plguidlo,GetAccountId());
566 WorldPacket data(SMSG_PETITION_SIGN_RESULTS, (8+8+4));
567 data << petitionguid;
568 data << _player->GetGUID();
569 data << (uint32)PETITION_SIGN_OK;
571 // close at signer side
572 SendPacket(&data);
574 // update signs count on charter, required testing...
575 //Item *item = _player->GetItemByGuid(petitionguid));
576 //if(item)
577 // item->SetUInt32Value(ITEM_FIELD_ENCHANTMENT+1, signs);
579 // update for owner if online
580 if(Player *owner = objmgr.GetPlayer(ownerguid))
581 owner->GetSession()->SendPacket(&data);
584 void WorldSession::HandlePetitionDeclineOpcode(WorldPacket & recv_data)
586 CHECK_PACKET_SIZE(recv_data, 8);
588 sLog.outDebug("Received opcode MSG_PETITION_DECLINE"); // ok
589 //recv_data.hexlike();
591 uint64 petitionguid;
592 uint64 ownerguid;
593 recv_data >> petitionguid; // petition guid
594 sLog.outDebug("Petition %u declined by %u", GUID_LOPART(petitionguid), _player->GetGUIDLow());
596 QueryResult *result = CharacterDatabase.PQuery("SELECT ownerguid FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
597 if(!result)
598 return;
600 Field *fields = result->Fetch();
601 ownerguid = MAKE_NEW_GUID(fields[0].GetUInt32(), 0, HIGHGUID_PLAYER);
602 delete result;
604 Player *owner = objmgr.GetPlayer(ownerguid);
605 if(owner) // petition owner online
607 WorldPacket data(MSG_PETITION_DECLINE, 8);
608 data << _player->GetGUID();
609 owner->GetSession()->SendPacket(&data);
613 void WorldSession::HandleOfferPetitionOpcode(WorldPacket & recv_data)
615 CHECK_PACKET_SIZE(recv_data, 4+8+8);
617 sLog.outDebug("Received opcode CMSG_OFFER_PETITION"); // ok
618 //recv_data.hexlike();
620 uint8 signs = 0;
621 uint64 petitionguid, plguid;
622 uint32 petitiontype;
623 Player *player;
624 recv_data >> petitiontype; // 2.0.8 - petition type?
625 recv_data >> petitionguid; // petition guid
626 recv_data >> plguid; // player guid
627 sLog.outDebug("OFFER PETITION: type %u, GUID1 %u, to player id: %u", petitiontype, GUID_LOPART(petitionguid), GUID_LOPART(plguid));
629 player = ObjectAccessor::FindPlayer(plguid);
630 if(!player || player->GetSocial()->HasIgnore(GetPlayer()->GetGUIDLow()))
631 return;
633 // not let offer to enemies
634 if (!sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_INTERACTION_GUILD) && GetPlayer()->GetTeam() != player->GetTeam() )
635 return;
637 QueryResult *result = CharacterDatabase.PQuery("SELECT petitionguid FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
638 if(!result)
640 sLog.outError("any petition on server...");
641 return;
644 delete result;
646 result = CharacterDatabase.PQuery("SELECT playerguid FROM petition_sign WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
647 // result==NULL also correct charter without signs
648 if(result)
649 signs = result->GetRowCount();
651 WorldPacket data(SMSG_PETITION_SHOW_SIGNATURES, (8+8+4+signs+signs*12));
652 data << petitionguid; // petition guid
653 data << _player->GetGUID(); // owner guid
654 data << GUID_LOPART(petitionguid); // guild guid (in mangos always same as GUID_LOPART(petition guid)
655 data << signs; // sign's count
657 for(uint8 i = 1; i <= signs; i++)
659 Field *fields = result->Fetch();
660 uint64 plguid = fields[0].GetUInt64();
662 data << plguid; // Player GUID
663 data << (uint32)0; // there 0 ...
665 result->NextRow();
668 delete result;
669 player->GetSession()->SendPacket(&data);
672 void WorldSession::HandleTurnInPetitionOpcode(WorldPacket & recv_data)
674 CHECK_PACKET_SIZE(recv_data, 8);
676 sLog.outDebug("Received opcode CMSG_TURN_IN_PETITION"); // ok
677 //recv_data.hexlike();
679 WorldPacket data;
680 uint64 petitionguid;
682 uint32 ownerguidlo;
683 uint32 type;
684 std::string name;
686 recv_data >> petitionguid;
688 sLog.outDebug("Petition %u turned in by %u", GUID_LOPART(petitionguid), _player->GetGUIDLow());
690 // data
691 QueryResult *result = CharacterDatabase.PQuery("SELECT ownerguid, name, type FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
692 if(result)
694 Field *fields = result->Fetch();
695 ownerguidlo = fields[0].GetUInt32();
696 name = fields[1].GetCppString();
697 type = fields[2].GetUInt32();
698 delete result;
700 else
702 sLog.outError("petition table has broken data!");
703 return;
706 if(type == 9)
708 if(_player->GetGuildId())
710 data.Initialize(SMSG_TURN_IN_PETITION_RESULTS, 4);
711 data << (uint32)PETITION_TURN_ALREADY_IN_GUILD; // already in guild
712 _player->GetSession()->SendPacket(&data);
713 return;
716 else
718 uint8 slot = ArenaTeam::GetSlotByType(type);
719 if(slot >= MAX_ARENA_SLOT)
720 return;
722 if(_player->GetArenaTeamId(slot))
724 //data.Initialize(SMSG_TURN_IN_PETITION_RESULTS, 4);
725 //data << (uint32)PETITION_TURN_ALREADY_IN_GUILD; // already in guild
726 //_player->GetSession()->SendPacket(&data);
727 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ALREADY_IN_ARENA_TEAM);
728 return;
732 if(_player->GetGUIDLow() != ownerguidlo)
733 return;
735 // signs
736 uint8 signs;
737 result = CharacterDatabase.PQuery("SELECT playerguid FROM petition_sign WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
738 if(result)
739 signs = result->GetRowCount();
740 else
741 signs = 0;
743 uint32 count;
744 //if(signs < sWorld.getConfig(CONFIG_MIN_PETITION_SIGNS))
745 if(type == 9)
746 count = sWorld.getConfig(CONFIG_MIN_PETITION_SIGNS);
747 else
748 count = type-1;
749 if(signs < count)
751 data.Initialize(SMSG_TURN_IN_PETITION_RESULTS, 4);
752 data << (uint32)PETITION_TURN_NEED_MORE_SIGNATURES; // need more signatures...
753 SendPacket(&data);
754 delete result;
755 return;
758 if(type == 9)
760 if(objmgr.GetGuildByName(name))
762 SendGuildCommandResult(GUILD_CREATE_S, name, GUILD_NAME_EXISTS);
763 delete result;
764 return;
767 else
769 if(objmgr.GetArenaTeamByName(name))
771 SendArenaTeamCommandResult(ERR_ARENA_TEAM_CREATE_S, name, "", ERR_ARENA_TEAM_NAME_EXISTS_S);
772 delete result;
773 return;
777 // and at last charter item check
778 Item *item = _player->GetItemByGuid(petitionguid);
779 if(!item)
781 delete result;
782 return;
785 // OK!
787 // delete charter item
788 _player->DestroyItem(item->GetBagSlot(),item->GetSlot(), true);
790 if(type == 9) // create guild
792 Guild* guild = new Guild;
793 if(!guild->create(_player->GetGUID(), name))
795 delete guild;
796 delete result;
797 return;
800 // register guild and add guildmaster
801 objmgr.AddGuild(guild);
803 // add members
804 for(uint8 i = 0; i < signs; ++i)
806 Field* fields = result->Fetch();
807 guild->AddMember(fields[0].GetUInt64(), guild->GetLowestRank());
808 result->NextRow();
811 else // or arena team
813 ArenaTeam* at = new ArenaTeam;
814 if(!at->create(_player->GetGUID(), type, name))
816 sLog.outError("PetitionsHandler: arena team create failed.");
817 delete at;
818 delete result;
819 return;
822 CHECK_PACKET_SIZE(recv_data, 8+5*4);
823 uint32 icon, iconcolor, border, bordercolor, backgroud;
824 recv_data >> backgroud >> icon >> iconcolor >> border >> bordercolor;
826 at->SetEmblem(backgroud, icon, iconcolor, border, bordercolor);
828 // register team and add captain
829 objmgr.AddArenaTeam(at);
830 sLog.outDebug("PetitonsHandler: arena team added to objmrg");
832 // add members
833 for(uint8 i = 0; i < signs; ++i)
835 Field* fields = result->Fetch();
836 uint64 memberGUID = fields[0].GetUInt64();
837 sLog.outDebug("PetitionsHandler: adding arena member %u", GUID_LOPART(memberGUID));
838 at->AddMember(memberGUID);
839 result->NextRow();
843 delete result;
845 CharacterDatabase.BeginTransaction();
846 CharacterDatabase.PExecute("DELETE FROM petition WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
847 CharacterDatabase.PExecute("DELETE FROM petition_sign WHERE petitionguid = '%u'", GUID_LOPART(petitionguid));
848 CharacterDatabase.CommitTransaction();
850 // created
851 sLog.outDebug("TURN IN PETITION GUID %u", GUID_LOPART(petitionguid));
853 data.Initialize(SMSG_TURN_IN_PETITION_RESULTS, 4);
854 data << (uint32)PETITION_TURN_OK;
855 SendPacket(&data);
858 void WorldSession::HandlePetitionShowListOpcode(WorldPacket & recv_data)
860 CHECK_PACKET_SIZE(recv_data, 8);
862 sLog.outDebug("Received CMSG_PETITION_SHOWLIST"); // ok
863 //recv_data.hexlike();
865 uint64 guid;
866 recv_data >> guid;
868 SendPetitionShowList(guid);
871 void WorldSession::SendPetitionShowList(uint64 guid)
873 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid, UNIT_NPC_FLAG_PETITIONER);
874 if (!pCreature)
876 sLog.outDebug("WORLD: HandlePetitionShowListOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)));
877 return;
880 // remove fake death
881 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
882 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
884 uint8 count = 0;
885 if(pCreature->isTabardDesigner())
886 count = 1;
887 else
888 count = 3;
890 WorldPacket data(SMSG_PETITION_SHOWLIST, 8+1+4*6);
891 data << guid; // npc guid
892 data << count; // count
893 if(count == 1)
895 data << uint32(1); // index
896 data << uint32(GUILD_CHARTER); // charter entry
897 data << uint32(16161); // charter display id
898 data << uint32(GUILD_CHARTER_COST); // charter cost
899 data << uint32(0); // unknown
900 data << uint32(9); // required signs?
902 else
904 // 2v2
905 data << uint32(1); // index
906 data << uint32(ARENA_TEAM_CHARTER_2v2); // charter entry
907 data << uint32(16161); // charter display id
908 data << uint32(ARENA_TEAM_CHARTER_2v2_COST); // charter cost
909 data << uint32(2); // unknown
910 data << uint32(2); // required signs?
911 // 3v3
912 data << uint32(2); // index
913 data << uint32(ARENA_TEAM_CHARTER_3v3); // charter entry
914 data << uint32(16161); // charter display id
915 data << uint32(ARENA_TEAM_CHARTER_3v3_COST); // charter cost
916 data << uint32(3); // unknown
917 data << uint32(3); // required signs?
918 // 5v5
919 data << uint32(3); // index
920 data << uint32(ARENA_TEAM_CHARTER_5v5); // charter entry
921 data << uint32(16161); // charter display id
922 data << uint32(ARENA_TEAM_CHARTER_5v5_COST); // charter cost
923 data << uint32(5); // unknown
924 data << uint32(5); // required signs?
926 //for(uint8 i = 0; i < count; i++)
928 // data << uint32(i); // index
929 // data << uint32(GUILD_CHARTER); // charter entry
930 // data << uint32(16161); // charter display id
931 // data << uint32(GUILD_CHARTER_COST+i); // charter cost
932 // data << uint32(0); // unknown
933 // data << uint32(9); // required signs?
935 SendPacket(&data);
936 sLog.outDebug("Sent SMSG_PETITION_SHOWLIST");