From 19ed70b525bbb0b16d8bd8910d87048532a2cba9 Mon Sep 17 00:00:00 2001 From: Eduardo Dantas Date: Mon, 28 Oct 2024 14:09:19 -0300 Subject: [PATCH] enhance: change from to std::ranges::find_if in Chat::getPrivateChannel (#2996) This refactors the iteration and conditional logic used to find a specific `PrivateChatChannel` in `privateChannels` to use `std::ranges::find_if`. The updated version of the code improves readability by reducing boilerplate, leveraging structured bindings and modern C++ features. --- src/creatures/interactions/chat.cpp | 13 ++++++++----- src/creatures/interactions/chat.hpp | 2 +- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/creatures/interactions/chat.cpp b/src/creatures/interactions/chat.cpp index 1d006ca95aa..9b73e6caad8 100644 --- a/src/creatures/interactions/chat.cpp +++ b/src/creatures/interactions/chat.cpp @@ -604,11 +604,14 @@ std::shared_ptr Chat::getChannelById(uint16_t channelId) { return it->second; } -std::shared_ptr Chat::getPrivateChannel(const std::shared_ptr &player) { - for (auto &it : privateChannels) { - if (it.second->getOwner() == player->getGUID()) { - return it.second; - } +std::shared_ptr Chat::getPrivateChannel(const std::shared_ptr &player) const { + auto it = std::ranges::find_if(privateChannels, [&player](const auto &channelPair) { + const auto &[channelId, channel] = channelPair; + return channel->getOwner() == player->getGUID(); + }); + + if (it != privateChannels.end()) { + return it->second; } return nullptr; } diff --git a/src/creatures/interactions/chat.hpp b/src/creatures/interactions/chat.hpp index 3643086184c..fcc74563c34 100644 --- a/src/creatures/interactions/chat.hpp +++ b/src/creatures/interactions/chat.hpp @@ -137,7 +137,7 @@ class Chat { std::shared_ptr getChannel(const std::shared_ptr &player, uint16_t channelId); std::shared_ptr getChannelById(uint16_t channelId); std::shared_ptr getGuildChannelById(uint32_t guildId); - std::shared_ptr getPrivateChannel(const std::shared_ptr &player); + std::shared_ptr getPrivateChannel(const std::shared_ptr &player) const; LuaScriptInterface* getScriptInterface() { return &scriptInterface;