fix(task): 修复商店购买有几率卡在确认购买对话框上

This commit is contained in:
XcantloadX 2025-07-06 17:30:10 +08:00
parent 3b3aac65dc
commit cf1605d913
7 changed files with 36 additions and 15 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 506 KiB

View File

@ -0,0 +1 @@
{"definitions":{"5d36b880-7b3f-49b1-a018-7de59867d376":{"name":"Daily.TextShopItemPurchased","displayName":"交換しました","type":"template","annotationId":"5d36b880-7b3f-49b1-a018-7de59867d376","useHintRect":false}},"annotations":[{"id":"5d36b880-7b3f-49b1-a018-7de59867d376","type":"rect","data":{"x1":275,"y1":626,"x2":432,"y2":655}}]}

Binary file not shown.

After

Width:  |  Height:  |  Size: 522 KiB

View File

@ -0,0 +1 @@
{"definitions":{"24dc7158-036c-4a66-9280-e934f470be53":{"name":"Daily.TextShopItemSoldOut","displayName":"交換済みです","type":"template","annotationId":"24dc7158-036c-4a66-9280-e934f470be53","useHintRect":false}},"annotations":[{"id":"24dc7158-036c-4a66-9280-e934f470be53","type":"rect","data":{"x1":287,"y1":625,"x2":434,"y2":655}}]}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 5.1 KiB

View File

@ -2,9 +2,11 @@
import logging
from typing import Optional
from kotonebot.backend.loop import Loop
from kotonebot.kaa.tasks import R
from kotonebot.kaa.common import conf, DailyMoneyShopItems
from kotonebot.util import cropped
from kotonebot.primitives.geometry import Point
from kotonebot.util import Countdown, cropped
from kotonebot import task, device, image, action, sleep
from kotonebot.backend.dispatch import SimpleDispatcher
from ..actions.scenes import goto_home, goto_shop, at_daily_shop
@ -37,10 +39,10 @@ def money_items2(items: Optional[list[DailyMoneyShopItems]] = None):
scroll = 0
while items:
for item in items:
if image.find(item.to_resource(), colored=True):
if ret := image.find(item.to_resource(), colored=True):
logger.info(f'Purchasing {item.to_ui_text(item)}...')
device.click()
handle_purchase_dialog()
confirm_purchase(ret.position)
finished.append(item)
items = [item for item in items if item not in finished]
# 全都买完了
@ -71,16 +73,17 @@ def dispatch_recommended_items():
while True:
device.screenshot()
if image.find(R.Daily.TextShopRecommended):
if rec := image.find(R.Daily.TextShopRecommended):
logger.info(f'Clicking on recommended item.') # TODO: 计数
device.click()
handle_purchase_dialog()
confirm_purchase(rec.position)
sleep(2.5) #
elif image.find(R.Daily.IconTitleDailyShop) and not image.find(R.Daily.TextShopRecommended):
logger.info(f'No recommended item found. Finished.')
break
@action('确认购买', screenshot_mode='manual-inherit')
def handle_purchase_dialog():
def confirm_purchase(target_item_pos: Point | None = None):
"""
确认购买
@ -89,11 +92,27 @@ def handle_purchase_dialog():
"""
# 前置条件:[screenshots\shop\dialog.png]
# TODO: 需要有个更好的方式检测是否已购买
purchased = (SimpleDispatcher('dispatch_purchase_dialog')
.until(R.Common.ButtonConfirm, result=False)
.until(R.Daily.TextShopPurchased, result=True)
.timeout(timeout=3, result=True)
).run()
purchased = False
cd = Countdown(sec=3)
for _ in Loop():
if cd.expired():
purchased = True
break
if image.find(R.Daily.TextShopItemSoldOut):
logger.info('Item sold out.')
purchased = True
break
elif image.find(R.Daily.TextShopItemPurchased):
logger.info('Item already purchased.')
purchased = True
break
elif image.find(R.Common.ButtonConfirm):
logger.info('Confirming purchase...')
device.click()
sleep(0.5)
else:
if target_item_pos:
device.click(target_item_pos)
if purchased:
logger.info('Item sold out.')
@ -133,7 +152,7 @@ def ap_items():
device.click(results[index])
sleep(0.5)
with cropped(device, y1=0.3):
purchased = image.wait_for(R.Daily.TextShopPurchased, timeout=1)
purchased = image.wait_for(R.Daily.TextShopItemSoldOut, timeout=1)
if purchased is not None:
logger.info(f'AP item #{index} already purchased.')
continue
@ -158,7 +177,7 @@ def purchase():
if not conf().purchase.enabled:
logger.info('Purchase is disabled.')
return
if not at_daily_shop():
goto_shop()
# 进入每日商店 [screenshots\shop\shop.png]
device.click(image.expect(R.Daily.ButtonDailyShop)) # TODO: memoable

@ -1 +1 @@
Subproject commit bca78ea35b0883c24814c523cec1856615205199
Subproject commit 4328e2b53dc6e3efa4fef691064f40ceb998c97a