Skip to content

Commit

Permalink
Properly use dynamic y offset if softbar enabled to click returning p…
Browse files Browse the repository at this point in the history
…layer
  • Loading branch information
Grennith committed Dec 24, 2023
1 parent 4076128 commit ee7ccdc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 50 deletions.
91 changes: 43 additions & 48 deletions mapadroid/ocr/screenPath.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,10 @@ def __init__(self, communicator: AbstractCommunicator, worker_state: WorkerState
self._worker_state: WorkerState = worker_state
self._mapping_manager: MappingManager = mapping_mananger
self._account_handler: AbstractAccountHandler = account_handler
self._ratio: float = 0.0

self._screenshot_y_offset: int = 0
self._nextscreen: ScreenType = ScreenType.UNDEFINED

self._communicator: AbstractCommunicator = communicator
logger.info("Starting Screendetector")
self._width: int = 0
self._height: int = 0

@classmethod
async def create(cls, communicator: AbstractCommunicator, worker_state: WorkerState,
Expand All @@ -53,8 +48,6 @@ async def create(cls, communicator: AbstractCommunicator, worker_state: WorkerSt
mapping_mananger=mapping_mananger,
account_handler=account_handler)
self._accountindex = await self.get_devicesettings_value(MappingManagerDevicemappingKey.ACCOUNT_INDEX, 0)
self._screenshot_y_offset = await self.get_devicesettings_value(
MappingManagerDevicemappingKey.SCREENSHOT_Y_OFFSET, 0)
return self

async def __evaluate_topmost_app(self, topmost_app: str) -> Tuple[ScreenType, dict, int]:
Expand Down Expand Up @@ -95,9 +88,7 @@ async def __evaluate_topmost_app(self, topmost_app: str) -> Tuple[ScreenType, di
logger.warning('Could not understand any text on screen - starting next round...')
return ScreenType.ERROR, global_dict, diff

self._ratio = self._height / self._width

logger.debug("Screenratio: {}", self._ratio)
logger.debug("Screenratio: {}", self._worker_state.resolution_calculator.x_y_ratio)

if 'text' not in global_dict:
logger.error('Error while text detection')
Expand Down Expand Up @@ -171,17 +162,17 @@ async def __handle_login_screen(self, global_dict: dict, diff: int) -> None:

# alternative select - calculate down from Facebook button
elif 'Facebook' in temp_dict:
click_x = self._width / 2
click_y = (temp_dict['Facebook'] + 2 * self._height / 10.11)
click_x = self._worker_state.resolution_calculator.screen_size_x / 2
click_y = (temp_dict['Facebook'] + 2 * self._worker_state.resolution_calculator.screen_size_y / 10.11)
logger.info("ScreenType.LOGINSELECT (f) using PTC (logintype in Device Settings)")
await self._communicator.click(int(click_x), int(click_y))
await asyncio.sleep(5)
return

# alternative select - calculate down from Google button
elif 'Google' in temp_dict:
click_x = self._width / 2
click_y = (temp_dict['Google'] + self._height / 10.11)
click_x = self._worker_state.resolution_calculator.screen_size_x / 2
click_y = (temp_dict['Google'] + self._worker_state.resolution_calculator.screen_size_y / 10.11)
logger.info("ScreenType.LOGINSELECT (g) using PTC (logintype in Device Settings)")
await self._communicator.click(int(click_x), int(click_y))
await asyncio.sleep(5)
Expand All @@ -197,7 +188,7 @@ async def __handle_login_screen(self, global_dict: dict, diff: int) -> None:

# alternative select
elif 'Facebook' in temp_dict and 'CLUB' in temp_dict:
click_x = self._width / 2
click_x = self._worker_state.resolution_calculator.screen_size_x / 2
click_y = (temp_dict['Facebook'] + ((temp_dict['CLUB'] - temp_dict['Facebook']) / 2))
logger.info("ScreenType.LOGINSELECT (fc) using Google Account (logintype in Device Settings)")
await self._communicator.click(int(click_x), int(click_y))
Expand All @@ -206,17 +197,17 @@ async def __handle_login_screen(self, global_dict: dict, diff: int) -> None:

# alternative select
elif 'Facebook' in temp_dict:
click_x = self._width / 2
click_y = (temp_dict['Facebook'] + self._height / 10.11)
click_x = self._worker_state.resolution_calculator.screen_size_x / 2
click_y = (temp_dict['Facebook'] + self._worker_state.resolution_calculator.screen_size_y / 10.11)
logger.info("ScreenType.LOGINSELECT (f) using Google Account (logintype in Device Settings)")
await self._communicator.click(int(click_x), int(click_y))
await asyncio.sleep(5)
return

# alternative select
elif 'CLUB' in temp_dict:
click_x = self._width / 2
click_y = (temp_dict['CLUB'] - self._height / 10.11)
click_x = self._worker_state.resolution_calculator.screen_size_x / 2
click_y = (temp_dict['CLUB'] - self._worker_state.resolution_calculator.screen_size_y / 10.11)
logger.info("ScreenType.LOGINSELECT (c) using Google Account (logintype in Device Settings)")
await self._communicator.click(int(click_x), int(click_y))
await asyncio.sleep(5)
Expand Down Expand Up @@ -470,8 +461,8 @@ async def __handle_ptc_login(self) -> ScreenType:
bounds = item.attrib['bounds']
match = re.search(r'^\[(\d+),(\d+)\]\[(\d+),(\d+)\]$', bounds)
logger.debug("Logo image Bounds {}", item.attrib['bounds'])
exit_keyboard_x = int(match.group(1)) + ((int(match.group(3)) - int(match.group(1))) / 2)
exit_keyboard_y = int(match.group(2)) + ((int(match.group(4)) - int(match.group(2))) / 2)
exit_keyboard_x = int(int(match.group(1)) + ((int(match.group(3)) - int(match.group(1))) / 2))
exit_keyboard_y = int(int(match.group(2)) + ((int(match.group(4)) - int(match.group(2))) / 2))
if item.attrib["resource-id"] == "email":
bounds = item.attrib['bounds']
logger.info("Found email/login field, clicking, filling, clicking")
Expand Down Expand Up @@ -540,26 +531,27 @@ async def __handle_failure_screen(self) -> None:
await self.__handle_returning_player_or_wrong_credentials()

async def __handle_ggl_consent_screen(self) -> ScreenType:
if self._width == 0 and self._height == 0:
if (self._worker_state.resolution_calculator.screen_size_x == 0
and self._worker_state.resolution_calculator.screen_size_y == 0):
logger.warning("Screen width and height are zero - try to get real values from new screenshot ...")
# this updates self._width, self._height
# this updates screen sizes etc
result = await self._take_and_analyze_screenshot()
if not result:
logger.error("Failed getting/analyzing screenshot")
return ScreenType.ERROR
if (self._width != 720 and self._height != 1280) and (self._width != 1080 and self._height != 1920) and (
self._width != 1440 and self._height != 2560):
if (self._worker_state.resolution_calculator.screen_size_x != 720 and self._worker_state.resolution_calculator.screen_size_y != 1280) and (self._worker_state.resolution_calculator.screen_size_x != 1080 and self._worker_state.resolution_calculator.screen_size_y != 1920) and (
self._worker_state.resolution_calculator.screen_size_x != 1440 and self._worker_state.resolution_calculator.screen_size_y != 2560):
logger.warning("The google consent screen can only be handled on 720x1280, 1080x1920 and 1440x2560 screens "
f"(width is {self._width}, height is {self._height})")
f"(width is {self._worker_state.resolution_calculator.screen_size_x}, height is {self._worker_state.resolution_calculator.screen_size_y})")
return ScreenType.ERROR
logger.info("Click accept button")
if self._width == 720 and self._height == 1280:
if self._worker_state.resolution_calculator.screen_size_x == 720 and self._worker_state.resolution_calculator.screen_size_y == 1280:
await self._communicator.touch_and_hold(int(360), int(1080), int(360), int(500))
await self._communicator.click(480, 1080)
if self._width == 1080 and self._height == 1920:
if self._worker_state.resolution_calculator.screen_size_x == 1080 and self._worker_state.resolution_calculator.screen_size_y == 1920:
await self._communicator.touch_and_hold(int(360), int(1800), int(360), int(400))
await self._communicator.click(830, 1638)
if self._width == 1440 and self._height == 2560:
if self._worker_state.resolution_calculator.screen_size_x == 1440 and self._worker_state.resolution_calculator.screen_size_y == 2560:
await self._communicator.touch_and_hold(int(360), int(2100), int(360), int(400))
await self._communicator.click(976, 2180)
await asyncio.sleep(10)
Expand All @@ -573,7 +565,8 @@ async def __handle_returning_player_or_wrong_credentials(self) -> None:
2.20, 3.01,
upper=True)
if coordinates:
coordinates = ScreenCoordinates(int(self._width / 2), int(self._height * 0.7))
coordinates = ScreenCoordinates(int(self._worker_state.resolution_calculator.screen_size_x / 2),
int(self._worker_state.resolution_calculator.screen_size_y * 0.7 - self._worker_state.resolution_calculator.y_offset))
await self._communicator.click(coordinates.x, coordinates.y)
await asyncio.sleep(2)

Expand All @@ -591,17 +584,17 @@ async def __handle_birthday_screen(self) -> None:

# After having restarted pogo, we should again be on the birthday screen now and PD is turned off
self._nextscreen = ScreenType.RETURNING
click_x = int((self._width / 2) + (self._width / 4))
click_y = int((self._height / 1.69) + self._screenshot_y_offset)
click_x = int((self._worker_state.resolution_calculator.screen_size_x / 2) + (self._worker_state.resolution_calculator.screen_size_x / 4))
click_y = int(self._worker_state.resolution_calculator.screen_size_y / 1.69)
await self._communicator.click(click_x, click_y)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._height / 2)), 200)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._worker_state.resolution_calculator.screen_size_y / 2)), 200)
await asyncio.sleep(1)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._height / 2)), 200)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._worker_state.resolution_calculator.screen_size_y / 2)), 200)
await asyncio.sleep(1)
await self._communicator.click(click_x, click_y)
await asyncio.sleep(1)
click_x = int(self._width / 2)
click_y = int(click_y + (self._height / 8.53))
click_x = int(self._worker_state.resolution_calculator.screen_size_x / 2)
click_y = int(click_y + (self._worker_state.resolution_calculator.screen_size_y / 8.53))
await self._communicator.click(click_x, click_y)
await asyncio.sleep(1)

