From f2852a95b71500c7f8c88ad773375c0675a82b88 Mon Sep 17 00:00:00 2001 From: hunuza Date: Sun, 2 Nov 2008 15:58:24 +0100 Subject: [PATCH] Removed some unnecessary database queries. Removed unnecessary database queries in some command handlers. Replaced them with access to cached data or queries for only the needed data. Move database access in gossip select code to less often called place. --- src/game/Creature.cpp | 14 ++++---- src/game/Level2.cpp | 29 ++++++++++++---- src/game/Level3.cpp | 81 ++++++++++++++++++++++--------------------- 3 files changed, 73 insertions(+), 51 deletions(-) diff --git a/src/game/Creature.cpp b/src/game/Creature.cpp index ce5d9f797..899a7abed 100644 --- a/src/game/Creature.cpp +++ b/src/game/Creature.cpp @@ -818,18 +818,20 @@ void Creature::OnGossipSelect(Player* player, uint32 option) return; } - uint32 textid=GetGossipTextId( action, zoneid); - if(textid==0) - textid=GetNpcTextId(); - switch (gossip->Action) { case GOSSIP_OPTION_GOSSIP: + { + uint32 textid = GetGossipTextId(action, zoneid); + if (textid == 0) + textid=GetNpcTextId(); + player->PlayerTalkClass->CloseGossip(); - player->PlayerTalkClass->SendTalking( textid ); + player->PlayerTalkClass->SendTalking(textid); break; + } case GOSSIP_OPTION_SPIRITHEALER: - if( player->isDead() ) + if (player->isDead()) CastSpell(this,17251,true,NULL,NULL,player->GetGUID()); break; case GOSSIP_OPTION_QUESTGIVER: diff --git a/src/game/Level2.cpp b/src/game/Level2.cpp index fb07a793f..55f8f25ab 100644 --- a/src/game/Level2.cpp +++ b/src/game/Level2.cpp @@ -1799,13 +1799,30 @@ bool ChatHandler::HandlePInfoCommand(const char* args) // get additional information from DB else { + // 0 + QueryResult *result = CharacterDatabase.PQuery("SELECT totaltime FROM characters WHERE guid = '%u'", targetGUID); + if (!result) + { + SendSysMessage(LANG_PLAYER_NOT_FOUND); + SetSentErrorMessage(true); + return false; + } + Field *fields = result->Fetch(); + total_player_time = fields[0].GetUInt32(); + delete result; + + Tokens data; + if (!Player::LoadValuesArrayFromDB(data,targetGUID)) + { + SendSysMessage(LANG_PLAYER_NOT_FOUND); + SetSentErrorMessage(true); + return false; + } + + money = Player::GetUInt32ValueFromArray(data, PLAYER_FIELD_COINAGE); + level = Player::GetUInt32ValueFromArray(data, UNIT_FIELD_LEVEL); + accId = objmgr.GetPlayerAccountIdByGUID(targetGUID); - WorldSession session(0,NULL,SEC_PLAYER,0,0,LOCALE_enUS); - Player plr(&session); // use fake session for temporary load - plr.MinimalLoadFromDB(NULL, targetGUID); - money = plr.GetMoney(); - total_player_time = plr.GetTotalPlayedTime(); - level = plr.getLevel(); } std::string username = GetMangosString(LANG_ERROR); diff --git a/src/game/Level3.cpp b/src/game/Level3.cpp index ad5e93558..ed8fccf03 100644 --- a/src/game/Level3.cpp +++ b/src/game/Level3.cpp @@ -1988,9 +1988,39 @@ bool ChatHandler::HandleAddItemSetCommand(const char* args) sLog.outDetail(GetMangosString(LANG_ADDITEMSET), itemsetId); - QueryResult *result = WorldDatabase.PQuery("SELECT entry FROM item_template WHERE itemset = %u",itemsetId); + bool found = false; + for (uint32 id = 0; id < sItemStorage.MaxEntry; id++) + { + ItemPrototype const *pProto = sItemStorage.LookupEntry(id); + if (!pProto) + continue; - if(!result) + if (pProto->ItemSet == itemsetId) + { + found = true; + ItemPosCountVec dest; + uint8 msg = plTarget->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, pProto->ItemId, 1 ); + if (msg == EQUIP_ERR_OK) + { + Item* item = plTarget->StoreNewItem( dest, pProto->ItemId, true); + + // remove binding (let GM give it to another player later) + if (pl==plTarget) + item->SetBinding( false ); + + pl->SendNewItem(item,1,false,true); + if (pl!=plTarget) + plTarget->SendNewItem(item,1,true,false); + } + else + { + pl->SendEquipError( msg, NULL, NULL ); + PSendSysMessage(LANG_ITEM_CANNOT_CREATE, pProto->ItemId, 1); + } + } + } + + if (!found) { PSendSysMessage(LANG_NO_ITEMS_FROM_ITEMSET_FOUND,itemsetId); @@ -1998,35 +2028,6 @@ bool ChatHandler::HandleAddItemSetCommand(const char* args) return false; } - do - { - Field *fields = result->Fetch(); - uint32 itemId = fields[0].GetUInt32(); - - ItemPosCountVec dest; - uint8 msg = plTarget->CanStoreNewItem( NULL_BAG, NULL_SLOT, dest, itemId, 1 ); - if( msg == EQUIP_ERR_OK ) - { - Item* item = plTarget->StoreNewItem( dest, itemId, true); - - // remove binding (let GM give it to another player later) - if(pl==plTarget) - item->SetBinding( false ); - - pl->SendNewItem(item,1,false,true); - if(pl!=plTarget) - plTarget->SendNewItem(item,1,true,false); - } - else - { - pl->SendEquipError( msg, NULL, NULL ); - PSendSysMessage(LANG_ITEM_CANNOT_CREATE, itemId, 1); - } - - }while( result->NextRow() ); - - delete result; - return true; } @@ -4678,16 +4679,18 @@ bool ChatHandler::HandleAddQuest(const char* args) } // check item starting quest (it can work incorrectly if added without item in inventory) - QueryResult *result = WorldDatabase.PQuery("SELECT entry FROM item_template WHERE startquest = '%u' LIMIT 1",entry); - if(result) + for (uint32 id = 0; id < sItemStorage.MaxEntry; id++) { - Field* fields = result->Fetch(); - uint32 item_id = fields[0].GetUInt32(); - delete result; + ItemPrototype const *pProto = sItemStorage.LookupEntry(id); + if (!pProto) + continue; - PSendSysMessage(LANG_COMMAND_QUEST_STARTFROMITEM, entry,item_id); - SetSentErrorMessage(true); - return false; + if (pProto->StartQuest == entry) + { + PSendSysMessage(LANG_COMMAND_QUEST_STARTFROMITEM, entry, pProto->ItemId); + SetSentErrorMessage(true); + return false; + } } // ok, normal (creature/GO starting) quest