Skip to content

Commit

Permalink
Merge pull request #385 from CL4R3T/issue-369
Browse files Browse the repository at this point in the history
彻底解决了 #357 #369
  • Loading branch information
moesnow authored Nov 24, 2024
2 parents 3ead5bb + 8e2acea commit 6bcbed1
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 41 deletions.
Binary file added assets/images/zh_CN/fight/fight_fail.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
139 changes: 112 additions & 27 deletions tasks/power/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
import time
import json


class Instance:
@staticmethod
def run(instance_type, instance_name, power_need, runs):
def run(instance_type, instance_name, power_need, runs, from_failure = False, runs_completed = 0):
if not Instance.validate_instance(instance_type, instance_name):
return False

log.hr(f"开始刷{instance_type} - {instance_name},总计{runs}次", 2)

if cfg.instance_team_enable and "饰品提取" not in instance_type:
Team.change_to(cfg.instance_team_number)

# 若是任务失败后的重新运行, 则改变运行逻辑
if from_failure:
log.hr(f"复活后重新进入副本{instance_type} - {instance_name},剩余{runs-runs_completed}次", 2)
else:
log.hr(f"开始刷{instance_type} - {instance_name},总计{runs}次", 2)
if cfg.instance_team_enable and "饰品提取" not in instance_type:
Team.change_to(cfg.instance_team_number)

if not Instance.prepare_instance(instance_type, instance_name):
return False
Expand All @@ -28,16 +30,26 @@ def run(instance_type, instance_name, power_need, runs):
return False

try:
for i in range(runs - 1):
Instance.wait_fight(i + 1)
Instance.start_instance_again(instance_type)
Instance.wait_fight(runs)
for i in range(runs_completed, runs):
fight_result = Instance.wait_fight(i + 1)

# 如果战斗失败则重新运行, 并记录成功运行次数
if not fight_result:
auto.click_element("./assets/images/zh_CN/fight/fight_fail.png", "image", 0.9, max_retries=10)
time.sleep(2)
Instance.run(instance_type, instance_name, power_need, runs, from_failure = True, runs_completed = i)
break

if i < runs-1:
Instance.start_instance_again(instance_type)
except RuntimeError:
return False

Instance.complete_run(instance_type)

log.info("副本任务完成")

# 若是任务失败后的重新运行, 则不再重复这些逻辑
if not from_failure:
Instance.complete_run(instance_type)
log.info("副本任务完成")

return True

@staticmethod
Expand All @@ -50,6 +62,8 @@ def validate_instance(instance_type, instance_name):
@staticmethod
def prepare_instance(instance_type, instance_name):
screen.change_to('guide3')
time.sleep(1)

instance_type_crop = (262.0 / 1920, 289.0 / 1080, 422.0 / 1920, 624.0 / 1080)

