summaryrefslogtreecommitdiff
path: root/src/server/game/Battlegrounds/BattlegroundSpamProtect.cpp
blob: 2105960aa84eb0b32b368a1a5735c430d1cbb804 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * 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 "BattlegroundSpamProtect.h"
#include "Battleground.h"
#include "GameTime.h"
#include "ObjectGuid.h"
#include "Player.h"
#include "World.h"

namespace
{
    std::unordered_map<ObjectGuid /*player guid*/, uint32 /*time*/> _players;

    void AddTime(ObjectGuid guid)
    {
        _players.insert_or_assign(guid, GameTime::GetGameTime().count());
    }

    uint32 GetTime(ObjectGuid guid)
    {
        auto const& itr = _players.find(guid);
        if (itr != _players.end())
        {
            return itr->second;
        }

        return 0;
    }

    bool IsCorrectDelay(ObjectGuid guid)
    {
        // Skip if spam time < 30 secs (default)
        return GameTime::GetGameTime().count() - GetTime(guid) >= sWorld->getIntConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_SPAM_DELAY);
    }
}

BGSpamProtect* BGSpamProtect::instance()
{
    static BGSpamProtect instance;
    return &instance;
}

bool BGSpamProtect::CanAnnounce(Player* player, Battleground* bg, uint32 minLevel, uint32 queueTotal)
{
    ObjectGuid guid = player->GetGUID();

    // Check prev time
    if (!IsCorrectDelay(guid))
    {
        return false;
    }

    if (bg)
    {
        // When limited, it announces only if there are at least CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_LIMIT_MIN_PLAYERS in queue
        auto limitQueueMinLevel = sWorld->getIntConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_LIMIT_MIN_LEVEL);
        if (limitQueueMinLevel && minLevel >= limitQueueMinLevel)
        {
            // limit only RBG for 80, WSG for lower levels
            auto bgTypeToLimit = minLevel == 80 ? BATTLEGROUND_RB : BATTLEGROUND_WS;

            if (bg->GetBgTypeID() == bgTypeToLimit && queueTotal < sWorld->getIntConfig(CONFIG_BATTLEGROUND_QUEUE_ANNOUNCER_LIMIT_MIN_PLAYERS))
            {
                return false;
            }
        }
    }

    AddTime(guid);
    return true;
}