lolautoaccept/src/clientapi.cpp

370 lines
9.1 KiB
C++
Raw Normal View History

2022-06-27 20:45:01 +02:00
#include "clientapi.h"
2022-07-01 00:13:38 +02:00
#include <cassert>
2022-06-27 20:45:01 +02:00
#include <iomanip>
2022-06-29 23:09:01 +02:00
#include <QJsonArray>
2022-06-27 20:45:01 +02:00
#include <QJsonObject>
#include <Log.h>
#include "json.h"
2023-05-01 22:36:11 +02:00
ClientAPI::ClientAPI(const ClientAccess& ca) : RestClient(ca.getURL()), access(ca), memImageCache(40), imageCache("runes", "") {
2022-06-27 20:45:01 +02:00
basicauth = ca.getBasicAuth();
disableCertCheck = true;
2023-06-19 21:22:44 +02:00
// enableDebugging();
2022-06-27 20:45:01 +02:00
}
ClientAPI::~ClientAPI() {}
2022-06-29 23:09:01 +02:00
ClientAPI::ReadyCheckState ClientAPI::getReadyCheckState() {
QJsonDocument doc = request("lol-matchmaking/v1/ready-check");
2022-06-27 20:45:01 +02:00
2022-06-29 23:09:01 +02:00
if (doc.isObject()) {
2022-07-02 12:36:38 +02:00
return toReadyCheckState(doc.object());
2022-06-27 20:45:01 +02:00
}
2022-06-29 23:09:01 +02:00
return ClientAPI::ReadyCheckState::NONE;
2022-06-27 20:45:01 +02:00
}
2022-06-29 23:09:01 +02:00
ClientAPI::GameflowPhase ClientAPI::getGameflowPhase() {
// it is just a json-string no object
QByteArray data = requestRaw("lol-gameflow/v1/gameflow-phase");
2022-06-27 20:45:01 +02:00
2023-05-31 22:22:23 +02:00
QString datastr = QString::fromLocal8Bit(data);
2022-06-29 23:09:01 +02:00
if (data.size() > 2) {
2023-05-31 22:22:23 +02:00
datastr = datastr.mid(1, datastr.size() -2);
2022-06-29 23:09:01 +02:00
2022-07-02 12:36:38 +02:00
return toGameflowPhase(datastr);
2022-06-29 23:09:01 +02:00
}
return ClientAPI::GameflowPhase::NONE;
}
void ClientAPI::acceptMatch() {
request("lol-matchmaking/v1/ready-check/accept", Method::POST);
2022-06-27 20:45:01 +02:00
}
void ClientAPI::declineMatch() {
2022-06-29 23:09:01 +02:00
request("lol-matchmaking/v1/ready-check/decline", Method::POST);
}
2022-07-02 12:36:38 +02:00
ClientAPI::ChampSelectSession ClientAPI::getChampSelectSession() {
2022-07-01 00:13:38 +02:00
QJsonDocument doc = request("lol-champ-select/v1/session");
2022-07-02 12:36:38 +02:00
if(doc.isObject()) {
return (ChampSelectSession) doc.object();
}
return {};
}
2022-07-02 13:21:09 +02:00
bool ClientAPI::setChampSelectAction(int32_t actionid, int32_t champid, bool completed) {
2022-07-02 12:36:38 +02:00
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;
2023-05-31 22:22:23 +02:00
QJsonDocument doc = request("lol-champ-select/v1/session/actions/" + QString::number(actionid), Method::PATCH, requeststr);
2022-07-02 12:36:38 +02:00
2023-05-31 22:22:23 +02:00
QString error;
if(doc.isObject()) {
QJsonObject obj = doc.object();
auto errref = obj["errorCode"];
auto msgref = obj["message"];
if(errref.isString()) {
2023-05-31 22:22:23 +02:00
error = errref.toString() + " ";
}
if(msgref.isString()) {
2023-05-31 22:22:23 +02:00
error += msgref.toString();
}
}
2023-05-31 22:22:23 +02:00
qDebug() << "patching action: " << actionid << " error: " << error;
return error.isEmpty();
2022-07-01 00:13:38 +02:00
}
ClientAPI::PlayerInfo ClientAPI::getSelf() {
QJsonDocument doc = request("lol-chat/v1/me");
if(doc.isObject()) {
QJsonObject obj = doc.object();
PlayerInfo info;
2023-05-31 22:22:23 +02:00
info.gameName = getValue<QString>(obj, "gameName");
info.name = getValue<QString>(obj, "name");
info.statusMessage = getValue<QString>(obj, "statusMessage", "");
2022-07-09 01:01:51 +02:00
info.summonerid = getValue<uint64_t>(obj, "summonerId");
2022-07-01 00:13:38 +02:00
auto lolref = obj["lol"];
if(lolref.isObject()) {
QJsonObject lol = lolref.toObject();
2023-05-31 22:22:23 +02:00
info.puuid = getValue<QString>(lol, "puuid");
2022-07-01 00:13:38 +02:00
info.level = getValue<int32_t>(lol, "level");
}
return info;
}
return {};
}
2022-06-29 23:09:01 +02:00
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;
}
2022-06-27 20:45:01 +02:00
2022-06-29 23:09:01 +02:00
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
2022-06-27 20:45:01 +02:00
}
2022-06-29 23:09:01 +02:00
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();
2022-07-02 12:36:38 +02:00
return (TimerInfo) obj;
2022-06-29 23:09:01 +02:00
}
2022-07-09 01:01:51 +02:00
2022-08-26 02:12:25 +02:00
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;
}
2023-05-31 22:22:23 +02:00
ClientAPI::Message ClientAPI::sendMessage(const QString& chatid, const QString& messagebody) {
2022-08-26 02:12:25 +02:00
QJsonObject requestj;
2023-05-31 22:22:23 +02:00
requestj["body"] = messagebody;
2022-08-26 02:12:25 +02:00
QJsonDocument requestdoc(requestj);
const QString requeststr = QString::fromLocal8Bit(requestdoc.toJson(QJsonDocument::JsonFormat::Compact));
qDebug().noquote() << "requeststr: " << requeststr;
2022-08-26 02:12:25 +02:00
QJsonDocument doc = request("lol-chat/v1/conversations/" + chatid + "/messages", Method::POST, requeststr);
2023-05-31 22:22:23 +02:00
QString error;
2022-08-26 02:12:25 +02:00
if(doc.isObject()) {
QJsonObject obj = doc.object();
auto errref = obj["errorCode"];
auto msgref = obj["message"];
if(errref.isString()) {
2023-05-31 22:22:23 +02:00
error = errref.toString() + " ";
2022-08-26 02:12:25 +02:00
}
if(msgref.isString()) {
2023-05-31 22:22:23 +02:00
error += msgref.toString();
2022-08-26 02:12:25 +02:00
}
2023-05-31 22:22:23 +02:00
if(error.isEmpty()) {
2022-08-26 02:12:25 +02:00
return (Message) obj;
}
}
2023-05-31 22:22:23 +02:00
qDebug() << "send message error: " << error;
2022-08-26 02:12:25 +02:00
return {};
}
2022-07-09 01:01:51 +02:00
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) {
2022-07-10 01:51:42 +02:00
if(it.isObject()) {
2022-07-09 01:01:51 +02:00
out.push_back((RunePage) it.toObject());
}
}
return out;
}
bool ClientAPI::selectRunePage(uint64_t id) {
2023-05-31 22:22:23 +02:00
QJsonDocument doc = request("lol-perks/v1/currentpage", Method::PUT, QString::number(id));
2022-07-09 01:01:51 +02:00
if(doc.isEmpty()) return true; // ok
// error
2023-05-31 22:22:23 +02:00
qWarning() << "error selecting runepage: " << id << " " << doc.toJson();
2022-07-09 01:01:51 +02:00
return false;
}
bool ClientAPI::editRunePage(const RunePage& page) {
QJsonObject pagereq;
2023-05-31 22:22:23 +02:00
pagereq["name"] = page.name;
pagereq["primaryStyleId"] = (int) page.runepage.primaryStyle;
pagereq["subStyleId"] = (int) page.runepage.secondaryStyle;
2022-07-09 01:01:51 +02:00
QJsonArray selected;
for(uint32_t sel : page.runepage.selectedAspects) {
2022-07-09 01:01:51 +02:00
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;
2023-05-31 22:22:23 +02:00
QJsonDocument doc = request("lol-perks/v1/pages/" + QString::number(page.id), Method::PUT, requestdocstr);
2022-07-09 01:01:51 +02:00
if(doc.isEmpty()) return true; // ok
// error
2023-05-31 22:22:23 +02:00
qWarning() << "error editing runepage: " << page.id << " " << doc.toJson();
2023-04-30 16:20:13 +02:00
return false;
}
bool ClientAPI::createRunePage(const RunePage& page) {
QJsonObject pagereq;
2023-05-31 22:22:23 +02:00
pagereq["name"] = page.name;
2023-04-30 16:20:13 +02:00
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);
2023-05-31 22:22:23 +02:00
const QString requestdocstr = reqdoc.toJson(QJsonDocument::JsonFormat::Compact);
2023-04-30 16:20:13 +02:00
qInfo() << "requeststr: " << requestdocstr;
QJsonDocument doc = request("lol-perks/v1/pages/", Method::POST, requestdocstr);
if(doc.isEmpty()) return true; // ok
// error
2023-05-31 22:22:23 +02:00
qWarning() << "error creating runepage: " << page.name << " " << doc.toJson();
2023-04-30 16:20:13 +02:00
return false;
}
bool ClientAPI::deleteRunePage(uint64_t id) {
2023-05-31 22:22:23 +02:00
QJsonDocument doc = request("lol-perks/v1/pages/" + QString::number(id), Method::DELETE);
2023-04-30 16:20:13 +02:00
if(doc.isEmpty()) return true; // ok
// error
2023-05-31 22:22:23 +02:00
qWarning() << "error deleteing runepage: " << id << " " << doc.toJson();
2022-07-09 01:01:51 +02:00
return false;
}
std::vector<RuneAspekt> ClientAPI::getAllRuneAspekts() {
QJsonDocument doc = request("lol-perks/v1/perks");
if(!doc.isArray()) {
2023-04-30 16:20:13 +02:00
qWarning() << __PRETTY_FUNCTION__ << " doc is not array";
2022-07-09 01:01:51 +02:00
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;
}
2022-07-10 15:56:09 +02:00
std::vector<RuneStyle> ClientAPI::getAllRuneStyles() {
QJsonDocument doc = request("lol-perks/v1/styles");
if(!doc.isArray()) {
2023-04-30 16:20:13 +02:00
qWarning() << __PRETTY_FUNCTION__ << " doc is not array";
2022-07-10 15:56:09 +02:00
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;
}
2023-05-01 22:36:11 +02:00
QPixmap ClientAPI::getImageResource(QString path) {
if(path.isEmpty()) return {};
2023-05-31 22:22:23 +02:00
QString simplePath;
2023-05-01 22:36:11 +02:00
{
QString simplePathQ = path;
2023-05-31 22:22:23 +02:00
simplePath = simplePathQ.replace('/', '_');
2023-05-01 22:36:11 +02:00
}
// query mem cache
2023-05-31 22:22:23 +02:00
QPixmap img = memImageCache.getImage(path, 0);
2023-05-01 22:36:11 +02:00
if(!img.isNull()) return img;
// query HDD cache
img = imageCache.getImage(simplePath);
if(!img.isNull()) {
// update mem cache
2023-05-31 22:22:23 +02:00
memImageCache.addImage(img, path, 0);
2023-05-01 22:36:11 +02:00
return img;
}
qInfo() << "requesting: " << path;
2023-05-31 22:22:23 +02:00
QByteArray arr = requestRaw(path);
2023-05-01 22:36:11 +02:00
QPixmap out;
out.loadFromData(arr);
// store HDD cache
imageCache.addImageRaw(arr, simplePath);
// store memchache
2023-05-31 22:22:23 +02:00
memImageCache.addImage(out, path, 0);
2023-05-01 22:36:11 +02:00
return out;
}