if not auto.click_element(instance_type, "text", crop=instance_type_crop):
Expand Down Expand Up @@ -131,18 +145,6 @@ def func(): return auto.click_element(("传送", "进入", "追踪"), "min_dista

@staticmethod
def start_instance(instance_type, power_need):
if "拟造花萼" in instance_type:
count = power_need // 10 - 1
if not 0 <= count <= 5:
Base.send_notification_with_screenshot(cfg.notify_template['InstanceNotCompleted'].format(error="拟造花萼次数错误"))
return False
result = auto.find_element("./assets/images/screen/guide/plus.png", "image", 0.8, max_retries=10,
crop=(1174.0 / 1920, 775.0 / 1080, 738.0 / 1920, 174.0 / 1080))
if result:
for i in range(count):
auto.click_element_with_pos(result)
time.sleep(0.5)
# time.sleep(1)

if "饰品提取" in instance_type:
time.sleep(1)
Expand Down Expand Up @@ -226,6 +228,10 @@ def wait_fight(num, timeout=1800):
log.info("战斗完成")
log.info(f"第{num}次副本完成")
return True
elif auto.find_element("./assets/images/zh_CN/fight/fight_fail.png", "image", 0.9):
log.info("战斗失败")
log.info(f"复活并重新开始第{num}次副本")
return False
elif cfg.auto_battle_detect_enable and auto.find_element("./assets/images/share/base/not_auto.png", "image", 0.9, crop=(0.0 / 1920, 903.0 / 1080, 144.0 / 1920, 120.0 / 1080)):
log.info("尝试开启自动战斗")
auto.press_key("v")
Expand All @@ -234,3 +240,82 @@ def wait_fight(num, timeout=1800):

log.error("战斗超时")
raise RuntimeError("战斗超时")

class CalyxInstance(Instance):
def run(instance_type, instance_name, power_need, runs):
if not CalyxInstance.validate_instance(instance_type, instance_name):
return False

log.hr(f"开始刷{instance_type} - {instance_name},总计{runs}次,每次包含{power_need//10}波次", 2)

if cfg.instance_team_enable:
Team.change_to(cfg.instance_team_number)

if not CalyxInstance.prepare_instance(instance_type, instance_name):
return False

if not CalyxInstance.start_instance(power_need):
return False

try:
for i in range(runs):
fight_result = CalyxInstance.wait_fight(i + 1)

# 如果战斗失败则重新运行, 并记录成功运行次数
if not fight_result:
auto.click_element("./assets/images/zh_CN/fight/fight_fail.png", "image", 0.9, max_retries=10)
return "Failed"

if i < runs-1:
CalyxInstance.start_instance_again(instance_type)

except RuntimeError:
return False

CalyxInstance.complete_run(instance_type)
log.info("副本任务完成")

return True

def start_instance(power_need):
count = power_need // 10 - 1
if not 0 <= count <= 5:
Base.send_notification_with_screenshot(cfg.notify_template['InstanceNotCompleted'].format(error="拟造花萼次数错误"))
return False
result = auto.find_element("./assets/images/screen/guide/plus.png", "image", 0.8, max_retries=10,
crop=(1174.0 / 1920, 775.0 / 1080, 738.0 / 1920, 174.0 / 1080))
if result:
for i in range(count):
auto.click_element_with_pos(result)
time.sleep(0.5)
# time.sleep(1)

if auto.click_element("挑战", "text", max_retries=10, need_ocr=True):
Character.borrow()
return auto.click_element("开始挑战", "text", max_retries=10, crop=(1518 / 1920, 960 / 1080, 334 / 1920, 61 / 1080))
return False


@staticmethod
def wait_fight(num, timeout=1800):
log.info("进入战斗")
time.sleep(5)

start_time = time.time()
while time.time() - start_time < timeout:
if auto.find_element("./assets/images/zh_CN/fight/fight_again.png", "image", 0.9):
log.info("战斗完成")
log.info(f"第{num}次副本完成")
return True
elif auto.find_element("./assets/images/zh_CN/fight/fight_fail.png", "image", 0.9):
log.info("战斗失败")
log.info(f"获取剩余体力并重新计算轮次")
return False
elif cfg.auto_battle_detect_enable and auto.find_element("./assets/images/share/base/not_auto.png", "image", 0.9, crop=(0.0 / 1920, 903.0 / 1080, 144.0 / 1920, 120.0 / 1080)):
log.info("尝试开启自动战斗")
auto.press_key("v")

time.sleep(2)

log.error("战斗超时")
raise RuntimeError("战斗超时")
39 changes: 25 additions & 14 deletions tasks/power/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from module.automation import auto
from module.logger import log
from module.config import cfg
from tasks.power.instance import Instance
from tasks.power.instance import Instance, CalyxInstance
from tasks.weekly.universe import Universe
import time

Expand All @@ -24,7 +24,7 @@ def run():
if "饰品提取" in instance_type:
Power.process_ornament(instance_type, instance_name, power)
elif "拟造花萼" in instance_type:
Power.process_calyx(instance_type, instance_name, power)
Power.process_calyx(instance_type, instance_name)
else:
Power.process_standard(instance_type, instance_name, power)

Expand Down Expand Up @@ -79,19 +79,28 @@ def process_ornament(instance_type, instance_name, power):
Instance.run(instance_type, instance_name, 40, immersifier_count + full_runs)

@staticmethod
def process_calyx(instance_type, instance_name, power):
instance_power_max = 60
def process_calyx(instance_type, instance_name):
instance_power_min = 10

full_runs = power // instance_power_max
if full_runs:
Instance.run(instance_type, instance_name, instance_power_max, full_runs)

partial_run_power = power % instance_power_max
if partial_run_power >= instance_power_min:
Instance.run(instance_type, instance_name, partial_run_power, 1)
elif full_runs == 0:
log.info(f"🟣开拓力 < {instance_power_max}")
instance_power_max = 60
while True:
power = Power.get()

if power<instance_power_min:
log.info(f"🟣开拓力 < {instance_power_min}")
break

full_runs = power//instance_power_max
if full_runs>=1:
result = CalyxInstance.run(instance_type, instance_name, instance_power_max, full_runs)
if result == "Failed":
continue

remain_runs = (power%instance_power_max)//instance_power_min
if remain_runs>=1:
result = CalyxInstance.run(instance_type, instance_name, remain_runs*instance_power_min, 1)
if result == "Failed":
continue
break

@staticmethod
def process_standard(instance_type, instance_name, power):
Expand Down Expand Up @@ -120,6 +129,8 @@ def customize_run(instance_type, instance_name, power_need, runs):
if power < power_need * runs:
log.info(f"🟣开拓力 < {power_need}*{runs}")
return False
elif "拟造花萼" in instance_type:
return CalyxInstance.run(instance_type, instance_name, power_need * runs)
else:
return Instance.run(instance_type, instance_name, power_need, runs)

Expand Down

0 comments on commit 6bcbed1

Please sign in to comment.