コード例 #1
0
ファイル: cs_send.cpp プロジェクト: mynew2/trinitycore-1
    // Send items by mail
    static bool HandleSendItemsCommand(ChatHandler* handler, char const* args)
    {
        // format: name "subject text" "mail text" item1[:count1] item2[:count2] ... item12[:count12]
        Player* receiver;
        uint64 receiverGuid;
        std::string receiverName;
        if (!handler->extractPlayerTarget((char*)args, &receiver, &receiverGuid, &receiverName))
            return false;

        char* tail1 = strtok(NULL, "");
        if (!tail1)
            return false;

        char const* msgSubject = handler->extractQuotedArg(tail1);
        if (!msgSubject)
            return false;

        char* tail2 = strtok(NULL, "");
        if (!tail2)
            return false;

        char const* msgText = handler->extractQuotedArg(tail2);
        if (!msgText)
            return false;

        // msgSubject, msgText isn't NUL after prev. check
        std::string subject = msgSubject;
        std::string text    = msgText;

        // extract items
        typedef std::pair<uint32, uint32> ItemPair;
        typedef std::list< ItemPair > ItemPairs;
        ItemPairs items;

        // get all tail string
        char* tail = strtok(NULL, "");

        // get from tail next item str
        while (char* itemStr = strtok(tail, " "))
        {
            // and get new tail
            tail = strtok(NULL, "");

            // parse item str
            char const* itemIdStr = strtok(itemStr, ":");
            char const* itemCountStr = strtok(NULL, " ");

            uint32 itemId = atoi(itemIdStr);
            if (!itemId)
                return false;

            ItemTemplate const* item_proto = sObjectMgr->GetItemTemplate(itemId);
            if (!item_proto)
            {
                handler->PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, itemId);
                handler->SetSentErrorMessage(true);
                return false;
            }

            uint32 itemCount = itemCountStr ? atoi(itemCountStr) : 1;
            if (itemCount < 1 || (item_proto->MaxCount > 0 && itemCount > uint32(item_proto->MaxCount)))
            {
                handler->PSendSysMessage(LANG_COMMAND_INVALID_ITEM_COUNT, itemCount, itemId);
                handler->SetSentErrorMessage(true);
                return false;
            }

            while (itemCount > item_proto->GetMaxStackSize())
            {
                items.push_back(ItemPair(itemId, item_proto->GetMaxStackSize()));
                itemCount -= item_proto->GetMaxStackSize();
            }

            items.push_back(ItemPair(itemId, itemCount));

            if (items.size() > MAX_MAIL_ITEMS)
            {
                handler->PSendSysMessage(LANG_COMMAND_MAIL_ITEMS_LIMIT, MAX_MAIL_ITEMS);
                handler->SetSentErrorMessage(true);
                return false;
            }
        }

        // from console show not existed sender
        MailSender sender(MAIL_NORMAL, handler->GetSession() ? handler->GetSession()->GetPlayer()->GetGUIDLow() : 0, MAIL_STATIONERY_GM);

        // fill mail
        MailDraft draft(subject, text);

        SQLTransaction trans = CharacterDatabase.BeginTransaction();

        for (ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
        {
            if (Item* item = Item::CreateItem(itr->first, itr->second, handler->GetSession() ? handler->GetSession()->GetPlayer() : 0))
            {
                item->SaveToDB(trans);                               // save for prevent lost at next mail load, if send fail then item will deleted
                draft.AddItem(item);
            }
        }

        draft.SendMailTo(trans, MailReceiver(receiver, GUID_LOPART(receiverGuid)), sender);
        CharacterDatabase.CommitTransaction(trans);

        std::string nameLink = handler->playerLink(receiverName);
        handler->PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());
        return true;
    }
