lolautoaccept/src/blitzapi.cpp

141 lines
5.8 KiB
C++

#include "blitzapi.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <Log.h>
#include "json.h"
// curl 'https://league-champion-aggregate.iesdev.com/graphql?query=query%20ChampionBuilds%28%24championId%3AInt%21%2C%24queue%3AQueue%21%2C%24role%3ARole%2C%24opponentChampionId%3AInt%2C%24key%3AChampionBuildKey%29%7BchampionBuildStats%28championId%3A%24championId%2Cqueue%3A%24queue%2Crole%3A%24role%2CopponentChampionId%3A%24opponentChampionId%2Ckey%3A%24key%29%7BchampionId%20opponentChampionId%20queue%20role%20builds%7BcompletedItems%7Bgames%20index%20averageIndex%20itemId%20wins%7Dgames%20mythicId%20mythicAverageIndex%20primaryRune%20runes%7Bgames%20index%20runeId%20wins%20treeId%7DskillOrders%7Bgames%20skillOrder%20wins%7DstartingItems%7Bgames%20startingItemIds%20wins%7DsummonerSpells%7Bgames%20summonerSpellIds%20wins%7Dwins%7D%7D%7D&variables=%7B%22championId%22%3A25%2C%22role%22%3A%22SUPPORT%22%2C%22queue%22%3A%22RANKED_SOLO_5X5%22%2C%22opponentChampionId%22%3Anull%2C%22key%22%3A%22PUBLIC%22%7D' -H 'Accept: application/json'
// query=query ChampionBuilds($championId:Int!,$queue:Queue!,$role:Role,$opponentChampionId:Int,$key:ChampionBuildKey){championBuildStats(championId:$championId,queue:$queue,role:$role,opponentChampionId:$opponentChampionId,key:$key){championId opponentChampionId queue role builds{completedItems{games index averageIndex itemId wins}games mythicId mythicAverageIndex primaryRune runes{games index runeId wins treeId}skillOrders{games skillOrder wins}startingItems{games startingItemIds wins}summonerSpells{games summonerSpellIds wins}wins}}}
// &variables={"championId":25,"role":"SUPPORT","queue":"RANKED_SOLO_5X5","opponentChampionId":null,"key":"PUBLIC"}
static const QString POSITIONNAMES[] = {"INVALID", "TOP", "JUNGLE", "MIDDLE", "BOTTOM", "SUPPORT"};
BlitzAPI::BlitzAPI() : RestClient("https://league-champion-aggregate.iesdev.com/graphql?") {}
BlitzAPI::ChampionInfo::ChampionInfo() {}
BlitzAPI::ChampionInfo::ChampionInfo(const QJsonObject& json) {
// kill order stuff
auto skillordersref = json["skillOrders"];
if(skillordersref.isArray()) {
QJsonArray arr = skillordersref.toArray();
if(!arr.empty()) {
auto skillorderref = arr.at(0);
if(skillorderref.isObject()) {
QJsonObject skillorder = skillorderref.toObject();
QJsonValueRef realorderref = skillorder["skillOrder"];
if(realorderref.isArray()) {
QJsonArray realorder = realorderref.toArray();
this->skillorder.reserve(realorder.size());
for(auto it : realorder) {
if(it.isDouble()) {
this->skillorder.push_back(it.toDouble());
}
}
}
}
}
}
// runes
// TODO: create a diffrent algorithm to choose wich runes should be picked, insted of just the first one
QJsonValue runesarrref = json["runes"];
std::vector<uint32_t>& runes = runepage.selectedAspects;
if(runesarrref.isArray()) {
QJsonArray runesarr = runesarrref.toArray();
runes.clear();
runes.resize(9, 0);// a list of runes, that are taken (the first one is a speare for the primary one)
for(auto it : runesarr) {
if(!it.isObject()) continue;
QJsonObject rune = it.toObject();
uint32_t index = rune["index"].toInt();
if(index <= 7) {
if(runes.at(index+1) == 0) { // index not set yet
auto runeid = rune["runeId"];
if(runeid.isDouble()) {
runes.at(index+1) = runeid.toDouble();
qDebug() << "found rune: index: " << index << " +1 set to: " << runes.at(index+1);
if(index == 0) {
runepage.primaryStyle = rune["treeId"].toInt();
} else if(index == 3) {
runepage.secondaryStyle = rune["treeId"].toInt();
}
}
}
}
}
}
// add the primary rune
runes.at(0) = getValue<uint32_t>(json, "primaryRune");
}
BlitzAPI::ChampionInfo BlitzAPI::getChampionInfo(uint32_t championID, Position p, uint32_t enemyChampionID) {
QJsonObject vars;
vars["championId"] = (int) championID;
if(p != Position::INVALID) {
vars["role"] = POSITIONNAMES[(int) p];
}
vars["queue"] = "RANKED_SOLO_5X5";
if(enemyChampionID == 0)
vars["opponentChampionId"] = QJsonValue::Null;
else
vars["opponentChampionId"] = (int) enemyChampionID;
vars["key"] = "PUBLIC"; // ? what does this do?
QJsonDocument jvars(vars);
const QString variables = jvars.toJson(QJsonDocument::Compact);
const QString query = "query ChampionBuilds($championId:Int!,$queue:Queue!,$role:Role,$opponentChampionId:Int,$key:ChampionBuildKey){championBuildStats(championId:$championId,queue:$queue,role:$role,opponentChampionId:$opponentChampionId,key:$key){championId opponentChampionId queue role builds{completedItems{games index averageIndex itemId wins}games mythicId mythicAverageIndex primaryRune runes{games index runeId wins treeId}skillOrders{games skillOrder wins}startingItems{games startingItemIds wins}summonerSpells{games summonerSpellIds wins}wins}}}";
const QString requeststr = "query=" + escape(query) + "&variables=" + escape(variables);
qDebug() << "GetChampionInfo requestVariables: " << variables << " requeststr: " << requeststr;
QJsonDocument doc;
try {
doc = request(requeststr);
} catch(RestClient::WebException& e) {
return {};
}
if(!doc.isObject()) {
// error
qCritical() << "could not get ChampionInfo. Returned Response: " << doc.toJson();
return {};
}
qDebug() << "championinfo Response: " << doc.toJson().trimmed();
QJsonObject obj = doc.object();
QJsonValueRef dataref = obj["data"];
if(!dataref.isObject()) return {};
QJsonObject data = dataref.toObject();
QJsonValueRef buildstatsref = data["championBuildStats"];
if(!buildstatsref.isObject()) return{};
QJsonObject buildstats = buildstatsref.toObject();
QJsonValueRef buildsref = buildstats["builds"];
if(!buildsref.isArray()) return {};
QJsonArray builds = buildsref.toArray();
if(builds.size() > 0) {
// just take the first
QJsonValue buildval = builds.at(0);
if(buildval.isObject()) {
return (ChampionInfo) buildval.toObject();
}
}
return {};
}