Beispiel #1
0
 def test_positions(self):
     """ Test each position. """
     line_up = palpiteiro.LineUp(self.line_up_list)
     assert line_up.goalkeepers == self.goalkeepers[:1]
     assert line_up.fullbacks == self.fullbacks[:2]
     assert line_up.defenders == self.defenders[:2]
     assert line_up.midfielders == self.midfielders[:4]
     assert line_up.forwards == self.forwards[:2]
     assert line_up.coaches == self.coaches[:1]
Beispiel #2
0
 def test_get_scheme(self):
     """ Test line_up scheme. """
     my_scheme = palpiteiro.LineUp(self.line_up_list).scheme
     ref_scheme = palpiteiro.Scheme(
         goalkeepers=1,
         fullbacks=2,
         defenders=2,
         midfielders=4,
         forwards=2,
         coaches=1,
     )
     assert my_scheme == ref_scheme
Beispiel #3
0
def random_line_up(
    players: Sequence[palpiteiro.Player],
    schemes: Sequence[palpiteiro.Scheme],
    max_price: float,
) -> palpiteiro.LineUp:
    """ Create a random valid line-up. """
    # Select a random scheme.
    scheme = random.choice(schemes)

    # Separates players by position.
    players_dict = {
        pos: [player for player in players if player.position == pos]
        for pos in scheme.dict.keys()
    }

    # Create an empty line_up
    line_up = palpiteiro.LineUp([])

    # Iterate on positions to be draft on a random order.
    positions = [
        pos for pos, amount in scheme.dict.items() for _ in range(amount)
    ]
    random.shuffle(positions)

    remaining_money = max_price
    for pos in positions:

        # Filter affordable players.
        affordable_players = [
            player for player in players_dict[pos]
            if player.price <= remaining_money and player not in line_up
        ]

        # If no affordable players, restart function.
        if len(affordable_players) == 0:
            return random_line_up(players=players,
                                  schemes=schemes,
                                  max_price=max_price)

        # Randomly choose a player and add to the line up.
        random_choice = random.choice(affordable_players)
        line_up.add(random_choice)

        # Remove price from money.
        remaining_money -= random_choice.price

    return assign_captain(line_up)
Beispiel #4
0
 def test_predicted_points(self):
     """ Test get current points. """
     line_up = palpiteiro.LineUp(self.line_up_list)
     line_up.captain = line_up.players[0]
     assert line_up.predicted_points > 0
Beispiel #5
0
 def test_captain(self):
     """ Test set captain. """
     line_up = palpiteiro.LineUp(self.line_up_list)
     line_up.captain = line_up.players[0]
     assert line_up.captain is self.line_up_list[0]
Beispiel #6
0
 def test_valid_line_up(self):
     """ Test setting a valid line-up. """
     assert palpiteiro.LineUp(self.line_up_list).is_valid(self.schemes)
Beispiel #7
0
 def test_invalid_line_up3(self):
     """ Test setting an invalid line-up. """
     assert not palpiteiro.LineUp(self.line_up_list + self.coaches[-1:]).is_valid(
         self.schemes
     )
Beispiel #8
0
 def test_invalid_line_up2(self):
     """ Test setting an invalid line-up. """
     invalid_line_up = self.line_up_list.copy()
     invalid_line_up[0] = self.fullbacks[-1]
     assert not palpiteiro.LineUp(invalid_line_up).is_valid(self.schemes)
Beispiel #9
0
 def test_invalid_line_up1(self):
     """ Test setting an invalid line-up. """
     assert not palpiteiro.LineUp([self.goalkeepers[0]]).is_valid(self.schemes)