Merge branch 'master' into 330
[getmangos.git] / src / game / CharacterHandler.cpp
blob634652dd3d51778d93f08634131cc46f5ec8d7bf
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 "Common.h"
20 #include "Database/DatabaseEnv.h"
21 #include "WorldPacket.h"
22 #include "SharedDefines.h"
23 #include "WorldSession.h"
24 #include "Opcodes.h"
25 #include "Log.h"
26 #include "World.h"
27 #include "ObjectMgr.h"
28 #include "ObjectDefines.h"
29 #include "Player.h"
30 #include "Guild.h"
31 #include "UpdateMask.h"
32 #include "Auth/md5.h"
33 #include "ObjectAccessor.h"
34 #include "Group.h"
35 #include "Database/DatabaseImpl.h"
36 #include "PlayerDump.h"
37 #include "SocialMgr.h"
38 #include "Util.h"
39 #include "ArenaTeam.h"
40 #include "Language.h"
42 class LoginQueryHolder : public SqlQueryHolder
44 private:
45 uint32 m_accountId;
46 uint64 m_guid;
47 public:
48 LoginQueryHolder(uint32 accountId, uint64 guid)
49 : m_accountId(accountId), m_guid(guid) { }
50 uint64 GetGuid() const { return m_guid; }
51 uint32 GetAccountId() const { return m_accountId; }
52 bool Initialize();
55 bool LoginQueryHolder::Initialize()
57 SetSize(MAX_PLAYER_LOGIN_QUERY);
59 bool res = true;
61 // NOTE: all fields in `characters` must be read to prevent lost character data at next save in case wrong DB structure.
62 // !!! NOTE: including unused `zone`,`online`
63 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADFROM, "SELECT guid, account, data, name, race, class, gender, level, xp, money, playerBytes, playerBytes2, playerFlags, position_x, position_y, position_z, map, orientation, taximask, cinematic, totaltime, leveltime, rest_bonus, logout_time, is_logout_resting, resettalents_cost, resettalents_time, trans_x, trans_y, trans_z, trans_o, transguid, extra_flags, stable_slots, at_login, zone, online, death_expire_time, taxi_path, dungeon_difficulty, arena_pending_points FROM characters WHERE guid = '%u'", GUID_LOPART(m_guid));
64 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADGROUP, "SELECT leaderGuid FROM group_member WHERE memberGuid ='%u'", GUID_LOPART(m_guid));
65 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADBOUNDINSTANCES, "SELECT id, permanent, map, difficulty, resettime FROM character_instance LEFT JOIN instance ON instance = id WHERE guid = '%u'", GUID_LOPART(m_guid));
66 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADAURAS, "SELECT caster_guid,spell,effect_index,stackcount,amount,maxduration,remaintime,remaincharges FROM character_aura WHERE guid = '%u'", GUID_LOPART(m_guid));
67 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADSPELLS, "SELECT spell,active,disabled FROM character_spell WHERE guid = '%u'", GUID_LOPART(m_guid));
68 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADQUESTSTATUS, "SELECT quest,status,rewarded,explored,timer,mobcount1,mobcount2,mobcount3,mobcount4,itemcount1,itemcount2,itemcount3,itemcount4 FROM character_queststatus WHERE guid = '%u'", GUID_LOPART(m_guid));
69 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADDAILYQUESTSTATUS,"SELECT quest,time FROM character_queststatus_daily WHERE guid = '%u'", GUID_LOPART(m_guid));
70 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADREPUTATION, "SELECT faction,standing,flags FROM character_reputation WHERE guid = '%u'", GUID_LOPART(m_guid));
71 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADINVENTORY, "SELECT data,bag,slot,item,item_template FROM character_inventory JOIN item_instance ON character_inventory.item = item_instance.guid WHERE character_inventory.guid = '%u' ORDER BY bag,slot", GUID_LOPART(m_guid));
72 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADACTIONS, "SELECT button,action,type FROM character_action WHERE guid = '%u' ORDER BY button", GUID_LOPART(m_guid));
73 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADMAILCOUNT, "SELECT COUNT(id) FROM mail WHERE receiver = '%u' AND (checked & 1)=0 AND deliver_time <= '" UI64FMTD "'", GUID_LOPART(m_guid), (uint64)time(NULL));
74 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADMAILDATE, "SELECT MIN(deliver_time) FROM mail WHERE receiver = '%u' AND (checked & 1)=0", GUID_LOPART(m_guid));
75 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADSOCIALLIST, "SELECT friend,flags,note FROM character_social WHERE guid = '%u' LIMIT 255", GUID_LOPART(m_guid));
76 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADHOMEBIND, "SELECT map,zone,position_x,position_y,position_z FROM character_homebind WHERE guid = '%u'", GUID_LOPART(m_guid));
77 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADSPELLCOOLDOWNS, "SELECT spell,item,time FROM character_spell_cooldown WHERE guid = '%u'", GUID_LOPART(m_guid));
78 if(sWorld.getConfig(CONFIG_DECLINED_NAMES_USED))
79 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADDECLINEDNAMES, "SELECT genitive, dative, accusative, instrumental, prepositional FROM character_declinedname WHERE guid = '%u'", GUID_LOPART(m_guid));
80 // in other case still be dummy query
81 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADGUILD, "SELECT guildid,rank FROM guild_member WHERE guid = '%u'", GUID_LOPART(m_guid));
82 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADARENAINFO, "SELECT arenateamid, played_week, played_season, wons_season, personal_rating FROM arena_team_member WHERE guid='%u'", GUID_LOPART(m_guid));
83 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADACHIEVEMENTS, "SELECT achievement, date FROM character_achievement WHERE guid = '%u'", GUID_LOPART(m_guid));
84 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADCRITERIAPROGRESS,"SELECT criteria, counter, date FROM character_achievement_progress WHERE guid = '%u'", GUID_LOPART(m_guid));
85 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADEQUIPMENTSETS, "SELECT setguid, setindex, name, iconname, item0, item1, item2, item3, item4, item5, item6, item7, item8, item9, item10, item11, item12, item13, item14, item15, item16, item17, item18 FROM character_equipmentsets WHERE guid = '%u' ORDER BY setindex", GUID_LOPART(m_guid));
86 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADBGDATA, "SELECT instance_id, team, join_x, join_y, join_z, join_o, join_map, taxi_start, taxi_end, mount_spell FROM character_battleground_data WHERE guid = '%u'", GUID_LOPART(m_guid));
87 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADACCOUNTDATA, "SELECT type, time, data FROM character_account_data WHERE guid='%u'", GUID_LOPART(m_guid));
88 res &= SetPQuery(PLAYER_LOGIN_QUERY_LOADSKILLS, "SELECT skill, value, max FROM character_skills WHERE guid = '%u'", GUID_LOPART(m_guid));
90 return res;
93 // don't call WorldSession directly
94 // it may get deleted before the query callbacks get executed
95 // instead pass an account id to this handler
96 class CharacterHandler
98 public:
99 void HandleCharEnumCallback(QueryResult * result, uint32 account)
101 WorldSession * session = sWorld.FindSession(account);
102 if(!session)
104 delete result;
105 return;
107 session->HandleCharEnum(result);
109 void HandlePlayerLoginCallback(QueryResult * /*dummy*/, SqlQueryHolder * holder)
111 if (!holder) return;
112 WorldSession *session = sWorld.FindSession(((LoginQueryHolder*)holder)->GetAccountId());
113 if(!session)
115 delete holder;
116 return;
118 session->HandlePlayerLogin((LoginQueryHolder*)holder);
120 } chrHandler;
122 void WorldSession::HandleCharEnum(QueryResult * result)
124 WorldPacket data(SMSG_CHAR_ENUM, 100); // we guess size
126 uint8 num = 0;
128 data << num;
130 if( result )
134 uint32 guidlow = (*result)[0].GetUInt32();
135 sLog.outDetail("Loading char guid %u from account %u.", guidlow, GetAccountId());
136 if(Player::BuildEnumData(result, &data))
137 ++num;
139 while( result->NextRow() );
141 delete result;
144 data.put<uint8>(0, num);
146 SendPacket( &data );
149 void WorldSession::HandleCharEnumOpcode( WorldPacket & /*recv_data*/ )
151 /// get all the data necessary for loading all characters (along with their pets) on the account
152 CharacterDatabase.AsyncPQuery(&chrHandler, &CharacterHandler::HandleCharEnumCallback, GetAccountId(),
153 !sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) ?
154 // ------- Query Without Declined Names --------
155 // 0 1 2 3 4 5 6 7
156 "SELECT characters.guid, characters.name, characters.race, characters.class, characters.gender, characters.playerBytes, characters.playerBytes2, characters.level, "
157 // 8 9 10 11 12 13 14
158 "characters.zone, characters.map, characters.position_x, characters.position_y, characters.position_z, guild_member.guildid, characters.playerFlags, "
159 // 15 16 17 18 19
160 "characters.at_login, character_pet.entry, character_pet.modelid, character_pet.level, characters.data "
161 "FROM characters LEFT JOIN character_pet ON characters.guid=character_pet.owner AND character_pet.slot='%u' "
162 "LEFT JOIN guild_member ON characters.guid = guild_member.guid "
163 "WHERE characters.account = '%u' ORDER BY characters.guid"
165 // --------- Query With Declined Names ---------
166 // 0 1 2 3 4 5 6 7
167 "SELECT characters.guid, characters.name, characters.race, characters.class, characters.gender, characters.playerBytes, characters.playerBytes2, characters.level, "
168 // 8 9 10 11 12 13 14
169 "characters.zone, characters.map, characters.position_x, characters.position_y, characters.position_z, guild_member.guildid, characters.playerFlags, "
170 // 15 16 17 18 19 20
171 "characters.at_login, character_pet.entry, character_pet.modelid, character_pet.level, characters.data, character_declinedname.genitive "
172 "FROM characters LEFT JOIN character_pet ON characters.guid = character_pet.owner AND character_pet.slot='%u' "
173 "LEFT JOIN character_declinedname ON characters.guid = character_declinedname.guid "
174 "LEFT JOIN guild_member ON characters.guid = guild_member.guid "
175 "WHERE characters.account = '%u' ORDER BY characters.guid",
176 PET_SAVE_AS_CURRENT, GetAccountId());
179 void WorldSession::HandleCharCreateOpcode( WorldPacket & recv_data )
181 std::string name;
182 uint8 race_, class_;
184 recv_data >> name;
186 recv_data >> race_;
187 recv_data >> class_;
189 WorldPacket data(SMSG_CHAR_CREATE, 1); // returned with diff.values in all cases
191 if(GetSecurity() == SEC_PLAYER)
193 if(uint32 mask = sWorld.getConfig(CONFIG_CHARACTERS_CREATING_DISABLED))
195 bool disabled = false;
197 uint32 team = Player::TeamForRace(race_);
198 switch(team)
200 case ALLIANCE: disabled = mask & (1 << 0); break;
201 case HORDE: disabled = mask & (1 << 1); break;
204 if(disabled)
206 data << (uint8)CHAR_CREATE_DISABLED;
207 SendPacket( &data );
208 return;
213 ChrClassesEntry const* classEntry = sChrClassesStore.LookupEntry(class_);
214 ChrRacesEntry const* raceEntry = sChrRacesStore.LookupEntry(race_);
216 if( !classEntry || !raceEntry )
218 data << (uint8)CHAR_CREATE_FAILED;
219 SendPacket( &data );
220 sLog.outError("Class: %u or Race %u not found in DBC (Wrong DBC files?) or Cheater?", class_, race_);
221 return;
224 // prevent character creating Expansion race without Expansion account
225 if (raceEntry->expansion > Expansion())
227 data << (uint8)CHAR_CREATE_EXPANSION;
228 sLog.outError("Expansion %u account:[%d] tried to Create character with expansion %u race (%u)", Expansion(), GetAccountId(), raceEntry->expansion, race_);
229 SendPacket( &data );
230 return;
233 // prevent character creating Expansion class without Expansion account
234 if (classEntry->expansion > Expansion())
236 data << (uint8)CHAR_CREATE_EXPANSION_CLASS;
237 sLog.outError("Expansion %u account:[%d] tried to Create character with expansion %u class (%u)", Expansion(), GetAccountId(), classEntry->expansion, class_);
238 SendPacket( &data );
239 return;
242 // prevent character creating with invalid name
243 if (!normalizePlayerName(name))
245 data << (uint8)CHAR_NAME_NO_NAME;
246 SendPacket( &data );
247 sLog.outError("Account:[%d] but tried to Create character with empty [name]", GetAccountId());
248 return;
251 // check name limitations
252 uint8 res = ObjectMgr::CheckPlayerName(name, true);
253 if (res != CHAR_NAME_SUCCESS)
255 data << uint8(res);
256 SendPacket( &data );
257 return;
260 if (GetSecurity() == SEC_PLAYER && sObjectMgr.IsReservedName(name))
262 data << (uint8)CHAR_NAME_RESERVED;
263 SendPacket( &data );
264 return;
267 if (sObjectMgr.GetPlayerGUIDByName(name))
269 data << (uint8)CHAR_CREATE_NAME_IN_USE;
270 SendPacket( &data );
271 return;
274 QueryResult *resultacct = loginDatabase.PQuery("SELECT SUM(numchars) FROM realmcharacters WHERE acctid = '%d'", GetAccountId());
275 if (resultacct)
277 Field *fields=resultacct->Fetch();
278 uint32 acctcharcount = fields[0].GetUInt32();
279 delete resultacct;
281 if (acctcharcount >= sWorld.getConfig(CONFIG_CHARACTERS_PER_ACCOUNT))
283 data << (uint8)CHAR_CREATE_ACCOUNT_LIMIT;
284 SendPacket( &data );
285 return;
289 QueryResult *result = CharacterDatabase.PQuery("SELECT COUNT(guid) FROM characters WHERE account = '%d'", GetAccountId());
290 uint8 charcount = 0;
291 if ( result )
293 Field *fields = result->Fetch();
294 charcount = fields[0].GetUInt8();
295 delete result;
297 if (charcount >= sWorld.getConfig(CONFIG_CHARACTERS_PER_REALM))
299 data << (uint8)CHAR_CREATE_SERVER_LIMIT;
300 SendPacket( &data );
301 return;
305 // speedup check for heroic class disabled case
306 uint32 heroic_free_slots = sWorld.getConfig(CONFIG_HEROIC_CHARACTERS_PER_REALM);
307 if(heroic_free_slots == 0 && GetSecurity() == SEC_PLAYER && class_ == CLASS_DEATH_KNIGHT)
309 data << (uint8)CHAR_CREATE_UNIQUE_CLASS_LIMIT;
310 SendPacket( &data );
311 return;
314 // speedup check for heroic class disabled case
315 uint32 req_level_for_heroic = sWorld.getConfig(CONFIG_MIN_LEVEL_FOR_HEROIC_CHARACTER_CREATING);
316 if(GetSecurity() == SEC_PLAYER && class_ == CLASS_DEATH_KNIGHT && req_level_for_heroic > sWorld.getConfig(CONFIG_MAX_PLAYER_LEVEL))
318 data << (uint8)CHAR_CREATE_LEVEL_REQUIREMENT;
319 SendPacket( &data );
320 return;
323 bool AllowTwoSideAccounts = sWorld.getConfig(CONFIG_ALLOW_TWO_SIDE_ACCOUNTS) || GetSecurity() > SEC_PLAYER;
324 uint32 skipCinematics = sWorld.getConfig(CONFIG_SKIP_CINEMATICS);
326 bool have_same_race = false;
328 // if 0 then allowed creating without any characters
329 bool have_req_level_for_heroic = (req_level_for_heroic==0);
331 if(!AllowTwoSideAccounts || skipCinematics == 1 || class_ == CLASS_DEATH_KNIGHT)
333 QueryResult *result2 = CharacterDatabase.PQuery("SELECT level,race,class FROM characters WHERE account = '%u' %s",
334 GetAccountId(), (skipCinematics == 1 || class_ == CLASS_DEATH_KNIGHT) ? "" : "LIMIT 1");
335 if(result2)
337 uint32 team_= Player::TeamForRace(race_);
339 Field* field = result2->Fetch();
340 uint8 acc_race = field[1].GetUInt32();
342 if(GetSecurity() == SEC_PLAYER && class_ == CLASS_DEATH_KNIGHT)
344 uint8 acc_class = field[2].GetUInt32();
345 if(acc_class == CLASS_DEATH_KNIGHT)
347 if(heroic_free_slots > 0)
348 --heroic_free_slots;
350 if(heroic_free_slots == 0)
352 data << (uint8)CHAR_CREATE_UNIQUE_CLASS_LIMIT;
353 SendPacket( &data );
354 return;
358 if(!have_req_level_for_heroic)
360 uint32 acc_level = field[0].GetUInt32();
361 if(acc_level >= req_level_for_heroic)
362 have_req_level_for_heroic = true;
366 // need to check team only for first character
367 // TODO: what to if account already has characters of both races?
368 if (!AllowTwoSideAccounts)
370 uint32 acc_team = 0;
371 if(acc_race > 0)
372 acc_team = Player::TeamForRace(acc_race);
374 if(acc_team != team_)
376 data << (uint8)CHAR_CREATE_PVP_TEAMS_VIOLATION;
377 SendPacket( &data );
378 delete result2;
379 return;
383 // search same race for cinematic or same class if need
384 // TODO: check if cinematic already shown? (already logged in?; cinematic field)
385 while ((skipCinematics == 1 && !have_same_race) || class_ == CLASS_DEATH_KNIGHT)
387 if(!result2->NextRow())
388 break;
390 field = result2->Fetch();
391 acc_race = field[1].GetUInt32();
393 if(!have_same_race)
394 have_same_race = race_ == acc_race;
396 if(GetSecurity() == SEC_PLAYER && class_ == CLASS_DEATH_KNIGHT)
398 uint8 acc_class = field[2].GetUInt32();
399 if(acc_class == CLASS_DEATH_KNIGHT)
401 if(heroic_free_slots > 0)
402 --heroic_free_slots;
404 if(heroic_free_slots == 0)
406 data << (uint8)CHAR_CREATE_UNIQUE_CLASS_LIMIT;
407 SendPacket( &data );
408 return;
412 if(!have_req_level_for_heroic)
414 uint32 acc_level = field[0].GetUInt32();
415 if(acc_level >= req_level_for_heroic)
416 have_req_level_for_heroic = true;
420 delete result2;
424 if(GetSecurity() == SEC_PLAYER && class_ == CLASS_DEATH_KNIGHT && !have_req_level_for_heroic)
426 data << (uint8)CHAR_CREATE_LEVEL_REQUIREMENT;
427 SendPacket( &data );
428 return;
431 // extract other data required for player creating
432 uint8 gender, skin, face, hairStyle, hairColor, facialHair, outfitId;
433 recv_data >> gender >> skin >> face;
434 recv_data >> hairStyle >> hairColor >> facialHair >> outfitId;
436 Player *pNewChar = new Player(this);
437 if(!pNewChar->Create( sObjectMgr.GenerateLowGuid(HIGHGUID_PLAYER), name, race_, class_, gender, skin, face, hairStyle, hairColor, facialHair, outfitId ))
439 // Player not create (race/class problem?)
440 delete pNewChar;
442 data << (uint8)CHAR_CREATE_ERROR;
443 SendPacket( &data );
445 return;
448 if ((have_same_race && skipCinematics == 1) || skipCinematics == 2)
449 pNewChar->setCinematic(1); // not show intro
451 // Player created, save it now
452 pNewChar->SaveToDB();
453 charcount += 1;
455 loginDatabase.PExecute("DELETE FROM realmcharacters WHERE acctid= '%d' AND realmid = '%d'", GetAccountId(), realmID);
456 loginDatabase.PExecute("INSERT INTO realmcharacters (numchars, acctid, realmid) VALUES (%u, %u, %u)", charcount, GetAccountId(), realmID);
458 delete pNewChar; // created only to call SaveToDB()
460 data << (uint8)CHAR_CREATE_SUCCESS;
461 SendPacket( &data );
463 std::string IP_str = GetRemoteAddress();
464 sLog.outBasic("Account: %d (IP: %s) Create Character:[%s]", GetAccountId(), IP_str.c_str(), name.c_str());
465 sLog.outChar("Account: %d (IP: %s) Create Character:[%s]", GetAccountId(), IP_str.c_str(), name.c_str());
468 void WorldSession::HandleCharDeleteOpcode( WorldPacket & recv_data )
470 uint64 guid;
471 recv_data >> guid;
473 // can't delete loaded character
474 if(sObjectMgr.GetPlayer(guid))
475 return;
477 uint32 accountId = 0;
478 std::string name;
480 // is guild leader
481 if(sObjectMgr.GetGuildByLeader(guid))
483 WorldPacket data(SMSG_CHAR_DELETE, 1);
484 data << (uint8)CHAR_DELETE_FAILED_GUILD_LEADER;
485 SendPacket( &data );
486 return;
489 // is arena team captain
490 if(sObjectMgr.GetArenaTeamByCaptain(guid))
492 WorldPacket data(SMSG_CHAR_DELETE, 1);
493 data << (uint8)CHAR_DELETE_FAILED_ARENA_CAPTAIN;
494 SendPacket( &data );
495 return;
498 QueryResult *result = CharacterDatabase.PQuery("SELECT account,name FROM characters WHERE guid='%u'", GUID_LOPART(guid));
499 if(result)
501 Field *fields = result->Fetch();
502 accountId = fields[0].GetUInt32();
503 name = fields[1].GetCppString();
504 delete result;
507 // prevent deleting other players' characters using cheating tools
508 if(accountId != GetAccountId())
509 return;
511 std::string IP_str = GetRemoteAddress();
512 sLog.outBasic("Account: %d (IP: %s) Delete Character:[%s] (guid: %u)", GetAccountId(), IP_str.c_str(), name.c_str(), GUID_LOPART(guid));
513 sLog.outChar("Account: %d (IP: %s) Delete Character:[%s] (guid: %u)", GetAccountId(), IP_str.c_str(), name.c_str(), GUID_LOPART(guid));
515 if(sLog.IsOutCharDump()) // optimize GetPlayerDump call
517 std::string dump = PlayerDumpWriter().GetDump(GUID_LOPART(guid));
518 sLog.outCharDump(dump.c_str(), GetAccountId(), GUID_LOPART(guid), name.c_str());
521 Player::DeleteFromDB(guid, GetAccountId());
523 WorldPacket data(SMSG_CHAR_DELETE, 1);
524 data << (uint8)CHAR_DELETE_SUCCESS;
525 SendPacket( &data );
528 void WorldSession::HandlePlayerLoginOpcode( WorldPacket & recv_data )
530 if(PlayerLoading() || GetPlayer() != NULL)
532 sLog.outError("Player tryes to login again, AccountId = %d", GetAccountId());
533 return;
536 m_playerLoading = true;
537 uint64 playerGuid = 0;
539 DEBUG_LOG( "WORLD: Recvd Player Logon Message" );
541 recv_data >> playerGuid;
543 LoginQueryHolder *holder = new LoginQueryHolder(GetAccountId(), playerGuid);
544 if(!holder->Initialize())
546 delete holder; // delete all unprocessed queries
547 m_playerLoading = false;
548 return;
551 CharacterDatabase.DelayQueryHolder(&chrHandler, &CharacterHandler::HandlePlayerLoginCallback, holder);
554 void WorldSession::HandlePlayerLogin(LoginQueryHolder *holder)
556 uint64 playerGuid = holder->GetGuid();
558 Player *pCurrChar = new Player(this);
559 pCurrChar->GetMotionMaster()->Initialize();
561 // "GetAccountId()==db stored account id" checked in LoadFromDB (prevent login not own character using cheating tools)
562 if(!pCurrChar->LoadFromDB(GUID_LOPART(playerGuid), holder))
564 KickPlayer(); // disconnect client, player no set to session and it will not deleted or saved at kick
565 delete pCurrChar; // delete it manually
566 delete holder; // delete all unprocessed queries
567 m_playerLoading = false;
568 return;
571 SetPlayer(pCurrChar);
573 pCurrChar->SendDungeonDifficulty(false);
575 WorldPacket data( SMSG_LOGIN_VERIFY_WORLD, 20 );
576 data << pCurrChar->GetMapId();
577 data << pCurrChar->GetPositionX();
578 data << pCurrChar->GetPositionY();
579 data << pCurrChar->GetPositionZ();
580 data << pCurrChar->GetOrientation();
581 SendPacket(&data);
583 // load player specific part before send times
584 LoadAccountData(holder->GetResult(PLAYER_LOGIN_QUERY_LOADACCOUNTDATA),PER_CHARACTER_CACHE_MASK);
585 SendAccountDataTimes(PER_CHARACTER_CACHE_MASK);
587 data.Initialize(SMSG_FEATURE_SYSTEM_STATUS, 2); // added in 2.2.0
588 data << uint8(2); // unknown value
589 data << uint8(0); // enable(1)/disable(0) voice chat interface in client
590 SendPacket(&data);
592 // Send MOTD
594 data.Initialize(SMSG_MOTD, 50); // new in 2.0.1
595 data << (uint32)0;
597 uint32 linecount=0;
598 std::string str_motd = sWorld.GetMotd();
599 std::string::size_type pos, nextpos;
601 pos = 0;
602 while ( (nextpos= str_motd.find('@',pos)) != std::string::npos )
604 if (nextpos != pos)
606 data << str_motd.substr(pos, nextpos-pos);
607 ++linecount;
609 pos = nextpos + 1;
612 if (pos < str_motd.length())
614 data << str_motd.substr(pos);
615 ++linecount;
618 data.put(0, linecount);
620 SendPacket( &data );
621 DEBUG_LOG( "WORLD: Sent motd (SMSG_MOTD)" );
624 //QueryResult *result = CharacterDatabase.PQuery("SELECT guildid,rank FROM guild_member WHERE guid = '%u'",pCurrChar->GetGUIDLow());
625 QueryResult *resultGuild = holder->GetResult(PLAYER_LOGIN_QUERY_LOADGUILD);
627 if(resultGuild)
629 Field *fields = resultGuild->Fetch();
630 pCurrChar->SetInGuild(fields[0].GetUInt32());
631 pCurrChar->SetRank(fields[1].GetUInt32());
632 delete resultGuild;
634 else if(pCurrChar->GetGuildId()) // clear guild related fields in case wrong data about non existed membership
636 pCurrChar->SetInGuild(0);
637 pCurrChar->SetRank(0);
640 if(pCurrChar->GetGuildId() != 0)
642 Guild* guild = sObjectMgr.GetGuildById(pCurrChar->GetGuildId());
643 if(guild)
645 data.Initialize(SMSG_GUILD_EVENT, (2+guild->GetMOTD().size()+1));
646 data << uint8(GE_MOTD);
647 data << uint8(1);
648 data << guild->GetMOTD();
649 SendPacket(&data);
650 DEBUG_LOG( "WORLD: Sent guild-motd (SMSG_GUILD_EVENT)" );
652 guild->DisplayGuildBankTabsInfo(this);
654 data.Initialize(SMSG_GUILD_EVENT, (5+10)); // we guess size
655 data << uint8(GE_SIGNED_ON);
656 data << uint8(1);
657 data << pCurrChar->GetName();
658 data << pCurrChar->GetGUID();
659 guild->BroadcastPacket(&data);
660 DEBUG_LOG( "WORLD: Sent guild-signed-on (SMSG_GUILD_EVENT)" );
662 else
664 // remove wrong guild data
665 sLog.outError("Player %s (GUID: %u) marked as member not existed guild (id: %u), removing guild membership for player.",pCurrChar->GetName(),pCurrChar->GetGUIDLow(),pCurrChar->GetGuildId());
666 pCurrChar->SetInGuild(0);
670 data.Initialize(SMSG_LEARNED_DANCE_MOVES, 4+4);
671 data << uint32(0);
672 data << uint32(0);
673 SendPacket(&data);
675 pCurrChar->SendInitialPacketsBeforeAddToMap();
677 //Show cinematic at the first time that player login
678 if( !pCurrChar->getCinematic() )
680 pCurrChar->setCinematic(1);
682 if(ChrClassesEntry const* cEntry = sChrClassesStore.LookupEntry(pCurrChar->getClass()))
684 if (cEntry->CinematicSequence)
685 pCurrChar->SendCinematicStart(cEntry->CinematicSequence);
686 else if (ChrRacesEntry const* rEntry = sChrRacesStore.LookupEntry(pCurrChar->getRace()))
687 pCurrChar->SendCinematicStart(rEntry->CinematicSequence);
691 if (!pCurrChar->GetMap()->Add(pCurrChar))
693 AreaTrigger const* at = sObjectMgr.GetGoBackTrigger(pCurrChar->GetMapId());
694 if(at)
695 pCurrChar->TeleportTo(at->target_mapId, at->target_X, at->target_Y, at->target_Z, pCurrChar->GetOrientation());
696 else
697 pCurrChar->TeleportToHomebind();
700 sObjectAccessor.AddObject(pCurrChar);
701 //sLog.outDebug("Player %s added to Map.",pCurrChar->GetName());
703 pCurrChar->SendInitialPacketsAfterAddToMap();
705 CharacterDatabase.PExecute("UPDATE characters SET online = 1 WHERE guid = '%u'", pCurrChar->GetGUIDLow());
706 loginDatabase.PExecute("UPDATE account SET active_realm_id = %d WHERE id = '%u'", realmID, GetAccountId());
707 pCurrChar->SetInGameTime( getMSTime() );
709 // announce group about member online (must be after add to player list to receive announce to self)
710 if(Group *group = pCurrChar->GetGroup())
712 //pCurrChar->groupInfo.group->SendInit(this); // useless
713 group->SendUpdate();
716 // friend status
717 sSocialMgr.SendFriendStatus(pCurrChar, FRIEND_ONLINE, pCurrChar->GetGUIDLow(), true);
719 // Place character in world (and load zone) before some object loading
720 pCurrChar->LoadCorpse();
722 // setting Ghost+speed if dead
723 if (pCurrChar->m_deathState != ALIVE)
725 // not blizz like, we must correctly save and load player instead...
726 if(pCurrChar->getRace() == RACE_NIGHTELF)
727 pCurrChar->CastSpell(pCurrChar, 20584, true, 0);// auras SPELL_AURA_INCREASE_SPEED(+speed in wisp form), SPELL_AURA_INCREASE_SWIM_SPEED(+swim speed in wisp form), SPELL_AURA_TRANSFORM (to wisp form)
728 pCurrChar->CastSpell(pCurrChar, 8326, true, 0); // auras SPELL_AURA_GHOST, SPELL_AURA_INCREASE_SPEED(why?), SPELL_AURA_INCREASE_SWIM_SPEED(why?)
730 pCurrChar->SetMovement(MOVE_WATER_WALK);
733 pCurrChar->ContinueTaxiFlight();
735 // reset for all pets before pet loading
736 if(pCurrChar->HasAtLoginFlag(AT_LOGIN_RESET_PET_TALENTS))
737 Pet::resetTalentsForAllPetsOf(pCurrChar);
739 // Load pet if any (if player not alive and in taxi flight or another then pet will remember as temporary unsummoned)
740 pCurrChar->LoadPet();
742 // Set FFA PvP for non GM in non-rest mode
743 if(sWorld.IsFFAPvPRealm() && !pCurrChar->isGameMaster() && !pCurrChar->HasFlag(PLAYER_FLAGS,PLAYER_FLAGS_RESTING) )
744 pCurrChar->SetByteFlag(UNIT_FIELD_BYTES_2, 1, UNIT_BYTE2_FLAG_FFA_PVP);
746 if(pCurrChar->HasFlag(PLAYER_FLAGS, PLAYER_FLAGS_CONTESTED_PVP))
747 pCurrChar->SetContestedPvP();
749 // Apply at_login requests
750 if(pCurrChar->HasAtLoginFlag(AT_LOGIN_RESET_SPELLS))
752 pCurrChar->resetSpells();
753 SendNotification(LANG_RESET_SPELLS);
756 if(pCurrChar->HasAtLoginFlag(AT_LOGIN_RESET_TALENTS))
758 pCurrChar->resetTalents(true);
759 pCurrChar->SendTalentsInfoData(false); // original talents send already in to SendInitialPacketsBeforeAddToMap, resend reset state
760 SendNotification(LANG_RESET_TALENTS);
763 // show time before shutdown if shutdown planned.
764 if(sWorld.IsShutdowning())
765 sWorld.ShutdownMsg(true,pCurrChar);
767 if(sWorld.getConfig(CONFIG_ALL_TAXI_PATHS))
768 pCurrChar->SetTaxiCheater(true);
770 if(pCurrChar->isGameMaster())
771 SendNotification(LANG_GM_ON);
773 std::string IP_str = GetRemoteAddress();
774 sLog.outChar("Account: %d (IP: %s) Login Character:[%s] (guid:%u)",
775 GetAccountId(), IP_str.c_str(), pCurrChar->GetName(), pCurrChar->GetGUIDLow());
777 if(!pCurrChar->IsStandState() && !pCurrChar->hasUnitState(UNIT_STAT_STUNNED))
778 pCurrChar->SetStandState(UNIT_STAND_STATE_STAND);
780 m_playerLoading = false;
781 delete holder;
784 void WorldSession::HandleSetFactionAtWar( WorldPacket & recv_data )
786 DEBUG_LOG( "WORLD: Received CMSG_SET_FACTION_ATWAR" );
788 uint32 repListID;
789 uint8 flag;
791 recv_data >> repListID;
792 recv_data >> flag;
794 GetPlayer()->GetReputationMgr().SetAtWar(repListID, flag);
797 //I think this function is never used :/ I dunno, but i guess this opcode not exists
798 void WorldSession::HandleSetFactionCheat( WorldPacket & /*recv_data*/ )
800 sLog.outError("WORLD SESSION: HandleSetFactionCheat, not expected call, please report.");
802 uint32 FactionID;
803 uint32 Standing;
805 recv_data >> FactionID;
806 recv_data >> Standing;
808 std::list<struct Factions>::iterator itr;
810 for(itr = GetPlayer()->factions.begin(); itr != GetPlayer()->factions.end(); ++itr)
812 if(itr->ReputationListID == FactionID)
814 itr->Standing += Standing;
815 itr->Flags = (itr->Flags | 1);
816 break;
820 GetPlayer()->GetReputationMgr().SendStates();
823 void WorldSession::HandleMeetingStoneInfo( WorldPacket & /*recv_data*/ )
825 DEBUG_LOG( "WORLD: Received CMSG_MEETING_STONE_INFO" );
827 SendLfgUpdate(0, 0, 0);
830 void WorldSession::HandleTutorialFlag( WorldPacket & recv_data )
832 uint32 iFlag;
833 recv_data >> iFlag;
835 uint32 wInt = (iFlag / 32);
836 if (wInt >= 8)
838 //sLog.outError("CHEATER? Account:[%d] Guid[%u] tried to send wrong CMSG_TUTORIAL_FLAG", GetAccountId(),GetGUID());
839 return;
841 uint32 rInt = (iFlag % 32);
843 uint32 tutflag = GetTutorialInt( wInt );
844 tutflag |= (1 << rInt);
845 SetTutorialInt( wInt, tutflag );
847 //sLog.outDebug("Received Tutorial Flag Set {%u}.", iFlag);
850 void WorldSession::HandleTutorialClear( WorldPacket & /*recv_data*/ )
852 for (int i = 0; i < 8; ++i)
853 SetTutorialInt( i, 0xFFFFFFFF );
856 void WorldSession::HandleTutorialReset( WorldPacket & /*recv_data*/ )
858 for (int i = 0; i < 8; ++i)
859 SetTutorialInt( i, 0x00000000 );
862 void WorldSession::HandleSetWatchedFactionOpcode(WorldPacket & recv_data)
864 DEBUG_LOG("WORLD: Received CMSG_SET_WATCHED_FACTION");
865 uint32 fact;
866 recv_data >> fact;
867 GetPlayer()->SetUInt32Value(PLAYER_FIELD_WATCHED_FACTION_INDEX, fact);
870 void WorldSession::HandleSetFactionInactiveOpcode(WorldPacket & recv_data)
872 DEBUG_LOG("WORLD: Received CMSG_SET_FACTION_INACTIVE");
873 uint32 replistid;
874 uint8 inactive;
875 recv_data >> replistid >> inactive;
877 _player->GetReputationMgr().SetInactive(replistid, inactive);
880 void WorldSession::HandleShowingHelmOpcode( WorldPacket & /*recv_data*/ )
882 DEBUG_LOG("CMSG_SHOWING_HELM for %s", _player->GetName());
883 _player->ToggleFlag(PLAYER_FLAGS, PLAYER_FLAGS_HIDE_HELM);
886 void WorldSession::HandleShowingCloakOpcode( WorldPacket & /*recv_data*/ )
888 DEBUG_LOG("CMSG_SHOWING_CLOAK for %s", _player->GetName());
889 _player->ToggleFlag(PLAYER_FLAGS, PLAYER_FLAGS_HIDE_CLOAK);
892 void WorldSession::HandleCharRenameOpcode(WorldPacket& recv_data)
894 uint64 guid;
895 std::string newname;
897 recv_data >> guid;
898 recv_data >> newname;
900 // prevent character rename to invalid name
901 if (!normalizePlayerName(newname))
903 WorldPacket data(SMSG_CHAR_RENAME, 1);
904 data << uint8(CHAR_NAME_NO_NAME);
905 SendPacket( &data );
906 return;
909 uint8 res = ObjectMgr::CheckPlayerName(newname,true);
910 if (res != CHAR_NAME_SUCCESS)
912 WorldPacket data(SMSG_CHAR_RENAME, 1);
913 data << uint8(res);
914 SendPacket( &data );
915 return;
918 // check name limitations
919 if (GetSecurity() == SEC_PLAYER && sObjectMgr.IsReservedName(newname))
921 WorldPacket data(SMSG_CHAR_RENAME, 1);
922 data << uint8(CHAR_NAME_RESERVED);
923 SendPacket( &data );
924 return;
927 std::string escaped_newname = newname;
928 CharacterDatabase.escape_string(escaped_newname);
930 // make sure that the character belongs to the current account, that rename at login is enabled
931 // and that there is no character with the desired new name
932 CharacterDatabase.AsyncPQuery(&WorldSession::HandleChangePlayerNameOpcodeCallBack,
933 GetAccountId(), newname,
934 "SELECT guid, name FROM characters WHERE guid = %d AND account = %d AND (at_login & %d) = %d AND NOT EXISTS (SELECT NULL FROM characters WHERE name = '%s')",
935 GUID_LOPART(guid), GetAccountId(), AT_LOGIN_RENAME, AT_LOGIN_RENAME, escaped_newname.c_str()
939 void WorldSession::HandleChangePlayerNameOpcodeCallBack(QueryResult *result, uint32 accountId, std::string newname)
941 WorldSession * session = sWorld.FindSession(accountId);
942 if(!session)
944 if(result) delete result;
945 return;
948 if (!result)
950 WorldPacket data(SMSG_CHAR_RENAME, 1);
951 data << uint8(CHAR_CREATE_ERROR);
952 session->SendPacket( &data );
953 return;
956 uint32 guidLow = result->Fetch()[0].GetUInt32();
957 uint64 guid = MAKE_NEW_GUID(guidLow, 0, HIGHGUID_PLAYER);
958 std::string oldname = result->Fetch()[1].GetCppString();
960 delete result;
962 CharacterDatabase.PExecute("UPDATE characters set name = '%s', at_login = at_login & ~ %u WHERE guid ='%u'", newname.c_str(), uint32(AT_LOGIN_RENAME), guidLow);
963 CharacterDatabase.PExecute("DELETE FROM character_declinedname WHERE guid ='%u'", guidLow);
965 sLog.outChar("Account: %d (IP: %s) Character:[%s] (guid:%u) Changed name to: %s", session->GetAccountId(), session->GetRemoteAddress().c_str(), oldname.c_str(), guidLow, newname.c_str());
967 WorldPacket data(SMSG_CHAR_RENAME, 1+8+(newname.size()+1));
968 data << uint8(RESPONSE_SUCCESS);
969 data << uint64(guid);
970 data << newname;
971 session->SendPacket(&data);
974 void WorldSession::HandleSetPlayerDeclinedNames(WorldPacket& recv_data)
976 uint64 guid;
978 recv_data >> guid;
980 // not accept declined names for unsupported languages
981 std::string name;
982 if(!sObjectMgr.GetPlayerNameByGUID(guid, name))
984 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
985 data << uint32(1);
986 data << uint64(guid);
987 SendPacket(&data);
988 return;
991 std::wstring wname;
992 if(!Utf8toWStr(name, wname))
994 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
995 data << uint32(1);
996 data << uint64(guid);
997 SendPacket(&data);
998 return;
1001 if(!isCyrillicCharacter(wname[0])) // name already stored as only single alphabet using
1003 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
1004 data << uint32(1);
1005 data << uint64(guid);
1006 SendPacket(&data);
1007 return;
1010 std::string name2;
1011 DeclinedName declinedname;
1013 recv_data >> name2;
1015 if(name2 != name) // character have different name
1017 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
1018 data << uint32(1);
1019 data << uint64(guid);
1020 SendPacket(&data);
1021 return;
1024 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
1026 recv_data >> declinedname.name[i];
1027 if(!normalizePlayerName(declinedname.name[i]))
1029 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
1030 data << uint32(1);
1031 data << uint64(guid);
1032 SendPacket(&data);
1033 return;
1037 if(!ObjectMgr::CheckDeclinedNames(GetMainPartOfName(wname, 0), declinedname))
1039 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
1040 data << uint32(1);
1041 data << uint64(guid);
1042 SendPacket(&data);
1043 return;
1046 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
1047 CharacterDatabase.escape_string(declinedname.name[i]);
1049 CharacterDatabase.BeginTransaction();
1050 CharacterDatabase.PExecute("DELETE FROM character_declinedname WHERE guid = '%u'", GUID_LOPART(guid));
1051 CharacterDatabase.PExecute("INSERT INTO character_declinedname (guid, genitive, dative, accusative, instrumental, prepositional) VALUES ('%u','%s','%s','%s','%s','%s')",
1052 GUID_LOPART(guid), declinedname.name[0].c_str(), declinedname.name[1].c_str(), declinedname.name[2].c_str(), declinedname.name[3].c_str(), declinedname.name[4].c_str());
1053 CharacterDatabase.CommitTransaction();
1055 WorldPacket data(SMSG_SET_PLAYER_DECLINED_NAMES_RESULT, 4+8);
1056 data << uint32(0); // OK
1057 data << uint64(guid);
1058 SendPacket(&data);
1061 void WorldSession::HandleAlterAppearance( WorldPacket & recv_data )
1063 sLog.outDebug("CMSG_ALTER_APPEARANCE");
1065 uint32 Hair, Color, FacialHair;
1066 recv_data >> Hair >> Color >> FacialHair;
1068 BarberShopStyleEntry const* bs_hair = sBarberShopStyleStore.LookupEntry(Hair);
1070 if(!bs_hair || bs_hair->type != 0 || bs_hair->race != _player->getRace() || bs_hair->gender != _player->getGender())
1071 return;
1073 BarberShopStyleEntry const* bs_facialHair = sBarberShopStyleStore.LookupEntry(FacialHair);
1075 if(!bs_facialHair || bs_facialHair->type != 2 || bs_facialHair->race != _player->getRace() || bs_facialHair->gender != _player->getGender())
1076 return;
1078 uint32 Cost = _player->GetBarberShopCost(bs_hair->hair_id, Color, bs_facialHair->hair_id);
1080 // 0 - ok
1081 // 1,3 - not enough money
1082 // 2 - you have to seat on barber chair
1083 if(_player->GetMoney() < Cost)
1085 WorldPacket data(SMSG_BARBER_SHOP_RESULT, 4);
1086 data << uint32(1); // no money
1087 SendPacket(&data);
1088 return;
1090 else
1092 WorldPacket data(SMSG_BARBER_SHOP_RESULT, 4);
1093 data << uint32(0); // ok
1094 SendPacket(&data);
1097 _player->ModifyMoney(-int32(Cost)); // it isn't free
1098 _player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_GOLD_SPENT_AT_BARBER, Cost);
1100 _player->SetByteValue(PLAYER_BYTES, 2, uint8(bs_hair->hair_id));
1101 _player->SetByteValue(PLAYER_BYTES, 3, uint8(Color));
1102 _player->SetByteValue(PLAYER_BYTES_2, 0, uint8(bs_facialHair->hair_id));
1104 _player->GetAchievementMgr().UpdateAchievementCriteria(ACHIEVEMENT_CRITERIA_TYPE_VISIT_BARBER_SHOP, 1);
1106 _player->SetStandState(0); // stand up
1109 void WorldSession::HandleRemoveGlyph( WorldPacket & recv_data )
1111 uint32 slot;
1112 recv_data >> slot;
1114 if(slot >= MAX_GLYPH_SLOT_INDEX)
1116 sLog.outDebug("Client sent wrong glyph slot number in opcode CMSG_REMOVE_GLYPH %u", slot);
1117 return;
1120 if(uint32 glyph = _player->GetGlyph(slot))
1122 if(GlyphPropertiesEntry const *gp = sGlyphPropertiesStore.LookupEntry(glyph))
1124 _player->RemoveAurasDueToSpell(gp->SpellId);
1125 _player->SetGlyph(slot, 0);
1126 _player->SendTalentsInfoData(false);
1131 void WorldSession::HandleCharCustomize(WorldPacket& recv_data)
1133 uint64 guid;
1134 std::string newname;
1136 recv_data >> guid;
1137 recv_data >> newname;
1139 uint8 gender, skin, face, hairStyle, hairColor, facialHair;
1140 recv_data >> gender >> skin >> hairColor >> hairStyle >> facialHair >> face;
1142 QueryResult *result = CharacterDatabase.PQuery("SELECT at_login FROM characters WHERE guid ='%u'", GUID_LOPART(guid));
1143 if (!result)
1145 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1146 data << uint8(CHAR_CREATE_ERROR);
1147 SendPacket( &data );
1148 return;
1151 Field *fields = result->Fetch();
1152 uint32 at_loginFlags = fields[0].GetUInt32();
1153 delete result;
1155 if (!(at_loginFlags & AT_LOGIN_CUSTOMIZE))
1157 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1158 data << uint8(CHAR_CREATE_ERROR);
1159 SendPacket( &data );
1160 return;
1163 // prevent character rename to invalid name
1164 if (!normalizePlayerName(newname))
1166 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1167 data << uint8(CHAR_NAME_NO_NAME);
1168 SendPacket( &data );
1169 return;
1172 uint8 res = ObjectMgr::CheckPlayerName(newname,true);
1173 if (res != CHAR_NAME_SUCCESS)
1175 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1176 data << uint8(res);
1177 SendPacket( &data );
1178 return;
1181 // check name limitations
1182 if (GetSecurity() == SEC_PLAYER && sObjectMgr.IsReservedName(newname))
1184 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1185 data << uint8(CHAR_NAME_RESERVED);
1186 SendPacket( &data );
1187 return;
1190 // character with this name already exist
1191 if (uint64 newguid = sObjectMgr.GetPlayerGUIDByName(newname))
1193 if (newguid != guid)
1195 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1);
1196 data << uint8(CHAR_CREATE_NAME_IN_USE);
1197 SendPacket( &data );
1198 return;
1202 CharacterDatabase.escape_string(newname);
1203 Player::Customize(guid, gender, skin, face, hairStyle, hairColor, facialHair);
1204 CharacterDatabase.PExecute("UPDATE characters set name = '%s', at_login = at_login & ~ %u WHERE guid ='%u'", newname.c_str(), uint32(AT_LOGIN_CUSTOMIZE), GUID_LOPART(guid));
1205 CharacterDatabase.PExecute("DELETE FROM character_declinedname WHERE guid ='%u'", GUID_LOPART(guid));
1207 std::string IP_str = GetRemoteAddress();
1208 sLog.outChar("Account: %d (IP: %s), Character guid: %u Customized to: %s", GetAccountId(), IP_str.c_str(), GUID_LOPART(guid), newname.c_str());
1210 WorldPacket data(SMSG_CHAR_CUSTOMIZE, 1+8+(newname.size()+1)+6);
1211 data << uint8(RESPONSE_SUCCESS);
1212 data << uint64(guid);
1213 data << newname;
1214 data << uint8(gender);
1215 data << uint8(skin);
1216 data << uint8(face);
1217 data << uint8(hairStyle);
1218 data << uint8(hairColor);
1219 data << uint8(facialHair);
1220 SendPacket(&data);
1223 void WorldSession::HandleEquipmentSetSave(WorldPacket &recv_data)
1225 sLog.outDebug("CMSG_EQUIPMENT_SET_SAVE");
1227 uint64 setGuid;
1228 if(!recv_data.readPackGUID(setGuid))
1229 return;
1231 uint32 index;
1232 recv_data >> index;
1233 if(index >= MAX_EQUIPMENT_SET_INDEX) // client set slots amount
1234 return;
1236 std::string name;
1237 recv_data >> name;
1239 std::string iconName;
1240 recv_data >> iconName;
1242 EquipmentSet eqSet;
1244 eqSet.Guid = setGuid;
1245 eqSet.Name = name;
1246 eqSet.IconName = iconName;
1247 eqSet.state = EQUIPMENT_SET_NEW;
1249 for(uint32 i = 0; i < EQUIPMENT_SLOT_END; ++i)
1251 uint64 itemGuid;
1252 if(!recv_data.readPackGUID(itemGuid))
1253 return;
1255 Item *item = _player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
1257 if(!item && itemGuid) // cheating check 1
1258 return;
1260 if(item && item->GetGUID() != itemGuid) // cheating check 2
1261 return;
1263 eqSet.Items[i] = GUID_LOPART(itemGuid);
1266 _player->SetEquipmentSet(index, eqSet);
1269 void WorldSession::HandleEquipmentSetDelete(WorldPacket &recv_data)
1271 sLog.outDebug("CMSG_EQUIPMENT_SET_DELETE");
1273 uint64 setGuid;
1274 if(!recv_data.readPackGUID(setGuid))
1275 return;
1277 _player->DeleteEquipmentSet(setGuid);
1280 void WorldSession::HandleEquipmentSetUse(WorldPacket &recv_data)
1282 sLog.outDebug("CMSG_EQUIPMENT_SET_USE");
1283 recv_data.hexlike();
1285 for(uint32 i = 0; i < EQUIPMENT_SLOT_END; ++i)
1287 uint64 itemGuid;
1288 if(!recv_data.readPackGUID(itemGuid))
1289 return;
1291 uint8 srcbag, srcslot;
1292 recv_data >> srcbag >> srcslot;
1294 sLog.outDebug("Item " I64FMT ": srcbag %u, srcslot %u", itemGuid, srcbag, srcslot);
1296 Item *item = _player->GetItemByGuid(itemGuid);
1298 uint16 dstpos = i | (INVENTORY_SLOT_BAG_0 << 8);
1300 if(!item)
1302 Item *uItem = _player->GetItemByPos(INVENTORY_SLOT_BAG_0, i);
1303 if(!uItem)
1304 continue;
1306 ItemPosCountVec sDest;
1307 uint8 msg = _player->CanStoreItem( NULL_BAG, NULL_SLOT, sDest, uItem, false );
1308 if(msg == EQUIP_ERR_OK)
1310 _player->RemoveItem(INVENTORY_SLOT_BAG_0, i, true);
1311 _player->StoreItem( sDest, uItem, true );
1313 else
1314 _player->SendEquipError(msg, uItem, NULL);
1316 continue;
1319 if(item->GetPos() == dstpos)
1320 continue;
1322 _player->SwapItem(item->GetPos(), dstpos);
1325 WorldPacket data(SMSG_EQUIPMENT_SET_USE_RESULT, 1);
1326 data << uint8(0); // 4 - equipment swap failed - inventory is full
1327 SendPacket(&data);