-
Notifications
You must be signed in to change notification settings - Fork 0
/
heroparser.cpp
54 lines (44 loc) · 1.38 KB
/
heroparser.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
// Qt
#include <QFileInfo>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonDocument>
#include <QDebug>
// Local
#include "heroparser.h"
QList<Hero> HeroParser::parse(QString heroesFilePath)
{
QList<Hero> heroes;
QFileInfo fileInfo(heroesFilePath);
QFile jsonFile(fileInfo.absoluteFilePath());
if (!jsonFile.open(QIODevice::ReadOnly)) {
qDebug() << "File open error: " << heroesFilePath;
return heroes;
}
QByteArray ba = jsonFile.readAll();
jsonFile.close();
QJsonParseError error;
QJsonDocument document = QJsonDocument::fromJson(ba, &error);
if (document.isNull())
{
qDebug() << "heroes.json parse error: " << error.errorString();
}
for (auto qObj: document.array())
{
QJsonObject obj = qObj.toObject();
Hero h;
h.name = obj.value("name").toString();
h.strength = obj.value("str").toDouble();
h.strength_per_lvl = obj.value("str_per_lvl").toDouble();
h.agility = obj.value("agi").toDouble();
h.agility_per_lvl = obj.value("agi_per_lvl").toDouble();
h.intelligence = obj.value("int").toDouble();
h.intelligence_per_lvl = obj.value("int_per_lvl").toDouble();
h.armor = obj.value("armor").toDouble();
h.movement_speed = obj.value("movement_speed").toDouble();
h.melee = obj.value("melee").toBool();
h.attack_range = obj.value("attack_range").toDouble();
heroes.append(h);
}
return heroes;
}