Expand All @@ -621,7 +614,7 @@ async def __handle_welcome_screen(self) -> ScreenType:
async def __handle_tos_screen(self) -> ScreenType:
#self._nextscreen = ScreenType.PRIVACY
screenshot_path = await self.get_screenshot_path()
await self._communicator.click(int(self._width / 2), int(self._height * 0.47))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x / 2), int(self._worker_state.resolution_calculator.screen_size_y * 0.47))
coordinates: Optional[ScreenCoordinates] = await self._worker_state.pogo_windows.look_for_button(
screenshot_path,
2.20, 3.01,
Expand Down Expand Up @@ -650,10 +643,10 @@ async def __handle_character_selection_screen(self) -> ScreenType:
await self._communicator.click(100, 100)
await asyncio.sleep(1)
await asyncio.sleep(1)
await self._communicator.click(int(self._width / 4), int(self._height / 2))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x / 4), int(self._worker_state.resolution_calculator.screen_size_y / 2))
await asyncio.sleep(2)
for _ in range(3):
await self._communicator.click(int(self._width * 0.91), int(self._height * 0.94))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x * 0.91), int(self._worker_state.resolution_calculator.screen_size_y * 0.94))
await asyncio.sleep(2)

if not await self._take_screenshot(delay_before=await self.get_devicesettings_value(
Expand All @@ -678,8 +671,8 @@ async def __handle_catch_tutorial(self) -> ScreenType:
await self._communicator.click(100, 100)
for x in range(1,10):
for y in range(1,10):
click_x = int(self._width * x/10)
click_y = int(self._height * y/20 + self._height / 2)
click_x = int(self._worker_state.resolution_calculator.screen_size_x * x/10)
click_y = int(self._worker_state.resolution_calculator.screen_size_y * y/20 + self._worker_state.resolution_calculator.screen_size_y / 2)
await self._communicator.click(click_x, click_y)
await asyncio.sleep(5)
if not await self._take_screenshot(delay_before=await self.get_devicesettings_value(
Expand All @@ -695,9 +688,9 @@ async def __handle_catch_tutorial(self) -> ScreenType:
break

for _ in range(3):
click_x = int(self._width / 2)
click_y = int(self._height * 0.93)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._height / 2)), 200)
click_x = int(self._worker_state.resolution_calculator.screen_size_x / 2)
click_y = int(self._worker_state.resolution_calculator.screen_size_y * 0.93)
await self._communicator.touch_and_hold(click_x, click_y, click_x, int(click_y - (self._worker_state.resolution_calculator.screen_size_y / 2)), 200)
await asyncio.sleep(15)

if not await self._take_screenshot(delay_before=await self.get_devicesettings_value(
Expand All @@ -715,7 +708,7 @@ async def __handle_catch_tutorial(self) -> ScreenType:
await self._communicator.click(coordinates.x, coordinates.y)
logger.info("Catched Pokémon.")
await asyncio.sleep(12)
await self._communicator.click(int(self._width / 2), int(self._height * 0.93))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x / 2), int(self._worker_state.resolution_calculator.screen_size_y * 0.93))
await asyncio.sleep(2)
return ScreenType.UNDEFINED

Expand All @@ -736,8 +729,8 @@ async def __handle_name_screen(self) -> ScreenType:
await self._communicator.enter_text(username)
await self._communicator.click(100, 100)
await asyncio.sleep(2)
await self._communicator.click(int(self._width / 2), int(self._height * 0.66))
await self._communicator.click(int(self._width / 2), int(self._height * 0.51))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x / 2), int(self._worker_state.resolution_calculator.screen_size_y * 0.66))
await self._communicator.click(int(self._worker_state.resolution_calculator.screen_size_x / 2), int(self._worker_state.resolution_calculator.screen_size_y * 0.51))
await asyncio.sleep(2)

