From 50e6610d9cb92fcde04c1de595c797477ecbddb4 Mon Sep 17 00:00:00 2001 From: Dominik Zeiger Date: Thu, 9 Mar 2023 21:26:24 +0100 Subject: [PATCH] FEATURE: use AccountHandler for interaction with account data, reason for switch_account/switch_user --- mapadroid/account_handler/AbstractAccountHandler.py | 4 ++++ mapadroid/account_handler/AccountHandler.py | 7 +++++++ mapadroid/websocket/WebsocketServer.py | 7 ++----- mapadroid/worker/strategy/AbstractWorkerStrategy.py | 5 +++-- mapadroid/worker/strategy/quest/QuestStrategy.py | 13 ++++++------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/mapadroid/account_handler/AbstractAccountHandler.py b/mapadroid/account_handler/AbstractAccountHandler.py index cb1125fa2..21a844a71 100644 --- a/mapadroid/account_handler/AbstractAccountHandler.py +++ b/mapadroid/account_handler/AbstractAccountHandler.py @@ -105,3 +105,7 @@ async def is_burnt(self, device_id: int) -> bool: """ pass + + @abstractmethod + async def get_assignment(self, device_id: int) -> Optional[SettingsPogoauth]: + pass \ No newline at end of file diff --git a/mapadroid/account_handler/AccountHandler.py b/mapadroid/account_handler/AccountHandler.py index 3e3553842..6f21eeb25 100644 --- a/mapadroid/account_handler/AccountHandler.py +++ b/mapadroid/account_handler/AccountHandler.py @@ -167,6 +167,13 @@ async def get_assigned_username(self, device_id: int) -> Optional[str]: session, device_entry.device_id) return None if not currently_assigned else currently_assigned.username + async def get_assignment(self, device_id: int) -> Optional[SettingsPogoauth]: + async with self._db_wrapper as session, session: + current_auth = await SettingsPogoauthHelper.get_assigned_to_device(session, device_id) + if current_auth: + session.expunge(current_auth) + return current_auth + def _is_burnt(self, auth: SettingsPogoauth) -> bool: """ Evaluates whether a login is considered not usable based on the last known burning diff --git a/mapadroid/websocket/WebsocketServer.py b/mapadroid/websocket/WebsocketServer.py index 89fad36c3..aa7b35cdb 100644 --- a/mapadroid/websocket/WebsocketServer.py +++ b/mapadroid/websocket/WebsocketServer.py @@ -14,7 +14,6 @@ AbstractStatsHandler from mapadroid.db.DbWrapper import DbWrapper from mapadroid.db.helper.SettingsDeviceHelper import SettingsDeviceHelper -from mapadroid.db.helper.SettingsPogoauthHelper import SettingsPogoauthHelper from mapadroid.db.model import (AuthLevel, SettingsAuth, SettingsDevice, SettingsPogoauth) from mapadroid.mapping_manager.MappingManager import MappingManager @@ -56,6 +55,7 @@ def __init__(self, args, mitm_mapper: AbstractMitmMapper, stats_handler: Abstrac self.__pogo_window_manager: PogoWindows = pogo_window_manager self.__mitm_mapper: AbstractMitmMapper = mitm_mapper self.__stats_handler: AbstractStatsHandler = stats_handler + self.__account_handler: AbstractAccountHandler = account_handler self.__enable_configmode: bool = enable_configmode # Event to signal that the server is to be stopped. Used to not accept new connections for example @@ -177,10 +177,7 @@ async def __connection_handler(self, websocket_client_connection: websockets.Web entry.websocket_client_connection = websocket_client_connection elif not entry: async with self.__db_wrapper as session, session: - current_auth: Optional[SettingsPogoauth] = await SettingsPogoauthHelper\ - .get_assigned_to_device(session, device_id) - if current_auth: - session.expunge(current_auth) + current_auth: Optional[SettingsPogoauth] = await self.__account_handler.get_assignment(device_id) # Just create a new entry... worker_state: WorkerState = WorkerState(origin=origin, device_id=device_id, diff --git a/mapadroid/worker/strategy/AbstractWorkerStrategy.py b/mapadroid/worker/strategy/AbstractWorkerStrategy.py index 85e59a25b..9ebdca5b1 100644 --- a/mapadroid/worker/strategy/AbstractWorkerStrategy.py +++ b/mapadroid/worker/strategy/AbstractWorkerStrategy.py @@ -6,6 +6,7 @@ from loguru import logger +from mapadroid.account_handler.AbstractAccountHandler import BurnType from mapadroid.data_handler.stats.AbstractStatsHandler import \ AbstractStatsHandler from mapadroid.db.DbWrapper import DbWrapper @@ -365,7 +366,7 @@ async def _handle_screen(self): await asyncio.sleep(300) elif screen_type == ScreenType.MAINTENANCE: logger.warning("Maintenance screen - switch account ...") - await self._switch_user() + await self._switch_user(BurnType.MAINTENANCE.value) elif screen_type in [ScreenType.ERROR, ScreenType.FAILURE]: logger.warning('Something wrong with screendetection or pogo failure screen') self._worker_state.login_error_count += 1 @@ -410,7 +411,7 @@ async def _restart_pogo_safe(self): await asyncio.sleep(1) return await self.start_pogo() - async def _switch_user(self): + async def _switch_user(self, reason=None): logger.info('Switching User - please wait ...') await self.stop_pogo() await asyncio.sleep(5) diff --git a/mapadroid/worker/strategy/quest/QuestStrategy.py b/mapadroid/worker/strategy/quest/QuestStrategy.py index 484375cc1..37f92a11b 100644 --- a/mapadroid/worker/strategy/quest/QuestStrategy.py +++ b/mapadroid/worker/strategy/quest/QuestStrategy.py @@ -300,8 +300,7 @@ async def move_to_location(self): async def _calculate_remaining_softban_avoidance_duration(self, cur_time, delay_to_avoid_softban, distance, speed): async with self._db_wrapper as session, session: - active_account: Optional[SettingsPogoauth] = await SettingsPogoauthHelper.get_assigned_to_device( - session, self._worker_state.device_id) + active_account: Optional[SettingsPogoauth] = await self._word_to_screen_matching._account_handler.get_assignment(self._worker_state.device_id) if active_account: logger.debug("Checking DB for last softban action") if active_account.last_softban_action \ @@ -340,13 +339,13 @@ async def _rotate_account_after_moving_locations_if_applicable(self, delay_used: False) and not await self._is_levelmode(): # Waiting time too long and more than one account - switch! (not level mode!!) logger.info('Can use more than 1 account - switch & no cooldown') - await self.switch_account() + await self.switch_account(reason="teleport") delay_used = -1 elif await self._is_levelmode() and await self._mitm_mapper.get_level(self._worker_state.origin) >= MIN_LEVEL_IV: logger.info('Levelmode: Account of {} is level {}, i.e., >= {}, switching to next to level', self._worker_state.origin, await self._mitm_mapper.get_level(self._worker_state.origin) >= MIN_LEVEL_IV, MIN_LEVEL_IV) - await self.switch_account() + await self.switch_account(reason="level") delay_used = -1 return delay_used @@ -404,7 +403,7 @@ async def _switch_account_if_needed(self): and await self._mitm_mapper.get_level(self._worker_state.origin) >= 30 \ and await self._is_levelmode(): # switch if player lvl >= 30 - await self.switch_account() + await self.switch_account(reason="level") async def worker_specific_setup_start(self): area_settings: Optional[SettingsAreaPokestop] = await self._mapping_manager.routemanager_get_settings( @@ -482,8 +481,8 @@ async def _check_pogo_close(self, takescreen=True): logger.debug("checkPogoClose: done") return False - async def switch_account(self): - if not await self._switch_user(): + async def switch_account(self, reason=None): + if not await self._switch_user(reason): logger.error('Something happened while account switching :(') raise InternalStopWorkerException("Failed switching accounts") else: