StarRailCopilot/tasks/assignment/claim.py

95 lines
3.1 KiB
Python
Raw Normal View History

2023-06-19 00:39:41 +00:00
from datetime import datetime, timedelta
from module.logger import logger
2023-06-19 00:39:41 +00:00
from module.ocr.ocr import Duration
from tasks.assignment.assets.assets_assignment_claim import *
from tasks.assignment.dispatch import AssignmentDispatch
from tasks.assignment.keywords import AssignmentEntry
from tasks.base.page import page_assignment
2023-06-19 00:39:41 +00:00
class AssignmentClaim(AssignmentDispatch):
def claim(self, assignment: AssignmentEntry, duration_expected: int, should_redispatch: bool):
"""
Args:
assignment (AssignmentEntry):
duration_expected (int): user specified duration
should_redispatch (bool):
Pages:
in: CLAIM
2023-06-19 17:00:34 +00:00
out: DISPATCHED or EMPTY_SLOT
2023-06-19 00:39:41 +00:00
"""
redispatched = False
self._wait_for_report()
if should_redispatch:
redispatched = self._is_duration_expected(duration_expected)
self._exit_report(redispatched)
if redispatched:
self._wait_until_assignment_started()
self.dispatched[assignment] = datetime.now(
) + timedelta(hours=duration_expected)
elif should_redispatch:
# Re-select duration and dispatch
self.goto_entry(assignment)
self.dispatch(assignment, duration_expected)
def _wait_for_report(self):
"""
Pages:
in: CLAIM
out: REDISPATCH
"""
2023-06-19 00:39:41 +00:00
skip_first_screenshot = True
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# End
2023-06-20 00:26:04 +00:00
if self.appear(REDISPATCH):
logger.info('Assignment report appears')
break
# Claim rewards
if self.appear_then_click(CLAIM, interval=2):
continue
def _exit_report(self, should_redispatch: bool):
"""
Args:
should_redispatch (bool): determined by user config and duration in report
Pages:
in: CLOSE_REPORT and REDISPATCH
out: page_assignment
"""
click_button = REDISPATCH if should_redispatch else CLOSE_REPORT
skip_first_screenshot = True
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# End
if self.appear(page_assignment.check_button):
logger.info('Assignment report is closed')
break
# Close report
if self.appear_then_click(click_button, interval=2):
2023-06-19 00:39:41 +00:00
continue
def _is_duration_expected(self, duration: int) -> bool:
"""
Check whether duration in assignment report page
is the same as user specified
Args:
duration (int): user specified duration
Returns:
bool: If same.
"""
duration_reported: timedelta = Duration(
OCR_ASSIGNMENT_REPORT_TIME).ocr_single_line(self.device.image)
return duration_reported.total_seconds() == duration*3600