From 2a9f262e6db5906db445d465e500d7ba8c90fab3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 5 May 2021 09:44:50 -0400 Subject: Implemented additional commands --- src/rules.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 10 deletions(-) (limited to 'src/rules.h') 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 #include #include @@ -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 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 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); } -- cgit v1.2.3