GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/being/playerrelations.h Lines: 3 4 75.0 %
Date: 2021-03-17 Branches: 0 0 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2008-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#ifndef BEING_PLAYERRELATIONS_H
25
#define BEING_PLAYERRELATIONS_H
26
27
#include "utils/stringvector.h"
28
29
#include "enums/being/relation.h"
30
31
#include <list>
32
#include <map>
33
34
#include "localconsts.h"
35
36
class Being;
37
class PlayerIgnoreStrategy;
38
class PlayerRelationsListener;
39
40
struct PlayerRelation;
41
42
/**
43
 * Player relations class, represents any particular relations and/or
44
 * preferences the user of the local client has wrt other players (identified
45
 * by std::string).
46
 */
47
class PlayerRelationsManager final
48
{
49
    public:
50
        PlayerRelationsManager();
51
52
        A_DELETE_COPY(PlayerRelationsManager)
53
54
        ~PlayerRelationsManager();
55
56
        /**
57
         * Initialise player relations manager (load config file etc.)
58
         */
59
        void init();
60
61
        /**
62
         * Load configuration from our config file, or substitute defaults.
63
         */
64
        void load();
65
66
        /**
67
         * Save configuration to our config file.
68
         */
69
        void store() const;
70
71
        /**
72
         * Determines whether the player in question is being ignored, filtered by
73
         * the specified flags.
74
         */
75
        unsigned int checkPermissionSilently(const std::string &player_name,
76
                                             const unsigned int flags)
77
                                             const A_WARN_UNUSED;
78
79
        /**
80
         * Tests whether the player in question is being ignored for any of the
81
         * actions in the specified flags. If so, trigger appropriate side effects
82
         * if requested by the player.
83
         */
84
        bool hasPermission(const Being *const being,
85
                           const unsigned int flags) const A_WARN_UNUSED;
86
87
        bool hasPermission(const std::string &being,
88
                           const unsigned int flags) const A_WARN_UNUSED;
89
90
        /**
91
         * Updates the relationship with this player.
92
         */
93
        void setRelation(const std::string &name,
94
                         const RelationT relation);
95
96
        /**
97
         * Updates the relationship with this player.
98
         */
99
        RelationT getRelation(const std::string &name) const A_WARN_UNUSED;
100
101
        /**
102
         * Deletes the information recorded for a player.
103
         */
104
        void removePlayer(const std::string &name);
105
106
        /**
107
         * Retrieves the default permissions.
108
         */
109
        unsigned int getDefault() const A_WARN_UNUSED;
110
111
        /**
112
         * Sets the default permissions.
113
         */
114
        void setDefault(const unsigned int permissions);
115
116
        /**
117
         * Retrieves all known player ignore strategies.
118
         *
119
         * The player ignore strategies are allocated statically and must
120
         * not be deleted.
121
         */
122
        STD_VECTOR<PlayerIgnoreStrategy *> *getPlayerIgnoreStrategies()
123
            A_WARN_UNUSED;
124
125
        /**
126
         * Return the current player ignore strategy.
127
         *
128
         * \return A player ignore strategy, or nullptr
129
         */
130
        const PlayerIgnoreStrategy *getPlayerIgnoreStrategy() const
131
                                                              noexcept2
132
                                                              A_WARN_UNUSED
133
12
        { return mIgnoreStrategy; }
134
135
        /**
136
         * Sets the strategy to call when ignoring players.
137
         */
138
        void setPlayerIgnoreStrategy(PlayerIgnoreStrategy *const strategy)
139
                                     noexcept2
140
        { mIgnoreStrategy = strategy; }
141
142
        /**
143
         * For a given ignore strategy short name, find the appropriate index
144
         * in the ignore strategies vector.
145
         *
146
         * \param The short name of the ignore strategy to look up
147
         * \return The appropriate index, or -1
148
         */
149
        int getPlayerIgnoreStrategyIndex(const std::string &shortname)
150
                                         A_WARN_UNUSED;
151
152
        /**
153
         * Retrieves a sorted vector of all players for which we have any
154
         * relations recorded.
155
         */
156
        StringVect *getPlayers() const RETURNS_NONNULL A_WARN_UNUSED;
157
158
        StringVect *getPlayersByRelation(const RelationT rel)
159
                                         const A_WARN_UNUSED;
160
161
        /**
162
         * Removes all recorded player info.
163
         */
164
        void clear();
165
166
        /**
167
         * Do we persist our `ignore' setup?
168
         */
169
        bool getPersistIgnores() const noexcept2 A_WARN_UNUSED
170
        { return mPersistIgnores; }
171
172
        void ignoreTrade(const std::string &name) const;
173
174
        bool isGoodName(Being *const being) const A_WARN_UNUSED;
175
176
        bool isGoodName(const std::string &name) const A_WARN_UNUSED;
177
178
        /**
179
         * Change the `ignore persist' flag.
180
         *
181
         * @param value Whether to persist ignores
182
         */
183
        void setPersistIgnores(const bool value) noexcept2
184
        { mPersistIgnores = value; }
185
186
        void addListener(PlayerRelationsListener *const listener)
187
6
        { mListeners.push_back(listener); }
188
189
        void removeListener(PlayerRelationsListener *const listener)
190
3
        { mListeners.remove(listener); }
191
192
        bool checkBadRelation(const std::string &name) const A_WARN_UNUSED;
193
194
    private:
195
        void signalUpdate(const std::string &name);
196
197
        bool mPersistIgnores;  // If NOT set, we delete the
198
                               // ignored data upon reloading
199
        unsigned int mDefaultPermissions;
200
201
        static bool checkName(const std::string &name) A_WARN_UNUSED;
202
203
        PlayerIgnoreStrategy *mIgnoreStrategy;
204
        std::map<std::string, PlayerRelation *> mRelations;
205
        std::list<PlayerRelationsListener *> mListeners;
206
        STD_VECTOR<PlayerIgnoreStrategy *> mIgnoreStrategies;
207
};
208
209
210
extern PlayerRelationsManager playerRelations;  // singleton representation
211
                                                // of player relations
212
213
214
#endif  // BEING_PLAYERRELATIONS_H