original_docstring: Match pairs of players for the current round.
  
          1. Order players by victory points (desc)
          2. Make pairs of players
          3. Search free and less played table for 1st player from pair
          4. Create a game with players from pair
          
docstring: Match pairs of players for the current round.
  1. Order players by victory points (desc)
  2. Make pairs of players
  3. Search free and less played table for 1st player from pair
  4. Create a game with players from pair
docstring_tokens: Match pairs of players for the current round . 1 . Order players by victory points ( desc ) 2 . Make pairs of players 3 . Search free and less played table for 1st player from pair 4 . Create a game with players from pair
code: def create_games(self) -> List[Type[models.Model]]:
          """Match pairs of players for the current round.
  
          1. Order players by victory points (desc)
          2. Make pairs of players
          3. Search free and less played table for 1st player from pair
          4. Create a game with players from pair
          """
  
          assert not self.is_played, "Round should`t be played."
          # Order players by victory points (desc)
          player_qs = (
              Player.objects.exclude(assigned_games__round=self)
              .prefetch_related("assigned_games", "games")
              .annotate(victory_points_sum=Sum("games__victory_points"))
              .order_by("-victory_points_sum")
              .all()
          )
          assert player_qs, "Player pairs already set"
  
          # Make pairs of players
          all_pairs = list()
          game_pair = list()
          for player in player_qs:
              if len(game_pair) > 1:
                  all_pairs.append(game_pair)
                  game_pair = list()
              game_pair.append(player)
          else:
              if len(game_pair) <= 2:
                  all_pairs.append(game_pair)
  
          # Create a game for each game_pair
          created_games = list()
          for pair in all_pairs:
              player_1 = pair[0]
              # Search free and less played table for 1st player from pair
              qs = Table.objects.exclude(game__round=self)
              qs = qs.annotate(
                  cnt=Coalesce(
                      Count(
                          Case(
                              When(game__players__id=player_1.id, then=1),
                              output_field=IntegerField(),
                          )
                      ),
                      0,
                  )
              ).order_by("cnt")
              least_seen_table = qs.first()
  
              # Create a game
              game = Game.objects.create(table=least_seen_table, round=self)
              game.players.add(*pair)
              created_games.append(game)
  
          return created_games
code_tokens: def create_games ( self ) -> List [ Type [ models . Model ] ] : assert not self . is_played , "Round should`t be played." player_qs = ( Player . objects . exclude ( assigned_games__round = self ) . prefetch_related ( "assigned_games" , "games" ) . annotate ( victory_points_sum = Sum ( "games__victory_points" ) ) . order_by ( "-victory_points_sum" ) . all ( ) ) assert player_qs , "Player pairs already set" all_pairs = list ( ) game_pair = list ( ) for player in player_qs : if len ( game_pair ) > 1 : all_pairs . append ( game_pair ) game_pair = list ( ) game_pair . append ( player ) else : if len ( game_pair ) <= 2 : all_pairs . append ( game_pair ) created_games = list ( ) for pair in all_pairs : player_1 = pair [ 0 ] qs = Table . objects . exclude ( game__round = self ) qs = qs . annotate ( cnt = Coalesce ( Count ( Case ( When ( game__players__id = player_1 . id , then = 1 ) , output_field = IntegerField ( ) , ) ) , 0 , ) ) . order_by ( "cnt" ) least_seen_table = qs . first ( ) game = Game . objects . create ( table = least_seen_table , round = self ) game . players . add ( * pair ) created_games . append ( game ) return created_games
short_docstring: Match pairs of players for the current round.
short_docstring_tokens: Match pairs of players for the current round .
comment: """Match pairs of players for the current round.
  
          1. Order players by victory points (desc)
          2. Make pairs of players
          3. Search free and less played table for 1st player from pair
          4. Create a game with players from pair
          """$SEP$# Order players by victory points (desc)$SEP$# Make pairs of players$SEP$# Create a game for each game_pair$SEP$# Search free and less played table for 1st player from pair$SEP$# Create a game