Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "ObjectMgr.h"
- #include "ScriptMgr.h"
- #include "ScriptedCreature.h"
- #include <string>
- #include <cstring>
- #include <array>
- using namespace std;
- class ItemRedeem : public CreatureScript
- {
- public:
- ItemRedeem() : CreatureScript("ItemRedeem"){}
- bool OnGossipHello(Player * pPlayer, Creature * pCreature)
- {
- pPlayer->ADD_GOSSIP_ITEM_EXTENDED(4, "I want to enter the code", GOSSIP_SENDER_MAIN, GOSSIP_ACTION_INFO_DEF+1,"", 0, true);
- pPlayer->ADD_GOSSIP_ITEM(4, "Nevermind", GOSSIP_SENDER_MAIN, 2);
- pPlayer->PlayerTalkClass->SendGossipMenu(9425, pCreature->GetGUID());
- return true;
- }
- void CreateItemAndMailToPlayer(Player *pPlayer, uint32 itemId)
- {
- Item *pItem = new Item();
- if(pItem->Create(sObjectMgr->GenerateLowGuid(HIGHGUID_ITEM), itemId, pPlayer) == false)
- return;
- MailSender toSend(MAIL_NORMAL, pPlayer->GetGUIDLow(), MAIL_STATIONERY_GM);
- SQLTransaction trans = CharacterDatabase.BeginTransaction();
- pItem->SaveToDB(trans);
- MailDraft mailDraft("Lottery", "Your Reward");
- mailDraft.AddItem(pItem);
- mailDraft.SendMailTo(trans, MailReceiver(pPlayer), toSend);
- CharacterDatabase.CommitTransaction(trans);
- }
- bool CheckCode(Player * player, const char* sCode)
- {
- QueryResult res = CharacterDatabase.PQuery("SELECT reward,quantity FROM lottery WHERE code ='%s' AND used = 1 LIMIT 1", sCode);
- if(res)
- {
- Field * fields = res->Fetch();
- uint32 itemid = fields[0].GetUInt32();
- uint32 quantity = fields[1].GetUInt32();
- ItemPosCountVec dest;
- uint8 canStoreNewItem = player->CanStoreNewItem(NULL_BAG, NULL_SLOT, dest, itemid, quantity);
- if(canStoreNewItem == EQUIP_ERR_OK)
- {
- Item *newItem = NULL;
- newItem = player->StoreNewItem(dest,itemid,quantity,true);
- player->SendNewItem(newItem,quantity,true,false);
- }
- else
- {
- CreateItemAndMailToPlayer(player, itemid);
- }
- return true;
- }
- else
- {
- return false;
- }
- }
- bool OnGossipSelectCode(Player* player, Creature* creature, uint32 sender, uint32 action, char const* code)
- {
- player->PlayerTalkClass->ClearMenus();
- if (sender == GOSSIP_SENDER_MAIN)
- {
- switch (action)
- {
- case GOSSIP_ACTION_INFO_DEF+1:
- if (CheckCode(player,code))
- {
- printf("Entered code %s", code);
- CharacterDatabase.PQuery("UPDATE lottery SET used = 0 WHERE code ='%s'", code);
- }
- else
- {
- player->MonsterWhisper("Wrong code sorry!", player->GetGUID());
- }
- player->CLOSE_GOSSIP_MENU();
- return true;
- }
- }
- return false;
- }
- };
- void ADDSC_ItemRedeem_mob()
- {
- new ItemRedeem);
- }
Advertisement
Add Comment
Please, Sign In to add comment