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

Use AccountHandler when getting account data, reason for switch_account/switch_user #1353

Open
wants to merge 1 commit into
base: async
Choose a base branch
from
Open
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: 4 additions & 0 deletions mapadroid/account_handler/AbstractAccountHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 7 additions & 0 deletions mapadroid/account_handler/AccountHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions mapadroid/websocket/WebsocketServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 3 additions & 2 deletions mapadroid/worker/strategy/AbstractWorkerStrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where is that reason used? What type should it have? Apparently string? Could use a type (imho enum) at this point

logger.info('Switching User - please wait ...')
await self.stop_pogo()
await asyncio.sleep(5)
Expand Down
13 changes: 6 additions & 7 deletions mapadroid/worker/strategy/quest/QuestStrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down