1
/*
2
 *  The Mana World
3
 *  Copyright (C) 2008  The Mana World Development Team
4
 *
5
 *  This file is part of The Mana World.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program; if not, write to the Free Software
19
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
 */
21
22
#include <algorithm>
23
24
#include "being.h"
25
#include "beingmanager.h"
26
#include "configuration.h"
27
#include "graphics.h"
28
#include "player.h"
29
#include "playerrelations.h"
30
31
#include "utils/dtor.h"
32
#include "utils/gettext.h"
33
34
#define PLAYER_IGNORE_STRATEGY_NOP "nop"
35
#define PLAYER_IGNORE_STRATEGY_EMOTE0 "emote0"
36
#define DEFAULT_IGNORE_STRATEGY PLAYER_IGNORE_STRATEGY_EMOTE0
37
38
#define NAME "name" // constant for xml serialisation
39
#define RELATION "relation" // constant for xml serialisation
40
41
#define IGNORE_EMOTE_TIME 100
42
43
// (De)serialisation class
44
class PlayerConfSerialiser : public ConfigurationListManager<std::pair<std::string, PlayerRelation *>,
45
                                                             std::map<std::string, PlayerRelation *> *>
46
{
47
    virtual ConfigurationObject *writeConfigItem(std::pair<std::string, PlayerRelation *> value,
48
                                                 ConfigurationObject *cobj)
49
    {
50
        if (!value.second)
51
            return NULL;
52
        cobj->setValue(NAME, value.first);
53
        cobj->setValue(RELATION, toString(value.second->mRelation));
54
55
        return cobj;
56
    }
57
58
    virtual std::map<std::string, PlayerRelation *> *
59
    readConfigItem(ConfigurationObject *cobj,
60
                   std::map<std::string, PlayerRelation *> *container)
61
    {
62
        std::string name = cobj->getValue(NAME, "");
63
        if (name.empty())
64
            return container;
65
66
        if (!(*container)[name]) {
67
            int v = (int)cobj->getValue(RELATION, PlayerRelation::NEUTRAL);
68
            (*container)[name] = new PlayerRelation(static_cast<PlayerRelation::Relation>(v));
69
        }
70
        // otherwise ignore the duplicate entry
71
72
        return container;
73
    }
74
};
75
76
static PlayerConfSerialiser player_conf_serialiser; // stateless singleton
77
78
const unsigned int PlayerRelation::RELATION_PERMISSIONS[RELATIONS_NR] = {
79
    /* NEUTRAL */    0, // we always fall back to the defaults anyway
80
    /* FRIEND  */    EMOTE | SPEECH_FLOAT | SPEECH_LOG | WHISPER | TRADE,
81
    /* DISREGARDED*/ EMOTE | SPEECH_FLOAT,
82
    /* IGNORED */    0
83
};
84
85
PlayerRelation::PlayerRelation(Relation relation)
86
{
87
    mRelation = relation;
88
}
89
90
PlayerRelationsManager::PlayerRelationsManager() :
91
    mPersistIgnores(false),
92
    mDefaultPermissions(PlayerRelation::DEFAULT),
93
    mIgnoreStrategy(NULL)
