1+ '''
2+ Author: tbjuechen
3+ Date: 2024-08-13
4+ Version: 1.0
5+ Description: performer class
6+ License: MIT
7+ '''
8+ import os
9+ import time
10+ from logging import Logger
11+ from abc import abstractmethod
12+ from typing import Type , List
13+
14+ from connector import ConnectorPool
15+ from connector .connector .base import BaseConnector
16+ from player .base import BasePlayer
17+ from player import CVPlayer
18+ from .utils import is_valid_ip
19+
20+ from InquirerPy import inquirer
21+
22+ class Performer :
23+ '''Performer class
24+
25+ Attributes
26+ ----------
27+ connect_pool : ConnectorPool
28+ The connector pool
29+
30+ player : BasePlayer
31+ The player
32+ '''
33+ def __init__ (self ,
34+ logger :Logger ,
35+ connCls :Type [ConnectorPool ]= ConnectorPool ,
36+ playerCls :Type [BasePlayer ]= CVPlayer ) -> None :
37+ self .logger = logger
38+ self .connect_pool = connCls ()
39+ self .player = playerCls ()
40+ self .init_env ()
41+ self .time_dleta = inquirer .text (
42+ message = "Enter the time delta (seconds):" ,
43+ default = '1' ,
44+ validate = lambda x : x .isdecimal () and float (x ) > 0 ,
45+ invalid_message = 'Invalid time delta!!!'
46+ ).execute ()
47+
48+ def init_env (self ):
49+ # set the path
50+ wanted_path = os .path .join (os .getcwd (), 'wanted' )
51+ os .environ ['PATH' ] += os .pathsep + wanted_path
52+ self .logger .debug (f'Add wanted path: { wanted_path } ' )
53+
54+ def add_connector (self ) -> None :
55+ '''interact add connector to the pool
56+ '''
57+ flag :bool = True # flag to control the loop
58+ while flag :
59+ ConnectorType :str = inquirer .select (
60+ message = "Select a connector type (mumu only for now)" ,
61+ choices = ['Mumu' , 'Nox' , 'BlueStacks' , 'Others' ],
62+ ).execute ()
63+ ConnectorHost :str = inquirer .text (
64+ message = "Enter the connector host:" ,
65+ default = '127.0.0.1' ,
66+ validate = is_valid_ip ,
67+ invalid_message = 'Invalid ip address!!!'
68+ ).execute ()
69+ ConnectorPort :int = inquirer .text (
70+ message = "Enter the connector port:" ,
71+ default = '16384' if ConnectorType == 'Mumu' else '7777' ,
72+ validate = lambda x : x .isdigit () and int (x ) in range (65536 ),
73+ invalid_message = 'Invalid port number!!!'
74+ ).execute ()
75+ self .connect_pool .add_connector (ConnectorType , ConnectorHost , ConnectorPort ).connect ()
76+ flag = inquirer .confirm (
77+ message = 'Add another connector?' ,
78+ default = False
79+ ).execute ()
80+
81+ def find_and_touch (self , target_list :List [str ], connector :BaseConnector = None ) -> None :
82+ '''Find and touch the target in the screenshot
83+
84+ Parameters
85+ ----------
86+ target_list : List[str]
87+ The list of target image files
88+
89+ connector : BaseConnector
90+ The connector to use, if None, use the current connector
91+ '''
92+ # use the current connector from pool
93+ connector = connector if connector else self .connect_pool .get_current_connector ()
94+ screenshot :str = connector .screen_shot ()
95+ for target in target_list :
96+ location = self .player .locate (target , screenshot )
97+ if location :
98+ self .logger .info (f'Found { target } at { location } ' )
99+ connector .touch (* location )
100+
101+ @abstractmethod
102+ def main_step (self ):
103+ '''Main steps for the performer
104+ '''
105+ pass
106+
107+ def run (self ) -> None :
108+ '''Run the script with preset steps
109+ '''
110+ self .add_connector ()
111+ while True :
112+ self .main_step ()
113+ self .logger .debug (f'Sleep for { self .time_dleta } seconds' )
114+ time .sleep (self .time_dleta )
0 commit comments