Mana
Loading...
Searching...
No Matches
playerrelations.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2008-2009 The Mana World Development Team
4 * Copyright (C) 2009-2024 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#include <algorithm>
23
24#include "actorspritemanager.h"
25#include "being.h"
26#include "configuration.h"
27#include "playerrelations.h"
28
29#include "utils/dtor.h"
30#include "utils/gettext.h"
31
36
38{
39 std::vector<PlayerIgnoreStrategy *> &strategies = getPlayerIgnoreStrategies();
40 for (unsigned int i = 0; i < strategies.size(); i++)
41 if (strategies[i]->mShortName == name)
42 return i;
43
44 return -1;
45}
46
48{
50 if (index >= 0)
52
53 // Ignores are always saved to the config file, but might not be loaded
56
58}
59
66
68{
69 store();
70
71 for (auto listener : mListeners)
72 listener->playerRelationsUpdated();
73}
74
76 const std::string &playerName,
77 unsigned int flags)
78{
79 unsigned int permissions = config.defaultPlayerPermissions;
80
81 switch (getRelation(playerName))
82 {
84 break;
85
86 // widen permissions for friends
88 permissions |=
94 break;
95
96 // narrow permissions for disregarded and ignored players
98 permissions &=
101 break;
103 permissions &= 0;
104 break;
105 }
106
107 return permissions & flags;
108}
109
110bool PlayerRelationsManager::hasPermission(Being *being, unsigned int flags)
111{
112 if (being->getType() == ActorSprite::PLAYER)
113 return hasPermission(being->getName(), flags) == flags;
114 return true;
115}
116
117bool PlayerRelationsManager::hasPermission(const std::string &name,
118 unsigned int flags)
119{
120 unsigned int rejections = flags & ~checkPermissionSilently(name, flags);
121 bool permitted = rejections == 0;
122
123 // execute `ignore' strategy, if possible
124 if (!permitted && mIgnoreStrategy)
125 {
127 mIgnoreStrategy->ignore(b, rejections);
128 }
129
130 return permitted;
131}
132
133void PlayerRelationsManager::setRelation(const std::string &playerName,
134 PlayerRelation relation)
135{
136 mRelations[playerName] = relation;
137 signalUpdate();
138}
139
140std::vector<std::string> PlayerRelationsManager::getPlayers() const
141{
142 std::vector<std::string> retval;
143
144 for (const auto &[name, _] : mRelations)
145 retval.push_back(name);
146
147 sort(retval.begin(), retval.end());
148
149 return retval;
150}
151
152void PlayerRelationsManager::removePlayer(const std::string &name)
153{
154 auto it = mRelations.find(name);
155 if (it != mRelations.end())
156 {
157 mRelations.erase(it);
158 signalUpdate();
159 }
160}
161
162
164{
165 auto it = mRelations.find(name);
166 return it != mRelations.end() ? it->second : PlayerRelation::Neutral;
167}
168
170// defaults
171
173{
175}
176
177void PlayerRelationsManager::setDefault(unsigned int permissions)
178{
179 config.defaultPlayerPermissions = permissions;
180 signalUpdate();
181}
182
183
185// ignore strategies
186
187
189{
190public:
192 {
193 mDescription = _("Completely ignore");
194 mShortName = "nop";
195 }
196
197 void ignore(Being *being, unsigned int flags) override
198 {
199 }
200};
201
203{
204public:
206 {
207 mDescription = _("Print '...'");
208 mShortName = "dotdotdot";
209 }
210
211 void ignore(Being *being, unsigned int flags) override
212 {
213 being->setSpeech("...", 2000);
214 }
215};
216
217
219{
220public:
222 {
223 mDescription = _("Blink name");
224 mShortName = "blinkname";
225 }
226
227 void ignore(Being *being, unsigned int flags) override
228 {
229 being->flashName(1500);
230 }
231};
232
233std::vector<PlayerIgnoreStrategy *> &
235{
236 if (mIgnoreStrategies.empty())
237 {
238 mIgnoreStrategies.push_back(new PIS_nothing());
239 mIgnoreStrategies.push_back(new PIS_dotdotdot());
240 mIgnoreStrategies.push_back(new PIS_blinkname());
241 }
242 return mIgnoreStrategies;
243}
244
245
ActorSpriteManager * actorSpriteManager
Definition game.cpp:110
Being * findBeingByName(const std::string &name, ActorSprite::Type type=Being::UNKNOWN) const
Finds a being by name and (optionally) by type.
Definition being.h:65
Type getType() const final
Returns the type of the ActorSprite.
Definition being.h:122
const std::string & getName() const
Returns the name of the being.
Definition being.h:190
void flashName(int time)
Definition being.cpp:1023
void setSpeech(const std::string &text, int time=5000)
Puts a "speech balloon" above this being for the specified amount of time.
Definition being.cpp:264
void ignore(Being *being, unsigned int flags) override
Handle the ignoring of the indicated action by the indicated player.
void ignore(Being *being, unsigned int flags) override
Handle the ignoring of the indicated action by the indicated player.
void ignore(Being *being, unsigned int flags) override
Handle the ignoring of the indicated action by the indicated player.
Ignore strategy: describes how we should handle ignores.
virtual void ignore(Being *being, unsigned int flags)=0
Handle the ignoring of the indicated action by the indicated player.
Player relations class, represents any particular relations and/or preferences the user of the local ...
void store()
Save configuration to our config file.
std::vector< PlayerIgnoreStrategy * > mIgnoreStrategies
PlayerIgnoreStrategy * mIgnoreStrategy
void init()
Initialise player relations manager (load config file etc.)
std::vector< std::string > getPlayers() const
Retrieves a sorted vector of all players for which we have any relations recorded.
PlayerRelation getRelation(const std::string &name) const
Updates the relationship with this player.
std::map< std::string, PlayerRelation > mRelations
unsigned int getDefault() const
Retrieves the default permissions.
bool hasPermission(Being *being, unsigned int flags)
Tests whether the player in question is being ignored for any of the actions in the specified flags.
std::vector< PlayerIgnoreStrategy * > & getPlayerIgnoreStrategies()
Retrieves all known player ignore strategies.
void setDefault(unsigned int permissions)
Sets the default permissions.
int getPlayerIgnoreStrategyIndex(const std::string &shortName)
For a given ignore strategy short name, find the appropriate index in the ignore strategies vector.
void setPlayerIgnoreStrategy(PlayerIgnoreStrategy *strategy)
Sets the strategy to call when ignoring players.
void setRelation(const std::string &name, PlayerRelation relation)
Updates the relationship with this player.
void removePlayer(const std::string &name)
Deletes the information recorded for a player.
unsigned int checkPermissionSilently(const std::string &player_name, unsigned int flags)
Determines whether the player in question is being ignored, filtered by the specified flags.
std::list< PlayerRelationsListener * > mListeners
Config config
Global settings (config.xml)
Definition client.cpp:97
void delete_all(Container &c)
Definition dtor.h:46
#define _(s)
Definition gettext.h:38
PlayerRelationsManager player_relations
PlayerRelation
bool persistentPlayerList
std::map< std::string, PlayerRelation > players
std::string playerIgnoreStrategy
unsigned defaultPlayerPermissions