-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.cpp
206 lines (200 loc) · 7.65 KB
/
reader.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
195
196
197
198
199
200
201
202
203
204
205
206
#include "reader.hpp"
#include <QtDebug>
Reader::Reader(QObject * parent) : QObject(parent)
{
manager = new QNetworkAccessManager();
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(parse(QNetworkReply*)));
}
void Reader::read()
{
manager->get(QNetworkRequest(QUrl("http://www.erepublik.com/en/battles/log/" + id)));
qDebug() << "reading";
}
void Reader::readData()
{
manager->get(QNetworkRequest(QUrl("http://www.erepublik.com/en/battles/show/" + id)));
}
void Reader::setID(int id)
{
this->id = QString::number(id);
}
void Reader::getPlayer(QString id)
{
manager->get(QNetworkRequest(QUrl("http://api.erepublik.com/v1/feeds/citizens/" + id)));
}
void Reader::parse(QNetworkReply *r)
{
QLocale loc = QLocale(QLocale::C);
QUrl url = r->url();
QByteArray ba = r->readAll();
if (url == QUrl("http://www.erepublik.com/en/battles/log/" + id))
{
bool finished = false;
if (ba.size() < 90)
finished = true;
QList<QByteArray> list = ba.split('\n');
for(int i = 0; i < list.size(); i++)
{
if (list[i].contains("current_dp:"))
{
qDebug() << list[i];
QList<QByteArray> chunks = list[i].split(':');
QByteArray num = chunks[1];
num.chop(1);
num = num.replace(" ", "");
emit wall(num.toInt(), finished);
}
}
}
else if (url == QUrl("http://www.erepublik.com/en/battles/show/" + id))
{
QList<QByteArray> list = ba.split('\n');
int wall;
QDateTime critical;
QDateTime server;
for(int i = 0; i < list.size(); i++)
{
if (list[i].contains("original_dp"))
{
QList<QByteArray> chunks = list[i].split('=');
QByteArray num = chunks[1];
num.chop(1);
num = num.replace(" ", "");
wall =num.toInt();
}
else if(list[i].contains("battle_critical_at"))
{
QList<QByteArray> chunks = list[i].split('=');
QByteArray num = chunks[1];
num.chop(9);
num = num.mid(16);
critical = loc.toDateTime(QString::fromAscii(num), "d MMM yyyy HH:mm:ss");
}
else if(list[i].contains("server_time"))
{
QList<QByteArray> chunks = list[i].split('=');
QByteArray num = chunks[1];
num.chop(9);
num = num.mid(16);
server = loc.toDateTime(QString::fromAscii(num), "d MMM yyyy HH:mm:ss");
}
}
QTime timeLeft = QTime(0,0,0,0);
timeLeft = timeLeft.addSecs(server.secsTo(critical));
qDebug() << timeLeft.toString();
emit startCounting(wall, timeLeft);
manager->get(QNetworkRequest(QUrl("http://api.erepublik.com/v1/feeds/battle_logs/" + id + "/0")));
}
else if (url ==QUrl("http://api.erepublik.com/v1/feeds/battle_logs/" + id + "/0"))
{
QDomDocument doc;
doc.setContent(ba);
QDomNode root = doc.documentElement();
QDomNode battle = root.namedItem("battle-info");
if (!battle.isNull())
{
QDomNode war = battle.namedItem("war");
QDomNode att = battle.namedItem("attacker");
if (!att.isNull())
attacker = att.toElement().text();
QDomNode def = battle.namedItem("defender");
if (!def.isNull())
defender = def.toElement().text();
if (!war.isNull())
{
war_id = war.toElement().text();
manager->get(QNetworkRequest(QUrl("http://api.erepublik.com/v1/feeds/war/" + war_id)));
}
}
}
else if (url == QUrl("http://api.erepublik.com/v1/feeds/war/" + war_id))
{
QDomDocument doc;
doc.setContent(ba);
QDomNode root = doc.documentElement();
QDomNode battles = root.namedItem("battles");
QDomNode battle = battles.firstChild();
while (!battle.isNull())
{
if (battle.namedItem("battle-id").toElement().text() == id)
{
region = battle.namedItem("region").toElement().text();
qDebug() << region;
qDebug() << attacker;
qDebug() << defender;
if (attacker != defender)
emit battleInfo("Battle in region "
+ region
+ " between "
+ attacker
+ " and "
+ defender
+ " | http://www.erepublik.com/en/battles/show/" + id);
else emit battleInfo("Independence war in region "
+ region
+ " against country "
+ defender
+ " | http://www.erepublik.com/en/battles/show/" + id);
}
battle = battle.nextSibling();
}
}
else if (url.toString().contains("http://api.erepublik.com/v1/feeds/citizens/"))
{
QDomDocument doc;
doc.setContent(ba);
QDomNode root = doc.documentElement();
double bonus = 1.2;
QString rank = root.namedItem("military-rank").toElement().text();
double wellness = root.namedItem("wellness").toElement().text().toDouble();
double strength = root.namedItem("strength").toElement().text().toDouble();
QString name = root.namedItem("name").toElement().text();
QString pregion = doc.namedItem("region").toElement().text();
qDebug() << rank << wellness << strength << name << pregion;
if (rank == "Private")
bonus = 1.2;
else if (rank == "Corporal")
bonus = 1.4;
else if (rank == "Sergeant")
bonus = 1.6;
else if (rank == "Lieutenant")
bonus = 1.8;
else if (rank == "Captain")
bonus = 2.0;
else if (rank == "Colonel")
bonus = 2.2;
else if (rank == "General")
bonus = 2.4;
else if (rank == "Field Marshal")
bonus = 2.6;
bonus = bonus * strength;
double q0 = 0.5 * bonus;
double q1 = 1.2 * bonus;
double q2 = 1.4 * bonus;
double q3 = 1.6 * bonus;
double q4 = 1.8 * bonus;
double q5 = 2.0 * bonus;
double wbmax = 1.75 * 2.0;
double wb = (1.0 + (wellness - 25)/100.0) * 2.0;
QString pid = "Player: " + name
+ "; Region: " + pregion
+ "; Well:" + QString::number(wellness, 'f', 2)
+ "; Str: " + QString::number(strength, 'f', 2)
+ "; Dmg: (Max): "
+ QString::number(q0 * wbmax, 'f', 0) + " "
+ QString::number(q1 * wbmax, 'f', 0) + " "
+ QString::number(q2 * wbmax, 'f', 0) + " "
+ QString::number(q3 * wbmax, 'f', 0) + " "
+ QString::number(q4 * wbmax, 'f', 0) + " "
+ QString::number(q5 * wbmax, 'f', 0)
+ "; (Cur): "
+ QString::number(q0 * wb, 'f', 0) + " "
+ QString::number(q1 * wb, 'f', 0) + " "
+ QString::number(q2 * wb, 'f', 0) + " "
+ QString::number(q3 * wb, 'f', 0) + " "
+ QString::number(q4 * wb, 'f', 0) + " "
+ QString::number(q5 * wb, 'f', 0);
emit playerInfo(pid);
}
r->deleteLater();
}