Skip to content

Commit

Permalink
chore: 规范相关参数类型
Browse files Browse the repository at this point in the history
  • Loading branch information
weiduhuo committed Oct 28, 2023
1 parent 0eb8ffb commit 1687473
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 25 deletions.
18 changes: 9 additions & 9 deletions utils/calculated.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ def is_blackscreen(self, threshold = 30):
screenshot = cv.cvtColor(self.take_screenshot()[0], cv.COLOR_BGR2GRAY)
return cv.mean(screenshot)[0] < threshold

def ocr_pos(self, characters:str = None, points = (0,0,0,0)):
def ocr_pos(self, characters: Optional[str]=None, points = (0,0,0,0)):
"""
说明:
获取指定文字的坐标
Expand All @@ -625,7 +625,8 @@ def ocr_pos(self, characters:str = None, points = (0,0,0,0)):
pos = data[characters] if characters in data else None
return characters, pos

def ocr_pos_for_single_line(self, characters_list:list[str] = None, points = (0,0,0,0), number = False, debug = False, img_pk:tuple = None) -> Union[int, str]:
def ocr_pos_for_single_line(self, characters_list: Optional[List[str]]=None, points=(0,0,0,0), number=False, debug=False, img_pk: Optional[Tuple]=None
) -> Optional[Union[int, str]]:
"""
说明:
获取指定坐标的单行文字
Expand Down Expand Up @@ -661,7 +662,7 @@ def read_img(self, path, prefix='./picture/pc/'):
"""
return cv.imread(f'{prefix}{path}')

def part_ocr(self, points = (0,0,0,0), debug=False, left=False, number = False, img_pk:tuple = None, is_single_line = False, only_white=False
def part_ocr(self, points = (0,0,0,0), debug=False, left=False, number=False, img_pk: Optional[tuple]=None, is_single_line=False, only_white=False
) -> Union[str, dict[str, tuple[int, int]]]:
"""
说明:
Expand Down Expand Up @@ -992,7 +993,7 @@ def change_team(self):

class Array2dict:

def __init__(self, arr:np.ndarray, key_index:int = -1, value_index:int = None):
def __init__(self, arr: np.ndarray, key_index: int = -1, value_index: Optional[int]=None):
"""
说明:
将np数组转化为字典暂住内存,用于对数组短时间内的频繁查找
Expand All @@ -1010,10 +1011,10 @@ def __init__(self, arr:np.ndarray, key_index:int = -1, value_index:int = None):
self.data_dict = {row[key_index]: row[value_index] for row in arr}
log.debug(self.data_dict)

def __getitem__(self, key):
def __getitem__(self, key: Any) -> Any:
return self.data_dict[key]

def get_data_hash(data:Any, key_filter:list[str] = None) -> str:
def get_data_hash(data: Any, key_filter: Optional[List[str]]=None) -> str:
"""
说明:
求任意类型数据 (包括list和dict) 的哈希值
Expand All @@ -1028,12 +1029,11 @@ def get_data_hash(data:Any, key_filter:list[str] = None) -> str:
tmp_data = dict(data).copy() # 注意dict'='为引用传递,此处需拷贝副本
[tmp_data.pop(key) if key in tmp_data else None for key in key_filter]
else:
log.eror(_("不支持dict以外类型的类型使用键值过滤器"))
return None
raise ValueError(_("不支持dict以外类型的类型使用键值过滤器"))
# pprint默认sort_dicts=True,对键值进行排序,以确保字典类型的唯一性
return hashlib.md5(pprint.pformat(tmp_data).encode('utf-8')).hexdigest()