94
{
95
}
96
97
PlayerRelationsManager::~PlayerRelationsManager()
98
{
99
    delete_all(mIgnoreStrategies);
100
}
101
102
void PlayerRelationsManager::clear()
103
{
104
    std::vector<std::string> *names = getPlayers();
105
    for (std::vector<std::string>::const_iterator
106
             it = names->begin(); it != names->end(); it++)
107
        removePlayer(*it);
108
    delete names;
109
}
110
111
#define PERSIST_IGNORE_LIST "persistent-player-list"
112
#define PLAYER_IGNORE_STRATEGY "player-ignore-strategy"
113
#define DEFAULT_PERMISSIONS "default-player-permissions"
114
115
int PlayerRelationsManager::getPlayerIgnoreStrategyIndex(const std::string &name)
116
{
117
    std::vector<PlayerIgnoreStrategy *> *strategies = getPlayerIgnoreStrategies();
118
    for (unsigned int i = 0; i < strategies->size(); i++)
119
        if ((*strategies)[i]->mShortName == name)
120
            return i;
121
122
    return -1;
123
}
124
125
void PlayerRelationsManager::load()
126
{
127
    clear();
128
129
    mPersistIgnores = config.getValue(PERSIST_IGNORE_LIST, 1);
130
    mDefaultPermissions = (int) config.getValue(DEFAULT_PERMISSIONS, mDefaultPermissions);
131
    std::string ignore_strategy_name = config.getValue(PLAYER_IGNORE_STRATEGY, DEFAULT_IGNORE_STRATEGY);
132
    int ignore_strategy_index = getPlayerIgnoreStrategyIndex(ignore_strategy_name);
133
    if (ignore_strategy_index >= 0)
134
        setPlayerIgnoreStrategy((*getPlayerIgnoreStrategies())[ignore_strategy_index]);
135
136
    config.getList<std::pair<std::string, PlayerRelation *>,
137
                   std::map<std::string, PlayerRelation *> *>
138
        ("player",  &(mRelations), &player_conf_serialiser);
139
}
140
141
142
void PlayerRelationsManager::init()
143
{
144
    load();
145
146
    if (!mPersistIgnores)
147
        clear(); // Yes, we still keep them around in the config file until the next update.
148
}
149
150
void PlayerRelationsManager::store()
151
{
152
    config.setList<std::map<std::string, PlayerRelation *>::const_iterator,
153
                   std::pair<std::string, PlayerRelation *>,
154
                   std::map<std::string, PlayerRelation *> *>
155
        ("player",
156
         mRelations.begin(), mRelations.end(),
157
         &player_conf_serialiser);
158
159
    config.setValue(DEFAULT_PERMISSIONS, mDefaultPermissions);
160
    config.setValue(PERSIST_IGNORE_LIST, mPersistIgnores);
161
    config.setValue(PLAYER_IGNORE_STRATEGY,
162
                    (mIgnoreStrategy)? mIgnoreStrategy->mShortName : DEFAULT_IGNORE_STRATEGY);
163
164
    config.write();
165
}
166
167
void PlayerRelationsManager::signalUpdate(const std::string &name)
168
{
169
    store();
170
171
    for (std::list<PlayerRelationsListener *>::const_iterator it = mListeners.begin(); it != mListeners.end(); it++)
172
        (*it)->updatedPlayer(name);
173
}
174
175
unsigned int PlayerRelationsManager::checkPermissionSilently(const std::string &player_name, unsigned int flags)
176
{
177
    PlayerRelation *r = mRelations[player_name];
178
    if (!r)
179
        return mDefaultPermissions & flags;
180
    else {
181
        unsigned int permissions = PlayerRelation::RELATION_PERMISSIONS[r->mRelation];
182
183
        switch (r->mRelation) {
184
        case PlayerRelation::NEUTRAL:
185
            permissions = mDefaultPermissions;
186
            break;
187
188
        case PlayerRelation::FRIEND:
189
            permissions |= mDefaultPermissions; // widen
190
            break;
191
192
        default:
193
            permissions &= mDefaultPermissions; // narrow
194
        }
195
196
        return permissions & flags;
197
    }
198
}
199
200
bool PlayerRelationsManager::hasPermission(Being *being, unsigned int flags)
201
{
202
    if (being->getType() == Being::PLAYER)
203
        return hasPermission(being->getName(), flags) == flags;
204
    return true;
205
}
206
207
bool PlayerRelationsManager::hasPermission(const std::string &name,
208
                                           unsigned int flags)
209
{
210
    unsigned int rejections = flags & ~checkPermissionSilently(name, flags);
211
    bool permitted = rejections == 0;
212
213
    if (!permitted) {
214
        // execute `ignore' strategy, if possible
215
        if (mIgnoreStrategy) {
216
            Player *to_ignore = dynamic_cast<Player *>(beingManager->findBeingByName(name, Being::PLAYER));
217
218
            if (to_ignore)
219
                mIgnoreStrategy->ignore(to_ignore, rejections);
220
        }
221
    }
222
223
    return permitted;
224
}
225
226
void PlayerRelationsManager::setRelation(const std::string &player_name,
227
                                         PlayerRelation::Relation relation)
