forked from MrBesen/lolautoaccept
297 lines
7.4 KiB
C++
297 lines
7.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) {
|
|
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");
|
|
|
|
std::string datastr = data.toStdString();
|
|
if (data.size() > 2) {
|
|
datastr = datastr.substr(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 std::string requeststr = requestdoc.toJson(QJsonDocument::JsonFormat::Compact).toStdString();
|
|
Log::debug << "requeststr: " << requeststr;
|
|
QJsonDocument doc = request("lol-champ-select/v1/session/actions/" + std::to_string(actionid), Method::PATCH, requeststr);
|
|
|
|
std::string error;
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
auto errref = obj["errorCode"];
|
|
auto msgref = obj["message"];
|
|
if(errref.isString()) {
|
|
error = errref.toString().toStdString() + " ";
|
|
}
|
|
if(msgref.isString()) {
|
|
error += msgref.toString().toStdString();
|
|
}
|
|
}
|
|
|
|
Log::note << "patching action: " << actionid << " error: " << error;
|
|
return error.empty();
|
|
}
|
|
|
|
ClientAPI::PlayerInfo ClientAPI::getSelf() {
|
|
QJsonDocument doc = request("lol-chat/v1/me");
|
|
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
|
|
PlayerInfo info;
|
|
info.gameName = getValue<std::string>(obj, "gameName");
|
|
info.name = getValue<std::string>(obj, "name");
|
|
info.statusMessage = getValue<std::string>(obj, "statusMessage", "");
|
|
info.summonerid = getValue<uint64_t>(obj, "summonerId");
|
|
|
|
auto lolref = obj["lol"];
|
|
if(lolref.isObject()) {
|
|
QJsonObject lol = lolref.toObject();
|
|
info.puuid = getValue<std::string>(lol, "puuid");
|
|
info.level = getValue<int32_t>(lol, "level");
|
|
}
|
|
|
|
return info;
|
|
}
|
|
return {};
|
|
}
|
|
|
|
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 std::string& chatid, const std::string& messagebody) {
|
|
QJsonObject requestj;
|
|
requestj["body"] = QString::fromStdString(messagebody);
|
|
QJsonDocument requestdoc(requestj);
|
|
const std::string requeststr = requestdoc.toJson(QJsonDocument::JsonFormat::Compact).toStdString();
|
|
Log::debug << "requeststr: " << requeststr;
|
|
QJsonDocument doc = request("lol-chat/v1/conversations/" + chatid + "/messages", Method::POST, requeststr);
|
|
|
|
std::string error;
|
|
if(doc.isObject()) {
|
|
QJsonObject obj = doc.object();
|
|
auto errref = obj["errorCode"];
|
|
auto msgref = obj["message"];
|
|
if(errref.isString()) {
|
|
error = errref.toString().toStdString() + " ";
|
|
}
|
|
if(msgref.isString()) {
|
|
error += msgref.toString().toStdString();
|
|
}
|
|
|
|
if(error.empty()) {
|
|
return (Message) obj;
|
|
}
|
|
}
|
|
|
|
Log::note << "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, std::to_string(id));
|
|
|
|
if(doc.isEmpty()) return true; // ok
|
|
|
|
// error
|
|
Log::warn << "error selecting runepage: " << id << " " << doc.toJson().toStdString();
|
|
|
|
return false;
|
|
}
|
|
|
|
bool ClientAPI::editRunePage(const RunePage& page) {
|
|
QJsonObject pagereq;
|
|
|
|
pagereq["name"] = QString::fromStdString(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 std::string requestdocstr = reqdoc.toJson(QJsonDocument::JsonFormat::Compact).toStdString();
|
|
Log::info << "requeststr: " << requestdocstr;
|
|
QJsonDocument doc = request("lol-perks/v1/pages/" + std::to_string(page.id), Method::PUT, requestdocstr);
|
|
|
|
if(doc.isEmpty()) return true; // ok
|
|
|
|
// error
|
|
Log::warn << "error editing runepage: " << page.id << " " << doc.toJson().toStdString();
|
|
|
|
return false;
|
|
}
|
|
|
|
std::vector<RuneAspekt> ClientAPI::getAllRuneAspekts() {
|
|
QJsonDocument doc = request("lol-perks/v1/perks");
|
|
|
|
if(!doc.isArray()) {
|
|
Log::warn << __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()) {
|
|
Log::warn << __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;
|
|
}
|