From c61c671ecc2bd9e4eaed0191db259a25216bfbc2 Mon Sep 17 00:00:00 2001 From: mikx Date: Mon, 13 Nov 2023 19:25:12 -0500 Subject: [PATCH] mod-mxwow-token NPC Scripted --- modules/mod-mxwow-token/src/mxwow_token.cpp | 172 +++++++++++++++++++- 1 file changed, 170 insertions(+), 2 deletions(-) diff --git a/modules/mod-mxwow-token/src/mxwow_token.cpp b/modules/mod-mxwow-token/src/mxwow_token.cpp index 641cc00..0c0f633 100644 --- a/modules/mod-mxwow-token/src/mxwow_token.cpp +++ b/modules/mod-mxwow-token/src/mxwow_token.cpp @@ -7,6 +7,7 @@ #include "ScriptMgr.h" #include "Player.h" #include "Chat.h" +#include class mxwow_token : public PlayerScript { @@ -55,7 +56,7 @@ public: GiveToken(player, tokenQtyDungeonNormal); } } - } + } } void GiveToken(const Player* player, const uint32 qty) @@ -70,13 +71,180 @@ public: tQty = (*queryPlayerTokenQty)[1].Get(); ntQty = tQty + qty; LoginDatabase.Execute("UPDATE mxw_account_token SET token='{}' WHERE aid='{}'", ntQty, aId); - ss << "|cffabeeff[MxW] Vous obtenez |cff00ff00%i|cffabeeff MxWToken. Vous avez un total de |cff00ff00%i|cffabeeff MxWToken."; + ss << "|cffabeeff[MxW] Vous obtenez |cff00ff00%i|cffabeeff MxWToken. Vous en avez un total de |cff00ff00%i|cffabeeff."; ChatHandler(player->GetSession()).PSendSysMessage(ss.str().c_str(), qty, ntQty); } } }; +class mxwow_tokenCreature : public CreatureScript +{ +public: + + mxwow_tokenCreature() : CreatureScript("mxwow_tokenCreature") { } + + bool OnGossipHello(Player* player, Creature* creature) override + { + if (!sConfigMgr->GetOption("MxWoW_Token.Enabled", true)) + return false; + + AddGossipItemFor(player, 10, GetItemIcon(44974, 30, 30, -18, 0) + "Mascotte(s)", GOSSIP_SENDER_MAIN, 1); + AddGossipItemFor(player, 10, GetItemIcon(41508, 30, 30, -18, 0) + "Monture(s)", GOSSIP_SENDER_MAIN, 2); + //AddGossipItemFor(player, 10, GetItemIcon(45978, 30, 30, -18, 0) + "Héritage(s)", GOSSIP_SENDER_MAIN, 3); + //AddGossipItemFor(player, 10, GetItemIcon(34803, 30, 30, -18, 0) + "Service(s)", GOSSIP_SENDER_MAIN, 4); + + SendGossipMenuFor(player, 20000000, creature->GetGUID()); + + return true; + } + + bool OnGossipSelect(Player* player, Creature* creature, uint32 page, uint32 action) override + { + player->PlayerTalkClass->ClearMenus(); + + if (action < 10) { + ShowVendor(player, creature, action); + return true; + } + else if (action == 10) { + OnGossipHello(player, creature); + return true; + } + else if (action > 10) { + uint32 price = GetPrice(action); + if (CheckToken(player, price)) { + AdjustToken(player, price, false); + ItemPosCountVec dest; + InventoryResult msg = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, action, 1); + if (msg == EQUIP_ERR_OK) + { + Item* item = player->StoreNewItem(dest, action, true); + player->SendNewItem(item, 1, true, false); + } + OnGossipHello(player, creature); + } + else { + OnGossipHello(player, creature); + } + return true; + } + } + + bool CheckToken(Player* player, uint32 qty) + { + std::ostringstream ss; + uint32 aId = player->GetSession()->GetAccountId(); + uint32 tQty = 0; + uint32 ntQty = 0; + QueryResult queryPlayerTokenQty = LoginDatabase.Query("SELECT * FROM mxw_account_token WHERE aid = {}", aId); + if (queryPlayerTokenQty) + { + tQty = (*queryPlayerTokenQty)[1].Get(); + if (tQty >= qty) { + ntQty = tQty - qty; + ss << "|cffabeeff[MxW] Vous dépensez |cff00ff00%i|cffabeeff MxWToken. Il vous en reste un total de |cff00ff00%i|cffabeeff."; + ChatHandler(player->GetSession()).PSendSysMessage(ss.str().c_str(), qty, ntQty); + return true; + } + else { + ss << "|cffabeeff[MxW] Vous n'avez pas assez de MxWToken. Vous en avez un total de |cff00ff00%i|cffabeeff."; + ChatHandler(player->GetSession()).PSendSysMessage(ss.str().c_str(), tQty); + return false; + } + } + } + + void AdjustToken(Player* player, uint32 qty, bool augment) + { + std::ostringstream ss; + uint32 aId = player->GetSession()->GetAccountId(); + uint32 tQty = 0; + uint32 ntQty = 0; + QueryResult queryPlayerTokenQty = LoginDatabase.Query("SELECT * FROM mxw_account_token WHERE aid = {}", aId); + if (queryPlayerTokenQty) + { + tQty = (*queryPlayerTokenQty)[1].Get(); + if (augment) { + ntQty = tQty + qty; + } + else { + ntQty = tQty - qty; + } + LoginDatabase.Execute("UPDATE mxw_account_token SET token='{}' WHERE aid='{}'", ntQty, aId); + } + } + + uint32 GetPrice(uint32 entry) + { + QueryResult queryEntryPrice = LoginDatabase.Query("SELECT * FROM mxw_vendor_token WHERE entry = {}", entry); + if (queryEntryPrice) { + return (*queryEntryPrice)[2].Get(); + } + } + + void ShowVendor(Player* player, Creature* creature, uint32 type) + { + uint32 vc = 0; + QueryResult queryTypeCount = LoginDatabase.Query("SELECT * FROM mxw_vendor_token WHERE type = {}", type); + if (queryTypeCount) { + vc = queryTypeCount->GetRowCount(); + } + WorldSession* session = player->GetSession(); + std::string query = "SELECT * FROM mxw_vendor_token WHERE type = " + std::to_string(type); + session->GetQueryProcessor().AddCallback(LoginDatabase.AsyncQuery(query).WithCallback([=, this](QueryResult result) + { + uint32 startValue = 0; + uint32 endValue = vc; + std::map entryToAmountMap; + std::vector itemEntries; + if (result) { + do { + uint32 itemEntry = (*result)[0].Get(); + AddGossipItemFor(player, 10, GetItemIcon(itemEntry, 30, 30, -18, 0) + GetItemLink(itemEntry, session) + " " + std::to_string((*result)[2].Get()), GOSSIP_SENDER_MAIN, itemEntry); + } while (result->NextRow()); + } + AddGossipItemFor(player, 10, "|TInterface/ICONS/Ability_Spy:30:30:-18:0|tRetour...", GOSSIP_SENDER_MAIN, 10); + SendGossipMenuFor(player, 20000000, creature->GetGUID()); + })); + } + + std::string GetItemLink(uint32 entry, WorldSession* session) const + { + int loc_idx = session->GetSessionDbLocaleIndex(); + const ItemTemplate* temp = sObjectMgr->GetItemTemplate(entry); + std::string name = temp->Name1; + if (ItemLocale const* il = sObjectMgr->GetItemLocale(temp->ItemId)) + ObjectMgr::GetLocaleString(il->Name, loc_idx, name); + + std::ostringstream oss; + oss << "|c" << std::hex << ItemQualityColors[temp->Quality] << std::dec << + "|Hitem:" << temp->ItemId << ":" << + (uint32)0 << "|h[" << name << "]|h|r"; + + return oss.str(); + } + + std::string GetItemIcon(uint32 entry, uint32 width, uint32 height, int x, int y) const + { + std::ostringstream ss; + ss << "|TInterface"; + const ItemTemplate* temp = sObjectMgr->GetItemTemplate(entry); + const ItemDisplayInfoEntry* dispInfo = NULL; + if (temp) + { + dispInfo = sItemDisplayInfoStore.LookupEntry(temp->DisplayInfoID); + if (dispInfo) + ss << "/ICONS/" << dispInfo->inventoryIcon; + } + if (!dispInfo) + ss << "/InventoryItems/WoWUnknownItem01"; + ss << ":" << width << ":" << height << ":" << x << ":" << y << "|t"; + return ss.str(); + } +}; + void AddMxWoWTokenScripts() { new mxwow_token(); + new mxwow_tokenCreature(); }