228
{
229
    PlayerRelation *r = mRelations[player_name];
230
    if (r == NULL)
231
        mRelations[player_name] = new PlayerRelation(relation);
232
    else
233
        r->mRelation = relation;
234
235
    signalUpdate(player_name);
236
}
237
238
std::vector<std::string> * PlayerRelationsManager::getPlayers()
239
{
240
    std::vector<std::string> *retval = new std::vector<std::string>();
241
242
    for (std::map<std::string, PlayerRelation *>::const_iterator it = mRelations.begin(); it != mRelations.end(); it++)
243
        if (it->second)
244
            retval->push_back(it->first);
245
246
    sort(retval->begin(), retval->end());
247
248
    return retval;
249
}
250
251
void PlayerRelationsManager::removePlayer(const std::string &name)
252
{
253
    if (mRelations[name])
254
        delete mRelations[name];
255
256
    mRelations.erase(name);
257
258
    signalUpdate(name);
259
}
260
261
262
PlayerRelation::Relation PlayerRelationsManager::getRelation(const std::string &name)
263
{
264
    if (mRelations[name])
265
        return mRelations[name]->mRelation;
266
267
    return PlayerRelation::NEUTRAL;
268
}
269
270
////////////////////////////////////////
271
// defaults
272
273
unsigned int PlayerRelationsManager::getDefault() const
274
{
275
    return mDefaultPermissions;
276
}
277
278
void PlayerRelationsManager::setDefault(unsigned int permissions)
279
{
280
    mDefaultPermissions = permissions;
281
282
    store();
283
    signalUpdate("");
284
}
285
286
287
////////////////////////////////////////
288
// ignore strategies
289
290
291
class PIS_nothing : public PlayerIgnoreStrategy
292
{
293
public:
294
    PIS_nothing()
295
    {
296
        mDescription = _("Completely ignore");
297
        mShortName = PLAYER_IGNORE_STRATEGY_NOP;
298
    }
299
300
    virtual void ignore(Player *player, unsigned int flags)
301
    {
302
    }
303
};
304
305
class PIS_dotdotdot : public PlayerIgnoreStrategy
306
{
307
public:
308
    PIS_dotdotdot()
309
    {
310
        mDescription = _("Print '...'");
311
        mShortName = "dotdotdot";
312
    }
313
314
    virtual void ignore(Player *player, unsigned int flags)
315
     {
316
         player->setSpeech("...", 500);
317
     }
318
};
319
320
321
class PIS_blinkname : public PlayerIgnoreStrategy
322
{
323
public:
324
    PIS_blinkname()
325
    {
326
        mDescription = _("Blink name");
327
        mShortName = "blinkname";
328
    }
329
330
    virtual void ignore(Player *player, unsigned int flags)
331
    {
332
        player->flashName(200);
333
    }
334
};
335
336
class PIS_emote : public PlayerIgnoreStrategy
337
{
338
public:
339
    PIS_emote(int emote_nr, const std::string &description, const std::string &shortname) :
340
        mEmotion(emote_nr)
341
    {
342
        mDescription = description;
343
        mShortName = shortname;
344
    }
345
346
    virtual void ignore(Player *player, unsigned int flags)
347
     {
348
         player->setEmote(mEmotion, IGNORE_EMOTE_TIME);
349
     }
350
private:
351
    int mEmotion;
352
};
353
354
355
356
std::vector<PlayerIgnoreStrategy *> *
357
PlayerRelationsManager::getPlayerIgnoreStrategies()
358
{
359
    if (mIgnoreStrategies.size() == 0)
360
    {
361
        // not initialised yet?
362
        mIgnoreStrategies.push_back(new PIS_emote(FIRST_IGNORE_EMOTE,
363
                                                  _("Floating '...' bubble"),
364
                                                   PLAYER_IGNORE_STRATEGY_EMOTE0));
365
        mIgnoreStrategies.push_back(new PIS_emote(FIRST_IGNORE_EMOTE + 1,
366
                                                  _("Floating bubble"),
367
                                                  "emote1"));
368
        mIgnoreStrategies.push_back(new PIS_nothing());
369
        mIgnoreStrategies.push_back(new PIS_dotdotdot());
370
        mIgnoreStrategies.push_back(new PIS_blinkname());
371
    }
372
    return &mIgnoreStrategies;
373
}
374
375
376
PlayerRelationsManager player_relations;