Skip to content

Commit 683f475

Browse files
committed
More refactorings and add a priority_list - WIP images for tests need to be added.
1 parent 72fd178 commit 683f475

File tree

8 files changed

+79
-49
lines changed

8 files changed

+79
-49
lines changed

images/reward_1_2_icon.png

5.01 KB
Loading

images/start_button_text5.en.png

4.26 KB
Loading

main.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
def set_up_logging_configuration():
77
logging.basicConfig(
8-
level=logging.DEBUG,
8+
level=logging.INFO,
99
format="%(asctime)s %(message)s",
1010
datefmt="%Y-%m-%d %H:%M:%S",
1111
)

src/find_image_result.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from attrs import define
2+
3+
4+
@define(frozen=True)
5+
class FindImageResult:
6+
val: float
7+
coords: tuple[int, int]

src/game_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ class GameActions(Enum):
1111
@define(frozen=True)
1212
class GameAction:
1313
action: GameActions = GameActions.no_action
14-
position: tuple[int,int] = (0, 0)
14+
position: tuple[int, int] = (0, 0)
1515
is_ingame: bool = False

src/image_decision_maker.py

Lines changed: 63 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from src import constants
66
from src import image_service
7+
from src.find_image_result import FindImageResult
78
from 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+
)

src/image_service.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import cv2
22

3+
from src.find_image_result import FindImageResult
4+
35

46
def show_image(img, top_left, bottom_right):
57
# Draw rectangle and Show the result
@@ -12,7 +14,7 @@ def convert_to_greyscale(img_to_convert):
1214
return cv2.cvtColor(img_to_convert, cv2.COLOR_BGR2GRAY)
1315

1416

15-
def find_image(img_large, img_small):
17+
def find_image(img_large, img_small) -> FindImageResult | None:
1618
if img_large is None:
1719
print("Image large is none")
1820
return
@@ -44,4 +46,4 @@ def find_image(img_large, img_small):
4446
# show_image(img_large, top_left, bottom_right)
4547

4648
# Return the maximum value and the center of the matching area
47-
return (max_val, (x, y))
49+
return FindImageResult(max_val, (x, y))

tests/test_image_decision_maker.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,7 @@ def test_make_decision_collects_rewards1(template_images):
7878

7979
assert result.action is not None
8080
assert result.action == GameActions.tap_position
81-
# TODO correct position
82-
assert result.position == (542, 2006)
83-
assert not result.position == (542, 2006)
81+
assert result.position == (285, 1780)
8482
assert not result.is_ingame
8583

8684

@@ -89,7 +87,7 @@ def test_make_decision_collects_rewards2(template_images):
8987

9088
assert result.action is not None
9189
assert result.action == GameActions.tap_position
92-
assert not result.position == (542, 2006)
90+
assert result.position == (127, 1805)
9391
assert not result.is_ingame
9492

9593

@@ -98,7 +96,7 @@ def test_make_decision_collects_rewards3(template_images):
9896

9997
assert result.action is not None
10098
assert result.action == GameActions.tap_position
101-
assert not result.position == (542, 2006)
99+
assert result.position == (590, 1758)
102100
assert not result.is_ingame
103101

104102

0 commit comments

Comments
 (0)