From 7b5d1e3d46e94262a9c0fd3a01ab4685aea9d12d Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 1 May 2021 15:10:54 -0400 Subject: Added bash completion, amongst others --- src/rules.h | 70 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 36 deletions(-) (limited to 'src/rules.h') diff --git a/src/rules.h b/src/rules.h index 3a3ffbc..d49f17c 100644 --- a/src/rules.h +++ b/src/rules.h @@ -4,16 +4,16 @@ #include #include #include - -using namespace std; +#include +#include namespace rules { class Ability : public Jsonable { public: - string getFull() const {return abilities.at(getAbbrev());} - string getAbbrev() const {return abbrev;} - operator string() const {return getAbbrev();} + std::string getFull() const {return abilities.at(getAbbrev());} + std::string getAbbrev() const {return abbrev;} + operator std::string() const {return getAbbrev();} virtual nlohmann::json toJson(void) const { return getAbbrev(); } @@ -32,23 +32,28 @@ namespace rules { static Ability Wis() {return Ability("wis");} static Ability Cha() {return Ability("cha");} + static Ability string2ability(std::string s) { + transform(s.begin(), s.end(), s.begin(), ::tolower); + for(auto [abbrev, full] : abilities) { + transform(full.begin(), full.end(), full.begin(), ::tolower); + if(s == abbrev || s == full) { + return Ability(abbrev); + } + } + throw std::invalid_argument("Cannot find an ability for input: \"" + s + "\""); + } + private: - const string abbrev; + const std::string abbrev; - const map abilities { - {"str", "Strength"}, - {"dex", "Dexterity"}, - {"con", "Constitution"}, - {"int", "Intelligence"}, - {"wis", "Wisdom"}, - {"cha", "Charisma"} - }; + static const std::map abilities; }; class Skill : public Jsonable { public: - string getName() const {return name;} + std::string getName() const {return name;} Ability getAbility() const {return Ability(skill2ability.at(getName()));} + operator std::string() const {return getName();} virtual nlohmann::json toJson(void) const { return getName(); } @@ -78,28 +83,21 @@ namespace rules { explicit Skill(const nlohmann::json& data) : name(data) {} + static Skill string2skill(std::string s) { + transform(s.begin(), s.end(), s.begin(), ::tolower); + for(auto& [name, _] : skill2ability) { + std::string n = name; + transform(n.begin(), n.end(), n.begin(), ::tolower); + if(s == n) { + return Skill(name); + } + } + throw std::invalid_argument("Cannot find a skill for input: \"" + s + "\""); + } + private: - const string name; + const std::string name; - const map skill2ability { - {"Athletics", "str"}, - {"Acrobatics", "dex"}, - {"Sleight of Hand", "dex"}, - {"Stealth", "dex"}, - {"Arcana", "int"}, - {"History", "int"}, - {"Investigation", "int"}, - {"Nature", "int"}, - {"Religion", "int"}, - {"Animal Handling", "wis"}, - {"Insight", "wis"}, - {"Medicine", "wis"}, - {"Perception", "wis"}, - {"Survival", "wis"}, - {"Deception", "cha"}, - {"Intimidation", "cha"}, - {"Performance", "cha"}, - {"Persuasion", "cha"} - }; + static const std::map skill2ability; }; } -- cgit v1.2.3