StarRailCopilot/tasks/assignment/claim.py

69 lines
2.5 KiB
Python
Raw Normal View History

2023-06-19 00:39:41 +00:00
from datetime import datetime, timedelta
from module.base.timer import Timer
from module.ocr.ocr import Duration
from tasks.assignment.assets.assets_assignment_claim import *
from tasks.assignment.assets.assets_assignment_dispatch import EMPTY_SLOT
from tasks.assignment.assets.assets_assignment_ui import DISPATCHED
from tasks.assignment.dispatch import AssignmentDispatch
from tasks.assignment.keywords import AssignmentEntry
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
skip_first_screenshot = True
counter = Timer(1, count=4).start()
while 1:
if skip_first_screenshot:
skip_first_screenshot = False
else:
self.device.screenshot()
# End
if self.appear(EMPTY_SLOT) or self.appear(DISPATCHED):
if counter.reached():
break
continue
# Claim reward
if self.appear(CLAIM, interval=2):
self.device.click(CLAIM)
continue
if self.appear(REDISPATCH, interval=2):
redispatched = should_redispatch and self._is_duration_expected(
duration_expected)
if redispatched:
self._confirm_assignment(REDISPATCH)
self.dispatched[assignment] = datetime.now(
) + timedelta(hours=duration_expected)
else:
self.device.click(CLOSE_REPORT)
continue
# Re-select duration and dispatch
if should_redispatch and not redispatched:
2023-06-19 17:00:34 +00:00
self.dispatch(assignment, duration_expected)
2023-06-19 00:39:41 +00:00
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