~ubuntu-branches/ubuntu/trusty/manaplus/trusty-proposed

« back to all changes in this revision

Viewing changes to src/being/playerrelations.h

  • Committer: Package Import Robot
  • Author(s): Patrick Matthäi
  • Date: 2013-09-17 10:35:51 UTC
  • mfrom: (1.1.10)
  • Revision ID: package-import@ubuntu.com-20130917103551-az7p3nz9jgxwqjfn
Tags: 1.3.9.15-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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-2013  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 <list>
 
29
#include <map>
 
30
 
 
31
#include "localconsts.h"
 
32
 
 
33
class Being;
 
34
 
 
35
struct PlayerRelation final
 
36
{
 
37
    static const unsigned int EMOTE        = (1 << 0);
 
38
    static const unsigned int SPEECH_FLOAT = (1 << 1);
 
39
    static const unsigned int SPEECH_LOG   = (1 << 2);
 
40
    static const unsigned int WHISPER      = (1 << 3);
 
41
    static const unsigned int TRADE        = (1 << 4);
 
42
    static const unsigned int INVISIBLE    = (1 << 5);
 
43
    static const unsigned int BLACKLIST    = (1 << 6);
 
44
    static const unsigned int ENEMY        = (1 << 7);
 
45
 
 
46
    static const unsigned int RELATIONS_NR = 7;
 
47
    static const unsigned int RELATION_PERMISSIONS[RELATIONS_NR];
 
48
 
 
49
    static const unsigned int DEFAULT = EMOTE
 
50
                                      | SPEECH_FLOAT
 
51
                                      | SPEECH_LOG
 
52
                                      | WHISPER
 
53
                                      | TRADE;
 
54
    enum Relation
 
55
    {
 
56
        NEUTRAL     = 0,
 
57
        FRIEND      = 1,
 
58
        DISREGARDED = 2,
 
59
        IGNORED     = 3,
 
60
        ERASED      = 4,
 
61
        BLACKLISTED = 5,
 
62
        ENEMY2      = 6
 
63
    };
 
64
 
 
65
    explicit PlayerRelation(const Relation relation);
 
66
 
 
67
    A_DELETE_COPY(PlayerRelation)
 
68
 
 
69
    Relation mRelation;  // bitmask for all of the above
 
70
};
 
71
 
 
72
 
 
73
/**
 
74
 * Ignore strategy: describes how we should handle ignores.
 
75
 */
 
76
class PlayerIgnoreStrategy
 
77
{
 
78
    public:
 
79
        std::string mDescription;
 
80
        std::string mShortName;
 
81
 
 
82
        PlayerIgnoreStrategy() :
 
83
            mDescription(),
 
84
            mShortName()
 
85
        {
 
86
        }
 
87
 
 
88
        A_DELETE_COPY(PlayerIgnoreStrategy)
 
89
 
 
90
        virtual ~PlayerIgnoreStrategy()
 
91
        { }
 
92
 
 
93
        /**
 
94
         * Handle the ignoring of the indicated action by the indicated player.
 
95
         */
 
96
        virtual void ignore(Being *const being,
 
97
                            const unsigned int flags) const = 0;
 
98
};
 
99
 
 
100
class PlayerRelationsListener
 
101
{
 
102
    public:
 
103
        PlayerRelationsListener()
 
104
        { }
 
105
 
 
106
        virtual ~PlayerRelationsListener()
 
107
        { }
 
108
 
 
109
        virtual void updatedPlayer(const std::string &name) = 0;
 
110
 
 
111
        virtual void updateAll() = 0;
 
112
};
 
113
 
 
114
/**
 
115
 * Player relations class, represents any particular relations and/or
 
116
 * preferences the user of the local client has wrt other players (identified
 
117
 * by std::string).
 
118
 */
 
119
class PlayerRelationsManager final
 
