def get_schedule_duration(self, schedule: str) -> Tuple[int, int]: """ Given a schedule type, returns the timestamp for the start and end of the current schedule of this type. """ if schedule not in ['daily', 'weekly']: raise Exception( 'Logic error, specify either \'daily\' or \'weekly\' for schedule type!' ) if schedule == 'daily': return (Time.beginning_of_today(), Time.end_of_today()) if schedule == 'weekly': return (Time.beginning_of_this_week(), Time.end_of_this_week()) # Should never happen return (0, 0)
def _add_shop_score(self, root: Node) -> None: shop_score = Node.void('shop_score') root.add_child(shop_score) today = Node.void('today') shop_score.add_child(today) yesterday = Node.void('yesterday') shop_score.add_child(yesterday) all_profiles = self.data.local.user.get_all_profiles(self.game, self.version) all_attempts = self.data.local.music.get_all_attempts(self.game, self.version, timelimit=(Time.beginning_of_today() - Time.SECONDS_IN_DAY)) machine = self.data.local.machine.get_machine(self.config['machine']['pcbid']) if machine.arcade is not None: lids = [ machine.id for machine in self.data.local.machine.get_all_machines(machine.arcade) ] else: lids = [machine.id] relevant_profiles = [ profile for profile in all_profiles if profile[1].get_int('lid', -1) in lids ] for (rootnode, timeoffset) in [ (today, 0), (yesterday, Time.SECONDS_IN_DAY), ]: # Grab all attempts made in the relevant day relevant_attempts = [ attempt for attempt in all_attempts if ( attempt[1].timestamp >= (Time.beginning_of_today() - timeoffset) and attempt[1].timestamp <= (Time.end_of_today() - timeoffset) ) ] # Calculate scores based on attempt scores_by_user: Dict[UserID, Dict[int, Dict[int, Attempt]]] = {} for (userid, attempt) in relevant_attempts: if userid not in scores_by_user: scores_by_user[userid] = {} if attempt.id not in scores_by_user[userid]: scores_by_user[userid][attempt.id] = {} if attempt.chart not in scores_by_user[userid][attempt.id]: # No high score for this yet, just use this attempt scores_by_user[userid][attempt.id][attempt.chart] = attempt else: # If this attempt is better than the stored one, replace it if scores_by_user[userid][attempt.id][attempt.chart].points < attempt.points: scores_by_user[userid][attempt.id][attempt.chart] = attempt # Calculate points earned by user in the day points_by_user: Dict[UserID, int] = {} for userid in scores_by_user: points_by_user[userid] = 0 for mid in scores_by_user[userid]: for chart in scores_by_user[userid][mid]: points_by_user[userid] = points_by_user[userid] + scores_by_user[userid][mid][chart].points # Output that day's earned points for (userid, profile) in relevant_profiles: data = Node.void('data') rootnode.add_child(data) data.add_child(Node.s16('day_id', int((Time.now() - timeoffset) / Time.SECONDS_IN_DAY))) data.add_child(Node.s32('user_id', profile.get_int('extid'))) data.add_child(Node.s16('icon_id', profile.get_dict('config').get_int('icon_id'))) data.add_child(Node.s16('point', min(points_by_user.get(userid, 0), 32767))) data.add_child(Node.s32('update_time', Time.now())) data.add_child(Node.string('name', profile.get_str('name'))) rootnode.add_child(Node.s32('time', Time.beginning_of_today() - timeoffset))