Repository navigation
Expand file tree
/
Copy pathDataLoader.cpp
More file actions
103 lines (72 loc) · 2.47 KB
/
Copy pathDataLoader.cpp
File metadata and controls
103 lines (72 loc) · 2.47 KB
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
#include "DataLoader.hpp"
DataLoader::DataLoader(LogQueue& logQueue) : logQueue(logQueue) {
}
std::map<std::string, Move> DataLoader::loadMoves() {
std::map<std::string, Move> moves = {};
std::ifstream f(std::string(DATA_PATH) + "moves.json");
json data = json::parse(f);
for (const auto& move : data.items()) {
const json& object = move.value();
std::array<int, 2> pp = { 0, 0 };
if (!object.at("pp").is_null()) {
int ppVal = object.at("pp").get<int>();
pp = { ppVal, ppVal };
}
unsigned int priority = 0;
if (!object.at("priority").is_null()) {
int priorVal = object.at("priority").get<int>();
priority = priorVal;
}
MoveCategory category = MoveCategory::DEFAULT;
if (!object.at("category").is_null()) {
int catVal = object.at("category").get<int>();
category = static_cast<MoveCategory>(catVal);
}
unsigned int accuracy = 0;
if (!object.at("accuracy").is_null()) {
int accVal = object.at("accuracy").get<int>();
accuracy = accVal;
}
std::optional<int> power = std::nullopt;
if (!object.at("power").is_null()) {
power = object["power"];
}
Move mv = { pp, power, priority, accuracy, category, typeFromString(object["type"]), object["name"]};
moves[object.at("name").get<std::string>()] = mv;
}
return moves;
}
std::vector<PokemonTemplate> DataLoader::loadPokemon() {
std::vector<PokemonTemplate> pokemonTemplate = {};
std::ifstream f(std::string(DATA_PATH) + "pokemon.json");
json data = json::parse(f);
int pkmnCount = 0;
for (const auto& move : data.items()) {
pkmnCount++;
const json& object = move.value();
std::array<Types, 2> types = {};
for (int i = 0; i < object.at("types").size(); i++) {
std::string t = object.at("types").at(i);
types[i] = typeFromString(t);
}
if (object.at("types").size() == 1) {
types[1] = Types::NONE;
}
std::vector<LearnsetEntry> learnset = {};
for (int i = 0; i < object.at("learnset").size(); i++) {
json move = object.at("learnset").at(i);
learnset.push_back(LearnsetEntry{ move.at("move"), move.at("level") });
}
std::array<unsigned int, 6> stats;
for (int i = 0; i < 6; i++) {
stats[i] = object.at("stats").at(i).get<int>();
}
PokemonTemplate pkmn = { object.at("id"), object.at("name"), types, stats, learnset };
pokemonTemplate.push_back(pkmn);
}
log("Successfully loaded " + std::to_string(pkmnCount) + " Pokemon!");
return pokemonTemplate;
}
void DataLoader::log(const std::string& text) {
logQueue.log("DataLoader", text);
}