def test_sort_roster(rosters: List[Roster], first): for roster in rosters: roster.wl_points = Roster.calculate_wl_points(roster) rosters = Roster.sort(rosters) assert rosters[0].name == first
def test_random_winner(): rosters = [Roster(name="one"), Roster(name="two")] winners = [] for x in range(1000): rosters = Roster.sort(rosters) sleep(0.001) winners.append(rosters[0].name) assert "one" in winners assert "two" in winners
def calculate(transaction): league = self.league_repo.get(command.league_id, transaction) rosters = self.league_roster_repo.get_all(command.league_id, transaction) rosters = {roster.id: roster for roster in rosters} matchups = self.matchup_repo.get_all(command.league_id, command.week_number, transaction) schedule = self.league_config_repo.get_schedule_config( command.league_id, transaction) week_index = command.week_number - 1 for matchup in matchups: schedule_matchup = next( m for m in schedule.weeks[week_index].matchups if m.id == matchup.id) if not matchup.home and not matchup.away: continue # not sure how this would happen, but there's nothing to calculate if matchup.home: if not command.past_week: # don't copy current roster if re-calculating a previous week matchup.home = self.archive_roster( rosters[matchup.home.id]) matchup.home.this_week_points_for = matchup.home.calculate_score( ) matchup.home.this_week_bench_points_for = matchup.home.calculate_bench_score( ) matchup.home_score = matchup.home.this_week_points_for if matchup.away: if not command.past_week: matchup.away = self.archive_roster( rosters[matchup.away.id]) matchup.away.this_week_points_for = matchup.away.calculate_score( ) matchup.away.this_week_bench_points_for = matchup.away.calculate_bench_score( ) matchup.away_score = matchup.away.this_week_points_for schedule_matchup.away_score = matchup.away_score schedule_matchup.home_score = matchup.home_score self.matchup_repo.set(command.league_id, command.week_number, matchup, transaction) next_week_matchups = schedule.weeks[ week_index + 1].matchups if len(schedule.weeks) > week_index + 1 else None next_week_playoffs = schedule.weeks[ week_index + 1].week_type.is_playoffs() if len( schedule.weeks) > week_index + 1 else False for roster in rosters.values(): update_record(roster, command.week_number, schedule, league.league_start_week) if next_week_matchups: roster_matchup = next( (m for m in next_week_matchups if (m.away and m.away.id == roster.id) or ( m.home and m.home.id == roster.id)), None) if roster_matchup: roster_matchup = MatchupPreview.from_matchup( roster_matchup) else: roster_matchup = None updates = { "matchup": roster_matchup.dict() if roster_matchup else None } self.user_league_repo.partial_update( roster.id, command.league_id, updates, transaction) for week in schedule.weeks: if week.week_number <= command.week_number: continue for matchup in week.matchups: if matchup.away: if not command.past_week: matchup.away = rosters[matchup.away.id].copy( deep=True) matchup.away.positions = [] matchup.away.waiver_bids = [] matchup.away.processed_waiver_bids = [] if matchup.home: if not command.past_week: matchup.home = rosters[matchup.home.id].copy( deep=True) matchup.home.positions = [] matchup.home.waiver_bids = [] matchup.home.processed_waiver_bids = [] rosters = list(rosters.values()) rosters = Roster.sort(rosters) rank = 1 for roster in rosters: roster.rank = rank rank += 1 roster.this_week_points_for = 0 roster.this_week_bench_points_for = 0 for position in roster.positions.values(): position.game_score = 0 self.league_roster_repo.set(command.league_id, roster, transaction) self.league_config_repo.set_schedule_config( command.league_id, schedule, transaction) return CalculateResultsResult( command=command, next_week_is_playoffs=next_week_playoffs)