Skip to content

Commit

Permalink
Created sex title function to remove multiple verifications.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed May 9, 2024
1 parent fa08b32 commit 2d789bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
11 changes: 6 additions & 5 deletions src/creatures/players/cyclopedia/player_title.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void PlayerTitle::setCurrentTitle(uint8_t id) {
m_player.kv()->scoped("titles")->set("current-title", id != 0 && isTitleUnlocked(id) ? id : 0);
}

std::string PlayerTitle::getCurrentTitleName() const {
std::string PlayerTitle::getCurrentTitleName() {
auto currentTitle = getCurrentTitle();
if (currentTitle == 0) {
return "";
Expand All @@ -73,7 +73,11 @@ std::string PlayerTitle::getCurrentTitleName() const {
if (title.m_id == 0) {
return "";
}
return (m_player.getSex() == PLAYERSEX_FEMALE && !title.m_femaleName.empty()) ? title.m_femaleName : title.m_maleName;
return getNameBySex(m_player.getSex(), title.m_maleName, title.m_femaleName);
}

const std::string &PlayerTitle::getNameBySex(PlayerSex_t sex, const std::string &male, const std::string &female) {
return sex == PLAYERSEX_FEMALE && !female.empty() ? female : male;
}

void PlayerTitle::checkAndUpdateNewTitles() {
Expand Down Expand Up @@ -155,9 +159,6 @@ void PlayerTitle::loadUnlockedTitles() {
continue;
}

auto femaleLog = !title.m_femaleName.empty() ? ("/" + title.m_femaleName) : "";
// g_logger().debug("[{}] - Title {}{} found for player {}.", __FUNCTION__, title.m_maleName, femaleLog, m_player.getName());

m_titlesUnlocked.emplace_back(title, getUnlockedKV()->get(titleName)->getNumber());
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/creatures/players/cyclopedia/player_title.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class PlayerTitle {
const std::vector<std::pair<Title, uint32_t>> &getUnlockedTitles();
[[nodiscard]] uint8_t getCurrentTitle() const;
void setCurrentTitle(uint8_t id);
[[nodiscard]] std::string getCurrentTitleName() const;
std::string getCurrentTitleName();
static const std::string &getNameBySex(PlayerSex_t sex, const std::string &male, const std::string &female);
void checkAndUpdateNewTitles();
void loadUnlockedTitles();
const std::shared_ptr<KV> &getUnlockedKV();
Expand Down
5 changes: 4 additions & 1 deletion src/lua/functions/creatures/player/player_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4321,7 +4321,10 @@ int PlayerFunctions::luaPlayerGetTitles(lua_State* L) {

int index = 0;
for (const auto &title : playerTitles) {
lua_pushnumber(L, title.first.m_id);
lua_createtable(L, 0, 3);
setField(L, "id", title.first.m_id);
setField(L, "name", player->title()->getNameBySex(player->getSex(), title.first.m_maleName, title.first.m_femaleName));
setField(L, "description", title.first.m_description);
lua_rawseti(L, -2, ++index);
}
return 1;
Expand Down
2 changes: 1 addition & 1 deletion src/server/network/protocol/protocolgame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3993,7 +3993,7 @@ void ProtocolGame::sendCyclopediaCharacterTitles() {
std::string messageTitleDesc = "ProtocolGame::sendCyclopediaCharacterTitles - title.description";
for (const auto &title : titles) {
msg.addByte(title.m_id);
auto titleName = player->getSex() == PLAYERSEX_FEMALE && !title.m_femaleName.empty() ? title.m_femaleName : title.m_maleName;
auto titleName = player->title()->getNameBySex(player->getSex(), title.m_maleName, title.m_femaleName);
msg.addString(titleName, messageTitleName);
msg.addString(title.m_description, messageTitleDesc);
msg.addByte(title.m_permanent ? 0x01 : 0x00);
Expand Down

0 comments on commit 2d789bf

Please sign in to comment.