-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_helper.cpp
194 lines (158 loc) · 5.32 KB
/
data_helper.cpp
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "data_helper.h"
#include "category.h"
#include "level.h"
#include "leveled_category.h"
#include "game.h"
#include "run.h"
/* QCore includes */
#include <QFile>
#include <QJsonArray>
/* std includes */
#include <exception>
#include <stdexcept>
#include <iostream>
DataHelper::DataHelper(const QString& filename) {
opened = false;
QFile file(filename);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
std::cerr << "Failed to open data file!" << std::endl;
return;
}
QJsonParseError json_error;
dataDoc = QJsonDocument::fromJson(file.readAll(), &json_error);
if (json_error.error != QJsonParseError::ParseError::NoError) {
std::cerr << "DataHelper: Failed to parse JSON file. " << json_error.errorString().toStdString() << std::endl;
return;
}
if (!dataDoc.isObject()) {
std::cerr << "DataHelper: Document root is not an object." << std::endl;
return;
}
root = dataDoc.object();
opened = true;
}
bool DataHelper::isOpen() {
return opened;
}
Game DataHelper::getGame() {
return buildGame(root);
}
Run DataHelper::buildRun(const QJsonObject& runObject) {
auto iter = runObject.find("place");
if (iter == runObject.end() || !iter.value().isDouble()) {
throw std::runtime_error("Could not find int member 'place'");
}
int place = iter.value().toInt();
iter = runObject.find("time");
if (iter == runObject.end() || !iter.value().isDouble()) {
throw std::runtime_error("Could not find int member 'time'");
}
int time = iter.value().toInt();
iter = runObject.find("username");
if (iter == runObject.end() || !iter.value().isString()) {
throw std::runtime_error("Could not find string member 'username'");
}
QString user = iter.value().toString();
iter = runObject.find("submitted_date");
if (iter == runObject.end()) {
throw std::runtime_error("Could not find string member 'submitted_date'");
}
/*else if (!iter.value().isString()) {
throw std::runtime_error("Member 'submitted_date' is not a string");
}*/
QDateTime submittedDate = QDateTime::fromString(iter.value().toString(), Qt::ISODate);
return Run(place, time, user, submittedDate);
}
Category DataHelper::buildCategory(const QString& name, const QJsonObject& object) {
QList<Run> runs;
auto iter = object.find("runs");
if (iter == object.end() || !iter.value().isArray()) {
throw std::runtime_error("Could not find array member 'runs'");
}
QJsonArray runArray = iter.value().toArray();
auto arrayIter = runArray.begin();
while (arrayIter != runArray.end()) {
if (!arrayIter->isObject()) {
throw std::runtime_error("Run array element is not an object.");
}
try {
runs.append(buildRun(arrayIter->toObject()));
}
catch (const std::exception& e) {
// Catch, display, and rethrow exceptions.
std::cerr << e.what() << std::endl;
throw;
}
++arrayIter;
}
return Category(name, runs);
}
Level DataHelper::buildLevel(const QString& name, const QJsonArray& levelArray) {
QList<Run> runs;
auto iter = levelArray.begin();
while (iter != levelArray.end()) {
if (!iter->isObject()) {
throw std::runtime_error("Run array element is not an object.");
}
try {
runs.append(buildRun(iter->toObject()));
}
catch (const std::exception& e) {
// Catch, display, and rethrow exceptions.
std::cerr << e.what() << std::endl;
throw;
}
++iter;
}
return Level(name, runs);
}
LeveledCategory DataHelper::buildLeveledCategory(const QString& name, const QJsonObject& object) {
QList<Level> levels;
auto iter = object.begin();
while (iter != object.end()) {
if (!iter.value().isArray()) {
throw std::runtime_error("Leveled category member is not an array.");
}
try {
levels.append(buildLevel(iter.key(), iter.value().toArray()));
}
catch (const std::exception& e) {
// Catch, display, and rethrow exceptions.
std::cerr << e.what() << std::endl;
throw;
}
++iter;
}
return LeveledCategory(name, levels);
}
Game DataHelper::buildGame(const QJsonObject& rootObject) {
QList<Category> categories;
QList<LeveledCategory> leveledCategories;
auto iter = rootObject.begin();
while (iter != rootObject.end()) {
if (!iter.value().isObject()) {
throw std::runtime_error("Root object member is not an object.");
}
QJsonObject object = iter.value().toObject();
if (object.contains("runs")) {
try {
categories.append(buildCategory(iter.key(), object));
}
catch (const std::exception& e) {
// Rethrow exception
throw;
}
}
else {
try {
leveledCategories.append(buildLeveledCategory(iter.key(), object));
}
catch (const std::exception& e) {
//Rethrow exception
throw;
}
}
++iter;
}
return Game(categories, leveledCategories);
}