コード例 #2
0
ファイル: Level0.cpp プロジェクト: eternalrevo/TrinityCore
bool ChatHandler::Additem(int itemid, int count, int type, bool remplacement)
{
	Player* pl = m_session->GetPlayer();

	if(!pl) return false;

	uint64 receiver_guid = pl->GetGUID();
	int levelItem = 0;

	if (type == 0)
	{
		int typeRecup;

		QueryResult result = CharacterDatabase.PQuery("SELECT `typeRecup`, `level_item` FROM `recup_infos` WHERE name = '%s'",pl->GetName());
		if (!result) return false;
		Field* fields = result->Fetch();
		if (!fields) return false;
		typeRecup = fields[0].GetUInt32();
		levelItem = fields[1].GetUInt32();

		if(remplacement)
		{
			QueryResult result = CharacterDatabase.PQuery("SELECT `itemEpique`, `itemRare` FROM `recup_itemRemplacement` WHERE item = '%u'",itemid);
			if (result)
			{
				Field* fields = result->Fetch();
				if (fields)
				{
					if(typeRecup == 0 || typeRecup == 1)
						itemid = fields[typeRecup].GetUInt32();
				}
			}
		}
	}

	if(!levelItem)
		levelItem = 200;

	QueryResult result = WorldDatabase.PQuery("SELECT `ItemLevel` FROM `item_template` WHERE `entry` = '%d'",itemid);

	if (!result)
		return false;

	if (result)
	{
		Field* fields = result->Fetch();

		if(!fields)
			return false;

		if(fields)
		{
			int itemLevel = fields[0].GetUInt32();

			if(itemLevel > levelItem)
			{
				return false;
			}
		}
	}

	std::string name = pl->GetName();
	if(name.empty())
	{
		SendSysMessage(LANG_PLAYER_NOT_FOUND);
		SetSentErrorMessage(true);
		return false;
	}

	char* msgSubject;
	char* msgText;


	switch(type)
	{
	case 0 : msgSubject="Recuperation";sLog->outDetail("Recuperation");break;
	case 1 : msgSubject="Achat";sLog->outDetail("Achat");break;
	}


	switch(type)
	{
	case 0 : msgText="Voici les objets de votre recuperation. Nous vous remercions d'avoir choisi Avalon";sLog->outDetail("Voici les objets de votre recuperation. Nous vous remercions d'avoir choisi Avalon");break;
	case 1 : msgText="Merci pour votre achat. Bon jeu sur Avalon";sLog->outDetail("Merci pour votre achat. Bon jeu sur Avalon");break;
	}

	if (!msgSubject)
		return false;

	if (!msgText)
		return false;

	// msgSubject, msgText isn't NUL after prev. check
	std::string subject = msgSubject;
	std::string text    = msgText;

	// extract items
	typedef std::pair<uint32,uint32> ItemPair;
	typedef std::list< ItemPair > ItemPairs;
	ItemPairs items;

	uint32 item_id = itemid;
	if (!item_id)
		return false;

	ItemTemplate const* item_proto = sObjectMgr->GetItemTemplate(item_id);
	if (!item_proto)
	{
		PSendSysMessage(LANG_COMMAND_ITEMIDINVALID, item_id);
		SetSentErrorMessage(true);
		return false;
	}

	uint32 item_count = count;
	if (item_count < 1 || (item_proto->MaxCount > 0 && item_count > uint32(item_proto->MaxCount)))
	{
		PSendSysMessage(LANG_COMMAND_INVALID_ITEM_COUNT, item_count,item_id);
		SetSentErrorMessage(true);
		return false;
	}

	while (item_count > item_proto->GetMaxStackSize())
	{
		items.push_back(ItemPair(item_id,item_proto->GetMaxStackSize()));
		item_count -= item_proto->GetMaxStackSize();
	}

	items.push_back(ItemPair(item_id,item_count));

	if (items.size() > MAX_MAIL_ITEMS)
	{
		PSendSysMessage(LANG_COMMAND_MAIL_ITEMS_LIMIT, MAX_MAIL_ITEMS);
		SetSentErrorMessage(true);
		return false;
	}

	// from console show not existed sender
	MailSender sender(MAIL_NORMAL,m_session ? m_session->GetPlayer()->GetGUIDLow() : 0, MAIL_STATIONERY_GM);

	// fill mail
	MailDraft draft(subject, text);

	SQLTransaction trans = CharacterDatabase.BeginTransaction();

	for (ItemPairs::const_iterator itr = items.begin(); itr != items.end(); ++itr)
	{
		if (Item* item = Item::CreateItem(itr->first,itr->second,m_session ? m_session->GetPlayer() : 0))
		{
			item->SaveToDB(trans);                               // save for prevent lost at next mail load, if send fail then item will deleted
			draft.AddItem(item);
		}
	}

	draft.SendMailTo(trans, MailReceiver(pl,GUID_LOPART(receiver_guid)), sender);
	CharacterDatabase.CommitTransaction(trans);

	std::string nameLink = playerLink(name);
	PSendSysMessage(LANG_MAIL_SENT, nameLink.c_str());

	return true;
}