Skip to content

Commit

Permalink
🎨 优化百鬼夜行
Browse files Browse the repository at this point in the history
  • Loading branch information
AquamarineCyan committed Jan 19, 2025
1 parent c757e99 commit b3a79aa
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 51 deletions.
38 changes: 20 additions & 18 deletions src/package/baiguiyexing.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from ..utils.adapter import Mouse
from ..utils.application import SCREENSHOT_DIR_PATH
from ..utils.decorator import log_function_call
from ..utils.event import event_thread
from ..utils.exception import GUIStopException
from ..utils.function import random_num, random_point, sleep
from ..utils.image import RuleImage
from ..utils.log import logger
from ..utils.point import RelativePoint
from ..utils.window import window
from .utils import Package

Expand All @@ -18,7 +18,7 @@ class BaiGuiYeXing(Package):
resource_list = [
"title", # 标题
"jinru", # 进入
"ya", # 押选
"choose", # 押选
"kaishi", # 开始
"baiguiqiyueshu", # 百鬼契约书
]
Expand All @@ -40,8 +40,9 @@ def load_asset(self):

def start(self):
"""开始"""
self.check_click(self.IMAGE_JINRU)
self.check_click(self.IMAGE_JINRU, timeout=3)

@log_function_call
def choose(self):
"""鬼王选择"""
_x1_left = 230
Expand All @@ -67,31 +68,34 @@ def choose(self):
x1 = _x3_left
x2 = _x3_right

point: RelativePoint = random_point(x1, x2, _y1, _y2)
point = random_point(x1, x2, _y1, _y2)
Mouse.click(point)
sleep()
if RuleImage(self.IMAGE_CHOOSE).match():
logger.ui("已选择鬼王")
break

self.check_click(self.IMAGE_START)
self.check_click(self.IMAGE_START, timeout=3)

@log_function_call
def fighting(self):
"""砸豆子"""
sleep()
sleep(4) # 等待进入
for _ in range(250, 0, -5):
if bool(event_thread):
raise GUIStopException

