P2-6 镜牢商店必买先按中文名购买, 取消 P2-7
- 商店每一格下面写着饰品中文名(实机截图可见)。install_shop_by_name 在 buy_gifts 期间往 must_purchase 最前面临时插 3 个占位项, 上游逐个 click_element 点到占位项时: 以 shop_coins_assets 为锚点在商品格范围内 OCR, 名字完全一致 (允许 + 后缀)就点上方的图标, 之后购买/确认仍走上游循环; 图标模板在后面兜底。 - 名字表 lc_mcp/data/shop_gifts.json: AALC 5 张必买模板对应的饰品, 中文名按零协 汉化; 同一名字一次进店最多点 4 次; 没命中的截图不重复 OCR。 - 审计记 shop/name_match; 装在商店审计之后, 购买仍记 purchase_clicked。 - LCP_SHOP_NAMES=0 关闭; 表读不了或上游不再逐个点 must_purchase 时记 [ERROR]。 - 单测执行上游真实的 Shop.buy_gifts, 坐标按截图实测。 - P2-7 观测饰品自动勾选按用户意见不做。 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -992,6 +992,12 @@ def _source_calls(func, method, target):
|
||||
return re.search(r'\.%s\(\s*["\']%s["\']' % (re.escape(method), re.escape(target)), source) is not None
|
||||
|
||||
|
||||
def _source_mentions(func, *needles):
|
||||
"""上游函数源码里是否都有这些片段; 拿不到源码返回 None。"""
|
||||
source = _upstream_source(func)
|
||||
return None if source is None else all(needle in source for needle in needles)
|
||||
|
||||
|
||||
def load_event_choices(path=EVENT_CHOICES_FILE):
|
||||
"""读事件表, 返回 [{'name', 'id', 'options', 'text'}]; 格式不对抛 ValueError。
|
||||
|
||||
@@ -1195,6 +1201,136 @@ def install_shop_audit(log=print):
|
||||
return original
|
||||
|
||||
|
||||
# ---------------------------------------------------------------- 商店按中文名购买
|
||||
|
||||
# 上游必买只认 mirror/shop/must_purchase/ 下 5 张图标模板(阈值 0.85)。汉化后商店每一格下面写着饰品中文名
|
||||
# (docs/feedback/mirror_20261002_181322/special_shop_missed.png), 这里按名字表 OCR, 命中就点那一格的图标;
|
||||
# 之后的"购买 -> 确认"仍是上游同一段循环, 模板留在后面兜底。
|
||||
# 挂法: buy_gifts 期间在 must_purchase 最前面临时插几个占位项; 上游 for commodity in must_purchase:
|
||||
# auto.click_element(commodity, ...) 点到占位项时改成按名字找。
|
||||
SHOP_NAMES_ENV = 'LCP_SHOP_NAMES' # 0 = 关闭; 文件路径 = 用这张表代替默认表
|
||||
SHOP_NAMES_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'data', 'shop_gifts.json')
|
||||
SHOP_BY_NAME = 'lc_mcp/shop_by_name' # 占位项; 不是图片, 只在补丁里认
|
||||
SHOP_BY_NAME_SLOTS = 3 # 每轮必买最多按名字买几件
|
||||
SHOP_NAME_CLICKS = 4 # 一次进店同一个名字最多点几次(钱不够时上游会重点)
|
||||
SHOP_COINS = 'mirror/shop/shop_coins_assets.png' # 上游 re_sort_points 也拿它当商品格的锚点
|
||||
SHOP_GRID_BOX = (0, 300, 1250, 900) # 名字行相对金币图标的范围(1440 标定; 实测两行名字在 +409/+716)
|
||||
SHOP_NAME_TO_ICON = 155 # 名字行中心到图标中心的纵向距离(1440 标定, 1080 截图实测 116)
|
||||
|
||||
|
||||
def load_shop_gifts(path=SHOP_NAMES_FILE):
|
||||
"""读按名字购买的表, 返回归一后的名字元组(靠前的先买); 格式不对抛 ValueError。"""
|
||||
with open(path, encoding='utf-8') as f:
|
||||
raw = json.load(f)
|
||||
gifts = raw.get('gifts') if isinstance(raw, dict) else None
|
||||
if not isinstance(gifts, list) or not gifts:
|
||||
raise ValueError('顶层应是 {"gifts": [{"name": ...}, ...]}')
|
||||
names = []
|
||||
for item in gifts:
|
||||
name = item.get('name') if isinstance(item, dict) else None
|
||||
if not isinstance(name, str) or not normalize_event_text(name):
|
||||
raise ValueError('gifts 的每一项应有非空的 name')
|
||||
if normalize_event_text(name) in names:
|
||||
raise ValueError('%s 重复' % name)
|
||||
names.append(normalize_event_text(name))
|
||||
return tuple(names)
|
||||
|
||||
|
||||
def shop_name_hits(lines, names, coins, scale):
|
||||
"""商品格里名字对上表的行, 按表里的顺序: [(名字, 图标位置), ...]。
|
||||
|
||||
只认金币图标右下方商品格范围内、与名字完全一致的行(允许强化后缀 +); 子串不算, 免得半截名字误买。
|
||||
"""
|
||||
left, top, right, bottom = (v * scale for v in SHOP_GRID_BOX)
|
||||
x0, y0 = float(coins[0]), float(coins[1])
|
||||
found = {}
|
||||
for text, x, y in lines:
|
||||
if not (x0 + left <= x <= x0 + right and y0 + top <= y <= y0 + bottom):
|
||||
continue
|
||||
clean = normalize_event_text(text).rstrip('+')
|
||||
if clean in names and clean not in found:
|
||||
found[clean] = (x, y - SHOP_NAME_TO_ICON * scale)
|
||||
return [(name, found[name]) for name in names if name in found]
|
||||
|
||||
|
||||
def install_shop_by_name(log=print, env=None):
|
||||
"""商店必买先按中文名找。返回原 Shop.buy_gifts, 未安装返回 None。"""
|
||||
env = os.environ if env is None else env
|
||||
setting = (env.get(SHOP_NAMES_ENV) or '').strip()
|
||||
if setting == '0':
|
||||
log('[lc_mcp] 商店按名字购买已关闭(%s=0), 只用 AALC 的图标模板' % SHOP_NAMES_ENV)
|
||||
return None
|
||||
path = setting or SHOP_NAMES_FILE
|
||||
try:
|
||||
names = load_shop_gifts(path)
|
||||
except (OSError, ValueError) as e:
|
||||
log('[ERROR] [lc_mcp] 商店按名字购买跳过: %s 读不了(%s: %s)' % (path, type(e).__name__, e))
|
||||
return None
|
||||
try:
|
||||
from tasks.mirror import in_shop
|
||||
from module.automation import auto
|
||||
from module.config import cfg
|
||||
except Exception as e:
|
||||
log('[lc_mcp] 商店按名字购买跳过: 无法导入 %s' % e)
|
||||
return None
|
||||
shop_cls, must = getattr(in_shop, 'Shop', None), getattr(in_shop, 'must_purchase', None)
|
||||
original = getattr(shop_cls, 'buy_gifts', None)
|
||||
if original is None or not isinstance(must, list):
|
||||
log('[ERROR] [lc_mcp] 商店按名字购买跳过: 上游没有 Shop.buy_gifts 或 must_purchase 列表')
|
||||
return None
|
||||
if _source_calls(original, 'click_element', 'mirror/shop/purchase_assets.png') is False or \
|
||||
_source_mentions(original, 'for commodity in must_purchase', 'auto.click_element(commodity') is False:
|
||||
log('[ERROR] [lc_mcp] 商店按名字购买跳过: 上游 Shop.buy_gifts 不再逐个 click_element(must_purchase), 挂不上')
|
||||
return None
|
||||
|
||||
def pick(state, real_find):
|
||||
"""按名字点一格返回 True; 画面上没有表里的名字返回 False。"""
|
||||
shot = auto.screenshot
|
||||
if shot is not None and shot is state['empty_shot']:
|
||||
return False # 这张截图刚 OCR 过且没有命中, 后面几个占位项不用再跑
|
||||
coins = real_find(SHOP_COINS)
|
||||
hits = []
|
||||
if coins:
|
||||
scale = cfg.set_win_size / 1440
|
||||
hits = [(name, pos) for name, pos in shop_name_hits(_ocr_lines(auto), names, coins, scale)
|
||||
if state['clicks'].get(name, 0) < SHOP_NAME_CLICKS]
|
||||
if not hits:
|
||||
state['empty_shot'] = shot
|
||||
return False
|
||||
name, (x, y) = hits[0]
|
||||
state['clicks'][name] = state['clicks'].get(name, 0) + 1
|
||||
if state['clicks'][name] == 1:
|
||||
log('[lc_mcp] 商店: 按名字找到「%s」, 点它的图标' % name)
|
||||
decisions.record('shop', name, 'name_match', on_screen=[n for n, _ in hits])
|
||||
auto.mouse_click(x, y)
|
||||
return True
|
||||
|
||||
def buy_gifts(self, *args, **kwargs):
|
||||
state = {'clicks': {}, 'empty_shot': None}
|
||||
real_find, real_click = auto.find_element, auto.click_element
|
||||
|
||||
def click_element(target, *a, **kw):
|
||||
if target != SHOP_BY_NAME:
|
||||
return real_click(target, *a, **kw)
|
||||
try:
|
||||
return pick(state, real_find)
|
||||
except Exception as e:
|
||||
log('[lc_mcp] 商店按名字购买出错(%s: %s), 只用图标模板' % (type(e).__name__, e))
|
||||
return False
|
||||
|
||||
must[0:0] = [SHOP_BY_NAME] * SHOP_BY_NAME_SLOTS
|
||||
try:
|
||||
with _override(auto, 'click_element', click_element):
|
||||
return original(self, *args, **kwargs)
|
||||
finally:
|
||||
must[:] = [item for item in must if item != SHOP_BY_NAME]
|
||||
|
||||
buy_gifts.__wrapped__ = original # 让后装的补丁查得到上游源码
|
||||
shop_cls.buy_gifts = buy_gifts
|
||||
log('[lc_mcp] patch: 商店必买先按中文名找(%s, %d 个名字), 图标模板兜底' % (os.path.basename(path), len(names)))
|
||||
return original
|
||||
|
||||
|
||||
THEME_PACK_PICK = re.compile(r'此次选择卡包关键词[::]\s*(\S+)')
|
||||
THEME_PACK_WEIGHTS = '当前主题包权重列表'
|
||||
THEME_PACK_SOURCE = 'select_theme_pack.py'
|
||||
|
||||
Reference in New Issue
Block a user