refactor(Scripts/Commands): convert cs_modify to new system (#9474)

This commit is contained in:
Kargatum
2021-12-04 21:56:47 +07:00
committed by GitHub
parent aa872743e7
commit 0b507d531d
4 changed files with 344 additions and 573 deletions

View File

@@ -19,7 +19,9 @@
#include "Common.h"
#include "Containers.h"
#include "IpAddress.h"
#include "StringConvert.h"
#include "StringFormat.h"
#include "Tokenize.h"
#include <algorithm>
#include <boost/core/demangle.hpp>
#include <cctype>
@@ -185,41 +187,55 @@ std::string secsToTimeString(uint64 timeInSecs, bool shortText)
return str;
}
int32 MoneyStringToMoney(const std::string& moneyString)
Optional<int32> MoneyStringToMoney(std::string_view moneyString)
{
int32 money = 0;
if (!(std::count(moneyString.begin(), moneyString.end(), 'g') == 1 ||
std::count(moneyString.begin(), moneyString.end(), 's') == 1 ||
std::count(moneyString.begin(), moneyString.end(), 'c') == 1))
{
return 0; // Bad format
}
bool hadG = false;
bool hadS = false;
bool hadC = false;
Tokenizer tokens(moneyString, ' ');
for (Tokenizer::const_iterator itr = tokens.begin(); itr != tokens.end(); ++itr)
for (std::string_view token : Acore::Tokenize(moneyString, ' ', false))
{
std::string tokenString(*itr);
size_t gCount = std::count(tokenString.begin(), tokenString.end(), 'g');
size_t sCount = std::count(tokenString.begin(), tokenString.end(), 's');
size_t cCount = std::count(tokenString.begin(), tokenString.end(), 'c');
if (gCount + sCount + cCount != 1)
uint32 unit;
switch (token[token.length() - 1])
{
return 0;
case 'g':
if (hadG)
{
return std::nullopt;
}
hadG = true;
unit = 100 * 100;
break;
case 's':
if (hadS)
{
return std::nullopt;
}
hadS = true;
unit = 100;
break;
case 'c':
if (hadC)
{
return std::nullopt;
}
hadC = true;
unit = 1;
break;
default:
return std::nullopt;
}
uint32 amount = atoi(*itr);
if (gCount == 1)
Optional<uint32> amount = Acore::StringTo<uint32>(token.substr(0, token.length() - 1));
if (amount)
{
money += amount * 100 * 100;
money += (unit * *amount);
}
else if (sCount == 1)
else
{
money += amount * 100;
}
else if (cCount == 1)
{
money += amount;
return std::nullopt;
}
}

View File

@@ -21,6 +21,7 @@
#include "Containers.h"
#include "Define.h"
#include "Errors.h"
#include "Optional.h"
#include <algorithm>
#include <array>
#include <cctype>
@@ -74,7 +75,7 @@ tm TimeBreakdown(time_t t);
void stripLineInvisibleChars(std::string& src);
int32 MoneyStringToMoney(const std::string& moneyString);
AC_COMMON_API Optional<int32> MoneyStringToMoney(std::string_view moneyString);
std::string secsToTimeString(uint64 timeInSecs, bool shortText = false);
uint32 TimeStringToSecs(const std::string& timestring);

View File

@@ -25,10 +25,16 @@
#include "SharedDefines.h"
#include <map>
static uint32 ReputationRankStrIndex[MAX_REPUTATION_RANK] =
constexpr std::array<uint32, MAX_REPUTATION_RANK> ReputationRankStrIndex =
{
LANG_REP_HATED, LANG_REP_HOSTILE, LANG_REP_UNFRIENDLY, LANG_REP_NEUTRAL,
LANG_REP_FRIENDLY, LANG_REP_HONORED, LANG_REP_REVERED, LANG_REP_EXALTED
LANG_REP_HATED,
LANG_REP_HOSTILE,
LANG_REP_UNFRIENDLY,
LANG_REP_NEUTRAL,
LANG_REP_FRIENDLY,
LANG_REP_HONORED,
LANG_REP_REVERED,
LANG_REP_EXALTED
};
typedef uint32 RepListID;

File diff suppressed because it is too large Load Diff