120
{
 
121
    public:
 
122
        PlayerRelationsManager();
 
123
 
 
124
        A_DELETE_COPY(PlayerRelationsManager)
 
125
 
 
126
        ~PlayerRelationsManager();
 
127
 
 
128
        /**
 
129
         * Initialise player relations manager (load config file etc.)
 
130
         */
 
131
        void init();
 
132
 
 
133
        /**
 
134
         * Load configuration from our config file, or substitute defaults.
 
135
         */
 
136
        void load(const bool oldConfig = false);
 
137
 
 
138
        /**
 
139
         * Save configuration to our config file.
 
140
         */
 
141
        void store() const;
 
142
 
 
143
        /**
 
144
         * Determines whether the player in question is being ignored, filtered by
 
145
         * the specified flags.
 
146
         */
 
147
        unsigned int checkPermissionSilently(const std::string &player_name,
 
148
                                             const unsigned int flags)
 
149
                                             const A_WARN_UNUSED;
 
150
 
 
151
        /**
 
152
         * Tests whether the player in question is being ignored for any of the
 
153
         * actions in the specified flags. If so, trigger appropriate side effects
 
154
         * if requested by the player.
 
155
         */
 
156
        bool hasPermission(const Being *const being,
 
157
                           const unsigned int flags) const A_WARN_UNUSED;
 
158
 
 
159
        bool hasPermission(const std::string &being,
 
160
                           const unsigned int flags) const A_WARN_UNUSED;
 
161
 
 
162
        /**
 
163
         * Updates the relationship with this player.
 
164
         */
 
165
        void setRelation(const std::string &name,
 
166
                         const PlayerRelation::Relation relation);
 
167
 
 
168
        /**
 
169
         * Updates the relationship with this player.
 
170
         */
 
171
        PlayerRelation::Relation getRelation(const std::string &name)
 
172
                                             const A_WARN_UNUSED;
 
173
 
 
174
        /**
 
175
         * Deletes the information recorded for a player.
 
176
         */
 
177
        void removePlayer(const std::string &name);
 
178
 
 
179
        /**
 
180
         * Retrieves the default permissions.
 
181
         */
 
182
        unsigned int getDefault() const A_WARN_UNUSED;
 
183
 
 
184
        /**
 
185
         * Sets the default permissions.
 
186
         */
 
187
        void setDefault(const unsigned int permissions);
 
188
 
 
189
        /**
 
190
         * Retrieves all known player ignore strategies.
 
191
         *
 
192
         * The player ignore strategies are allocated statically and must
 
193
         * not be deleted.
 
194
         */
 
195
        std::vector<PlayerIgnoreStrategy *> *getPlayerIgnoreStrategies()
 
196
            A_WARN_UNUSED;
 
197
 
 
198
        /**
 
199
         * Return the current player ignore strategy.
 
200
         *
 
201
         * \return A player ignore strategy, or nullptr
 
202
         */
 
203
        const PlayerIgnoreStrategy *getPlayerIgnoreStrategy() const
 
204
                                                              A_WARN_UNUSED
 
205
        { return mIgnoreStrategy; }
 
206
 
 
207
        /**
 
208
         * Sets the strategy to call when ignoring players.
 
209
         */
 
210
        void setPlayerIgnoreStrategy(PlayerIgnoreStrategy *const strategy)
 
211
        { mIgnoreStrategy = strategy; }
 
212
 
 
213
        /**
 
214
         * For a given ignore strategy short name, find the appropriate index
 
215
         * in the ignore strategies vector.
 
216
         *
 
217
         * \param The short name of the ignore strategy to look up
 
218
         * \return The appropriate index, or -1
 
219
         */
 
220
        int getPlayerIgnoreStrategyIndex(const std::string &shortname)
 
221
                                         A_WARN_UNUSED;
 
222
 
 
223
        /**
 
224
         * Retrieves a sorted vector of all players for which we have any
 
225
         * relations recorded.
 
226
         */
 
227
        StringVect *getPlayers() const A_WARN_UNUSED;
 
228
 
 
229
        StringVect *getPlayersByRelation(const PlayerRelation::Relation rel)
 
230
                                         const A_WARN_UNUSED;
 
231
 
 
232
        /**
 
233
         * Removes all recorded player info.
 
234
         */
 
235
        void clear();
 
236
 
 
237
        /**
 
238
         * Do we persist our `ignore' setup?
 
239
         */
 
240
        bool getPersistIgnores() const
 
241
        { return mPersistIgnores; }
 
242
 
 
243
        void ignoreTrade(const std::string &name);
 
244
 
 
245
        bool isGoodName(Being *const being) const A_WARN_UNUSED;
 
246
 
 
247
        bool isGoodName(const std::string &name) const A_WARN_UNUSED;
 
248
 
 
249
        /**
 
250
         * Change the `ignore persist' flag.
 
251
         *
 
252
         * @param value Whether to persist ignores
 
253
         */
 
254
        void setPersistIgnores(const bool value)
 
255
        { mPersistIgnores = value; }
 
256
 
 
257
        void addListener(PlayerRelationsListener *const listener)
 
258
        { mListeners.push_back(listener); }
 
259
 
 
260
        void removeListener(PlayerRelationsListener *const listener)
 
261
        { mListeners.remove(listener); }
 
262
 
 
263
        bool checkBadRelation(const std::string &name) const A_WARN_UNUSED;
 
264
 
 
265
    private:
 
266
        void signalUpdate(const std::string &name);
 
267
 
 
268
        bool mPersistIgnores;  // If NOT set, we delete the
 
269
                               // ignored data upon reloading
 
270
        unsigned int mDefaultPermissions;
 
271
 
 
272
        bool checkName(const std::string &name) const A_WARN_UNUSED;
 
273
 
 
274
        PlayerIgnoreStrategy *mIgnoreStrategy;
 
275
        std::map<std::string, PlayerRelation *> mRelations;
 
276
        std::list<PlayerRelationsListener *> mListeners;
 
277
        std::vector<PlayerIgnoreStrategy *> mIgnoreStrategies;
 
278
};
 
279
 
 
280
 
 
281
extern PlayerRelationsManager player_relations;  // singleton representation
 
282
                                                 // of player relations
 
283
 
 
284
 
 
285
#endif  // BEING_PLAYERRELATIONS_H