From 9e95bd6562baf6e8f526e4f828261bc13ab43925 Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 3 Jul 2022 12:01:51 +0200 Subject: [PATCH] picking and banning of multiple champions working --- include/lolautoaccept.h | 6 ++-- src/clientapi.cpp | 19 +++++++++-- src/lolautoaccept.cpp | 70 ++++++++++++++++++++--------------------- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/include/lolautoaccept.h b/include/lolautoaccept.h index 885f73d..589458a 100644 --- a/include/lolautoaccept.h +++ b/include/lolautoaccept.h @@ -49,12 +49,14 @@ private: void innerRun(); void resetAllOffsets(); - uint32_t getNextChampOfState(State s); + uint32_t getChampOfState(State s); + void nextChampOfState(State s); using ownactions_t = std::vector; ownactions_t getOwnActions(int32_t cellid, const std::vector actions); void prepickPhase(const ownactions_t& ownactions); void banPhase(const ownactions_t& ownactions); - void pickPhase( const ownactions_t& ownactions); + void pickPhase(const ownactions_t& ownactions); + void phase(const ownactions_t& ownactions, ClientAPI::ChampSelectActionType type, State s, bool complete); void champSelect(); }; \ No newline at end of file diff --git a/src/clientapi.cpp b/src/clientapi.cpp index bc6c112..1ee96f2 100644 --- a/src/clientapi.cpp +++ b/src/clientapi.cpp @@ -64,11 +64,24 @@ bool ClientAPI::setChampSelectAction(int32_t actionid, int32_t champid, bool com requestj["id"] = actionid; QJsonDocument requestdoc(requestj); const std::string requeststr = requestdoc.toJson(QJsonDocument::JsonFormat::Compact).toStdString(); - Log::debug << "requeststr: " << std::quoted(requeststr); + Log::debug << "requeststr: " << 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; + 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() { diff --git a/src/lolautoaccept.cpp b/src/lolautoaccept.cpp index a759994..82e6bd5 100644 --- a/src/lolautoaccept.cpp +++ b/src/lolautoaccept.cpp @@ -76,6 +76,7 @@ void LolAutoAccept::stopJoinThread() { if(lolaathread.joinable()) { lolaathread.join(); } + resetAllOffsets(); } void LolAutoAccept::innerRun() { @@ -118,20 +119,37 @@ void LolAutoAccept::resetAllOffsets() { } } -uint32_t LolAutoAccept::getNextChampOfState(State s) { +uint32_t LolAutoAccept::getChampOfState(State s) { assert(((int) s) >= 0 && s <= State::PICK); auto stage = stages[(int) s]; int size = stage->champids.size(); - if(size >= stage->currentOffset) { + if(stage->currentOffset >= size) { // no champ to try left Log::warn << "no champ left at stage: " << (int) s; Log::warn << "stage size: " << stage->champids.size(); - stage->currentOffset = 0; return 0; } - return stage->champids[stage->currentOffset++]; + return stage->champids[stage->currentOffset]; +} + +void LolAutoAccept::nextChampOfState(State s) { + assert(((int) s) >= 0 && s <= State::PICK); + + auto stage = stages[(int) s]; + int size = stage->champids.size(); + + stage->currentOffset++; + + if(stage->currentOffset >= size) { + // no champ to try left + Log::warn << "no champ left at stage: " << (int) s; + Log::warn << "stage size: " << stage->champids.size(); + + stage->currentOffset = 0; + return; + } } LolAutoAccept::ownactions_t LolAutoAccept::getOwnActions(int32_t cellid, const std::vector actions) { @@ -148,49 +166,29 @@ LolAutoAccept::ownactions_t LolAutoAccept::getOwnActions(int32_t cellid, const s } void LolAutoAccept::prepickPhase(const ownactions_t& ownactions) { - Log::info << "prepick phase"; - for(auto it : ownactions) { - if(it.type == ClientAPI::ChampSelectActionType::PICK) { - Log::info << "pre-pick action anvailable: " << it.id << " champid: " << it.championID; - if(it.championID == 0) { - // try to prepick a champion - uint32_t champid = getNextChampOfState(State::PREPICK); - Log::info << "try to prepick champ: " << champid; - clientapi->setChampSelectAction(it.id, champid, false); - break; - } - } - } + phase(ownactions, ClientAPI::ChampSelectActionType::PICK, State::PREPICK, false); } void LolAutoAccept::banPhase(const ownactions_t& ownactions) { - Log::info << "ban phase"; - - for(auto it : ownactions) { - if(it.type == ClientAPI::ChampSelectActionType::BAN) { - Log::info << "ban action anvailable: " << it.id << " champid: " << it.championID; - if(it.championID == 0) { - // try to ban a champion - uint32_t champid = getNextChampOfState(State::BAN); - Log::info << "try to ban champ: " << champid; - clientapi->setChampSelectAction(it.id, champid, true); - return; - } - } - } + phase(ownactions, ClientAPI::ChampSelectActionType::BAN, State::BAN, true); } void LolAutoAccept::pickPhase(const ownactions_t& ownactions) { - Log::info << "pick phase"; + phase(ownactions, ClientAPI::ChampSelectActionType::PICK, State::PICK, true); +} +void LolAutoAccept::phase(const ownactions_t& ownactions, ClientAPI::ChampSelectActionType type, State s, bool complete) { + Log::note << type << " phase"; for(auto it : ownactions) { - if(it.type == ClientAPI::ChampSelectActionType::PICK) { - Log::info << "pick action anvailable: " << it.id << " champid: " << it.championID; - uint32_t champid = getNextChampOfState(State::PICK); + if(it.type == type) { + Log::info << type << " action anvailable: " << it.id << " champid: " << it.championID; + uint32_t champid = getChampOfState(s); if(it.championID != champid || !it.completed) { // try to prepick a champion Log::info << "try to pick champ: " << champid; - clientapi->setChampSelectAction(it.id, champid, true); + + clientapi->setChampSelectAction(it.id, champid, complete); + nextChampOfState(s); return; } }