44
55from src import constants
66from src import image_service
7+ from src .find_image_result import FindImageResult
78from src .game_action import GameAction , GameActions
89
910
@@ -25,46 +26,68 @@ def make_decision(template_images: dict[str, cv2.Mat], image_name: str) -> GameA
2526 img_screenshot = cv2 .imread (image_name , cv2 .IMREAD_COLOR )
2627
2728 # Check if any of the image files match the screenshot
28- max_val = 0
29- max_image_file : str = ""
30- max_coords = None
29+ find_image_results : list [tuple [str , FindImageResult ]] = []
3130 for image_file , img_template in template_images .items ():
3231 result = image_service .find_image (img_screenshot , img_template )
3332 if result :
34- val , coords = result
35- else :
36- # handle case where find_image returns None
37- val , coords = 0 , None
38-
39- # Update the maximum value and corresponding image file and coordinates if necessary
40- if val > max_val :
41- max_val = val
42- max_image_file = image_file
43- max_coords = coords
44-
45- # Check if the maximum value is above a certain threshold
46- if max_val > 0.90 :
47- logging .info (f"Image { max_image_file } matches with { max_val * 100 } %" )
48-
49- if max_image_file .startswith ("max_number_of_games_played_text." ):
50- return GameAction (action = GameActions .exit_program )
51-
52- # If ingame return is_ingame with true
53- if is_ingame (max_image_file ):
54-
55- # Send tap to attack
56- position_to_tap = max_coords
57- if is_screen_to_attack (max_image_file ):
58- position_to_tap = constants .ATTACK_TAP_POSITION
59-
60- return GameAction (
61- action = GameActions .tap_position ,
62- position = position_to_tap ,
63- is_ingame = True ,
64- )
65- else :
66- # Send an ADB command to tap on the corresponding coordinates
67- return GameAction (action = GameActions .tap_position , position = max_coords )
68-
69- logging .info (f"No image matches." )
70- return GameAction ()
33+ if result .val > 0.90 :
34+ find_image_results .append ((image_file , result ))
35+
36+ logging .debug (find_image_results )
37+ return analyze_results_and_return_action_with_priority (find_image_results )
38+
39+
40+ def analyze_results_and_return_action_with_priority (
41+ find_image_results : list [tuple [str , FindImageResult ]]
42+ ) -> GameAction :
43+ if len (find_image_results ) == 0 :
44+ logging .debug ("No image matches." )
45+ return GameAction ()
46+
47+ priority_list = [
48+ "max_number_of_games_played_text." ,
49+ "reward_" ,
50+ "start_button_text" ,
51+ # TODO: Add other images here
52+ ]
53+
54+ for priority_file in priority_list :
55+ for result in find_image_results :
56+ image_file = result [0 ]
57+ find_image_result = result [1 ]
58+ if image_file .startswith (priority_file ):
59+ return analyze_results_and_return_action (image_file , find_image_result )
60+
61+ # Handle case where image is not in priority_list
62+ # Just use the best matching image
63+ max_image_file , max_result = max (find_image_results , key = lambda x : x [1 ].val )
64+ return analyze_results_and_return_action (max_image_file , max_result )
65+
66+
67+ def analyze_results_and_return_action (
68+ image_file : str , find_image_result : FindImageResult
69+ ) -> GameAction :
70+ logging .info (f"Image { image_file } matches with { find_image_result .val * 100 } %" )
71+
72+ if image_file .startswith ("max_number_of_games_played_text." ):
73+ return GameAction (action = GameActions .exit_program )
74+
75+ # If ingame return is_ingame with true
76+ if is_ingame (image_file ):
77+
78+ # Send tap to attack
79+ position_to_tap = find_image_result .coords
80+ if is_screen_to_attack (image_file ):
81+ position_to_tap = constants .ATTACK_TAP_POSITION
82+
83+ return GameAction (
84+ action = GameActions .tap_position ,
85+ position = position_to_tap ,
86+ is_ingame = True ,
87+ )
88+ else :
89+ # Send an ADB command to tap on the corresponding coordinates
90+ return GameAction (
91+ action = GameActions .tap_position ,
92+ position = find_image_result .coords ,
93+ )
0 commit comments