Skip to content

Commit

Permalink
improve: changing concatenation to fmt::format (opentibiabr#767)
Browse files Browse the repository at this point in the history
Fixed some sonarcloud reports.
  • Loading branch information
murilo09 authored Jan 12, 2023
1 parent 6df2a92 commit cfda8a5
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 77 deletions.
2 changes: 1 addition & 1 deletion src/creatures/combat/condition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ bool ConditionRegeneration::executeCondition(Creature* creature, int32_t interva

if (isBuff && realHealthGain > 0) {
if (player) {
std::string healString = std::to_string(realHealthGain) + (realHealthGain != 1 ? " hitpoints." : " hitpoint.");
std::string healString = fmt::format("{} hitpoint{}.", realHealthGain, (realHealthGain != 1 ? "s" : ""));

TextMessage message(MESSAGE_HEALED, "You were healed for " + healString);
message.position = player->getPosition();
Expand Down
2 changes: 1 addition & 1 deletion src/creatures/creature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,7 @@ void Creature::onGainExperience(uint64_t gainExp, Creature* target)
return;
}

TextMessage message(MESSAGE_EXPERIENCE_OTHERS, ucfirst(getNameDescription()) + " gained " + std::to_string(gainExp) + (gainExp != 1 ? " experience points." : " experience point."));
TextMessage message(MESSAGE_EXPERIENCE_OTHERS, fmt::format("{} gained {} experience point{}.", ucfirst(getNameDescription()), gainExp, (gainExp != 1 ? "s" : "")));
message.position = position;
message.primary.color = TEXTCOLOR_WHITE_EXP;
message.primary.value = gainExp;
Expand Down
25 changes: 17 additions & 8 deletions src/creatures/players/player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1238,7 +1238,7 @@ void Player::onApplyImbuement(Imbuement *imbuement, Item *item, uint8_t slot, bo

if (!g_game().removeMoney(this, price, 0, true))
{
std::string message = "You don't have " + std::to_string(price) + " gold coins.";
std::string message = fmt::format("You don't have {} gold coins.", price);

SPDLOG_ERROR("[Player::onApplyImbuement] - An error occurred while player with name {} try to apply imbuement, player do not have money", this->getName());
sendImbuementResult(message);
Expand Down Expand Up @@ -1308,7 +1308,7 @@ void Player::onClearImbuement(Item* item, uint8_t slot)

if (!g_game().removeMoney(this, baseImbuement->removeCost, 0, true))
{
std::string message = "You don't have " + std::to_string(baseImbuement->removeCost) + " gold coins.";
std::string message = fmt::format("You don't have {} gold coins.", baseImbuement->removeCost);

SPDLOG_ERROR("[Player::onClearImbuement] - An error occurred while player with name {} try to apply imbuement, player do not have money", this->getName());
this->sendImbuementResult(message);
Expand Down Expand Up @@ -2121,7 +2121,7 @@ void Player::addExperience(Creature* target, uint64_t exp, bool sendText/* = fal
experience += exp;

if (sendText) {
std::string expString = std::to_string(exp) + (exp != 1 ? " experience points." : " experience point.");
std::string expString = fmt::format("{} experience point{}.", exp, (exp != 1 ? "s" : ""));

TextMessage message(MESSAGE_EXPERIENCE, "You gained " + expString);
message.position = position;
Expand Down Expand Up @@ -2215,9 +2215,9 @@ void Player::removeExperience(uint64_t exp, bool sendText/* = false*/)
if (sendText) {
lostExp -= experience;

std::string expString = std::to_string(lostExp) + (lostExp != 1 ? " experience points." : " experience point.");
std::string expString = fmt::format("You lost {} experience point{}.", lostExp, (lostExp != 1 ? "s" : ""));

TextMessage message(MESSAGE_EXPERIENCE, "You lost " + expString);
TextMessage message(MESSAGE_EXPERIENCE, expString);
message.position = position;
message.primary.value = lostExp;
message.primary.color = TEXTCOLOR_RED;
Expand Down Expand Up @@ -5436,9 +5436,18 @@ bool Player::addOfflineTrainingTries(skills_t skill, uint64_t tries)
sendStats();
}

std::ostringstream ss;
ss << std::fixed << std::setprecision(2) << "Your " << ucwords(getSkillName(skill)) << " skill changed from level " << oldSkillValue << " (with " << oldPercentToNextLevel << "% progress towards level " << (oldSkillValue + 1) << ") to level " << newSkillValue << " (with " << newPercentToNextLevel << "% progress towards level " << (newSkillValue + 1) << ')';
sendTextMessage(MESSAGE_EVENT_ADVANCE, ss.str());
std::string message = fmt::format(
"Your {} skill changed from level {} (with {:.2f}% progress towards level {}) to level {} (with {:.2f}% progress towards level {})",
ucwords(getSkillName(skill)),
oldSkillValue,
oldPercentToNextLevel,
oldSkillValue + 1,
newSkillValue,
newPercentToNextLevel,
newSkillValue + 1
);

sendTextMessage(MESSAGE_EVENT_ADVANCE, message);
return sendUpdate;
}

Expand Down
2 changes: 1 addition & 1 deletion src/game/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6248,7 +6248,7 @@ bool Game::combatChangeMana(Creature* attacker, Creature* target, CombatDamage&
realManaChange = target->getMana() - realManaChange;

if (realManaChange > 0 && !target->isInGhostMode()) {
std::string damageString = std::to_string(realManaChange) + " mana.";
std::string damageString = fmt::format("{} mana", realManaChange);

std::string spectatorMessage;
if (!attacker) {
Expand Down
5 changes: 1 addition & 4 deletions src/game/movement/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ Direction Position::getRandomDirection()

std::ostream& operator<<(std::ostream& os, const Position& pos)
{
os << "( " << std::setw(5) << std::setfill('0') << pos.x;
os << " / " << std::setw(5) << std::setfill('0') << pos.y;
os << " / " << std::setw(3) << std::setfill('0') << pos.getZ();
os << " )";
os << pos.toString();
return os;
}

Expand Down
67 changes: 28 additions & 39 deletions src/items/item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1099,25 +1099,23 @@ std::vector<std::pair<std::string, std::string>>
}

ss.str("");
ss << getCombatName(indexToCombatType(i)) << ' '
<< std::showpos << it.abilities->absorbPercent[i] << std::noshowpos << '%';
ss << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->absorbPercent[i]);
descriptions.emplace_back("Protection", ss.str());
}
}
for (size_t i = 0; i < COMBAT_COUNT; ++i) {
if (it.abilities->fieldAbsorbPercent[i] == 0) {
continue;
}

ss.str("");
ss << getCombatName(indexToCombatType(i)) << ' '
<< std::showpos << it.abilities->fieldAbsorbPercent[i] << std::noshowpos << '%';
ss << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->fieldAbsorbPercent[i]);
descriptions.emplace_back("Field Protection", ss.str());
}
}

if (it.isKey()) {
ss.str("");
ss << std::setfill('0') << std::setw(4) << item->getActionId();
ss << fmt::format("{:04}", item->getActionId());
descriptions.emplace_back("Key", ss.str());
}

Expand Down Expand Up @@ -1353,8 +1351,7 @@ std::vector<std::pair<std::string, std::string>>
}

ss.str("");
ss << getCombatName(indexToCombatType(i)) << ' '
<< std::showpos << it.abilities->absorbPercent[i] << std::noshowpos << '%';
ss << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->absorbPercent[i]);
descriptions.emplace_back("Protection", ss.str());
}

