lolautoaccept/src/lolautoaccept.cpp

328 lines
9.7 KiB
C++

#include "lolautoaccept.h"
#include <algorithm>
#include <thread>
#include <Log.h>
LolAutoAccept::Stage::Stage() {}
LolAutoAccept::Stage::~Stage() {}
LolAutoAccept::LolAutoAccept(Config::RootConfig& config, DataDragon& dd, onposchange_func onposch) : config(config), dd(dd), onPoschange(onposch) {
std::lock_guard lock(stagesMutex);
stages.resize(3); // accept, ban, pick
}
LolAutoAccept::~LolAutoAccept() {
stopJoinThread();
}
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();
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::reload() {
Log::note << "reload LolAutoAccept";
loadPosition(currentPosition);
}
const std::vector<RuneAspekt>& LolAutoAccept::getRuneAspekts() {
if(runeaspekts.empty()) {
if(clientapi) {
runeaspekts = clientapi->getAllRuneAspekts();
Log::info << "Loaded " << runeaspekts.size() << " rune aspekts";
}
}
return runeaspekts;
}
void LolAutoAccept::applyRunes() {
// TODO
Log::warn << "LolAutoAccept::applyRunes() not implemented";
}
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::MATCHMAKING) {
extrasleep = 200;
} else 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 = 10000; // 10 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 extrasleep: " << extrasleep;
std::this_thread::sleep_for(std::chrono::milliseconds(400 + extrasleep));
}
}
void LolAutoAccept::resetAllOffsets() {
for(Stage& stage : stages) {
stage.currentOffset = 0;
}
currentPosition = Position::INVALID;
}
void LolAutoAccept::applyConfigToStage(Stage& stage, const Config::StageConfig& stageconf) {
stage.champids = dd.resolveChampIDs(stageconf.champs);
stage.enabled = stage.enabled;
stage.currentOffset = 0;
}
void LolAutoAccept::loadPosition(Position pos) {
Log::trace << __PRETTY_FUNCTION__ << " pos: " << pos;
// reinit the stages
std::lock_guard lock(stagesMutex);
stages.resize(1); // first stage does not change
stages.resize(3);
const Config::PositionConfig& posconf = config.getPositionConfig(pos);
applyConfigToStage(stages.at((int) State::BAN), posconf.ban);
applyConfigToStage(stages.at((int) State::PICK), posconf.pick);
currentPosition = pos;
}
uint32_t LolAutoAccept::getChampOfState(State s) {
assert(((int) s) >= 0 && s <= State::PICK);
Stage& 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(s > State::LOBBY && 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;
}
bool LolAutoAccept::isChampBanned(uint32_t champid, const ClientAPI::ChampSelectSession& session) {
for(const ClientAPI::ChampSelectAction& act : session.actions) {
if(act.type == ClientAPI::ChampSelectActionType::BAN && act.championID == (int32_t) champid && act.completed) {
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) && !isChampBanned(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)) {
Log::trace << "champid: " << champid << " filter says no - next champ";
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;
if(pos == Position::INVALID) {
pos = Position::UTILITY; // default: support TODO: make the default changeable? extra tab?
}
// reload config based on position if changed
if(pos != currentPosition) {
Log::note << "LolAutoAccept reloading config for position: " << pos << " because it was: " << currentPosition;
loadPosition(pos);
if(onPoschange) {
onPoschange(pos);
}
}
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?
}
}