1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
#pragma once
#include "utils.h"
#include <vector>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <stdexcept>
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;}
bool operator==(const RuleItem& rhs) const {return getPayload() == rhs.getPayload();}
bool operator<(const RuleItem& rhs) const {return getPayload() < rhs.getPayload();}
operator std::string() const {return getPayload();}
operator bool() const {return ! getPayload().empty();}
protected:
std::string payload;
};
class Ability : public RuleItem {
public:
std::string getFull() const {return abilities.at(getAbbrev());}
std::string getAbbrev() const {return getPayload();}
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<std::string, std::string> abilities;
};
class Skill : public RuleItem {
public:
std::string getName() const {return getPayload();}
Ability getAbility() const {return Ability(skill2ability.at(getName()));}
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<std::string, std::string> 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());}
virtual ~Qualifier() {}
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<std::string, std::string> negative2positive;
};
class Condition : public RuleItem {
public:
Condition() {}
Condition(std::string name) {
utils::lower(name);
if(conditions.count(name) == 0) {
throw std::invalid_argument("No such condition: " + name);
}
payload = name;
}
virtual ~Condition() {}
void incExhaustion() {
if(payload.find("exhausted") == std::string::npos) {
throw std::invalid_argument("Cannot increment exhaustion on condition " + payload);
}
int exhLvl = utils::parseInt(std::string(1, payload.back()));
if(exhLvl >= 6) {
throw std::invalid_argument("Cannot increment exhaustion beyond level 6");
}
payload.back() = '0'+exhLvl+1;
}
private:
static const std::set<std::string> conditions;
};
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);
std::ostream& operator<<(std::ostream& os, const Condition& c);
template<typename T> T tryGetAbilityOrSkill(std::string src) {
try {
return T::fromString(src);
} catch(std::exception& e) {} // eat.
return T();
}
}
|