forked from MrBesen/lolautoaccept
211 lines
5.9 KiB
C++
211 lines
5.9 KiB
C++
#include "lolautoaccept.h"
|
|
|
|
#include <thread>
|
|
#include <Log.h>
|
|
|
|
|
|
LolAutoAccept::Stage::Stage() {}
|
|
LolAutoAccept::Stage::~Stage() {}
|
|
|
|
void LolAutoAccept::Stage::action(LolAutoAccept& lolaa) {}
|
|
|
|
LolAutoAccept::LolAutoAccept() {
|
|
stages.reserve(4);
|
|
stages.push_back(new Stage()); // accept
|
|
stages.push_back(new Stage()); // prepick
|
|
stages.push_back(new Stage()); // ban
|
|
stages.push_back(new Stage()); // pick
|
|
}
|
|
|
|
LolAutoAccept::~LolAutoAccept() {
|
|
stopJoinThread();
|
|
|
|
for(auto s : stages) {
|
|
delete s;
|
|
}
|
|
}
|
|
|
|
void LolAutoAccept::setChamp(uint32_t champ, State s) {
|
|
if(s == State::LOBBY || s >= State::GAME) {
|
|
Log::warn << "setChamp() called on invalid State";
|
|
return;
|
|
}
|
|
|
|
stages.at((int) s)->champid = champ;
|
|
Log::info << "set state: " << (int) s << " to " << champ;
|
|
}
|
|
|
|
void LolAutoAccept::setEnabled(bool b, State s) {
|
|
if(s >= State::GAME) {
|
|
Log::warn << "setEnabled() called on invalid State";
|
|
return;
|
|
}
|
|
|
|
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();
|
|
}
|
|
}
|
|
|
|
void LolAutoAccept::innerRun() {
|
|
shouldrun = true;
|
|
|
|
while(shouldrun) {
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
auto phase = clientapi->getGameflowPhase();
|
|
Log::debug << "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();
|
|
}
|
|
}
|
|
} else if(phase == ClientAPI::GameflowPhase::CHAMPSELECT) {
|
|
champSelect();
|
|
}
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> dur = (end-start);
|
|
//if(dur.count() > 1e-5)
|
|
Log::info << "iteration took: " << (dur.count() * 1000) << " ms";
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(1200));
|
|
}
|
|
}
|
|
|
|
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) {
|
|
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 = stages.at((int) State::PREPICK)->champid;
|
|
Log::info << "try to prepick champ: " << champid;
|
|
clientapi->setChampSelectAction(it.id, champid, false);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
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 = stages.at((int) State::BAN)->champid;
|
|
Log::info << "try to ban champ: " << champid;
|
|
clientapi->setChampSelectAction(it.id, champid, true);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void LolAutoAccept::pickPhase(const ownactions_t& ownactions) {
|
|
Log::info << "pick phase";
|
|
|
|
for(auto it : ownactions) {
|
|
if(it.type == ClientAPI::ChampSelectActionType::PICK) {
|
|
Log::info << "pick action anvailable: " << it.id << " champid: " << it.championID;
|
|
uint32_t champid = stages.at((int) State::PICK)->champid;
|
|
if(it.championID != champid || !it.completed) {
|
|
// try to prepick a champion
|
|
Log::info << "try to pick champ: " << champid;
|
|
clientapi->setChampSelectAction(it.id, champid, true);
|
|
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 = ⁢
|
|
break;
|
|
}
|
|
}
|
|
|
|
ClientAPI::Position pos = me ? me->position : ClientAPI::Position::INVALID;
|
|
|
|
Log::info << "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::note << "ownactions: " << ownactions.size();
|
|
|
|
// try to prepick champ
|
|
Log::info << "champselectphase: " << session.timer.phase;
|
|
if(session.timer.phase == ClientAPI::ChampSelectPhase::PLANNING) {
|
|
prepickPhase(ownactions);
|
|
} else if(session.timer.phase == ClientAPI::ChampSelectPhase::BAN_PICK) {
|
|
banPhase(ownactions);
|
|
pickPhase(ownactions);
|
|
} else if(session.timer.phase == ClientAPI::ChampSelectPhase::FINALIZATION) {
|
|
// trade?
|
|
Log::info << "trade phase";
|
|
}
|
|
}
|