diff options
author | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-07 12:40:35 +0200 |
---|---|---|
committer | Thorbjørn Lindeijer <bjorn@lindeijer.nl> | 2024-10-29 17:16:37 +0100 |
commit | 93ad5ec32de124dfa0c054acfd1f2a378cb9ca75 (patch) | |
tree | bfe8e842a780cd87ee105806f4112e01784ff7fc /src/playerrelations.cpp | |
parent | 63fffadacc51fb7c4915361f0286682e08a55cb1 (diff) | |
download | mana-93ad5ec32de124dfa0c054acfd1f2a378cb9ca75.tar.gz mana-93ad5ec32de124dfa0c054acfd1f2a378cb9ca75.tar.bz2 mana-93ad5ec32de124dfa0c054acfd1f2a378cb9ca75.tar.xz mana-93ad5ec32de124dfa0c054acfd1f2a378cb9ca75.zip |
Optimise PlayerRelationsManager::clear
Previous implementation was O(n^2), doing lots of work (saving file and
updating UI) for each removed player.
Diffstat (limited to 'src/playerrelations.cpp')
-rw-r--r-- | src/playerrelations.cpp | 33 |
1 files changed, 12 insertions, 21 deletions
diff --git a/src/playerrelations.cpp b/src/playerrelations.cpp index 731092f3..fc9c991d 100644 --- a/src/playerrelations.cpp +++ b/src/playerrelations.cpp @@ -39,7 +39,7 @@ class PlayerConfSerialiser : public ConfigurationListManager<std::pair<std::string, PlayerRelation>, std::map<std::string, PlayerRelation> *> { - ConfigurationObject *writeConfigItem(std::pair<std::string, PlayerRelation> value, + ConfigurationObject *writeConfigItem(const std::pair<std::string, PlayerRelation> &value, ConfigurationObject *cobj) override { cobj->setValue(NAME, value.first); @@ -68,8 +68,6 @@ class PlayerConfSerialiser : public ConfigurationListManager<std::pair<std::stri } }; -static PlayerConfSerialiser player_conf_serialiser; // stateless singleton - const unsigned int PlayerRelation::RELATION_PERMISSIONS[RELATIONS_NR] = { /* NEUTRAL */ 0, // we always fall back to the defaults anyway /* FRIEND */ EMOTE | SPEECH_FLOAT | SPEECH_LOG | WHISPER | TRADE, @@ -82,13 +80,6 @@ PlayerRelation::PlayerRelation(Relation relation) mRelation = relation; } -PlayerRelationsManager::PlayerRelationsManager() : - mPersistIgnores(false), - mDefaultPermissions(PlayerRelation::DEFAULT), - mIgnoreStrategy(nullptr) -{ -} - PlayerRelationsManager::~PlayerRelationsManager() { delete_all(mIgnoreStrategies); @@ -96,8 +87,8 @@ PlayerRelationsManager::~PlayerRelationsManager() void PlayerRelationsManager::clear() { - for (const auto &name : getPlayers()) - removePlayer(name); + mRelations.clear(); + signalUpdate(); } #define PERSIST_IGNORE_LIST "persistent-player-list" @@ -125,9 +116,10 @@ void PlayerRelationsManager::load() if (ignore_strategy_index >= 0) setPlayerIgnoreStrategy(getPlayerIgnoreStrategies()[ignore_strategy_index]); + PlayerConfSerialiser player_conf_serialiser; config.getList<std::pair<std::string, PlayerRelation>, std::map<std::string, PlayerRelation> *> - ("player", &mRelations, &player_conf_serialiser); + ("player", &mRelations, player_conf_serialiser); } @@ -141,12 +133,13 @@ void PlayerRelationsManager::init() void PlayerRelationsManager::store() { + PlayerConfSerialiser player_conf_serialiser; config.setList<std::map<std::string, PlayerRelation>::const_iterator, std::pair<std::string, PlayerRelation>, std::map<std::string, PlayerRelation> *> ("player", mRelations.begin(), mRelations.end(), - &player_conf_serialiser); + player_conf_serialiser); config.setValue(DEFAULT_PERMISSIONS, mDefaultPermissions); config.setValue(PERSIST_IGNORE_LIST, mPersistIgnores); @@ -156,12 +149,12 @@ void PlayerRelationsManager::store() config.write(); } -void PlayerRelationsManager::signalUpdate(const std::string &name) +void PlayerRelationsManager::signalUpdate() { store(); for (auto listener : mListeners) - listener->updatedPlayer(name); + listener->playerRelationsUpdated(); } unsigned int PlayerRelationsManager::checkPermissionSilently( @@ -226,7 +219,7 @@ void PlayerRelationsManager::setRelation(const std::string &playerName, PlayerRelation::Relation relation) { mRelations[playerName] = PlayerRelation(relation); - signalUpdate(playerName); + signalUpdate(); } std::vector<std::string> PlayerRelationsManager::getPlayers() const @@ -247,7 +240,7 @@ void PlayerRelationsManager::removePlayer(const std::string &name) if (it != mRelations.end()) { mRelations.erase(it); - signalUpdate(name); + signalUpdate(); } } @@ -272,9 +265,7 @@ unsigned int PlayerRelationsManager::getDefault() const void PlayerRelationsManager::setDefault(unsigned int permissions) { mDefaultPermissions = permissions; - - store(); - signalUpdate(std::string()); + signalUpdate(); } |