Skip to content

Commit

Permalink
Add queue exit on timeout subroutine to the queue loop. (#90)
Browse files Browse the repository at this point in the history
* Add queue exit on timeout subroutine to the queue loop. Timeout is configurable through the YAML. 150 secs seems a good base value.

* Implement code review changes

* Refactor queue timeout documentation and update function argument description

* fix wording from game to queue

* fix whitespace

---------

Co-authored-by: Deko <[email protected]>
  • Loading branch information
StanleyAlbayeros and akshualy authored Nov 29, 2024
1 parent 4270756 commit c50ad65
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
9 changes: 9 additions & 0 deletions alune/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,12 @@ def get_chance_to_buy_xp(self) -> int:
The chance in percent from 0 to 100.
"""
return self._config["chances"]["buy_xp"]

def get_queue_timeout(self) -> int:
"""
Get the queue timeout in seconds.
Returns:
The queue timeout in seconds.
"""
return self._config["queue_timeout"]
1 change: 1 addition & 0 deletions alune/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ class Button:
# Buttons with an image, the variable name must be the same as the image name.
play = ImageButton(BoundingBox(950, 600, 1200, 650))
accept = ImageButton(BoundingBox(525, 520, 755, 545))
exit_lobby = ImageButton(BoundingBox(50, 600, 130, 680))
exit_now = ImageButton(
click_box=BoundingBox(550, 425, 740, 440),
capture_area=BoundingBox(520, 400, 775, 425),
Expand Down
Binary file added alune/images/exit_lobby.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions alune/resources/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ log_level: "INFO"
# Please note that changing this may require changing traits as well.
game_mode: "normal"

# Timeout for the bot to stay in the queue.
# This is the maximum time the bot will wait in the queue before it will exit the queue, in seconds.
queue_timeout: 150

# The traits you want the bot to roll for.
# The trait names are almost as written in-game, with spaces replaced by _ and . and : being ignored.
# For example: admin, star_guardian, mecha_prime, lasercorps
Expand Down
12 changes: 7 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,17 +77,19 @@ async def wait_for_accept_button(adb_instance: ADB):
search_result = screen.get_button_on_screen(screenshot, Button.accept)


async def queue(adb_instance: ADB):
async def queue(adb_instance: ADB, config: AluneConfig):
"""
Utility method to queue a match.
Args:
adb_instance: An instance of the ADB connection to click in.
config: An instance of the alune config to use.
"""
try:
await asyncio.wait_for(wait_for_accept_button(adb_instance), timeout=120)
await asyncio.wait_for(wait_for_accept_button(adb_instance), timeout=config.get_queue_timeout())
except asyncio.TimeoutError:
logger.warning("Waiting for accept button timed out, re-checking app state")
await adb_instance.click_button(Button.exit_lobby)
logger.info("Queue exited due to timeout.")
return
await adb_instance.click_button(Button.accept)
await asyncio.sleep(2)
Expand All @@ -103,7 +105,7 @@ async def queue(adb_instance: ADB):
screenshot = await adb_instance.get_screen()
if screen.get_button_on_screen(screenshot, Button.accept) or screen.get_button_on_screen(screenshot, Button.play):
logger.debug("Queue was declined by someone else, staying in queue lock state")
await queue(adb_instance)
await queue(adb_instance, config)


_random = Random()
Expand Down Expand Up @@ -325,7 +327,7 @@ async def take_app_decision(game_state_image_result: GameStateImageResult, adb_i
case GameState.LOBBY:
logger.info("App state is in lobby, locking bot into queue logic.")
await adb_instance.click_button(Button.play)
await queue(adb_instance)
await queue(adb_instance, config)
logger.info("Queue lock released, likely loading into game now.")
case GameState.IN_GAME:
logger.info("App state is in game, looping decision making and waiting for the exit button.")
Expand Down

0 comments on commit c50ad65

Please sign in to comment.