-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequester.cpp
More file actions
88 lines (75 loc) · 2.56 KB
/
Copy pathrequester.cpp
File metadata and controls
88 lines (75 loc) · 2.56 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
#include "requester.h"
Requester::Requester(QObject *parent)
: QObject{parent}
{
manage = new QNetworkAccessManager(this);
connect(manage, SIGNAL(finished(QNetworkReply*)), this, SLOT(showReply(QNetworkReply*)));
QUrl url("https://api.opendota.com/api/teams");
get_teams(url);
container = new DataContainer();
}
void Requester::get_teams(QUrl &path)
{
QNetworkRequest req(path);
req.setAttribute(QNetworkRequest::User, "teams");
manage->get(QNetworkRequest(req));
}
void Requester::get_heroes(int id)
{
mTeamID = id;
QString str = QString("https://api.opendota.com/api/teams/%1/heroes").arg(mTeamID);
QUrl url(str);
QNetworkRequest req(url);
req.setAttribute(QNetworkRequest::User, "heroes");
manage->get(QNetworkRequest(req));
}
void Requester::showReply(QNetworkReply *r)
{
// qDebug() << r->readAll();
QString type = r->request().attribute(QNetworkRequest::User).toString();
QByteArray response = r->readAll();
QJsonDocument jsonResponse = QJsonDocument::fromJson(response);
//qDebug() << jsonResponse;
if(jsonResponse.isArray())
{
QJsonArray jsonArray = jsonResponse.array();
if(type == "teams")
{
foreach (const QJsonValue &value, jsonArray)
{
QJsonObject jsonObj = value.toObject();
QString team_name = jsonObj["name"].toString();
int team_id = jsonObj["team_id"].toInt();
//team->team_list.insert(team_id,team_name);
// qDebug() << team_name << team_id;
Team *team = new Team();
team->id = team_id;
team->name = team_name;
container->addTeam(team);
}
emit teamsReady();
}
else if(type == "heroes")
{
QList<Hero*> list_1;
foreach (const QJsonValue &value, jsonArray)
{
QJsonObject obj = value.toObject();
QString name = obj["localized_name"].toString();
int games = obj["games_played"].toInt();
int wins = obj["wins"].toInt();
Hero *hero =new Hero(name,games,wins);
list_1.append(hero);
}
container->addHeroToTeam(mTeamID,list_1);
emit heroesReady();
}
}
}
void Requester::handleTeam_map(const QMap<int, QString> &mp_teams)
{
// for(auto it = mp_teams.begin(); it != mp_teams.end(); ++it)
// {
// qDebug() << "ID : " << it.key() << " , team name : " << it.value();
// }
}