Skip to content

Commit

Permalink
fix(Core/Mail): calculate unReadMails and m_nextMailDelivereTime usin… (
Browse files Browse the repository at this point in the history
azerothcore#18996)

* fix(Core/Mail): calculate unReadMails and m_nextMailDelivereTime using mail cache

- these values were grepped directly from DB before
-> this change was introduced with azerothcore#3420

- the whole mailing system relies on the mails beeing cached in the core
-> these get stored in DB regularly or on specific events

- so apparently the DB is not always in sync with the current mail cache state of the core
-> so grepping data directly from DB is not a good idea at this point

* Update PlayerUpdates.cpp
  • Loading branch information
sudlud authored Jun 2, 2024
1 parent dbb7107 commit 1663045
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions src/server/game/Entities/Player/PlayerUpdates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,31 +435,27 @@ void Player::UpdateNextMailTimeAndUnreads()
{
// Update the next delivery time and unread mails
time_t cTime = GameTime::GetGameTime().count();
// Get the next delivery time
CharacterDatabasePreparedStatement* stmtNextDeliveryTime =
CharacterDatabase.GetPreparedStatement(CHAR_SEL_NEXT_MAIL_DELIVERYTIME);
stmtNextDeliveryTime->SetData(0, GetGUID().GetCounter());
stmtNextDeliveryTime->SetData(1, uint32(cTime));
PreparedQueryResult resultNextDeliveryTime =
CharacterDatabase.Query(stmtNextDeliveryTime);
if (resultNextDeliveryTime)
{
Field* fields = resultNextDeliveryTime->Fetch();
m_nextMailDelivereTime = time_t(fields[0].Get<uint32>());
}

// Get unread mails count
CharacterDatabasePreparedStatement* stmtUnreadAmount =
CharacterDatabase.GetPreparedStatement(
CHAR_SEL_CHARACTER_MAILCOUNT_UNREAD_SYNCH);
stmtUnreadAmount->SetData(0, GetGUID().GetCounter());
stmtUnreadAmount->SetData(1, uint32(cTime));
PreparedQueryResult resultUnreadAmount =
CharacterDatabase.Query(stmtUnreadAmount);
if (resultUnreadAmount)
{
Field* fields = resultUnreadAmount->Fetch();
unReadMails = uint8(fields[0].Get<uint64>());

m_nextMailDelivereTime = 0;
unReadMails = 0;

for (Mail const* mail : GetMails())
{
if (mail->deliver_time > cTime)
{
if (!m_nextMailDelivereTime || m_nextMailDelivereTime > mail->deliver_time)
m_nextMailDelivereTime = mail->deliver_time;
}

// must be not checked yet
if (mail->checked & MAIL_CHECK_MASK_READ)
continue;

// and already delivered or expired
if (cTime < mail->deliver_time || cTime > mail->expire_time)
continue;

unReadMails++;
}
}

Expand Down

0 comments on commit 1663045

Please sign in to comment.