def get_messages_by_player(player, include_all_while_online=False): """Returns all messages by player. Args: player - name of the player include_all_while_online - Include all messages while a player is online. """ all_lines = [line for line in get_all_log_lines()] all_lines.sort(key=lambda line: line.date) if include_all_while_online: player_durations = [d for d in yield_play_durations(all_lines)] player_durations = [d for d in player_durations if d.user == player] lines = [ line for line in all_lines if bool( [True for duration in player_durations if line.date >= duration.start and line.date <= duration.end] ) ] return lines else: lines = [line for line in all_lines if line.user == player] return lines
def yield_play_durations(lines=[]): '''Yields tuples of play durations (user, start_time, end_time) Args: lines: optional list of supplied lines (could be filtered) ''' # Get all the login and logouts and sort them by user and then date if not lines: lines = [l for l in get_all_log_lines() if l.is_login or l.is_logout] else: lines = [l for l in lines if l.is_login or l.is_logout] # We want to sort by user and then by date to allow us to process correctly lines.sort(key=lambda line: (line.user, line.date)) login_date = None login_user = None for line in lines: # If user is None then we are not "logged in" if line.is_login and login_user is None: login_date = line.date login_user = line.user continue elif line.is_logout and login_user == line.user: yield PlayDuration(login_user, login_date, line.date) # Either we got bad data or we parsed a logout # Either case we clear what we had login_date = None login_user = None