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
|
#pragma once
#include "json.hpp"
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <memory>
#include <stdexcept>
namespace utils {
nlohmann::json loadJson(const std::string& path);
// Recursively loads all .json files under directory
// If called multiple times with same directory, returns a cached result.
std::vector<nlohmann::json> loadAllJson(const std::string& directory);
// looks up directory in settings. Returns element matching name.
nlohmann::json loadJson(const std::string& type, const std::string& name);
void saveJson(const nlohmann::json& data, const std::string& path);
template<typename S, typename D> std::shared_ptr<S> loadDFromJson(const nlohmann::json& data) {
try {
return std::shared_ptr<S>(new D(loadJson(data["type"], data["name"]), data));
} catch(std::exception& e) {
// Covers errors in building the creature or fs traversal.
// Fall back on the data passed in.
return std::shared_ptr<S>(new D(data, data));
}
}
template<typename Container> std::string join(Container parts, std::string joiner) {
std::stringstream out;
bool isFirst = true;
for(std::string p : parts) {
if(! isFirst) {
out << joiner;
}
isFirst = false;
out << p;
}
return out.str();
}
const std::map<std::string, int> cpValue {
{"cp", 1},
{"sp", 10},
{"ep", 50},
{"gp", 100},
{"pp", 1000}
};
// Accepts coins formatted "X Yp" where X is an integer and Y is any of c, s, e, g, p.
int coins2copper(const std::string& coins);
// Greedily selects highest coin values to minimize total number of coins
// Returns a vector of pairs mapping coin type to coint
std::vector<std::pair<std::string, int>> copper2coins(int coppers);
std::string getCostString(int coppers);
std::string toOrdinal(std::size_t number);
}
|