def str_just(text:str, width:int, left = True):
def str_just(text: str, width: int, left=True):
"""
说明:
封装str.rjust()&str.ljust(),以适应中文字符的实际宽度
Expand Down
34 changes: 18 additions & 16 deletions utils/relic.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import questionary
import numpy as np
from collections import Counter
from typing import Any, Dict, List, Literal, Optional, Tuple, Union

from .calculated import calculated, Array2dict, get_data_hash, str_just
from .config import (read_json_file, modify_json_file, rewrite_json_file,
Expand Down Expand Up @@ -256,7 +257,7 @@ def equip_loadout_for_char(self):
self.calculated.relative_click((97,6) if IS_PC else (96,5)) # 退出[遗器]界面,返回[人物]界面
time.sleep(1)

def equip_loadout(self, relics_hash:list[str]):
def equip_loadout(self, relics_hash:List[str]):
"""
说明:
装备当前[人物]-[遗器]-[遗器详情]页面内的指定遗器配装。
Expand Down Expand Up @@ -324,7 +325,7 @@ def save_loadout_for_char(self):
self.calculated.relative_click((97,6) if IS_PC else (96,5)) # 退出[遗器]界面,返回[人物]界面
time.sleep(2)

def save_loadout(self, character_name:str=None, max_retries=3):
def save_loadout(self, character_name: Optional[str]=None, max_retries=3):
"""
说明:
保存当前[人物]-[遗器]-[遗器详情]界面内的遗器配装
Expand Down Expand Up @@ -367,15 +368,15 @@ class Relic_filter:
rarity_pos_list = [(77,38),(89,38),(77,42),(89,42)] if IS_PC else [(71,45),(86,45),(71,52),(86,52)]
"""稀有度筛选项的点击位点 (分别为2,3,4,5星稀有度)"""

def __init__(self, calculated:calculated):
def __init__(self, calculated: calculated):
self.calculated = calculated
# 记录上一次的筛选状态
self.pre_relic_set_index = -1
"""过去遗器套装索引"""
self.pre_rarity = -1
"""过去稀有度"""

def do(self, relic_set_index:int, rairty:int):
def do(self, relic_set_index: int, rairty: int):
"""
说明:
在当前[人物]-[遗器]-[遗器详情]内进行遗器筛选
Expand Down Expand Up @@ -411,7 +412,7 @@ def do(self, relic_set_index:int, rairty:int):
... # 其他筛选条件
self.calculated.relative_click((3,92) if IS_PC else (4,92)) # 任意点击筛选框外退出[遗器筛选]界面

