aboutsummaryrefslogtreecommitdiff
path: root/src/rules.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/rules.h')
-rw-r--r--src/rules.h70
1 files changed, 34 insertions, 36 deletions
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 <map>
#include <string>
#include <iostream>
-
-using namespace std;
+#include <algorithm>
+#include <stdexcept>
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<string, string> abilities {
- {"str", "Strength"},
- {"dex", "Dexterity"},
- {"con", "Constitution"},
- {"int", "Intelligence"},
- {"wis", "Wisdom"},
- {"cha", "Charisma"}
- };
+ static const std::map<std::string, std::string> 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<string, string> 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<std::string, std::string> skill2ability;
};
}