opt.加入一点奇怪的语法糖(

This commit is contained in:
MistEO
2022-01-08 02:00:35 +08:00
parent d2e129ebd7
commit a33ed4fffb
13 changed files with 148 additions and 130 deletions

View File

@@ -13,9 +13,10 @@
using namespace asst;
AbstractTask::AbstractTask(AsstCallback callback, void* callback_arg)
AbstractTask::AbstractTask(AsstCallback callback, void* callback_arg, std::string task_chain)
: m_callback(callback),
m_callback_arg(callback_arg)
m_callback_arg(callback_arg),
m_task_chain(std::move(task_chain))
{
;
}
@@ -43,6 +44,18 @@ bool asst::AbstractTask::run()
return false;
}
AbstractTask& asst::AbstractTask::set_exit_flag(bool* exit_flag) noexcept
{
m_exit_flag = exit_flag;
return *this;
}
AbstractTask& asst::AbstractTask::set_retry_times(int times) noexcept
{
m_retry_times = times;
return *this;
}
json::value asst::AbstractTask::basic_info() const
{
return json::object{

View File

@@ -18,16 +18,15 @@ namespace asst
class AbstractTask
{
public:
AbstractTask(AsstCallback callback, void* callback_arg);
AbstractTask(AsstCallback callback, void* callback_arg, std::string task_chain);
virtual ~AbstractTask() noexcept = default;
AbstractTask(const AbstractTask&) = default;
AbstractTask(AbstractTask&&) noexcept = default;
virtual bool run();
void set_exit_flag(bool* exit_flag) noexcept { m_exit_flag = exit_flag; }
void set_retry_times(int times) noexcept { m_retry_times = times; }
void set_task_chain(std::string name) noexcept { m_task_chain = std::move(name); }
AbstractTask& set_exit_flag(bool* exit_flag) noexcept;
AbstractTask& set_retry_times(int times) noexcept;
const std::string& get_task_chain() const noexcept { return m_task_chain; }
constexpr static int RetryTimesDefault = 20;
@@ -45,7 +44,7 @@ namespace asst
AsstCallback m_callback;
void* m_callback_arg = nullptr;
bool* m_exit_flag = nullptr;
std::string m_task_chain;
const std::string m_task_chain;
int m_cur_retry = 0;
int m_retry_times = RetryTimesDefault;
};

View File

@@ -164,11 +164,10 @@ bool asst::Assistant::append_start_up(bool only_append)
std::unique_lock<std::mutex> lock(m_mutex);
auto task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this);
task_ptr->set_task_chain("StartUp");
task_ptr->set_tasks({ "StartUp" });
task_ptr->set_times_limit("ReturnToTerminal", 0);
task_ptr->set_times_limit("Terminal", 0);
auto task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this, "StartUp");
task_ptr->set_tasks({ "StartUp" })
.set_times_limit("ReturnToTerminal", 0)
.set_times_limit("Terminal", 0);
m_tasks_queue.emplace(task_ptr);
@@ -189,32 +188,29 @@ bool asst::Assistant::append_fight(const std::string& stage, int mecidine, int s
constexpr const char* TaskChain = "Fight";
// 进入选关界面(主界面的“终端”点进去)
auto terminal_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this);
terminal_task_ptr->set_task_chain(TaskChain);
terminal_task_ptr->set_tasks({ "StageBegin" });
terminal_task_ptr->set_times_limit("LastBattle", 0);
terminal_task_ptr->set_times_limit("StartButton1", 0);
terminal_task_ptr->set_times_limit("StartButton2", 0);
terminal_task_ptr->set_times_limit("MedicineConfirm", 0);
terminal_task_ptr->set_times_limit("StoneConfirm", 0);
auto terminal_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this, TaskChain);
terminal_task_ptr->set_tasks({ "StageBegin" })
.set_times_limit("LastBattle", 0)
.set_times_limit("StartButton1", 0)
.set_times_limit("StartButton2", 0)
.set_times_limit("MedicineConfirm", 0)
.set_times_limit("StoneConfirm", 0);
// 进入对应的关卡
auto stage_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this);
stage_task_ptr->set_task_chain(TaskChain);
stage_task_ptr->set_tasks({ stage });
stage_task_ptr->set_times_limit("StartButton1", 0);
stage_task_ptr->set_times_limit("StartButton2", 0);
stage_task_ptr->set_times_limit("MedicineConfirm", 0);
stage_task_ptr->set_times_limit("StoneConfirm", 0);
auto stage_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this, TaskChain);
stage_task_ptr->set_tasks({ stage })
.set_times_limit("StartButton1", 0)
.set_times_limit("StartButton2", 0)
.set_times_limit("MedicineConfirm", 0)
.set_times_limit("StoneConfirm", 0);
// 开始战斗任务
auto fight_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this);
fight_task_ptr->set_task_chain(TaskChain);
fight_task_ptr->set_tasks({ "FightBegin" });
fight_task_ptr->set_times_limit("MedicineConfirm", mecidine);
fight_task_ptr->set_times_limit("StoneConfirm", stone);
fight_task_ptr->set_times_limit("StartButton1", times);
fight_task_ptr->set_times_limit("StartButton2", times);
auto fight_task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this, TaskChain);
fight_task_ptr->set_tasks({ "FightBegin" })
.set_times_limit("MedicineConfirm", mecidine)
.set_times_limit("StoneConfirm", stone)
.set_times_limit("StartButton1", times)
.set_times_limit("StartButton2", times);
std::unique_lock<std::mutex> lock(m_mutex);
@@ -262,13 +258,12 @@ bool asst::Assistant::append_mall(bool with_shopping, bool only_append)
std::unique_lock<std::mutex> lock(m_mutex);
const std::string task_chain = "Mall";
const std::string TaskChain = "Mall";
append_process_task("MallBegin", task_chain);
append_process_task("MallBegin", TaskChain);
if (with_shopping) {
auto shopping_task_ptr = std::make_shared<CreditShoppingTask>(task_callback, (void*)this);
shopping_task_ptr->set_task_chain(task_chain);
auto shopping_task_ptr = std::make_shared<CreditShoppingTask>(task_callback, (void*)this, TaskChain);
m_tasks_queue.emplace(shopping_task_ptr);
}
@@ -292,10 +287,9 @@ bool Assistant::append_process_task(const std::string& task_name, std::string ta
task_chain = task_name;
}
auto task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this);
task_ptr->set_task_chain(task_chain);
task_ptr->set_tasks({ task_name });
task_ptr->set_retry_times(retry_times);
auto task_ptr = std::make_shared<ProcessTask>(task_callback, (void*)this, task_chain);
task_ptr->set_tasks({ task_name })
.set_retry_times(retry_times);
m_tasks_queue.emplace(task_ptr);
@@ -316,14 +310,13 @@ bool asst::Assistant::append_recruit(unsigned max_times, const std::vector<int>&
append_process_task("RecruitBegin", TaskChain);
auto recruit_task_ptr = std::make_shared<AutoRecruitTask>(task_callback, (void*)this);
recruit_task_ptr->set_max_times(max_times);
recruit_task_ptr->set_need_refresh(need_refresh);
recruit_task_ptr->set_use_expedited(use_expedited);
recruit_task_ptr->set_select_level(select_level);
recruit_task_ptr->set_confirm_level(confirm_level);
recruit_task_ptr->set_task_chain(TaskChain);
recruit_task_ptr->set_retry_times(AutoRecruitTaskRetryTimesDefault);
auto recruit_task_ptr = std::make_shared<AutoRecruitTask>(task_callback, (void*)this, TaskChain);
recruit_task_ptr->set_max_times(max_times)
.set_need_refresh(need_refresh)
.set_use_expedited(use_expedited)
.set_select_level(select_level)
.set_confirm_level(confirm_level)
.set_retry_times(AutoRecruitTaskRetryTimesDefault);
m_tasks_queue.emplace(recruit_task_ptr);
@@ -342,11 +335,10 @@ bool Assistant::append_debug()
{
constexpr static const char* DebugTaskChain = "Debug";
auto shift_task_ptr = std::make_shared<InfrastControlTask>(task_callback, (void*)this);
auto shift_task_ptr = std::make_shared<InfrastControlTask>(task_callback, (void*)this, DebugTaskChain);
shift_task_ptr->set_work_mode(infrast::WorkMode::Aggressive);
shift_task_ptr->set_facility("Control");
shift_task_ptr->set_product("MoodAddition");
shift_task_ptr->set_task_chain(DebugTaskChain);
m_tasks_queue.emplace(shift_task_ptr);
}
@@ -363,10 +355,9 @@ bool Assistant::start_recruit_calc(const std::vector<int>& select_level, bool se
std::unique_lock<std::mutex> lock(m_mutex);
auto task_ptr = std::make_shared<RecruitTask>(task_callback, (void*)this);
task_ptr->set_param(select_level, set_time);
auto task_ptr = std::make_shared<RecruitTask>(task_callback, (void*)this, "RecruitCalc");
task_ptr->set_retry_times(OpenRecruitTaskRetryTimesDefault);
task_ptr->set_task_chain("RecruitCalc");
task_ptr->set_param(select_level, set_time);
m_tasks_queue.emplace(task_ptr);
return start(false);
@@ -392,45 +383,37 @@ bool asst::Assistant::append_infrast(infrast::WorkMode work_mode, const std::vec
append_infrast_begin();
auto info_task_ptr = std::make_shared<InfrastInfoTask>(task_callback, (void*)this);
info_task_ptr->set_work_mode(work_mode);
info_task_ptr->set_task_chain(InfrastTaskCahin);
info_task_ptr->set_mood_threshold(dorm_threshold);
auto info_task_ptr = std::make_shared<InfrastInfoTask>(task_callback, (void*)this, InfrastTaskCahin);
info_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
m_tasks_queue.emplace(info_task_ptr);
// 因为后期要考虑多任务间的联动等所以这些任务的声明暂时不放到for循环中
auto mfg_task_ptr = std::make_shared<InfrastMfgTask>(task_callback, (void*)this);
mfg_task_ptr->set_work_mode(work_mode);
mfg_task_ptr->set_task_chain(InfrastTaskCahin);
mfg_task_ptr->set_mood_threshold(dorm_threshold);
mfg_task_ptr->set_uses_of_drone(uses_of_drones);
auto trade_task_ptr = std::make_shared<InfrastTradeTask>(task_callback, (void*)this);
trade_task_ptr->set_work_mode(work_mode);
trade_task_ptr->set_task_chain(InfrastTaskCahin);
trade_task_ptr->set_mood_threshold(dorm_threshold);
trade_task_ptr->set_uses_of_drone(uses_of_drones);
auto power_task_ptr = std::make_shared<InfrastPowerTask>(task_callback, (void*)this);
power_task_ptr->set_work_mode(work_mode);
power_task_ptr->set_task_chain(InfrastTaskCahin);
power_task_ptr->set_mood_threshold(dorm_threshold);
auto office_task_ptr = std::make_shared<InfrastOfficeTask>(task_callback, (void*)this);
office_task_ptr->set_work_mode(work_mode);
office_task_ptr->set_task_chain(InfrastTaskCahin);
office_task_ptr->set_mood_threshold(dorm_threshold);
auto recpt_task_ptr = std::make_shared<InfrastReceptionTask>(task_callback, (void*)this);
recpt_task_ptr->set_work_mode(work_mode);
recpt_task_ptr->set_task_chain(InfrastTaskCahin);
recpt_task_ptr->set_mood_threshold(dorm_threshold);
auto control_task_ptr = std::make_shared<InfrastControlTask>(task_callback, (void*)this);
control_task_ptr->set_work_mode(work_mode);
control_task_ptr->set_task_chain(InfrastTaskCahin);
control_task_ptr->set_mood_threshold(dorm_threshold);
auto mfg_task_ptr = std::make_shared<InfrastMfgTask>(task_callback, (void*)this, InfrastTaskCahin);
mfg_task_ptr->set_uses_of_drone(uses_of_drones)
.set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto trade_task_ptr = std::make_shared<InfrastTradeTask>(task_callback, (void*)this, InfrastTaskCahin);
trade_task_ptr->set_uses_of_drone(uses_of_drones)
.set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto power_task_ptr = std::make_shared<InfrastPowerTask>(task_callback, (void*)this, InfrastTaskCahin);
power_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto office_task_ptr = std::make_shared<InfrastOfficeTask>(task_callback, (void*)this, InfrastTaskCahin);
office_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto recpt_task_ptr = std::make_shared<InfrastReceptionTask>(task_callback, (void*)this, InfrastTaskCahin);
recpt_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto control_task_ptr = std::make_shared<InfrastControlTask>(task_callback, (void*)this, InfrastTaskCahin);
control_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
auto dorm_task_ptr = std::make_shared<InfrastDormTask>(task_callback, (void*)this);
dorm_task_ptr->set_work_mode(work_mode);
dorm_task_ptr->set_task_chain(InfrastTaskCahin);
dorm_task_ptr->set_mood_threshold(dorm_threshold);
auto dorm_task_ptr = std::make_shared<InfrastDormTask>(task_callback, (void*)this, InfrastTaskCahin);
dorm_task_ptr->set_work_mode(work_mode)
.set_mood_threshold(dorm_threshold);
for (const auto& facility : order) {
if (facility == "Dorm") {

View File

@@ -7,29 +7,34 @@
#include "ProcessTask.h"
#include "RecruitTask.h"
void asst::AutoRecruitTask::set_select_level(std::vector<int> select_level) noexcept
asst::AutoRecruitTask& asst::AutoRecruitTask::set_select_level(std::vector<int> select_level) noexcept
{
m_select_level = std::move(select_level);
return *this;
}
void asst::AutoRecruitTask::set_confirm_level(std::vector<int> confirm_level) noexcept
asst::AutoRecruitTask& asst::AutoRecruitTask::set_confirm_level(std::vector<int> confirm_level) noexcept
{
m_confirm_level = std::move(confirm_level);
return *this;
}
void asst::AutoRecruitTask::set_need_refresh(bool need_refresh) noexcept
asst::AutoRecruitTask& asst::AutoRecruitTask::set_need_refresh(bool need_refresh) noexcept
{
m_need_refresh = need_refresh;
return *this;
}
void asst::AutoRecruitTask::set_max_times(int max_times) noexcept
asst::AutoRecruitTask& asst::AutoRecruitTask::set_max_times(int max_times) noexcept
{
m_max_times = max_times;
return *this;
}
void asst::AutoRecruitTask::set_use_expedited(bool use_or_not) noexcept
asst::AutoRecruitTask& asst::AutoRecruitTask::set_use_expedited(bool use_or_not) noexcept
{
m_use_expedited = use_or_not;
return *this;
}
bool asst::AutoRecruitTask::_run()
@@ -104,10 +109,9 @@ bool asst::AutoRecruitTask::recruit_index(size_t index)
bool asst::AutoRecruitTask::calc_and_recruit()
{
RecruitTask recurit_task(m_callback, m_callback_arg);
RecruitTask recurit_task(m_callback, m_callback_arg, m_task_chain);
recurit_task.set_retry_times(m_retry_times);
recurit_task.set_param(m_select_level, true);
recurit_task.set_task_chain(m_task_chain);
// 识别错误,放弃这个公招位,直接返回
if (!recurit_task.run()) {

View File

@@ -11,11 +11,11 @@ namespace asst
using AbstractTask::AbstractTask;
virtual ~AutoRecruitTask() = default;
void set_select_level(std::vector<int> select_level) noexcept;
void set_confirm_level(std::vector<int> confirm_level) noexcept;
void set_need_refresh(bool need_refresh) noexcept;
void set_max_times(int max_times) noexcept;
void set_use_expedited(bool use_or_not) noexcept;
AutoRecruitTask& set_select_level(std::vector<int> select_level) noexcept;
AutoRecruitTask& set_confirm_level(std::vector<int> confirm_level) noexcept;
AutoRecruitTask& set_need_refresh(bool need_refresh) noexcept;
AutoRecruitTask& set_max_times(int max_times) noexcept;
AutoRecruitTask& set_use_expedited(bool use_or_not) noexcept;
protected:
virtual bool _run() override;

View File

@@ -12,8 +12,8 @@
int asst::InfrastAbstractTask::m_face_hash_thres = 0;
int asst::InfrastAbstractTask::m_name_hash_thres = 0;
asst::InfrastAbstractTask::InfrastAbstractTask(AsstCallback callback, void* callback_arg)
: AbstractTask(callback, callback_arg)
asst::InfrastAbstractTask::InfrastAbstractTask(AsstCallback callback, void* callback_arg, std::string task_chain)
: AbstractTask(callback, callback_arg, std::move(task_chain))
{
if (m_face_hash_thres == 0) {
m_face_hash_thres = static_cast<int>(std::dynamic_pointer_cast<MatchTaskInfo>(
@@ -25,7 +25,7 @@ asst::InfrastAbstractTask::InfrastAbstractTask(AsstCallback callback, void* call
}
}
void asst::InfrastAbstractTask::set_work_mode(infrast::WorkMode work_mode) noexcept
asst::InfrastAbstractTask& asst::InfrastAbstractTask::set_work_mode(infrast::WorkMode work_mode) noexcept
{
m_work_mode = work_mode;
switch (work_mode) {
@@ -42,11 +42,13 @@ void asst::InfrastAbstractTask::set_work_mode(infrast::WorkMode work_mode) noexc
m_work_mode_name.clear();
break;
}
return *this;
}
void asst::InfrastAbstractTask::set_mood_threshold(double mood_thres) noexcept
asst::InfrastAbstractTask& asst::InfrastAbstractTask::set_mood_threshold(double mood_thres) noexcept
{
m_mood_threshold = mood_thres;
return *this;
}
bool asst::InfrastAbstractTask::on_run_fails()

View File

@@ -9,11 +9,11 @@ namespace asst
{
public:
using AbstractTask::AbstractTask;
InfrastAbstractTask(AsstCallback callback, void* callback_arg);
InfrastAbstractTask(AsstCallback callback, void* callback_arg, std::string task_chain);
virtual ~InfrastAbstractTask() = default;
virtual void set_work_mode(infrast::WorkMode work_mode) noexcept;
virtual void set_mood_threshold(double mood_thres) noexcept;
virtual InfrastAbstractTask& set_work_mode(infrast::WorkMode work_mode) noexcept;
virtual InfrastAbstractTask& set_mood_threshold(double mood_thres) noexcept;
constexpr static int OperSelectRetryTimes = 3;
protected:

View File

@@ -14,6 +14,24 @@
#include "RuntimeStatus.h"
#include "ProcessTask.h"
asst::InfrastProductionTask& asst::InfrastProductionTask::set_uses_of_drone(std::string uses_of_drones) noexcept
{
m_uses_of_drones = std::move(uses_of_drones);
return *this;
}
asst::InfrastProductionTask& asst::InfrastProductionTask::set_facility(std::string facility_name) noexcept
{
m_facility = std::move(facility_name);
return *this;
}
asst::InfrastProductionTask& asst::InfrastProductionTask::set_product(std::string product_name) noexcept
{
m_product = std::move(product_name);
return *this;
}
bool asst::InfrastProductionTask::shift_facility_list()
{
LogTraceFunction;

View File

@@ -13,24 +13,16 @@ namespace asst
using InfrastAbstractTask::InfrastAbstractTask;
virtual ~InfrastProductionTask() = default;
void set_uses_of_drone(std::string uses_of_drones) noexcept
{
m_uses_of_drones = std::move(uses_of_drones);
}
InfrastProductionTask& set_uses_of_drone(std::string uses_of_drones) noexcept;
#ifdef ASST_DEBUG
public:
#else
// 为了方便调试把这两个个接口拿到public来了
protected:
#endif
void set_facility(std::string facility_name) noexcept
{
m_facility = std::move(facility_name);
}
void set_product(std::string product_name) noexcept
{
m_product = std::move(product_name);
}
InfrastProductionTask& set_facility(std::string facility_name) noexcept;
InfrastProductionTask& set_product(std::string product_name) noexcept;
protected:
bool shift_facility_list();

View File

@@ -45,6 +45,18 @@ bool asst::ProcessTask::run()
return false;
}
asst::ProcessTask& asst::ProcessTask::set_tasks(std::vector<std::string> tasks_name) noexcept
{
m_cur_tasks_name = std::move(tasks_name);
return *this;
}
ProcessTask& asst::ProcessTask::set_times_limit(std::string name, int limit)
{
m_times_limit.emplace(std::move(name), limit);
return *this;
}
bool ProcessTask::_run()
{
LogTraceFunction;

View File

@@ -17,14 +17,8 @@ namespace asst
virtual bool run() override;
void set_tasks(std::vector<std::string> tasks_name) noexcept
{
m_cur_tasks_name = std::move(tasks_name);
}
void set_times_limit(std::string name, int limit)
{
m_times_limit.emplace(std::move(name), limit);
}
ProcessTask& set_tasks(std::vector<std::string> tasks_name) noexcept;
ProcessTask& set_times_limit(std::string name, int limit);
protected:
virtual bool _run() override;

View File

@@ -213,8 +213,9 @@ bool RecruitTask::_run()
return true;
}
void RecruitTask::set_param(std::vector<int> select_level, bool set_time) noexcept
RecruitTask& RecruitTask::set_param(std::vector<int> select_level, bool set_time) noexcept
{
m_select_level = std::move(select_level);
m_set_time = set_time;
return *this;
}

View File

@@ -12,7 +12,7 @@ namespace asst
using AbstractTask::AbstractTask;
virtual ~RecruitTask() = default;
void set_param(std::vector<int> select_level, bool set_time = true) noexcept;
RecruitTask& set_param(std::vector<int> select_level, bool set_time = true) noexcept;
bool get_has_special_tag() const noexcept
{