#pragma once #include "jsonable.h" #include "utils.h" #include #include #include #include #include #include namespace rules { 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 getPayload();} operator std::string() 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() {} Ability(const std::string& abbrev) { if(! abilities.contains(abbrev)) { throw std::invalid_argument("No such ability: " + abbrev); } payload = abbrev; } virtual ~Ability() {} static Ability Str() {return Ability("str");} static Ability Dex() {return Ability("dex");} static Ability Con() {return Ability("con");} static Ability Int() {return Ability("int");} static Ability Wis() {return Ability("wis");} static Ability Cha() {return Ability("cha");} static Ability fromString(std::string s) { utils::lower(s); for(auto [abbrev, full] : abilities) { utils::lower(full); if(s == abbrev || s == full) { return Ability(abbrev); } } throw std::invalid_argument("Cannot find an ability for input: \"" + s + "\""); } private: static const std::map abilities; }; class Skill : public RuleItem { public: std::string getName() const {return getPayload();} Ability getAbility() const {return Ability(skill2ability.at(getName()));} operator std::string() 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();} virtual ~Skill() {} static Skill Athletics() {return Skill("Athletics");} static Skill Acrobatics() {return Skill("Acrobatics");} static Skill SleightOfHand() {return Skill("Sleight of Hand");} static Skill Stealth() {return Skill("Stealth");} static Skill Arcana() {return Skill("Arcana");} static Skill History() {return Skill("History");} static Skill Investigation() {return Skill("Investigation");} static Skill Nature() {return Skill("Nature");} static Skill Religion() {return Skill("Religion");} static Skill AnimalHandling() {return Skill("Animal Handling");} static Skill Insight() {return Skill("Insight");} static Skill Medicine() {return Skill("Medicine");} static Skill Perception() {return Skill("Perception");} static Skill Survival() {return Skill("Survival");} static Skill Deception() {return Skill("Deception");} static Skill Intimidation() {return Skill("Intimidation");} static Skill Performance() {return Skill("Performance");} static Skill Persuasion() {return Skill("Persuasion");} Skill() {}; 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) { utils::lower(s); for(auto& [name, _] : skill2ability) { std::string n = name; utils::lower(n); if(s == n) { return Skill(name); } } throw std::invalid_argument("Cannot find a skill for input: \"" + s + "\""); } private: static const std::map skill2ability; }; class Qualifier : public RuleItem { public: Qualifier() {} Qualifier(const std::string& negative) { if(! negative2positive.contains(negative)) { throw std::invalid_argument("No such qualifier: " + negative); } payload = negative; } std::string getNegative() const {return getPayload();} std::string getPositive() const {return negative2positive.at(getNegative());} operator std::string() 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: 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); template T tryGetAbilityOrSkill(std::string src) { try { return T::fromString(src); } catch(std::exception& e) {} // eat. return T(); } }