lolautoaccept/src/clientapi.cpp

155 lines
3.9 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: " << std::quoted(requeststr);
QJsonDocument doc = request("lol-champ-select/v1/session/actions/" + std::to_string(actionid), Method::PATCH, requeststr);
Log::info << "patching action: " << actionid << " result: " << doc.toJson().toStdString();
return true;
}
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", "");
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 {};
}
std::vector<std::string> ClientAPI::getLog() {
std::vector<std::string> out;
QJsonDocument doc = request("LoggingGetEntries");
if(doc.isArray()) {
QJsonArray arr = doc.array();
for(auto it : arr) {
std::string message;
if(it.isObject()) {
QJsonObject logobj = it.toObject();
message = getValue<std::string>(logobj, "severity");
message += getValue<std::string>(logobj, "message");
}
out.push_back(message);
}
}
return out;
}
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;
}