sleep(0.2, 1)
point: RelativePoint = random_point(
# 屏幕中心区域
point = random_point(
60,
window.window_width - 120,
300,
window.window_height - 100,
)
Mouse.click(point, duration=0.25)

@log_function_call
def finish(self):
"""结束"""
while True:
Expand All @@ -100,31 +104,29 @@ def finish(self):

result = RuleImage(self.IMAGE_FINISH)
if result.match():
logger.ui("结束")
point = result.random_point()
sleep()
self.screenshot()
if 1: # TODO 可选
self.screenshot()
logger.ui("「百鬼契约书」已截图")
Mouse.click(point)
return

def finish_info(self):
logger.ui(f"截图保存在{SCREENSHOT_DIR_PATH / self.resource_path}")
def task_finish_info(self):
logger.ui(f"截图保存在\n{SCREENSHOT_DIR_PATH / self.resource_path}")

@log_function_call
def run(self):
self.check_title()
while self.n < self.max:
if bool(event_thread):
raise GUIStopException

sleep()

self.start()
sleep(2)
self.choose()
sleep(2, 4)
self.fighting()
sleep(2, 4)
self.finish()
self.done()
sleep(3)

# TODO 更新随机判断
if self.n in {12, 25, 39}:
sleep(10, 20)
4 changes: 2 additions & 2 deletions src/package/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ def run(self):
"""任务内容,支持重写"""
pass

def finish_info(self):
def task_finish_info(self):
"""任务结束信息,支持重写"""
pass

Expand Down Expand Up @@ -418,7 +418,7 @@ def task_start(self):
except Exception:
logger.error("耗时统计计算失败")

self.finish_info()
self.task_finish_info()
if xuanshangfengyin_count.get():
logger.ui_warn(f"接到 {xuanshangfengyin_count.get()} 个悬赏封印")
xuanshangfengyin_count.reset()
Expand Down
2 changes: 1 addition & 1 deletion src/resource/baiguiyexing/assets.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
{
"name": "choose",
"file": "ya.png",
"file": "choose.png",
"region": [0, 0, 0, 0],
"score": "0.8"
}
Expand Down
File renamed without changes
29 changes: 9 additions & 20 deletions src/utils/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .event import event_thread
from .exception import GUIStopException
from .log import logger
from .point import Point, Rectangle, RelativePoint
from .point import Rectangle, RelativePoint


def is_Chinese_Path() -> bool:
Expand Down Expand Up @@ -58,7 +58,7 @@ def random_num(minimum: int | float, maximum: int | float) -> float:
return round((random.random() * (maximum - minimum) + minimum), 2)


def random_point(x1: int, x2: int, y1: int, y2: int) -> Point:
def random_point(x1: int, x2: int, y1: int, y2: int) -> RelativePoint:
"""伪随机坐标,返回给定坐标区间的随机值
参数:
Expand All @@ -73,7 +73,7 @@ def random_point(x1: int, x2: int, y1: int, y2: int) -> Point:
x = random_normal(x1, x2)
y = random_normal(y1, y2)
logger.info(f"random_coor: {x},{y}")
return Point(x, y)
return RelativePoint(x, y)


def random_sleep(minimum: int | float = 1.0, maximum: int | float = None) -> None:
Expand Down Expand Up @@ -141,12 +141,8 @@ def finish_random_left_right(

# 计算鼠标到两个矩形中心的距离
position = Mouse.position()
distance_to_left = distance_between_two_points(
position.coor, rect_left.get_center()
)
distance_to_right = distance_between_two_points(
position.coor, rect_right.get_center()
)
distance_to_left = distance_between_two_points(position.coor, rect_left.get_center())
distance_to_right = distance_between_two_points(position.coor, rect_right.get_center())

# 判断距离
if distance_to_left < distance_to_right:
Expand All @@ -160,14 +156,11 @@ def finish_random_left_right(
chosen_rect = random.choice([rect_left, rect_right])
logger.info(f"鼠标距离两个矩形相等,随机选择矩形{chosen_rect}")

point: RelativePoint = random_point(
chosen_rect.x1, chosen_rect.x2, chosen_rect.y1, chosen_rect.y2
)
point = RelativePoint(point.x, point.y)
point = random_point(chosen_rect.x1, chosen_rect.x2, chosen_rect.y1, chosen_rect.y2)

if bool(event_thread):
raise GUIStopException

if is_click:
Mouse.click(point)
return point
Expand All @@ -176,9 +169,7 @@ def finish_random_left_right(
def check_user_file_exists(file: str) -> Path | None:
"""检查用户素材"""
_full_path = RESOURCE_DIR_PATH / file
_full_path_user = (USER_DATA_DIR_PATH / "myresource").joinpath(
*_full_path.parts[-2:]
)
_full_path_user = (USER_DATA_DIR_PATH / "myresource").joinpath(*_full_path.parts[-2:])
if _full_path_user.exists():
logger.info(f"使用用户素材{_full_path_user}")
return _full_path_user
Expand Down Expand Up @@ -238,9 +229,7 @@ def merge_dict(base_dict, update_dict) -> dict:

def get_asset_data(resource_path) -> tuple[Path | dict]:
_full_path = RESOURCE_DIR_PATH / resource_path / "assets.json"
_full_path_user = (USER_DATA_DIR_PATH / "myresource").joinpath(
*_full_path.parts[-2:]
)
_full_path_user = (USER_DATA_DIR_PATH / "myresource").joinpath(*_full_path.parts[-2:])

data_default = open_asset_file(_full_path)
assets_file = _full_path
Expand Down
16 changes: 7 additions & 9 deletions src/utils/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,11 @@ def __init__(
x2: int = None,
y2: int = None,
):
assert (
width is None or x2 is None
), "Cannot specify both x2, y2 and width, height at the same time"
assert width is None or x2 is None, "Cannot specify both x2, y2 and width, height at the same time"

assert (width is not None and height is not None) or (
x2 is not None and y2 is not None
), "width, height or x2, y2 must have one pair of data"
assert (width is not None and height is not None) or (x2 is not None and y2 is not None), (
"width, height or x2, y2 must have one pair of data"
)

self.x1 = x
self.y1 = y
Expand All @@ -97,13 +95,13 @@ def __init__(
self.width = x2 - x
self.height = y2 - y

def get_box(self):
def get_box(self) -> tuple[int, int, int, int]:
return (self.x1, self.y1, self.width, self.height)

def get_coordinates(self):
def get_coordinates(self) -> tuple[int, int, int, int]:
return (self.x1, self.y1, self.x2, self.y2)

def get_center(self):
def get_center(self) -> tuple[float, float]:
return (self.x1 + self.width / 2, self.y1 + self.height / 2)

def get_rela_center(self) -> "RelativePoint":
Expand Down
2 changes: 1 addition & 1 deletion tests/package/test_baiguiyexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class BaiGuiYeXing(Package):
resource_list = [
"title",
"jinru",
"ya",
"choose",
"kaishi",
"baiguiqiyueshu",
]
Expand Down

0 comments on commit b3a79aa

Please sign in to comment.