# Path: foosball/games/models.py
# class Game(TimeStampedModel):
#     created_by = models.ForeignKey(
#         User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
#     )
#     modified_by = models.ForeignKey(
#         User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
#     )
# 
#     played_at = models.DateTimeField(
#         default=timezone.now,
#         verbose_name=_("played at"),
#         help_text=_("Time when the game was played")
#     )
# 
#     table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))
# 
#     class Meta:
#         ordering = ['-played_at']
# 
#     def __str__(self):
#         return " - ".join(map(str, self.teams.all()))
# 
# class Team(models.Model):
#     UNKNOWN = 0
#     RED = 1
#     BLUE = 2
#     SIDE_CHOICES = (
#         (UNKNOWN, _("Unknown")),
#         (RED, _("Red")),
#         (BLUE, _("Blue")),
#     )
#     side = models.IntegerField(db_index=True, choices=SIDE_CHOICES, default=UNKNOWN, verbose_name=_("side"))
# 
#     game = models.ForeignKey(Game, related_name="teams", on_delete=models.CASCADE)
#     score = models.PositiveSmallIntegerField(
#         verbose_name=_("score"),
#         help_text=_("Team's score in the game"),
#         validators=[MaxValueValidator(limit_value=10)]
#     )
#     players = models.ManyToManyField(User)
# 
#     def __str__(self):
#         return "%s (%s)" % (
#             self.score,
#             ", ".join(map(str, self.players.all()))
#         )
# 
# class Player(models.Model):
#     user = models.OneToOneField(User)
#     singles_wins = models.PositiveIntegerField(
#         verbose_name=_("wins - singles"),
#         help_text=_("Wins in a single player team"),
#         default=0
#     )
#     singles_win_percentage = models.DecimalField(
#         verbose_name=_("win percentage - singles"),
#         help_text=_("Win percentage for single player team games"),
#         default=0,
#         max_digits=5, decimal_places=2,
#         validators=[MinValueValidator(limit_value=0), MaxValueValidator(limit_value=100)]
#     )
#     doubles_wins = models.PositiveIntegerField(
#         verbose_name=_("wins - doubles"),
#         help_text=_("Wins in a two player team"),
#         default=0
#     )
#     doubles_win_percentage = models.DecimalField(
#         verbose_name=_("win percentage - doubles"),
#         help_text=_("Win percentage in two player team games"),
#         default=0,
#         max_digits=5, decimal_places=2,
#         validators=[MinValueValidator(limit_value=0), MaxValueValidator(limit_value=100)]
#     )
#     total_played = models.PositiveIntegerField(
#         verbose_name=_("total played"),
#         help_text=_("Total games played in"),
#         default=0
#     )
#     singles_played = models.PositiveIntegerField(
#         verbose_name=_("singles played"),
#         help_text=_("Total games played as a single player team"),
#         default=0
#     )
#     doubles_played = models.PositiveIntegerField(
#         verbose_name=_("doubles played"),
#         help_text=_("Total games played in a two player team"),
#         default=0
#     )
#     colour = models.CharField(
#         verbose_name=_("colour code"),
#         help_text=_("Player colour as a hex colour code"),
#         max_length=7, null=True
#     )
# 
#     def __str__(self):
#         return "Player %s" % self.user.name if self.user.name else self.user.username
# 
#     def save(self, *args, **kwargs):
#         """
#         Init some data for the player.
#         """
#         if not self.pk or not self.colour:
#             self.colour = random_colour()
#         return super(Player, self).save(*args, **kwargs)


from foosball.games.models import Game, Team, Player
        totals = {1: 0, 2: 0}
        wins = {1: 0, 2: 0}
        teams = Team.objects.filter(players__id=user.id).prefetch_related("players")
        for team in teams:
            player_count = len(team.players.all())
            totals[player_count] += 1
            if team.score == 10:
                wins[player_count] += 1
        player, _created = Player.objects.get_or_create(user=user)
        player.singles_wins = wins[1]
        player.doubles_wins = wins[2]
        player.singles_played = totals[1]
        player.doubles_played = totals[2]
        if totals[1]:
            player.singles_win_percentage = round((wins[1]/totals[1])*100, 2)
        else:
            player.singles_win_percentage = 0
        if totals[2]:
            player.doubles_win_percentage = round((wins[2] / totals[2])*100, 2)
        else:
            player.doubles_win_percentage = 0
        player.total_played = totals[1] + totals[2]
        player.save()


def stats_red_or_blue(params):
    """
    Return statistics on the most important question.
    """
    red_wins = Game.objects.filter(teams__side=Team.RED, teams__score=10).count()

