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