Skip to content

Commit

Permalink
enhance: change from to std::ranges::find_if in Chat::getPrivateChann…
Browse files Browse the repository at this point in the history
…el (opentibiabr#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.
  • Loading branch information
dudantas authored Oct 28, 2024
1 parent 5244ed5 commit 19ed70b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/creatures/interactions/chat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -604,11 +604,14 @@ std::shared_ptr<ChatChannel> Chat::getChannelById(uint16_t channelId) {
return it->second;
}

std::shared_ptr<PrivateChatChannel> Chat::getPrivateChannel(const std::shared_ptr<Player> &player) {
for (auto &it : privateChannels) {
if (it.second->getOwner() == player->getGUID()) {
return it.second;
}
std::shared_ptr<PrivateChatChannel> Chat::getPrivateChannel(const std::shared_ptr<Player> &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;
}
2 changes: 1 addition & 1 deletion src/creatures/interactions/chat.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class Chat {
std::shared_ptr<ChatChannel> getChannel(const std::shared_ptr<Player> &player, uint16_t channelId);
std::shared_ptr<ChatChannel> getChannelById(uint16_t channelId);
std::shared_ptr<ChatChannel> getGuildChannelById(uint32_t guildId);
std::shared_ptr<PrivateChatChannel> getPrivateChannel(const std::shared_ptr<Player> &player);
std::shared_ptr<PrivateChatChannel> getPrivateChannel(const std::shared_ptr<Player> &player) const;

LuaScriptInterface* getScriptInterface() {
return &scriptInterface;
Expand Down

0 comments on commit 19ed70b

Please sign in to comment.