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
|
#include "JsonRepository.h"
#include <fstream>
#include <functional>
#include <map>
#include <stdexcept>
#include <string>
#include <nlohmann/json.hpp>
#include <kurator/universe/NotFound.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/TurretType.h>
using json = nlohmann::json;
namespace kurator
{
namespace universe
{
template <typename Type>
void
maybe_to(const json& item, const char* member, Type& output)
{
if (item.contains(member))
item.at(member).get_to(output);
}
void
from_json(const json& item, ShipType& ship)
{
item.at("name").get_to(ship.name);
item.at("base_structure_points").get_to(ship.base_structure_points);
item.at("base_armour_points").get_to(ship.base_armour_points);
item.at("base_shield_points").get_to(ship.base_shield_points);
item.at("max_speed").get_to(ship.max_speed);
maybe_to(item, "base_structure_resists", ship.base_structure_resists);
maybe_to(item, "base_armour_resists", ship.base_armour_resists);
maybe_to(item, "base_shield_resists", ship.base_shield_resists);
}
void
from_json(const json& item, TurretType& turret)
{
item.at("name").get_to(turret.name);
maybe_to(item, "base_damage", turret.base_damage);
maybe_to(item, "rounds", turret.rounds);
maybe_to(item, "rate_of_fire", turret.rate_of_fire);
maybe_to(item, "reload", turret.reload);
maybe_to(item, "optimal_range", turret.optimal_range);
maybe_to(item, "falloff_modifier", turret.falloff_modifier);
maybe_to(item, "falloff_intensity", turret.falloff_intensity);
}
template<typename Type>
std::map<std::string, Type>
parse(const std::string& path)
try {
std::fstream file{path, file.binary | file.in}; // std::filesystem?
if (!file.is_open())
throw "could not load universe part"; // FIXME: proper errors and handling above
std::map<std::string, Type> map;
const auto root = json::parse(file);
for (const auto& item : root) {
auto def = item.get<Type>();
map[def.name] = def;
}
return map;
}
catch (const json::parse_error&) {
throw;
}
JsonRepository::JsonRepository(const std::string& path)
{
ships = parse<ShipType>(path + "/ship_types.json");
turrets = parse<TurretType>(path + "/turret_types.json");
}
ShipType
JsonRepository::ship_type() const
{
const auto it = ships.begin();
if (it == ships.end())
throw NotFound("any/default");
return it->second;
}
ShipType
JsonRepository::ship_type(const std::string& id) const
try {
return ships.at(id);
}
catch (const std::out_of_range&) {
throw NotFound(id);
}
TurretType
JsonRepository::turret_type() const
{
const auto it = turrets.begin();
if (it == turrets.end())
throw NotFound("any/default");
return it->second;
}
TurretType
JsonRepository::turret_type(const std::string& id) const
try {
return turrets.at(id);
}
catch (const std::out_of_range&) {
throw NotFound(id);
}
void
JsonRepository::for_ship_types(std::function<void(const ShipType&)> func) const
{
for (const auto& [_, type] : ships)
func(type);
}
void
JsonRepository::for_turret_types(std::function<void(const TurretType&)> func) const
{
for (const auto& [_, type] : turrets)
func(type);
}
} // namespace universe
} // namespace kurator
|