# Path: foosball/games/models.py
# class Game(TimeStampedModel):
#     created_by = models.ForeignKey(
#         User, related_name="games_created", editable=False, null=True, verbose_name=_("created by")
#     )
#     modified_by = models.ForeignKey(
#         User, related_name="games_modified", editable=False, null=True, verbose_name=_("modified by")
#     )
# 
#     played_at = models.DateTimeField(
#         default=timezone.now,
#         verbose_name=_("played at"),
#         help_text=_("Time when the game was played")
#     )
# 
#     table = models.ForeignKey(Table, blank=True, null=True, related_name="games", verbose_name=_("table"))
# 
#     class Meta:
#         ordering = ['-played_at']
# 
#     def __str__(self):
#         return " - ".join(map(str, self.teams.all()))
# 
# class Team(models.Model):
#     UNKNOWN = 0
#     RED = 1
#     BLUE = 2
#     SIDE_CHOICES = (
#         (UNKNOWN, _("Unknown")),
#         (RED, _("Red")),
#         (BLUE, _("Blue")),
#     )
#     side = models.IntegerField(db_index=True, choices=SIDE_CHOICES, default=UNKNOWN, verbose_name=_("side"))
# 
#     game = models.ForeignKey(Game, related_name="teams", on_delete=models.CASCADE)
#     score = models.PositiveSmallIntegerField(
#         verbose_name=_("score"),
#         help_text=_("Team's score in the game"),
#         validators=[MaxValueValidator(limit_value=10)]
#     )
#     players = models.ManyToManyField(User)
# 
#     def __str__(self):
#         return "%s (%s)" % (
#             self.score,
#             ", ".join(map(str, self.players.all()))
#         )
# 
# class Player(models.Model):
#     user = models.OneToOneField(User)
#     singles_wins = models.PositiveIntegerField(
#         verbose_name=_("wins - singles"),
#         help_text=_("Wins in a single player team"),
#         default=0
#     )
#     singles_win_percentage = models.DecimalField(
#         verbose_name=_("win percentage - singles"),
#         help_text=_("Win percentage for single player team games"),
#         default=0,
#         max_digits=5, decimal_places=2,
#         validators=[MinValueValidator(limit_value=0), MaxValueValidator(limit_value=100)]
#     )
#     doubles_wins = models.PositiveIntegerField(
#         verbose_name=_("wins - doubles"),
#         help_text=_("Wins in a two player team"),
#         default=0
#     )
#     doubles_win_percentage = models.DecimalField(
#         verbose_name=_("win percentage - doubles"),
#         help_text=_("Win percentage in two player team games"),
#         default=0,
#         max_digits=5, decimal_places=2,
#         validators=[MinValueValidator(limit_value=0), MaxValueValidator(limit_value=100)]
#     )
#     total_played = models.PositiveIntegerField(
#         verbose_name=_("total played"),
#         help_text=_("Total games played in"),
#         default=0
#     )
#     singles_played = models.PositiveIntegerField(
#         verbose_name=_("singles played"),
#         help_text=_("Total games played as a single player team"),
#         default=0
#     )
#     doubles_played = models.PositiveIntegerField(
#         verbose_name=_("doubles played"),
#         help_text=_("Total games played in a two player team"),
#         default=0
#     )
#     colour = models.CharField(
#         verbose_name=_("colour code"),
#         help_text=_("Player colour as a hex colour code"),
#         max_length=7, null=True
#     )
# 
#     def __str__(self):
#         return "Player %s" % self.user.name if self.user.name else self.user.username
# 
#     def save(self, *args, **kwargs):
#         """
#         Init some data for the player.
#         """
#         if not self.pk or not self.colour:
#             self.colour = random_colour()
#         return super(Player, self).save(*args, **kwargs)


# Path: foosball/games/stats.py
from foosball.games.models import Game, Team, Player

        totals = {1: 0, 2: 0}
        wins = {1: 0, 2: 0}
        teams = Team.objects.filter(players__id=user.id).prefetch_related("players")
        for team in teams:
            player_count = len(team.players.all())
            totals[player_count] += 1
            if team.score == 10:
                wins[player_count] += 1
        player, _created = Player.objects.get_or_create(user=user)
        player.singles_wins = wins[1]
        player.doubles_wins = wins[2]
        player.singles_played = totals[1]
        player.doubles_played = totals[2]
        if totals[1]:
            player.singles_win_percentage = round((wins[1]/totals[1])*100, 2)
        else:
            player.singles_win_percentage = 0
        if totals[2]:
            player.doubles_win_percentage = round((wins[2] / totals[2])*100, 2)
        else:
            player.doubles_win_percentage = 0
        player.total_played = totals[1] + totals[2]
        player.save()


def stats_red_or_blue(params):
    """
    Return statistics on the most important question.
    """
    red_wins = Game.objects.filter(teams__side=Team.RED, teams__score=10).count()

    blue_wins = Game.objects.filter(teams__side=Team.BLUE, teams__score=10).count()