-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemDataLoader.cpp
More file actions
60 lines (53 loc) · 1.67 KB
/
ItemDataLoader.cpp
File metadata and controls
60 lines (53 loc) · 1.67 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
#include "ItemDataLoader.h"
#include "rapidjson/document.h"
void ItemDataLoader::connect()
{
QObject::connect(currentReply.get(), &QNetworkReply::finished, [this](){readWebsiteContent(currentReply.get());});
}
void ItemDataLoader::initialize()
{
apiUrl = apiUrl.replace("{type}", itemType).replace("{league}", league);\
}
void ItemDataLoader::parseItem(rapidjson::Value::ConstValueIterator iter)
{
ItemData* item = new ItemData();
item->name = (*iter)["name"].GetString();
item->chaosValue = (*iter)["chaosValue"].GetDouble();
if((*iter).HasMember("gemLevel"))
{
item->gemLevel = (*iter)["gemLevel"].GetInt();
}
items->push_back(std::shared_ptr<ItemData>(item));
}
void ItemDataLoader::onError(QNetworkReply::NetworkError errorCode)//TODO: provide better implementation
{
qDebug() << errorCode;
}
void ItemDataLoader::readWebsiteContent(QNetworkReply *reply)
{
++readWebsiteContentHits;
QString websiteContent = reply->readAll();
rapidjson::Document jsonWebsiteContent;
jsonWebsiteContent.Parse(websiteContent.toStdString().c_str());
if(!jsonWebsiteContent.IsNull())
{
for(rapidjson::Value::ConstValueIterator iter = jsonWebsiteContent["lines"].Begin(); iter != jsonWebsiteContent["lines"].End(); ++iter)
{
parseItem(iter);
}
}
if(readWebsiteContentHits == readWebsiteContentDesiredHits)
{
emit allItemsLoaded();
}
}
void ItemDataLoader::load()
{
initialize();
currentReply = std::unique_ptr<QNetworkReply>(accessManager->get(QNetworkRequest(QUrl(apiUrl))));
connect();
}
QVector<std::shared_ptr<ItemData>>* ItemDataLoader::getItems()
{
return items.get();
}