42 lines
3.1 KiB
Python
42 lines
3.1 KiB
Python
"""狂鼠之家好友矿鼠一键喂养循环。"""
|
|
import time
|
|
from .engine import WorkflowError
|
|
|
|
|
|
class FeedMiceRunner:
|
|
def __init__(self, desktop, matcher, flow, region_provider, log=print, timeout=12):
|
|
self.desktop,self.matcher,self.flow=desktop,matcher,flow; self.region_provider,self.log,self.timeout=region_provider,log,timeout
|
|
def observe(self):
|
|
shot=self.desktop.screenshot(); region=self.region_provider(shot); return shot,region,self.matcher.frame(shot,region)
|
|
def wait(self,*names):
|
|
end=time.monotonic()+self.timeout; previous=None; stable=0
|
|
while time.monotonic()<end:
|
|
shot,region,frame=self.observe(); found=next((n for n in names if self.matcher.matches(frame,self.flow['states'][n])),None)
|
|
stable=stable+1 if found and found==previous else (1 if found else 0); previous=found
|
|
if stable>=2:return found,shot,region,frame
|
|
time.sleep(.25)
|
|
raise WorkflowError('未识别到预期页面 '+ '/'.join(names)+',已停止。')
|
|
def click(self,point,obs):
|
|
_,shot,region,_=obs
|
|
if self.region_provider(shot)!=region:raise WorkflowError('识别后游戏窗口移动,已停止,请重新运行。')
|
|
self.desktop.click(point,region,shot.size,hold=.8)
|
|
def action(self,name,obs):self.click(self.flow['clicks'][name],obs);self.log('已执行:'+name)
|
|
def location(self,frame,key):
|
|
spec=self.flow[key]; score,loc=self.matcher.locate(frame,spec)
|
|
if score<spec.get('threshold',.86):return None
|
|
x,y,w,h=loc;return ((x+w/2)/562,(y+h/2)/1002)
|
|
def return_home(self,rat):
|
|
self.action('rat_back',rat);self.action('territory_back',self.wait('territory'));self.wait('home');self.log('已确认返回首页。')
|
|
def run(self,dry_run=False):
|
|
home=self.wait('home')
|
|
if dry_run:self.log('首页识别通过。计划:领地 → 狂鼠之家 → 好友首位 → 一键喂养循环 → 返回首页。未点击。');return 'preview'
|
|
self.action('territory',home);self.action('rat_house',self.wait('territory'));rat=self.wait('rat')
|
|
count=0
|
|
while count<self.flow['max_feeds']:
|
|
self.action('friends',rat); friends=self.wait('friends'); target=self.location(friends[3],'friend_button')
|
|
if not target:self.log('好友列表没有可喂狂鼠,结束。');self.action('friends_back',friends);self.return_home(self.wait('rat'));return 'fed' if count else 'no_friend'
|
|
self.click(target,friends); mine=self.wait('friend_mine'); one=self.location(mine[3],'onekey_button')
|
|
if not one:self.log('首位好友没有一键喂养,结束。');self.action('friend_home',mine);self.return_home(self.wait('rat'));return 'fed' if count else 'no_feed'
|
|
self.click(one,mine);self.log('已执行:一键喂养。');reward=self.wait('reward');self.action('dismiss_reward',reward);self.action('friend_home',self.wait('friend_mine'));rat=self.wait('rat');count+=1;self.log('已完成第 %s 位好友喂养。'%count)
|
|
raise WorkflowError('喂养次数超过安全上限,已停止。')
|