diff options
author | Your Name <you@example.com> | 2021-05-05 09:44:50 -0400 |
---|---|---|
committer | Your Name <you@example.com> | 2021-05-05 09:44:50 -0400 |
commit | 2a9f262e6db5906db445d465e500d7ba8c90fab3 (patch) | |
tree | 34f850754b0c9114ede9d7b2bb8da90dffddc4fe /src/rules.h | |
parent | 8614137f7f32f2c9f3c11419110cd70dd7f3b505 (diff) | |
download | dmtool-2a9f262e6db5906db445d465e500d7ba8c90fab3.tar.gz dmtool-2a9f262e6db5906db445d465e500d7ba8c90fab3.tar.bz2 dmtool-2a9f262e6db5906db445d465e500d7ba8c90fab3.zip |
Implemented additional commands
Diffstat (limited to 'src/rules.h')
-rw-r--r-- | src/rules.h | 60 |
1 files changed, 50 insertions, 10 deletions
diff --git a/src/rules.h b/src/rules.h index 18cdef0..f41f7ad 100644 --- a/src/rules.h +++ b/src/rules.h @@ -1,5 +1,6 @@ #pragma once #include "jsonable.h" +#include "utils.h" #include <vector> #include <map> #include <string> @@ -19,9 +20,14 @@ namespace rules { } 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) {} + explicit Ability(const nlohmann::json& data) : abbrev(data) { + if(! abilities.contains(abbrev)) { + throw std::invalid_argument("No such ability: " + abbrev); + } + } virtual ~Ability() {} @@ -32,10 +38,10 @@ 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); + static Ability fromString(std::string s) { + utils::lower(s); for(auto [abbrev, full] : abilities) { - transform(full.begin(), full.end(), full.begin(), ::tolower); + utils::lower(full); if(s == abbrev || s == full) { return Ability(abbrev); } @@ -59,6 +65,7 @@ namespace rules { } 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();} virtual ~Skill() {} @@ -80,14 +87,19 @@ namespace rules { static Skill Intimidation() {return Skill("Intimidation");} static Skill Performance() {return Skill("Performance");} static Skill Persuasion() {return Skill("Persuasion");} + + Skill() {}; + explicit Skill(const nlohmann::json& data) : name(data) { + if(! skill2ability.contains(name)) { + throw std::invalid_argument("No such skill: " + name); + } + } - explicit Skill(const nlohmann::json& data) : name(data) {} - - static Skill string2skill(std::string s) { - transform(s.begin(), s.end(), s.begin(), ::tolower); + static Skill fromString(std::string s) { + utils::lower(s); for(auto& [name, _] : skill2ability) { std::string n = name; - transform(n.begin(), n.end(), n.begin(), ::tolower); + utils::lower(n); if(s == n) { return Skill(name); } @@ -100,7 +112,35 @@ namespace rules { static const std::map<std::string, std::string> skill2ability; }; - + + class Qualifier : public Jsonable { + public: + Qualifier() {} + Qualifier(const nlohmann::json& data) : negative(data) { + if(! negative2positive.contains(negative)) { + throw std::invalid_argument("No such qualifier: " + negative); + } + } + std::string getNegative() const {return negative;} + 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();} + + static Qualifier Magical() {return Qualifier("nonmagical");} + static Qualifier Silvered() {return Qualifier("non-silvered");} + static Qualifier Adamantine() {return Qualifier("non-adamantine");} + + private: + const std::string negative; + + static const std::map<std::string, std::string> negative2positive; + }; + std::ostream& operator<<(std::ostream& os, const Ability& a); std::ostream& operator<<(std::ostream& os, const Skill& s); + std::ostream& operator<<(std::ostream& os, const Qualifier& q); } |