EverWrath/src/server/scripts/World/server_mail.cpp
Kitzunu 231096132c
refactor(Core/ServerMail): Refactor to Dedicated Manager Class with Multi-Item & Condition Support (#21590)
1. Core Cleanup
    - Move all ServerMail logic from `ObjectMgr` into a new dedicated `ServerMailMgr` class
    - Move faction logic for money from SendServerMail into the script
2. Separation of items into a new table
    - Create a new `mail_server_template_items` table
    - Allows to send multiple items in one mail
    - Separate items per faction Alliance/Horde
3. Separation of conditions into a new table
    - Create a new `mail_server_template_conditions` table
    - Allows to use multiple conditions for one mail
    - Available condition types
        - Minimum playtime (playerLevel >= condition)
        - Minimum playtime (playerPlayTime >= condition)
        - Rewarded quest
        - Earned achievement
        - Earned reputation (playerReputation >= conditionState)
        - Faction
        - Race
        - Class
4. Updated ServerMail loading
    - Move item and condition loading to their own functions
        - LoadMailServerTemplateItems()
        - LoadMailServerTemplateConditions()
5. Reworked eligibility check
    - Player needs to pass all conditions to be eligible for the mail
    - All players are automatically eligible if no conditions exist for a server mail template.
6. Updated foreign keys
    - For table `mail_server_character`, `mail_server_template_conditions`, `mail_server_template_items` foreign key with on delete cascade is added for automatic removal of entries if mail_server_template.id is deleted.
7. Database changes
    - See the PR
2025-03-09 09:18:01 +01:00

80 lines
3.1 KiB
C++

/*
* This file is part of the AzerothCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by the
* Free Software Foundation; either version 3 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "CreatureScript.h"
#include "Player.h"
#include "PlayerScript.h"
#include "QueryResult.h"
#include "ServerMailMgr.h"
class ServerMailReward : public PlayerScript
{
public:
ServerMailReward() : PlayerScript("ServerMailReward", {PLAYERHOOK_ON_LOGIN}) { }
// CHARACTER_LOGIN = 8
void OnPlayerLogin(Player* player) override
{
// Retrieve all server mail records and session only once
auto const& serverMailStore = sServerMailMgr->GetAllServerMailStore();
WorldSession* session = player->GetSession();
// We should always have a session, just incase
if (!session)
return;
uint32 playerGUID = player->GetGUID().GetCounter();
bool isAlliance = player->GetTeamId() == TEAM_ALLIANCE;
for (auto const& [mailId, servMail] : serverMailStore)
{
CharacterDatabasePreparedStatement* stmt = CharacterDatabase.GetPreparedStatement(CHAR_SEL_MAIL_SERVER_CHARACTER);
stmt->SetData(0, playerGUID);
stmt->SetData(1, mailId);
// Capture servMail by value
auto callback = [session, servMailWrapper = std::reference_wrapper<ServerMail const>(servMail), isAlliance](PreparedQueryResult result)
{
ServerMail const& servMail = servMailWrapper.get(); // Dereference the wrapper to get the original object
if (!result)
{
uint32 money = isAlliance ? servMail.moneyA : servMail.moneyH;
std::vector<ServerMailItems> const& items = isAlliance ? servMail.itemsA : servMail.itemsH;
std::vector<ServerMailCondition> const& conditions = servMail.conditions;
sServerMailMgr->SendServerMail(
session->GetPlayer(),
servMail.id,
money,
items,
conditions,
servMail.subject,
servMail.body
);
}
};
// Execute the query asynchronously and add the callback
session->GetQueryProcessor().AddCallback(CharacterDatabase.AsyncQuery(stmt).WithPreparedCallback(callback));
}
}
};
void AddSC_server_mail()
{
new ServerMailReward();
}