diff --git a/common/strings.cpp b/common/strings.cpp index 7419de9bc..5f9f17cd5 100644 --- a/common/strings.cpp +++ b/common/strings.cpp @@ -380,118 +380,59 @@ std::string Strings::NumberToWords(unsigned long long int n) return res; } -std::string Strings::Money(uint64 platinum, uint64 gold, uint64 silver, uint64 copper) -{ - std::string money_string = "Unknown"; - if (copper && silver && gold && platinum) { // CSGP - money_string = fmt::format( - "{} platinum, {} gold, {} silver, and {} copper", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(silver)), - Strings::Commify(std::to_string(copper)) - ); +std::string Strings::Money(uint64 platinum, uint64 gold, uint64 silver, uint64 copper, bool commify) { + uint64 values[] = { platinum, gold, silver, copper }; + const char* names[] = { " platinum", " gold", " silver", " copper" }; + + std::vector parts; + for (int i = 0; i < 4; ++i) { + if (values[i] > 0) { + std::string s = std::to_string(values[i]); + parts.push_back((commify ? Strings::Commify(s) : s) + names[i]); + } } - else if (copper && silver && !gold && platinum) { // CSP - money_string = fmt::format( - "{} platinum, {} silver, and {} copper", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(silver)), - Strings::Commify(std::to_string(copper)) - ); + + if (parts.empty()) return "0 copper"; + if (parts.size() == 1) return parts[0]; + + std::string result; + for (size_t i = 0; i < parts.size(); ++i) { + result += parts[i]; + if (i < parts.size() - 2) { + result += ", "; + } + else if (i == parts.size() - 2) { + // Oxford comma logic: ", and " for 3+ items, " and " for 2 + result += (parts.size() > 2) ? ", and " : " and "; + } } - else if (copper && silver && gold && !platinum) { // CSG - money_string = fmt::format( - "{} gold, {} silver, and {} copper", - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(silver)), - Strings::Commify(std::to_string(copper)) - ); - } - else if (copper && !silver && !gold && platinum) { // CP - money_string = fmt::format( - "{} platinum and {} copper", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(copper)) - ); - } - else if (copper && silver && !gold && !platinum) { // CS - money_string = fmt::format( - "{} silver and {} copper", - Strings::Commify(std::to_string(silver)), - Strings::Commify(std::to_string(copper)) - ); - } - else if (!copper && silver && gold && platinum) { // SGP - money_string = fmt::format( - "{} platinum, {} gold, and {} silver", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(silver)) - ); - } - else if (!copper && silver && !gold && platinum) { // SP - money_string = fmt::format( - "{} platinum and {} silver", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(silver)) - ); - } - else if (!copper && silver && gold && !platinum) { // SG - money_string = fmt::format( - "{} gold and {} silver", - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(silver)) - ); - } - else if (copper && !silver && gold && platinum) { // CGP - money_string = fmt::format( - "{} platinum, {} gold, and {} copper", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(copper)) - ); - } - else if (copper && !silver && gold && !platinum) { // CG - money_string = fmt::format( - "{} gold and {} copper", - Strings::Commify(std::to_string(gold)), - Strings::Commify(std::to_string(copper)) - ); - } - else if (!copper && !silver && gold && platinum) { // GP - money_string = fmt::format( - "{} platinum and {} gold", - Strings::Commify(std::to_string(platinum)), - Strings::Commify(std::to_string(gold)) - ); - } - else if (!copper && !silver && !gold && platinum) { // P - money_string = fmt::format( - "{} platinum", - Strings::Commify(std::to_string(platinum)) - ); - } - else if (!copper && !silver && gold && !platinum) { // G - money_string = fmt::format( - "{} gold", - Strings::Commify(std::to_string(gold)) - ); - } - else if (!copper && silver && !gold && !platinum) { // S - money_string = fmt::format( - "{} silver", - Strings::Commify(std::to_string(silver)) - ); - } - else if (copper && !silver && !gold && !platinum) { // C - money_string = fmt::format( - "{} copper", - Strings::Commify(std::to_string(copper)) - ); - } - return money_string; + + return result; } + +std::string Strings::MoneyShort(uint64 copper, bool commify) { + // Matches merchant format + uint64 values[] = { + copper / 1000, + (copper / 100) % 10, + (copper / 10) % 10, + copper % 10 + }; + const char* names[] = { " platinum", " gold", " silver", " copper" }; + + std::string result; + for (int i = 0; i < 4; ++i) { + if (values[i] > 0) { + if (!result.empty()) result += " "; + + std::string s = std::to_string(values[i]); + result += (commify ? Strings::Commify(s) : s) + names[i]; + } + } + + return result.empty() ? "0 copper" : result; +} + std::string Strings::SecondsToTime(int duration, bool is_milliseconds) { if (duration <= 0) { diff --git a/common/strings.h b/common/strings.h index 5a5b93deb..85ae4415b 100644 --- a/common/strings.h +++ b/common/strings.h @@ -62,7 +62,8 @@ public: static std::string Join(const std::vector &ar, const std::string &delim); static std::string Join(const std::vector &ar, const std::string &delim); static std::string MillisecondsToTime(int duration); - static std::string Money(uint64 platinum, uint64 gold = 0, uint64 silver = 0, uint64 copper = 0); + static std::string Money(uint64 platinum, uint64 gold = 0, uint64 silver = 0, uint64 copper = 0, bool commify = true); + static std::string MoneyShort(uint64 copper = 0, bool commify = true); // Matches merchant format when commify is false static std::string NumberToWords(unsigned long long int n); static std::string Repeat(std::string s, int n); static std::string Replace(std::string subject, const std::string &search, const std::string &replace);