aboutsummaryrefslogtreecommitdiff
path: root/src/rules.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/rules.h')
-rw-r--r--src/rules.h60
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);
}