Expand All @@ -1364,15 +1361,14 @@ std::vector<std::pair<std::string, std::string>>
}

ss.str("");
ss << getCombatName(indexToCombatType(i)) << ' '
<< std::showpos << it.abilities->fieldAbsorbPercent[i] << std::noshowpos << '%';
ss << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->fieldAbsorbPercent[i]);
descriptions.emplace_back("Field Protection", ss.str());
}
}

if (it.isKey()) {
ss.str("");
ss << std::setfill('0') << std::setw(4) << 0;
ss << fmt::format("{:04}", 0);
descriptions.emplace_back("Key", ss.str());
}

Expand Down Expand Up @@ -1499,10 +1495,7 @@ std::string Item::parseImbuementDescription(const Item* item)

int minutes = imbuementInfo.duration / 60;
int hours = minutes / 60;
s << baseImbuement->name << " "
<< imbuementInfo.imbuement->getName() << " "
<< std::setw(2) << std::setfill('0') << hours << ":"
<< std::setw(2) << std::setfill('0') << (minutes % 60) << "h";
s << fmt::format("{} {} {:02}:{:02}h", baseImbuement->name, imbuementInfo.imbuement->getName(), hours, minutes % 60);
}
s << ").";
}
Expand All @@ -1515,13 +1508,12 @@ std::string Item::parseClassificationDescription(const Item* item) {
if (item && item->getClassification() >= 1) {
string << std::endl << "Classification: " << std::to_string(item->getClassification()) << " Tier: " << std::to_string(item->getTier());
if (item->getTier() != 0) {
string << " (";
if (Item::items[item->getID()].weaponType != WEAPON_NONE) {
string << item->getFatalChance() << "% Onslaught).";
string << fmt::format(" ({}% Onslaught).", item->getFatalChance());
} else if (g_game().getObjectCategory(item) == OBJECTCATEGORY_HELMETS) {
string << item->getMomentumChance() << "% Momentum).";
string << fmt::format(" ({}% Momentum).", item->getMomentumChance());
} else if (g_game().getObjectCategory(item) == OBJECTCATEGORY_ARMORS) {
string << std::setprecision(2) << std::fixed << item->getDodgeChance() << "% Ruse).";
string << fmt::format(" ({:.2f}% Ruse).", item->getDodgeChance());
}
}
}
Expand Down Expand Up @@ -1622,8 +1614,7 @@ std::string Item::parseShowAttributesDescription(const Item *item, const uint16_
} else {
itemDescription << ", ";
}

itemDescription << getCombatName(indexToCombatType(i)) << ' ' << std::showpos << itemType.abilities->absorbPercent[i] << std::noshowpos << '%';
itemDescription << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), itemType.abilities->absorbPercent[i]);
}
} else {
if (begin) {
Expand All @@ -1633,7 +1624,7 @@ std::string Item::parseShowAttributesDescription(const Item *item, const uint16_
itemDescription << ", ";
}

itemDescription << "protection all " << std::showpos << show << std::noshowpos << '%';
itemDescription << fmt::format("protection all {:+}%", show);
}

show = itemType.abilities->fieldAbsorbPercent[0];
Expand Down Expand Up @@ -1669,8 +1660,7 @@ std::string Item::parseShowAttributesDescription(const Item *item, const uint16_
itemDescription << ", ";
}

itemDescription << getCombatName(indexToCombatType(i)) << " field " << std::showpos << itemType.abilities->fieldAbsorbPercent[i] << std::noshowpos << '%';
}
itemDescription << fmt::format("{} field {:+}%", getCombatName(indexToCombatType(i)), itemType.abilities->fieldAbsorbPercent[i]); }
} else {
if (begin) {
begin = false;
Expand All @@ -1679,7 +1669,7 @@ std::string Item::parseShowAttributesDescription(const Item *item, const uint16_
itemDescription << ", ";
}

itemDescription << "protection all fields " << std::showpos << show << std::noshowpos << '%';
itemDescription << fmt::format("protection all fields {:+}%", show);
}

if (itemType.abilities->speed) {
Expand All @@ -1690,8 +1680,7 @@ std::string Item::parseShowAttributesDescription(const Item *item, const uint16_
itemDescription << ", ";
}


itemDescription << "speed " << std::showpos << (itemType.abilities->speed) << std::noshowpos;
itemDescription << fmt::format("speed {:+}", show);
}
}

