Files
mod-playerbots/src/Ai/Base/Actions/CheckMailAction.cpp
bashermens 13fff46fa0 Improper singletons migration to clean Meyer's singletons (cherry-pick) (#2082)
# Pull Request

- Applies the clean and corrected singletons, Meyer pattern. (cherry
picked from @SmashingQuasar )

Testing by just playing the game in various ways. Been tested by myself
@Celandriel and @SmashingQuasar
---

## Complexity & Impact

- Does this change add new decision branches?
    - [x] No
    - [ ] Yes (**explain below**)

- Does this change increase per-bot or per-tick processing?
    - [x] No
    - [ ] Yes (**describe and justify impact**)

- Could this logic scale poorly under load?
    - [x] No
    - [ ] Yes (**explain why**)

---

## Defaults & Configuration

- Does this change modify default bot behavior?
    - [x] No
    - [ ] Yes (**explain why**)

---

## AI Assistance

- Was AI assistance (e.g. ChatGPT or similar tools) used while working
on this change?
    - [x] No
    - [ ] Yes (**explain below**)
---

## Final Checklist

- [x] Stability is not compromised
- [x] Performance impact is understood, tested, and acceptable
- [x] Added logic complexity is justified and explained
- [x] Documentation updated if needed

---

## Notes for Reviewers

Anything that significantly improves realism at the cost of stability or
performance should be carefully discussed
before merging.

---------

Co-authored-by: Nicolas Lebacq <nicolas.cordier@outlook.com>
Co-authored-by: Keleborn <22352763+Celandriel@users.noreply.github.com>
2026-01-30 21:49:37 +01:00

104 lines
3.0 KiB
C++

/*
* Copyright (C) 2016+ AzerothCore <www.azerothcore.org>, released under GNU AGPL v3 license, you may redistribute it
* and/or modify it under version 3 of the License, or (at your option), any later version.
*/
#include "CheckMailAction.h"
#include "Event.h"
#include "GuildTaskMgr.h"
#include "Playerbots.h"
bool CheckMailAction::Execute(Event event)
{
WorldPacket p;
bot->GetSession()->HandleQueryNextMailTime(p);
CharacterDatabaseTransaction trans = CharacterDatabase.BeginTransaction();
std::vector<uint32> ids;
for (PlayerMails::const_iterator i = bot->GetMails().begin(); i != bot->GetMails().end(); ++i)
{
Mail* mail = *i;
if (!mail || mail->state == MAIL_STATE_DELETED)
continue;
Player* owner = ObjectAccessor::FindConnectedPlayer(ObjectGuid::Create<HighGuid::Player>(mail->sender));
if (!owner)
continue;
uint32 account = owner->GetSession()->GetAccountId();
if (sPlayerbotAIConfig.IsInRandomAccountList(account))
continue;
ProcessMail(mail, owner, trans);
ids.push_back(mail->messageID);
mail->state = MAIL_STATE_DELETED;
}
for (uint32 id : ids)
{
bot->SendMailResult(id, MAIL_DELETED, MAIL_OK);
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_MAIL_BY_ID);
stmt->SetData(0, id);
trans->Append(stmt);
stmt = CharacterDatabase.GetPreparedStatement(CHAR_DEL_MAIL_ITEM_BY_ID);
stmt->SetData(0, id);
trans->Append(stmt);
bot->RemoveMail(id);
}
CharacterDatabase.CommitTransaction(trans);
return true;
}
bool CheckMailAction::isUseful()
{
if (botAI->GetMaster() || !bot->GetMailSize() || bot->InBattleground())
return false;
return true;
}
void CheckMailAction::ProcessMail(Mail* mail, Player* owner, CharacterDatabaseTransaction trans)
{
if (mail->items.empty())
{
return;
}
if (mail->subject.find("Item(s) you asked for") != std::string::npos)
return;
for (MailItemInfoVec::iterator i = mail->items.begin(); i != mail->items.end(); ++i)
{
Item* item = bot->GetMItem(i->item_guid);
if (!item)
continue;
if (!GuildTaskMgr::instance().CheckItemTask(i->item_template, item->GetCount(), owner, bot, true))
{
std::ostringstream body;
body << "Hello, " << owner->GetName() << ",\n";
body << "\n";
body << "Here are the item(s) you've sent me by mistake";
body << "\n";
body << "Thanks,\n";
body << bot->GetName() << "\n";
MailDraft draft("Item(s) you've sent me", body.str());
draft.AddItem(item);
bot->RemoveMItem(i->item_guid);
draft.SendMailTo(trans, MailReceiver(owner), MailSender(bot));
return;
}
bot->RemoveMItem(i->item_guid);
item->DestroyForPlayer(bot);
}
}