lolautoaccept/src/clientapi.cpp
2022-06-29 23:09:01 +02:00

140 lines
4.2 KiB
C++

#include "clientapi.h"
#include <iomanip>
#include <QJsonArray>
#include <QJsonObject>
#include <Log.h>
#include "json.h"
// calculate the size of a constant sized array
#define ARRSIZE(ARRNAME) (sizeof(ARRNAME) / sizeof(ARRNAME[0]))
static const std::string ReadyCheckStateNames[] = { "Invalid", "None", "InProgress", "Accepted", "Declined" };
static const uint32_t ReadyCheckStateNamesCount = ARRSIZE(ReadyCheckStateNames);
static const std::string GameflowPhaseNames[] = {"None", "Lobby", "Matchmaking", "CheckedIntoTournament", "ReadyCheck", "ChampSelect", "GameStart", "FailedToLaunch", "InProgress", "Reconnect", "WaitingForStats", "PreEndOfGame", "EndOfGame", "TerminatedInError"};
static const uint32_t GameflowPhaseNamesCount = ARRSIZE(GameflowPhaseNames);
template<typename T>
static T mapEnum(const std::string& input, const std::string* names, uint32_t count, T defaul) {
for (uint32_t i = 0; i < count; ++i) {
if (names[i] == input) {
return (T) i;
}
}
Log::warn << "no mapping of enum-string: " << std::quoted(input);
return defaul;
}
ClientAPI::ClientAPI(const ClientAccess& ca) : RestClient(ca.getURL()), access(ca) {
basicauth = ca.getBasicAuth();
disableCertCheck = true;
}
ClientAPI::~ClientAPI() {}
ClientAPI::ReadyCheckState ClientAPI::getReadyCheckState() {
QJsonDocument doc = request("lol-matchmaking/v1/ready-check");
if (doc.isObject()) {
QJsonObject obj = doc.object();
Log::info << "ready check obj: " << doc.toJson().toStdString();
std::string searchState = getValue<std::string>(obj, "state", "Invalid");
std::string playerresponse = getValue<std::string>(obj, "playerResponse", "None");
ClientAPI::ReadyCheckState response = mapEnum(playerresponse, ReadyCheckStateNames, ReadyCheckStateNamesCount, ClientAPI::ReadyCheckState::NONE);
if(response == ClientAPI::ReadyCheckState::NONE) {
auto sstate = mapEnum(searchState, ReadyCheckStateNames, ReadyCheckStateNamesCount, ClientAPI::ReadyCheckState::INVALID);
if(sstate == ClientAPI::ReadyCheckState::INPROGRESS) {
return sstate;
}
}
return response;
}
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 mapEnum(datastr, GameflowPhaseNames, GameflowPhaseNamesCount, ClientAPI::GameflowPhase::NONE);
}
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);
}
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 {};
TimerInfo ti;
QJsonObject obj = doc.object();
ti.adjustedTimeLeftInPhase = getValue<int64_t>(obj, "adjustedTimeLeftInPhase", 0);
ti.internalNowInEpochMs = getValue<int64_t>(obj, "internalNowInEpochMs", 0);
ti.isefinite = getValue<bool>(obj, "isefinite", 0);
ti.phase = getValue<std::string>(obj, "phase", "");
ti.totalTimeInPhase = getValue<int64_t>(obj, "totalTimeInPhase", 0);
return ti;
}
#define PRINTENUM(ENUMNAME) \
std::ostream& operator<<(std::ostream& str, const ClientAPI:: ENUMNAME & state) { \
return str << ENUMNAME ## Names[(int) state] << " (" << (int) state << ')'; \
}
PRINTENUM(ReadyCheckState)
PRINTENUM(GameflowPhase)