Expand Down Expand Up @@ -1875,7 +1864,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << getCombatName(indexToCombatType(i)) << ' ' << std::showpos << it.abilities->absorbPercent[i] << std::noshowpos << '%';
s << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->absorbPercent[i]);
}
} else {
if (begin) {
Expand All @@ -1885,7 +1874,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "protection all " << std::showpos << show << std::noshowpos << '%';
s << fmt::format("protection all {:+}%", show);
}

show = it.abilities->fieldAbsorbPercent[0];
Expand Down Expand Up @@ -1921,7 +1910,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << getCombatName(indexToCombatType(i)) << " field " << std::showpos << it.abilities->fieldAbsorbPercent[i] << std::noshowpos << '%';
s << fmt::format("{} field {:+}%", getCombatName(indexToCombatType(i)), it.abilities->fieldAbsorbPercent[i]);
}
} else {
if (begin) {
Expand All @@ -1931,7 +1920,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "protection all fields " << std::showpos << show << std::noshowpos << '%';
s << fmt::format("protection all fields {:+}%", show);
}

if (it.abilities->speed) {
Expand All @@ -1942,7 +1931,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "speed " << std::showpos << (it.abilities->speed) << std::noshowpos;
s << fmt::format("speed {:+}", it.abilities->speed);
}
}

