2023-06-19 00:39:41 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from module.logger import logger
|
|
|
|
from tasks.assignment.claim import AssignmentClaim
|
2023-09-26 07:06:43 +00:00
|
|
|
from tasks.assignment.keywords import (KEYWORDS_ASSIGNMENT_GROUP,
|
|
|
|
AssignmentEntry, AssignmentEventGroup)
|
2023-09-26 13:27:38 +00:00
|
|
|
from tasks.assignment.ui import AssignmentStatus
|
2023-06-19 17:00:34 +00:00
|
|
|
from tasks.base.page import page_assignment, page_menu
|
2023-08-30 17:06:24 +00:00
|
|
|
from tasks.battle_pass.keywords import KEYWORD_BATTLE_PASS_QUEST
|
2023-08-27 09:27:47 +00:00
|
|
|
from tasks.daily.keywords import KEYWORDS_DAILY_QUEST
|
2023-06-19 17:00:34 +00:00
|
|
|
from tasks.daily.synthesize import SynthesizeUI
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
|
2023-06-19 17:00:34 +00:00
|
|
|
class Assignment(AssignmentClaim, SynthesizeUI):
|
2023-09-26 07:06:43 +00:00
|
|
|
def run(self, assignments: list[AssignmentEntry] = None, duration: int = None, event_first: bool = None):
|
2023-08-30 17:06:24 +00:00
|
|
|
self.config.update_battle_pass_quests()
|
2023-08-27 09:27:47 +00:00
|
|
|
self.config.update_daily_quests()
|
|
|
|
|
2023-06-19 00:39:41 +00:00
|
|
|
if assignments is None:
|
2023-06-25 16:07:52 +00:00
|
|
|
assignments = (
|
2023-08-27 09:27:47 +00:00
|
|
|
getattr(self.config, f'Assignment_Name_{i + 1}', None) for i in range(4))
|
2023-06-25 16:07:52 +00:00
|
|
|
# remove duplicate while keeping order
|
|
|
|
assignments = list(dict.fromkeys(
|
|
|
|
x for x in assignments if x is not None))
|
|
|
|
assignments = [AssignmentEntry.find(x) for x in assignments]
|
|
|
|
if len(assignments) < 4:
|
|
|
|
logger.warning(
|
|
|
|
'There are duplicate assignments in config, check it out')
|
2023-06-19 00:39:41 +00:00
|
|
|
if duration is None:
|
|
|
|
duration = self.config.Assignment_Duration
|
2023-09-26 07:06:43 +00:00
|
|
|
if event_first is None:
|
2023-09-26 13:07:51 +00:00
|
|
|
event_first = self.config.Assignment_Event
|
2023-06-19 00:39:41 +00:00
|
|
|
|
2023-07-10 16:07:55 +00:00
|
|
|
self.dispatched = dict()
|
2023-08-30 17:06:24 +00:00
|
|
|
self.has_new_dispatch = False
|
2023-06-19 17:00:34 +00:00
|
|
|
self.ensure_scroll_top(page_menu)
|
2023-06-19 00:39:41 +00:00
|
|
|
self.ui_ensure(page_assignment)
|
2023-09-26 07:06:43 +00:00
|
|
|
event_ongoing = next((
|
|
|
|
g for g in self._iter_groups()
|
|
|
|
if isinstance(g, AssignmentEventGroup)
|
|
|
|
), None)
|
|
|
|
if event_first and event_ongoing is not None:
|
2023-09-27 02:31:10 +00:00
|
|
|
undispatched = assignments
|
2023-09-26 07:06:43 +00:00
|
|
|
remain = self._check_all()
|
|
|
|
remain = self._dispatch_event(remain)
|
|
|
|
else:
|
|
|
|
# Iterate in user-specified order, return undispatched ones
|
|
|
|
undispatched = list(self._check_inlist(assignments, duration))
|
|
|
|
remain = self._check_all()
|
2023-09-27 02:31:10 +00:00
|
|
|
undispatched = [x for x in undispatched if x not in self.dispatched]
|
2023-06-19 00:39:41 +00:00
|
|
|
# There are unchecked assignments
|
2023-06-19 17:00:34 +00:00
|
|
|
if remain > 0:
|
2023-06-19 00:39:41 +00:00
|
|
|
for assignment in undispatched[:remain]:
|
|
|
|
self.goto_entry(assignment)
|
2023-06-19 17:00:34 +00:00
|
|
|
self.dispatch(assignment, duration)
|
2023-06-19 00:39:41 +00:00
|
|
|
if remain < len(undispatched):
|
2023-06-19 17:00:34 +00:00
|
|
|
logger.warning('The following assignments can not be dispatched due to limit: '
|
|
|
|
f'{", ".join([x.name for x in undispatched[remain:]])}')
|
|
|
|
elif remain > len(undispatched):
|
|
|
|
self._dispatch_remain(duration, remain - len(undispatched))
|
2023-09-27 05:53:15 +00:00
|
|
|
# Refresh dashboard before return
|
|
|
|
_ = self._limit_status
|
2023-06-19 00:39:41 +00:00
|
|
|
# Scheduler
|
2023-08-30 17:06:24 +00:00
|
|
|
logger.attr('has_new_dispatch', self.has_new_dispatch)
|
2023-08-27 09:27:47 +00:00
|
|
|
with self.config.multi_set():
|
2023-08-30 17:06:24 +00:00
|
|
|
# Check battle pass
|
|
|
|
quests = self.config.stored.BattlePassTodayQuest.load_quests()
|
|
|
|
if self.has_new_dispatch:
|
|
|
|
if KEYWORD_BATTLE_PASS_QUEST.Dispatch_1_assignments in quests:
|
2023-09-26 07:06:43 +00:00
|
|
|
logger.info(
|
|
|
|
'Achieved battle pass quest Dispatch_1_assignments')
|
2023-09-27 15:28:05 +00:00
|
|
|
if self.config.stored.BattlePassLevel.is_full():
|
|
|
|
logger.info('BattlePassLevel full, no task call')
|
|
|
|
else:
|
|
|
|
self.config.task_call('BattlePass')
|
2023-08-30 17:06:24 +00:00
|
|
|
# Check daily
|
2023-08-27 09:27:47 +00:00
|
|
|
quests = self.config.stored.DailyQuest.load_quests()
|
|
|
|
if KEYWORDS_DAILY_QUEST.Go_on_assignment_1_time in quests:
|
|
|
|
logger.info('Achieved daily quest Go_on_assignment_1_time')
|
|
|
|
self.config.task_call('DailyQuest')
|
2023-08-30 17:06:24 +00:00
|
|
|
# Delay self
|
2023-09-15 17:41:42 +00:00
|
|
|
if len(self.dispatched):
|
|
|
|
delay = min(self.dispatched.values())
|
|
|
|
logger.info(f'Delay assignment check to {str(delay)}')
|
|
|
|
self.config.task_delay(target=delay)
|
|
|
|
else:
|
|
|
|
# ValueError: min() arg is an empty sequence
|
|
|
|
logger.error('Empty dispatched list, delay 2 hours instead')
|
|
|
|
self.config.task_delay(minute=120)
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
def _check_inlist(self, assignments: list[AssignmentEntry], duration: int):
|
|
|
|
"""
|
|
|
|
Dispatch assignments according to user config
|
|
|
|
|
|
|
|
Args:
|
|
|
|
assignments (list[AssignmentEntry]): user specified assignments
|
|
|
|
duration (int): user specified duration
|
|
|
|
"""
|
|
|
|
if not assignments:
|
|
|
|
return
|
2023-09-15 17:52:33 +00:00
|
|
|
logger.hr('Assignment check inlist', level=1)
|
2023-06-19 00:39:41 +00:00
|
|
|
logger.info(
|
|
|
|
f'User specified assignments: {", ".join([x.name for x in assignments])}')
|
2023-06-19 17:00:34 +00:00
|
|
|
_, remain, _ = self._limit_status
|
2023-06-19 00:39:41 +00:00
|
|
|
for assignment in assignments:
|
2023-09-26 07:06:43 +00:00
|
|
|
if assignment in self.dispatched:
|
|
|
|
continue
|
2023-09-15 17:52:33 +00:00
|
|
|
logger.hr('Assignment inlist', level=2)
|
|
|
|
logger.info(f'Check assignment inlist: {assignment}')
|
2023-06-19 00:39:41 +00:00
|
|
|
self.goto_entry(assignment)
|
2023-09-26 13:27:38 +00:00
|
|
|
status = self._check_assignment_status()
|
|
|
|
if status == AssignmentStatus.CLAIMABLE:
|
2023-06-19 00:39:41 +00:00
|
|
|
self.claim(assignment, duration, should_redispatch=True)
|
|
|
|
continue
|
2023-09-26 13:27:38 +00:00
|
|
|
if status == AssignmentStatus.DISPATCHED:
|
2023-09-26 07:06:43 +00:00
|
|
|
self.dispatched[assignment] = datetime.now() + \
|
|
|
|
self._get_assignment_time()
|
2023-06-19 00:39:41 +00:00
|
|
|
continue
|
2023-09-26 13:27:38 +00:00
|
|
|
# General assignments must be dispatchable here
|
2023-10-22 14:15:09 +00:00
|
|
|
if remain <= 0:
|
|
|
|
yield assignment
|
|
|
|
continue
|
2023-09-27 02:31:10 +00:00
|
|
|
self.dispatch(assignment, duration)
|
|
|
|
remain -= 1
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
def _check_all(self):
|
|
|
|
"""
|
|
|
|
States of assignments from top to bottom are in following order:
|
|
|
|
1. Claimable
|
|
|
|
2. Dispatched
|
|
|
|
3. Dispatchable
|
|
|
|
Break when a dispatchable assignment is encountered
|
|
|
|
"""
|
2023-09-15 17:52:33 +00:00
|
|
|
logger.hr('Assignment check all', level=1)
|
2023-09-27 02:31:10 +00:00
|
|
|
current, remain, _ = self._limit_status
|
2023-10-02 04:28:31 +00:00
|
|
|
# current = #Claimable + #Dispatched
|
2023-09-27 02:31:10 +00:00
|
|
|
if current == len(self.dispatched):
|
2023-06-19 17:00:34 +00:00
|
|
|
return remain
|
2023-06-19 00:39:41 +00:00
|
|
|
for group in self._iter_groups():
|
|
|
|
self.goto_group(group)
|
2023-09-26 15:11:45 +00:00
|
|
|
insight = False
|
2023-09-26 07:06:43 +00:00
|
|
|
for assignment in self._iter_entries():
|
2023-06-19 00:39:41 +00:00
|
|
|
if assignment in self.dispatched:
|
|
|
|
continue
|
2023-09-15 17:52:33 +00:00
|
|
|
logger.hr('Assignment all', level=2)
|
|
|
|
logger.info(f'Check assignment all: {assignment}')
|
2023-09-26 15:11:45 +00:00
|
|
|
self.goto_entry(assignment, insight)
|
2023-09-26 13:27:38 +00:00
|
|
|
status = self._check_assignment_status()
|
|
|
|
if status == AssignmentStatus.CLAIMABLE:
|
2023-06-19 00:39:41 +00:00
|
|
|
self.claim(assignment, None, should_redispatch=False)
|
2023-09-27 02:31:10 +00:00
|
|
|
current -= 1
|
2023-06-19 17:00:34 +00:00
|
|
|
remain += 1
|
2023-09-26 15:11:45 +00:00
|
|
|
insight = True # Order of entries change after claiming
|
2023-10-02 04:28:31 +00:00
|
|
|
if current == len(self.dispatched):
|
|
|
|
return remain
|
2023-06-19 00:39:41 +00:00
|
|
|
continue
|
2023-09-26 13:27:38 +00:00
|
|
|
if status == AssignmentStatus.DISPATCHED:
|
2023-09-26 07:06:43 +00:00
|
|
|
self.dispatched[assignment] = datetime.now() + \
|
|
|
|
self._get_assignment_time()
|
2023-10-02 04:28:31 +00:00
|
|
|
insight = False # Order of entries does not change here
|
2023-09-27 02:31:10 +00:00
|
|
|
if current == len(self.dispatched):
|
2023-07-10 16:07:55 +00:00
|
|
|
return remain
|
2023-06-19 00:39:41 +00:00
|
|
|
continue
|
2023-06-22 05:51:50 +00:00
|
|
|
break
|
2023-06-19 17:00:34 +00:00
|
|
|
return remain
|
2023-06-19 00:39:41 +00:00
|
|
|
|
|
|
|
def _dispatch_remain(self, duration: int, remain: int):
|
|
|
|
"""
|
|
|
|
Dispatch assignments according to preset priority
|
|
|
|
|
|
|
|
Args:
|
|
|
|
duration (int): user specified duration
|
|
|
|
remain (int):
|
|
|
|
The number of remaining assignments after
|
|
|
|
processing the ones specified by user
|
|
|
|
"""
|
|
|
|
if remain <= 0:
|
|
|
|
return
|
|
|
|
logger.hr('Assignment dispatch remain', level=2)
|
|
|
|
logger.warning(f'{remain} remain')
|
|
|
|
logger.info(
|
|
|
|
'Dispatch remaining assignments according to preset priority')
|
|
|
|
group_priority = (
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.EXP_Materials_Credits,
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.Character_Materials,
|
|
|
|
KEYWORDS_ASSIGNMENT_GROUP.Synthesis_Materials
|
|
|
|
)
|
|
|
|
for group in group_priority:
|
|
|
|
for assignment in group.entries:
|
|
|
|
if assignment in self.dispatched:
|
|
|
|
continue
|
|
|
|
self.goto_entry(assignment)
|
2023-06-19 17:00:34 +00:00
|
|
|
self.dispatch(assignment, duration)
|
2023-06-19 00:39:41 +00:00
|
|
|
remain -= 1
|
|
|
|
if remain <= 0:
|
|
|
|
return
|
2023-09-26 07:06:43 +00:00
|
|
|
|
|
|
|
def _dispatch_event(self, remain: int):
|
|
|
|
if remain <= 0:
|
|
|
|
return remain
|
|
|
|
logger.hr('Assignment dispatch event', level=1)
|
|
|
|
for group in self._iter_groups():
|
|
|
|
if not isinstance(group, AssignmentEventGroup):
|
|
|
|
continue
|
|
|
|
self.goto_group(group)
|
|
|
|
for assignment in self._iter_entries():
|
|
|
|
if assignment in self.dispatched:
|
|
|
|
continue
|
|
|
|
logger.hr('Assignment event', level=2)
|
|
|
|
logger.info(f'Check assignment event: {assignment}')
|
2023-09-27 03:05:55 +00:00
|
|
|
# Order of entries does not change during iteration
|
|
|
|
self.goto_entry(assignment, insight=False)
|
2023-09-26 13:27:38 +00:00
|
|
|
status = self._check_assignment_status()
|
|
|
|
# Should only be dispatchable or locked after _check_all
|
|
|
|
if status == AssignmentStatus.DISPATCHABLE:
|
2023-09-26 07:06:43 +00:00
|
|
|
self.dispatch(assignment, None)
|
|
|
|
remain -= 1
|
|
|
|
if remain <= 0:
|
|
|
|
return remain
|
2023-09-26 11:44:06 +00:00
|
|
|
continue
|
|
|
|
break
|
2023-09-26 07:06:43 +00:00
|
|
|
return remain
|