375 lines
9.4 KiB
C++
375 lines
9.4 KiB
C++
#include "clientapi.h"
|
|
|
|
#include <cassert>
|
|
#include <iomanip>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
|
|
#include <Log.h>
|
|
|
|
#include "json.h"
|
|
|
|
ClientAPI::ClientAPI(const ClientAccess& ca) : RestClient(ca.getURL()), access(ca), memImageCache(40), imageCache("runes", "") {
|
|
basicauth = ca.getBasicAuth();
|
|
disableCertCheck = true;
|
|
// enableDebugging();
|
|
}
|
|
ClientAPI::~ClientAPI() {}
|
|
|
|
ClientAPI::ReadyCheckState ClientAPI::getReadyCheckState() {
|
|
QJsonDocument doc = request("lol-matchmaking/v1/ready-check");
|
|
|
|
if (doc.isObject()) {
|
|
return toReadyCheckState(doc.object());
|
|
}
|
|
|
|
return ClientAPI::ReadyCheckState::NONE;
|
|
}
|
|
|
|
ClientAPI::GameflowPhase ClientAPI::getGameflowPhase() {
|
|
// it is just a json-string no object
|
|
QByteArray data = requestRaw("lol-gameflow/v1/gameflow-phase");
|
|
|
|
QString datastr = QString::fromLocal8Bit(data);
|
|
if (data.size() > 2) {
|
|
datastr = datastr.mid(1, datastr.size() -2);
|
|
|
|
return toGameflowPhase(datastr);
|
|
}
|
|
|
|
return ClientAPI::GameflowPhase::NONE;
|
|
}
|
|
|
|
void ClientAPI::acceptMatch() {
|
|
request("lol-matchmaking/v1/ready-check/accept", Method::POST);
|
|
}
|
|
|
|
void ClientAPI::declineMatch() {
|
|
request("lol-matchmaking/v1/ready-check/decline", Method::POST);
|
|
}
|
|
|
|
ClientAPI::ChampSelectSession ClientAPI::getChampSelectSession() {
|
|
QJsonDocument doc = request("lol-champ-select/v1/session");
|
|
|
|
if(doc.isObject()) {
|
|
return (ChampSelectSession) doc.object();
|
|
}
|
|
return {};
|
|
}
|
|
|
|
bool ClientAPI::setChampSelectAction(int32_t actionid, int32_t champid, bool completed) {
|
|
QJsonObject requestj;
|
|
requestj["championId"] = champid;
|
|
requestj["completed"] = completed;
|
|
requestj["id"] = actionid;
|
|
QJsonDocument requestdoc(requestj);
|
|
const QString requeststr = QString::fromLocal8Bit(requestdoc.toJson(QJsonDocument::JsonFormat::Compact));
|
|
qDebug().noquote() << "requeststr: " << requeststr;
|
|
QJsonDocument doc = request("lol-champ-select/v1/session/actions/" + QString::number(actionid), Method::PATCH, requeststr);
|
|
|
|
QString error;
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
auto errref = obj["errorCode"];
|
|
auto msgref = obj["message"];
|
|
if(errref.isString()) {
|
|
error = errref.toString() + " ";
|
|
}
|
|
if(msgref.isString()) {
|
|
error += msgref.toString();
|
|
}
|
|
}
|
|
|
|
qDebug() << "patching action: " << actionid << " error: " << error;
|
|
return error.isEmpty();
|
|
}
|
|
|
|
ClientAPI::PlayerInfo ClientAPI::getSelf() {
|
|
QJsonDocument doc = request("lol-chat/v1/me");
|
|
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
|
|
PlayerInfo info;
|
|
info.gameName = getValue<QString>(obj, "gameName");
|
|
info.name = getValue<QString>(obj, "name");
|
|
info.statusMessage = getValue<QString>(obj, "statusMessage", "");
|
|
info.summonerid = getValue<uint64_t>(obj, "summonerId");
|
|
|
|
auto lolref = obj["lol"];
|
|
if(lolref.isObject()) {
|
|
QJsonObject lol = lolref.toObject();
|
|
info.puuid = getValue<QString>(lol, "puuid");
|
|
info.level = getValue<int32_t>(lol, "level");
|
|
}
|
|
|
|
return info;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void ClientAPI::dodge() {
|
|
QJsonDocument doc = request("lol-login/v1/session/invoke?destination=lcdsServiceProxy&method=call&args=[\"\",\"teambuilder-draft\",\"quitV2\", \"\"]", Method::POST, "");
|
|
qDebug() << "dodge result:" << doc;
|
|
}
|
|
|
|
static std::vector<int32_t> fromArrayToVector(const QJsonArray& arr) {
|
|
std::vector<int32_t> out;
|
|
out.reserve(arr.size());
|
|
for (auto it = arr.begin(); it != arr.end(); ++it) {
|
|
if (it->isDouble())
|
|
out.push_back(it->toInt());
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::vector<int32_t> ClientAPI::getBannableChampIDs() {
|
|
QJsonDocument doc = request("lol-champ-select/v1/bannable-champion-ids");
|
|
|
|
if (doc.isArray()) {
|
|
return fromArrayToVector(doc.array());
|
|
}
|
|
|
|
return {}; // empty vector
|
|
}
|
|
|
|
std::vector<int32_t> ClientAPI::getPickableChampIDs() {
|
|
QJsonDocument doc = request("lol-champ-select/v1/pickable-champion-ids");
|
|
|
|
if (doc.isArray()) {
|
|
return fromArrayToVector(doc.array());
|
|
}
|
|
|
|
return {}; // empty vector
|
|
}
|
|
|
|
ClientAPI::TimerInfo ClientAPI::getTimerInfo() {
|
|
QJsonDocument doc = request("lol-champ-select/v1/session/timer");
|
|
|
|
if (!doc.isObject()) return {};
|
|
|
|
QJsonObject obj = doc.object();
|
|
|
|
return (TimerInfo) obj;
|
|
}
|
|
|
|
std::vector<ClientAPI::Conversation> ClientAPI::getAllConversations() {
|
|
std::vector<Conversation> out;
|
|
QJsonDocument doc = request("lol-chat/v1/conversations");
|
|
if(doc.isArray()) {
|
|
QJsonArray arr = doc.array();
|
|
out.reserve(arr.size());
|
|
|
|
for(auto elem : arr) {
|
|
if(elem.isObject()) {
|
|
out.push_back((Conversation) elem.toObject());
|
|
}
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
ClientAPI::Message ClientAPI::sendMessage(const QString& chatid, const QString& messagebody) {
|
|
QJsonObject requestj;
|
|
requestj["body"] = messagebody;
|
|
QJsonDocument requestdoc(requestj);
|
|
const QString requeststr = QString::fromLocal8Bit(requestdoc.toJson(QJsonDocument::JsonFormat::Compact));
|
|
qDebug().noquote() << "requeststr: " << requeststr;
|
|
QJsonDocument doc = request("lol-chat/v1/conversations/" + chatid + "/messages", Method::POST, requeststr);
|
|
|
|
QString error;
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
auto errref = obj["errorCode"];
|
|
auto msgref = obj["message"];
|
|
if(errref.isString()) {
|
|
error = errref.toString() + " ";
|
|
}
|
|
if(msgref.isString()) {
|
|
error += msgref.toString();
|
|
}
|
|
|
|
if(error.isEmpty()) {
|
|
return (Message) obj;
|
|
}
|
|
}
|
|
|
|
qDebug() << "send message error: " << error;
|
|
return {};
|
|
}
|
|
|
|
|
|
ClientAPI::RunePage ClientAPI::getCurrentRunePage() {
|
|
QJsonDocument doc = request("lol-perks/v1/currentpage");
|
|
|
|
if(!doc.isObject()) return {};
|
|
|
|
return (RunePage) doc.object();
|
|
}
|
|
|
|
std::vector<ClientAPI::RunePage> ClientAPI::getAllRunePages() {
|
|
QJsonDocument doc = request("lol-perks/v1/pages");
|
|
|
|
if(!doc.isArray()) return {};
|
|
|
|
QJsonArray arr = doc.array();
|
|
std::vector<RunePage> out;
|
|
out.reserve(arr.size());
|
|
for(auto it : arr) {
|
|
if(it.isObject()) {
|
|
out.push_back((RunePage) it.toObject());
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
bool ClientAPI::selectRunePage(uint64_t id) {
|
|
QJsonDocument doc = request("lol-perks/v1/currentpage", Method::PUT, QString::number(id));
|
|
|
|
if(doc.isEmpty()) return true; // ok
|
|
|
|
// error
|
|
qWarning() << "error selecting runepage: " << id << " " << doc.toJson();
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ClientAPI::editRunePage(const RunePage& page) {
|
|
QJsonObject pagereq;
|
|
|
|
pagereq["name"] = page.name;
|
|
pagereq["primaryStyleId"] = (int) page.runepage.primaryStyle;
|
|
pagereq["subStyleId"] = (int) page.runepage.secondaryStyle;
|
|
|
|
QJsonArray selected;
|
|
for(uint32_t sel : page.runepage.selectedAspects) {
|
|
selected.push_back((int) sel);
|
|
}
|
|
|
|
pagereq["selectedPerkIds"] = selected;
|
|
|
|
QJsonDocument reqdoc(pagereq);
|
|
const QString requestdocstr = QString::fromLocal8Bit(reqdoc.toJson(QJsonDocument::JsonFormat::Compact));
|
|
qInfo().noquote() << "requeststr: " << requestdocstr;
|
|
QJsonDocument doc = request("lol-perks/v1/pages/" + QString::number(page.id), Method::PUT, requestdocstr);
|
|
|
|
if(doc.isObject() && doc["isValid"].isBool() && doc["isValid"].toBool()) return true; // ok
|
|
|
|
// error
|
|
qWarning() << "error editing runepage: " << page.id << " " << doc.toJson();
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ClientAPI::createRunePage(const RunePage& page) {
|
|
QJsonObject pagereq;
|
|
|
|
pagereq["name"] = page.name;
|
|
pagereq["primaryStyleId"] = (int) page.runepage.primaryStyle;
|
|
pagereq["subStyleId"] = (int) page.runepage.secondaryStyle;
|
|
|
|
QJsonArray selected;
|
|
for(uint32_t sel : page.runepage.selectedAspects) {
|
|
selected.push_back((int) sel);
|
|
}
|
|
|
|
pagereq["selectedPerkIds"] = selected;
|
|
|
|
QJsonDocument reqdoc(pagereq);
|
|
const QString requestdocstr = reqdoc.toJson(QJsonDocument::JsonFormat::Compact);
|
|
qInfo() << "requeststr: " << requestdocstr;
|
|
QJsonDocument doc = request("lol-perks/v1/pages/", Method::POST, requestdocstr);
|
|
|
|
if(doc.isObject() && doc["isValid"].isBool() && doc["isValid"].toBool()) return true; // ok
|
|
|
|
// error
|
|
qWarning() << "error creating runepage: " << page.name << " " << doc.toJson();
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ClientAPI::deleteRunePage(uint64_t id) {
|
|
QJsonDocument doc = request("lol-perks/v1/pages/" + QString::number(id), Method::DELETE);
|
|
|
|
if(doc.isEmpty()) return true; // ok
|
|
|
|
// error
|
|
qWarning() << "error deleteing runepage: " << id << " " << doc.toJson();
|
|
|
|
return false;
|
|
}
|
|
|
|
std::vector<RuneAspekt> ClientAPI::getAllRuneAspekts() {
|
|
QJsonDocument doc = request("lol-perks/v1/perks");
|
|
|
|
if(!doc.isArray()) {
|
|
qWarning() << __PRETTY_FUNCTION__ << " doc is not array";
|
|
return {};
|
|
}
|
|
|
|
QJsonArray arr = doc.array();
|
|
std::vector<RuneAspekt> out;
|
|
out.reserve(arr.size());
|
|
for(auto it : arr) {
|
|
if(it.isObject()) {
|
|
out.push_back((RuneAspekt) it.toObject());
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
std::vector<RuneStyle> ClientAPI::getAllRuneStyles() {
|
|
QJsonDocument doc = request("lol-perks/v1/styles");
|
|
|
|
if(!doc.isArray()) {
|
|
qWarning() << __PRETTY_FUNCTION__ << " doc is not array";
|
|
return {};
|
|
}
|
|
|
|
QJsonArray arr = doc.array();
|
|
std::vector<RuneStyle> out;
|
|
out.reserve(arr.size());
|
|
for(auto it : arr) {
|
|
if(it.isObject()) {
|
|
out.push_back((RuneStyle) it.toObject());
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
QPixmap ClientAPI::getImageResource(QString path) {
|
|
if(path.isEmpty()) return {};
|
|
|
|
QString simplePath;
|
|
{
|
|
QString simplePathQ = path;
|
|
simplePath = simplePathQ.replace('/', '_');
|
|
}
|
|
|
|
// query mem cache
|
|
QPixmap img = memImageCache.getImage(path, 0);
|
|
if(!img.isNull()) return img;
|
|
|
|
// query HDD cache
|
|
img = imageCache.getImage(simplePath);
|
|
if(!img.isNull()) {
|
|
// update mem cache
|
|
memImageCache.addImage(img, path, 0);
|
|
return img;
|
|
}
|
|
|
|
qInfo() << "requesting: " << path;
|
|
QByteArray arr = requestRaw(path);
|
|
QPixmap out;
|
|
out.loadFromData(arr);
|
|
|
|
// store HDD cache
|
|
imageCache.addImageRaw(arr, simplePath);
|
|
|
|
// store memchache
|
|
memImageCache.addImage(out, path, 0);
|
|
|
|
return out;
|
|
}
|