lolautoaccept/src/lolautoaccept.cpp

270 lines
7.7 KiB
C++

#include "lolautoaccept.h"
#include <thread>
#include <Log.h>
LolAutoAccept::Stage::Stage() {}
LolAutoAccept::Stage::~Stage() {}
LolAutoAccept::LolAutoAccept() {
std::lock_guard lock(stagesMutex);
stages.reserve(3);
stages.push_back(new Stage()); // accept
stages.push_back(new Stage()); // ban
stages.push_back(new Stage()); // pick
}
LolAutoAccept::~LolAutoAccept() {
stopJoinThread();
std::lock_guard lock(stagesMutex);
for(auto s : stages) {
delete s;
}
}
void LolAutoAccept::setChamps(const std::vector<uint32_t>& champs, State s) {
if(s == State::LOBBY || s >= State::PICK) {
Log::warn << "setChamps() called on invalid State";
return;
}
{
std::lock_guard lock(stagesMutex);
stages.at((int) s)->champids = champs;
stages.at((int) s)->currentOffset = 0;
}
Log::info << "set champs on state: " << (int) s << " count: " << champs.size();
}
void LolAutoAccept::setEnabled(bool b, State s) {
if(s >= State::PICK) {
Log::warn << "setEnabled() called on invalid State";
return;
}
std::lock_guard lock(stagesMutex);
stages.at((int) s)->enabled = b;
}
bool LolAutoAccept::init() {
if(clientapi) return true;
auto ca = ClientAccess::find();
if(ca) {
clientapi = std::make_shared<ClientAPI>(*ca.get());
auto selfinfo = clientapi->getSelf();
summonerid = selfinfo.summonerid;
Log::info << "selfinfo: gameName: " << selfinfo.gameName << " name: " << selfinfo.name << " summonerid: " << selfinfo.summonerid << " statusMessage: " << selfinfo.statusMessage << " puuid: " << selfinfo.puuid << " level: " << selfinfo.level;
}
return (bool) clientapi;
}
void LolAutoAccept::run() {
// make sure its not running
stopJoinThread();
if(!clientapi) return; // no client api
lolaathread = std::thread(&LolAutoAccept::innerRun, this);
}
void LolAutoAccept::stop() {
shouldrun = false;
}
void LolAutoAccept::stopJoinThread() {
stop();
if(lolaathread.joinable()) {
lolaathread.join();
}
resetAllOffsets();
}
void LolAutoAccept::innerRun() {
shouldrun = true;
while(shouldrun) {
uint32_t extrasleep = 800;
auto start = std::chrono::high_resolution_clock::now();
auto phase = clientapi->getGameflowPhase();
Log::info << "current Gameflowphase: " << phase;
// do processing
if(phase == ClientAPI::GameflowPhase::READYCHECK) {
if(stages.at(0)->enabled) { // auto accept enabled
auto state = clientapi->getReadyCheckState();
Log::info << "readychack state: " << state;
if(state == ClientAPI::ReadyCheckState::INPROGRESS) {
Log::info << "auto accepting";
clientapi->acceptMatch();
}
}
resetAllOffsets();
extrasleep = 0; // no extra sleep
} else if(phase == ClientAPI::GameflowPhase::CHAMPSELECT) {
champSelect();
extrasleep = 0; // no extra sleep
} else if(phase == ClientAPI::GameflowPhase::INPROGRESS) {
extrasleep = 30000; // 30s bonus sleep
} else if(phase == ClientAPI::GameflowPhase::ENDOFGAME) {
extrasleep = 2000; // 2 s bonus sleep
} else if(phase == ClientAPI::GameflowPhase::PREENDOFGAME) {
extrasleep = 4000; // 4 s bonus sleep
} else if(phase == ClientAPI::GameflowPhase::WAITINGFORSTATS) {
extrasleep = 4000; // 4 s bonus sleep
} else if(phase == ClientAPI::GameflowPhase::NONE) {
extrasleep = 30000; // 30 s bonus sleep - no lobby
}
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> dur = (end-start);
//if(dur.count() > 1e-5)
Log::note << "iteration took: " << (dur.count() * 1000) << " ms";
std::this_thread::sleep_for(std::chrono::milliseconds(400 + extrasleep));
}
}
void LolAutoAccept::resetAllOffsets() {
for(Stage* stage : stages) {
stage->currentOffset = 0;
}
}
uint32_t LolAutoAccept::getChampOfState(State s) {
assert(((int) s) >= 0 && s <= State::PICK);
auto stage = stages[(int) s];
uint32_t size = stage->champids.size();
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();
return 0;
}
return stage->champids[stage->currentOffset];
}
void LolAutoAccept::nextChampOfState(State s) {
assert(((int) s) >= 0 && s <= State::PICK);
auto stage = stages[(int) s];
uint32_t 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;
}
}
bool LolAutoAccept::isChampIntentofTeammate(uint32_t champid, const ClientAPI::ChampSelectSession& session) {
for(const ClientAPI::ChampSelectCell& player : session.myTeam) {
if(player.championID == (int32_t) champid || player.championPickIntentID == (int32_t) champid) {
Log::info << "player " << player.cellID << " @ " << player.position << " wants to play " << champid;
return true;
}
}
return false;
}
LolAutoAccept::ownactions_t LolAutoAccept::getOwnActions(int32_t cellid, const std::vector<ClientAPI::ChampSelectAction> actions) {
ownactions_t out;
for(const auto& it : actions) {
if(it.actorCellID == cellid) {
Log::debug << "ownaction: id: " << it.id << " champid: " << it.championID << " completed: " << it.completed << " type: " << it.type;
if(!it.completed) { // completed cant be interacted anyways, so just ignore them.
out.push_back(it);
}
}
}
return out;
}
void LolAutoAccept::prepickPhase(const ownactions_t& ownactions) {
phase(ownactions, ClientAPI::ChampSelectActionType::PICK, State::PICK, false);
}
void LolAutoAccept::banPhase(const ownactions_t& ownactions, const ClientAPI::ChampSelectSession& session) {
phase(ownactions, ClientAPI::ChampSelectActionType::BAN, State::BAN, true, [session](uint32_t champid) {
return !isChampIntentofTeammate(champid, session);
});
}
void LolAutoAccept::pickPhase(const ownactions_t& ownactions) {
phase(ownactions, ClientAPI::ChampSelectActionType::PICK, State::PICK, true);
}
void LolAutoAccept::phase(const ownactions_t& ownactions, ClientAPI::ChampSelectActionType type, State s, bool complete, std::function<bool(uint32_t)> filter) {
for(auto it : ownactions) {
if(it.type == type) {
Log::info << type << " action anvailable: " << it.id << " champid: " << it.championID;
uint32_t champid = getChampOfState(s);
if(filter) {
// filter says no
if(!filter(champid)) {
nextChampOfState(s);
return;
}
}
if(it.championID != (int32_t) champid || !it.completed) {
// try to prepick a champion
Log::info << "try to pick champ: " << champid;
if(!clientapi->setChampSelectAction(it.id, champid, complete)) {
nextChampOfState(s);
}
return;
}
}
}
}
void LolAutoAccept::champSelect() {
auto session = clientapi->getChampSelectSession();
int32_t cellid = session.localPlayerCellId;
// find own cellid info
const ClientAPI::ChampSelectCell* me = nullptr;
for(const auto& it : session.myTeam) {
if(it.cellID == cellid) {
me = &it;
break;
}
}
Position pos = me ? me->position : Position::INVALID;
Log::debug << "cellid: " << cellid << " position: " << pos << " counter: " << session.counter << " timer: timeleftip: " << session.timer.adjustedTimeLeftInPhase << " totaltimephase: " << session.timer.totalTimeInPhase;
// find actions for own cell
auto ownactions = getOwnActions(cellid, session.actions);
Log::debug << "ownactions: " << ownactions.size();
// try to prepick champ
Log::info << "champselectphase: " << session.timer.phase;
std::lock_guard lock(stagesMutex);
if(session.timer.phase == ClientAPI::ChampSelectPhase::PLANNING) {
prepickPhase(ownactions);
} else if(session.timer.phase == ClientAPI::ChampSelectPhase::BAN_PICK) {
banPhase(ownactions, session);
pickPhase(ownactions);
} else if(session.timer.phase == ClientAPI::ChampSelectPhase::FINALIZATION) {
// trade?
}
}