Skip to content

Commit

Permalink
improvements on getArticle function.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsongabriel committed Jun 26, 2024
1 parent 14f6c21 commit 6375531
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 16 deletions.
22 changes: 9 additions & 13 deletions src/creatures/players/cyclopedia/player_cyclopedia.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,20 @@ void PlayerCyclopedia::loadDeathHistory(uint16_t page, uint16_t entriesPerPage)
std::vector<RecentDeathEntry> entries;
entries.reserve(result->countResults());
do {
std::string cause1 = result->getString("killed_by");
std::string cause2 = result->getString("mostdamage_by");
std::string killed_by = result->getString("killed_by");
std::string mostdamage_by = result->getString("mostdamage_by");

std::ostringstream cause;
cause << "Died at Level " << result->getNumber<uint32_t>("level") << " by";
if (!cause1.empty()) {
cause << getArticle(cause1) << cause1;
std::string cause = fmt::format("Died at Level {}", result->getNumber<uint32_t>("level"));

if (!killed_by.empty()) {
cause.append(fmt::format(" by{}", getArticle(killed_by)));
}

if (!cause2.empty()) {
if (!cause1.empty()) {
cause << " and";
}
cause << getArticle(cause2) << cause2;
if (!mostdamage_by.empty()) {
cause.append(fmt::format("{}{}", !killed_by.empty() ? " and" : "", getArticle(mostdamage_by)));
}
cause << '.';

entries.emplace_back(std::move(cause.str()), result->getNumber<uint32_t>("time"));
entries.emplace_back(cause, result->getNumber<uint32_t>("time"));
} while (result->next());
player->sendCyclopediaCharacterRecentDeaths(page, static_cast<uint16_t>(pages), entries);
};
Expand Down
22 changes: 19 additions & 3 deletions src/utils/tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1838,9 +1838,25 @@ std::string getArticle(const std::string &value, bool withSpace) {
if (value.empty()) {
return "";
}
const char &character = value.front();
auto result = character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ? "an" : "a";
return withSpace ? fmt::format(" {} ", result) : result;

auto removeArticle = [](const std::string &str) -> std::string {
const std::string articles[] = { "a ", "an " };
for (const auto &article : articles) {
if (str.size() > article.size() && std::equal(article.begin(), article.end(), str.begin(), [](char a, char b) { return std::tolower(a) == std::tolower(b); })) {
return str.substr(article.size());
}
}
return str;
};

std::string modifiedValue = removeArticle(value);
if (modifiedValue.empty()) {
return "";
}

const char &character = std::tolower(modifiedValue.front());
auto article = character == 'a' || character == 'e' || character == 'i' || character == 'o' || character == 'u' ? "an" : "a";
return fmt::format("{}{} {}.", withSpace ? " " : "", article, modifiedValue);
}

std::vector<std::string> split(const std::string &str, char delimiter /* = ','*/) {
Expand Down

0 comments on commit 6375531

Please sign in to comment.