diff options
author | Your Name <you@example.com> | 2021-05-09 13:56:46 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-09 13:56:46 -0400 |
commit | d13358b71ec15085f2638fd9c3fc634df62dfc94 (patch) | |
tree | 467c643a068bf2d83da3632823a6434244ae004e /src/rules.h | |
parent | e3aaa68a2ea1a403256150121c57a0287014162f (diff) | |
download | dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.tar.gz dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.tar.bz2 dmtool-d13358b71ec15085f2638fd9c3fc634df62dfc94.zip |
Reduced dependency on json-related hacks
Diffstat (limited to 'src/rules.h')
-rw-r--r-- | src/rules.h | 49 |
1 files changed, 25 insertions, 24 deletions
diff --git a/src/rules.h b/src/rules.h index c1b130d..83d471f 100644 --- a/src/rules.h +++ b/src/rules.h @@ -10,23 +10,34 @@ namespace rules { - class Ability : public Jsonable { + class RuleItem { + public: + friend void to_json(nlohmann::json& j, const RuleItem& ri) { + j = ri.payload; + } + friend void from_json(const nlohmann::json& j, RuleItem& ri) { + ri.payload = j; + } + std::string getPayload(void) const {return payload;} + protected: + std::string payload; + }; + + class Ability : public RuleItem { public: std::string getFull() const {return abilities.at(getAbbrev());} - std::string getAbbrev() const {return abbrev;} + std::string getAbbrev() const {return getPayload();} operator std::string() const {return getAbbrev();} - virtual nlohmann::json toJson(void) const { - return getAbbrev(); - } bool operator<(const Ability& rhs) const {return getAbbrev() < rhs.getAbbrev();} bool operator==(const Ability& rhs) const {return getAbbrev() == rhs.getAbbrev();} operator bool() const {return ! getAbbrev().empty();} Ability() {} - explicit Ability(const nlohmann::json& data) : abbrev(data) { + Ability(const std::string& abbrev) { if(! abilities.contains(abbrev)) { throw std::invalid_argument("No such ability: " + abbrev); } + payload = abbrev; } virtual ~Ability() {} @@ -50,19 +61,14 @@ namespace rules { } private: - const std::string abbrev; - static const std::map<std::string, std::string> abilities; }; - class Skill : public Jsonable { + class Skill : public RuleItem { public: - std::string getName() const {return name;} + std::string getName() const {return getPayload();} Ability getAbility() const {return Ability(skill2ability.at(getName()));} operator std::string() const {return getName();} - virtual nlohmann::json toJson(void) const { - return getName(); - } bool operator<(const Skill& rhs) const {return getName() < rhs.getName();} bool operator==(const Skill& rhs) const {return getName() == rhs.getName();} operator bool() const {return ! getName().empty();} @@ -89,10 +95,11 @@ namespace rules { static Skill Persuasion() {return Skill("Persuasion");} Skill() {}; - explicit Skill(const nlohmann::json& data) : name(data) { + Skill(const std::string& name) { if(! skill2ability.contains(name)) { throw std::invalid_argument("No such skill: " + name); } + payload = name; } static Skill fromString(std::string s) { @@ -108,25 +115,21 @@ namespace rules { } private: - const std::string name; - static const std::map<std::string, std::string> skill2ability; }; - class Qualifier : public Jsonable { + class Qualifier : public RuleItem { public: Qualifier() {} - Qualifier(const nlohmann::json& data) : negative(data) { + Qualifier(const std::string& negative) { if(! negative2positive.contains(negative)) { throw std::invalid_argument("No such qualifier: " + negative); } + payload = negative; } - std::string getNegative() const {return negative;} + std::string getNegative() const {return getPayload();} std::string getPositive() const {return negative2positive.at(getNegative());} operator std::string() const {return getNegative();} - virtual nlohmann::json toJson(void) const { - return getNegative(); - } virtual ~Qualifier() {} bool operator==(const Qualifier& rhs) const {return getNegative() == rhs.getNegative();} @@ -135,8 +138,6 @@ namespace rules { static Qualifier Adamantine() {return Qualifier("non-adamantine");} private: - const std::string negative; - static const std::map<std::string, std::string> negative2positive; }; |