def search_relic_set_for_filter(self, relic_set_index:int):
def search_relic_set_for_filter(self, relic_set_index: int):
"""
说明:
在当前滑动[人物]-[遗器]-[遗器详情]-[遗器筛选]-[遗器套装筛选]界面内,搜索遗器套装名,并点击。
Expand All @@ -434,7 +435,8 @@ def search_relic_set_for_filter(self, relic_set_index:int):
self.calculated.ocr_click(Relic.relic_set_name[relic_set_index, 1], points=points)


def search_relic(self, equip_indx:int, key_hash:str=None, key_data:dict=None, overtime=180, max_retries=3) -> tuple[int, int]:
def search_relic(self, equip_indx: int, key_hash: Optional[str]=None, key_data: Optional[Dict[str, Any]]=None, overtime=180, max_retries=3
) -> Optional[tuple[int, int]]:
"""
说明:
在当前滑动[人物]-[遗器]-[遗器详情]界面内,搜索匹配的遗器。
Expand Down Expand Up @@ -496,7 +498,7 @@ def search_relic(self, equip_indx:int, key_hash:str=None, key_data:dict=None, ov
break
return None

def compare_relics(self, old_data:dict, new_data:dict) -> bool:
def compare_relics(self, old_data: Dict[str, Any], new_data: Dict[str, Any]) -> bool:
"""
说明:
比对两者遗器数据,判断新遗器是否为旧遗器升级后
Expand All @@ -514,7 +516,7 @@ def compare_relics(self, old_data:dict, new_data:dict) -> bool:
return False
return True

def check_relic_data_hash(self, updata=False):
def check_relic_data_hash(self, updata=False) -> bool:
"""
说明:
检查遗器数据是否发生手动修改 (应对json数据格式变动或手动矫正仪器数值),
Expand All @@ -541,7 +543,7 @@ def check_relic_data_hash(self, updata=False):
log.error(_(f"发现 {cnt} 件遗器的哈希值校验失败"))
return False

def updata_relic_data(self, old_hash:str, new_hash:str, equip_indx:int, new_data:dict=None, delete_old_data=False):
def updata_relic_data(self, old_hash: str, new_hash: str, equip_indx: int, new_data: Optional[Dict[str, Any]]=None, delete_old_data=False):
"""
说明:
更改仪器数据,先后修改遗器与配装文件
Expand All @@ -568,7 +570,7 @@ def updata_relic_data(self, old_hash:str, new_hash:str, equip_indx:int, new_data
rewrite_json_file(LOADOUT_FILE_NAME, self.loadout_data)
# 队伍配装文件无需修改

def add_relic_data(self, data:dict, data_hash:str=None) -> bool:
def add_relic_data(self, data: Dict[str, Any], data_hash: Optional[str]=None) -> bool:
"""
说明:
录入仪器数据
Expand Down Expand Up @@ -597,7 +599,7 @@ def ocr_character_name(self) -> str:
log.info(_("创建新人物"))
return character_name

def try_ocr_relic(self, equip_set_index:int = None, max_retries = 3) -> dict:
def try_ocr_relic(self, equip_set_index:Optional[int]=None, max_retries=3) -> Dict[str, Any]:
"""
说明:
在规定次数内尝试OCR遗器数据
Expand All @@ -618,7 +620,7 @@ def try_ocr_relic(self, equip_set_index:int = None, max_retries = 3) -> dict:
retry += 1
log.info(_(f"第 {retry} 次尝试重新OCR"))

def ocr_relic(self, equip_set_index:int = None) -> dict:
def ocr_relic(self, equip_set_index: Optional[int]=None) -> Dict[str, Any]:
"""
说明:
OCR当前静态[人物]-[遗器]-[遗器详情]界面内的遗器数据,单次用时约0.5s。
Expand All @@ -643,7 +645,7 @@ def ocr_relic(self, equip_set_index:int = None) -> dict:
if relic_set_index < 0:
raise RelicOCRException(_("遗器部位OCR错误"))
relic_set_name = self.relic_set_name[relic_set_index, -1]
# [3] 稀有度识别
# [3]稀有度识别
hue, __, __ = self.calculated.get_relative_pix_hsv((43,55) if IS_PC else (41,55)) # 识别指定位点色相
log.debug(f"hue = {hue}")
if hue < 40: # [黄]模拟器测试结果的均值为 25
Expand Down Expand Up @@ -721,7 +723,7 @@ def ocr_relic(self, equip_set_index:int = None) -> dict:
log.info(f"用时\033[1;92m『{seconds:.1f}秒』\033[0m")
return result_data

def print_relic(self, data:dict):
def print_relic(self, data: Dict[str, Any]):
"""
说明:
打印遗器信息,
Expand Down Expand Up @@ -753,7 +755,7 @@ def print_relic(self, data:dict):
print(_(" {name:<4}\t{value:>5}{pre} [ERROR]").format(name=name, value=value, pre=pre))
print('-'*50)

def get_loadout_brief(self, relics_hash:list[str]) -> str:
def get_loadout_brief(self, relics_hash: List[str]) -> str:
"""
说明:
获取配装的简要信息 (包含内外圈套装信息与主词条信息)
Expand All @@ -780,7 +782,7 @@ def get_loadout_brief(self, relics_hash:list[str]) -> str:
msg += ".".join([name for idx, name in enumerate(base_stats_list) if idx > 1]) # 排除头部与手部
return msg

def get_subs_stats_detail(self, data:tuple[str, float], rarity:int, stats_index:int = None) -> tuple[int, int, float]:
def get_subs_stats_detail(self, data: Tuple[str, float], rarity: int, stats_index: Optional[int]=None) -> Optional[Tuple[int, int, float]]:
"""
说明:
计算副词条的详细信息 (如强化次数、档位积分,以及提高原数据的小数精度)
Expand Down

0 comments on commit 1687473

Please sign in to comment.