Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix mode selection #20

Merged
merged 3 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions alune/screen.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ def get_on_screen(
return None

return ImageSearchResult(
x=max_location[0] + bounding_box.min_x if bounding_box else 0,
y=max_location[1] + bounding_box.min_y if bounding_box else 0,
x=max_location[0] + (bounding_box.min_x if bounding_box else 0),
y=max_location[1] + (bounding_box.min_y if bounding_box else 0),
height=image_to_find.shape[0],
width=image_to_find.shape[1],
)
46 changes: 32 additions & 14 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import asyncio
from dataclasses import dataclass
from enum import auto
from enum import StrEnum
import importlib.metadata
Expand All @@ -27,6 +28,7 @@
from alune.images import Button
from alune.images import Image
from alune.screen import BoundingBox
from alune.screen import ImageSearchResult


class GameState(StrEnum):
Expand All @@ -44,6 +46,16 @@ class GameState(StrEnum):
CHOICE_CONFIRM = auto()


@dataclass
class GameStateImageResult:
"""
Combines a game state with an image search result (both optional)
"""

game_state: GameState
image_result: ImageSearchResult | None = None


async def wait_for_accept_button(adb_instance: ADB):
"""
Utility method to wait for the queue accept button.
Expand Down Expand Up @@ -238,9 +250,13 @@ async def loop(adb_instance: ADB, config: AluneConfig):
await asyncio.sleep(5)

screenshot = await adb_instance.get_screen()
game_state = await get_game_state(screenshot)
game_state_image_result = await get_game_state(screenshot)

if not game_state_image_result:
await asyncio.sleep(2)
continue

match game_state:
match game_state_image_result.game_state:
case GameState.LOADING:
logger.info("App state is loading...")
# TODO Check if the log-in prompt is on screen
Expand All @@ -253,7 +269,7 @@ async def loop(adb_instance: ADB, config: AluneConfig):
await adb_instance.click_button(Button.check_choice)
case GameState.CHOOSE_MODE:
logger.info("App state is choose mode, selecting normal game.")
await adb_instance.click_button(Button.normal_game)
await adb_instance.click_image(game_state_image_result.image_result)
case GameState.QUEUE_MISSED:
logger.info("App state is queue missed, clicking it.")
await adb_instance.click_button(Button.check)
Expand All @@ -272,7 +288,7 @@ async def loop(adb_instance: ADB, config: AluneConfig):
screenshot = await adb_instance.get_screen()
search_result = screen.get_button_on_screen(screenshot, Button.exit_now)
game_state = await get_game_state(screenshot)
if game_state == GameState.POST_GAME:
if game_state and game_state.game_state == GameState.POST_GAME:
break
await adb_instance.click_button(Button.exit_now)
await asyncio.sleep(10)
Expand All @@ -284,36 +300,38 @@ async def loop(adb_instance: ADB, config: AluneConfig):


# pylint: disable-next=too-many-return-statements
async def get_game_state(screenshot: ndarray) -> GameState | None:
async def get_game_state(screenshot: ndarray) -> GameStateImageResult | None:
"""
Get the current app/game state based off a screenshot.

Args:
screenshot: A screenshot that was taken by :class:`alune.adb.ADB`
"""
if screen.get_button_on_screen(screenshot, Button.check_choice):
return GameState.CHOICE_CONFIRM
return GameStateImageResult(GameState.CHOICE_CONFIRM)

if screen.get_on_screen(screenshot, Image.RITO_LOGO):
return GameState.LOADING
return GameStateImageResult(GameState.LOADING)

if screen.get_on_screen(screenshot, Button.play.image_path) and not screen.get_on_screen(screenshot, Image.BACK):
return GameState.MAIN_MENU
return GameStateImageResult(GameState.MAIN_MENU)

if screen.get_button_on_screen(screenshot, Button.normal_game):
return GameState.CHOOSE_MODE
if image_result := screen.get_button_on_screen(screenshot, Button.normal_game):
return GameStateImageResult(game_state=GameState.CHOOSE_MODE, image_result=image_result)

if screen.get_button_on_screen(screenshot, Button.check):
return GameState.QUEUE_MISSED
return GameStateImageResult(GameState.QUEUE_MISSED)

if screen.get_on_screen(screenshot, Image.CLOSE_LOBBY) and screen.get_button_on_screen(screenshot, Button.play):
return GameState.LOBBY
return GameStateImageResult(GameState.LOBBY)

if screen.get_on_screen(screenshot, Image.COMPOSITION) or screen.get_on_screen(screenshot, Image.ITEMS):
return GameState.IN_GAME
return GameStateImageResult(GameState.IN_GAME)

if screen.get_on_screen(screenshot, Image.FIRST_PLACE) and screen.get_on_screen(screenshot, Image.BACK):
return GameState.POST_GAME
return GameStateImageResult(GameState.POST_GAME)

return None


async def check_phone_preconditions(adb_instance: ADB):
Expand Down