if not await self._take_screenshot(delay_before=await self.get_devicesettings_value(
Expand Down Expand Up @@ -1001,7 +994,9 @@ async def _take_and_analyze_screenshot(self, delay_after=0.0, delay_before=0.0,
logger.error("Failed analyzing screen")
return None
else:
returntype, global_dict, self._width, self._height, diff = result
returntype, global_dict, width, height, diff = result
self._worker_state.resolution_calculator.screen_size_x = width
self._worker_state.resolution_calculator.screen_size_y = height
return returntype, global_dict, diff

async def clear_game_data(self):
Expand Down
4 changes: 4 additions & 0 deletions mapadroid/utils/resolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ def __init__(self):
self._y_offset: int = 0
self._x_offset: int = 0

@property
def x_y_ratio(self):
return self._x_y_ratio

@property
def y_offset(self) -> int:
return self._y_offset
Expand Down
3 changes: 1 addition & 2 deletions mapadroid/worker/strategy/AbstractMitmBaseStrategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ async def pre_work_loop(self) -> None:
await self.worker_stats()
logger.info("Worker starting actual work")
try:
await self.turn_screen_on_and_start_pogo()

await self._update_screen_size()
await self.turn_screen_on_and_start_pogo()
except WebsocketWorkerRemovedException:
raise InternalStopWorkerException("Timeout during init of worker")

Expand Down

0 comments on commit ee7ccdc

Please sign in to comment.