Expand Down Expand Up @@ -2069,7 +2058,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << getCombatName(indexToCombatType(i)) << ' ' << std::showpos << it.abilities->absorbPercent[i] << std::noshowpos << '%';
s << fmt::format("{} {:+}%", getCombatName(indexToCombatType(i)), it.abilities->absorbPercent[i]);
}
} else {
if (begin) {
Expand All @@ -2079,7 +2068,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "protection all " << std::showpos << show << std::noshowpos << '%';
s << fmt::format("protection all {:+}%", show);
}

show = it.abilities->fieldAbsorbPercent[0];
Expand Down Expand Up @@ -2115,7 +2104,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << getCombatName(indexToCombatType(i)) << " field " << std::showpos << it.abilities->fieldAbsorbPercent[i] << std::noshowpos << '%';
s << fmt::format("{} field {:+}%", getCombatName(indexToCombatType(i)), it.abilities->fieldAbsorbPercent[i]);
}
} else {
if (begin) {
Expand All @@ -2125,7 +2114,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "protection all fields " << std::showpos << show << std::noshowpos << '%';
s << fmt::format("protection all fields {:+}%", show);
}

if (it.abilities->speed) {
Expand All @@ -2136,7 +2125,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,
s << ", ";
}

s << "speed " << std::showpos << (it.abilities->speed) << std::noshowpos;
s << fmt::format("speed {:+}", it.abilities->speed);
}
}

Expand All @@ -2162,7 +2151,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,

if (it.abilities && it.slotPosition & SLOTP_RING) {
if (it.abilities->speed > 0) {
s << " (speed " << std::showpos << (it.abilities->speed) << std::noshowpos << ')';
s << fmt::format(" (speed {:+})", it.abilities->speed);
} else if (hasBitSet(CONDITION_DRUNK, it.abilities->conditionSuppressions)) {
s << " (hard drinking)";
} else if (it.abilities->invisible) {
Expand All @@ -2180,7 +2169,7 @@ std::string Item::getDescription(const ItemType& it, int32_t lookDistance,

if (!found) {
if (it.isKey()) {
s << " (Key:" << std::setfill('0') << std::setw(4) << (item ? item->getActionId() : 0) << ')';
s << fmt::format(" (Key:{:04})", item ? item->getActionId() : 0);
} else if (it.isFluidContainer()) {
if (subType > 0) {
const std::string& itemName = items[subType].name;
Expand Down
3 changes: 2 additions & 1 deletion src/lua/functions/core/game/game_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,8 @@ int GameFunctions::luaGameGetClientVersion(lua_State* L) {
lua_createtable(L, 0, 3);
setField(L, "min", CLIENT_VERSION);
setField(L, "max", CLIENT_VERSION);
setField(L, "string", std::to_string(CLIENT_VERSION_UPPER) + "." + std::to_string(CLIENT_VERSION_LOWER));
std::string version = fmt::format("{}.{}",CLIENT_VERSION_UPPER,CLIENT_VERSION_LOWER);
setField(L, "string", version);
return 1;
}

Expand Down
6 changes: 3 additions & 3 deletions src/otserv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ void toggleForceCloseButton() {
std::string getCompiler() {
std::string compiler;
#if defined(__clang__)
return compiler = "Clang++ " + std::to_string(__clang_major__) + "." + std::to_string(__clang_minor__) + "." + std::to_string(__clang_patchlevel__) +"";
return compiler = fmt::format("Clang++ {}.{}.{}", __clang_major__, __clang_minor__, __clang_patchlevel__);
#elif defined(_MSC_VER)
return compiler = "Microsoft Visual Studio " + std::to_string(_MSC_VER) +"";
return compiler = fmt::format("Microsoft Visual Studio {}", _MSC_VER);
#elif defined(__GNUC__)
return compiler = "G++ " + std::to_string(__GNUC__) + "." + std::to_string(__GNUC_MINOR__) + "." + std::to_string(__GNUC_PATCHLEVEL__) +"";
return compiler = fmt::format("G++ {}.{}.{}", __GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__);
#else
return compiler = "unknown";
#endif
Expand Down
Loading

0 comments on commit cfda8a5

Please sign in to comment.