<commit_before>from django.db import models


class Player(models.Model):
    name = models.CharField(max_length=50)
    slack_id = models.CharField(max_length=50, unique=True)
    score = models.IntegerField(default=0)


class Territory(models.Model):
    name = models.CharField(max_length=50, unique=True)
    continent = models.ForeignKey("Continent")
    x = models.FloatField()
    y = models.FloatField()
    adjacents = models.ManyToManyField("self")


class Continent(models.Model):
    name = models.CharField(max_length=50, unique=True)
    bonus = models.IntegerField(default=0)


class Game(models.Model):
    is_active = models.BooleanField(default=True)


class PlayerGame(models.Model):
    game = models.ForeignKey("Game")
    player = models.ForeignKey("Player")
    color = models.CharField(max_length=50)
    is_active = models.BooleanField(default=False)
    turn_order = models.IntegerField(default=0)
    reinforcement = models.IntegerField(default=0)

    def owns_continents(self):
        for continent in Continent.objects.all():
            continent_territories = continent.territory_set.count()
            player_territories = self.territorygame_set.filter(
                territory__continent=continent).count()
            if continent_territories == player_territories:
                yield continent

    def get_reinforcements(self):
        pass


class TerritoryGame(models.Model):
    game = models.ForeignKey("Game")
    territory = models.ForeignKey("Territory")
    owner = models.ForeignKey("PlayerGame")
    army = models.IntegerField(default=0)<commit_msg>feat: game init functions<commit_after>from random import shuffle

from django.db import models


class Player(models.Model):
    name = models.CharField(max_length=50)
    slack_id = models.CharField(max_length=50, unique=True)
    score = models.IntegerField(default=0)


class Territory(models.Model):
    name = models.CharField(max_length=50, unique=True)
    continent = models.ForeignKey("Continent")
    x = models.FloatField()
    y = models.FloatField()
    adjacents = models.ManyToManyField("self")


class Continent(models.Model):
    name = models.CharField(max_length=50, unique=True)
    bonus = models.IntegerField(default=0)


class Game(models.Model):
    is_active = models.BooleanField(default=True)

    def init(self, players):
        self._set_players(players)
        self._distribute_territories()
        self._distribute_reinforcements()

    def start(self, players):
        pass

    def _set_players(self, players):
        for player in players:
            p, _ = Player.objects.get_or_create(slack_id=player)
            PlayerGame.objects.create(game=self, player=p, color='black')

    def _distribute_territories(self):
        players = self.playergame_set.all()
        n_players = len(players)
        territories = list(Territory.objects.all())
        shuffle(territories)
        for i, territory in enumerate(territories):
            TerritoryGame.object.create(
                game=self, territory=territory, owner=players[i % n_players])

    def _distribute_reinforcements(self):
        players = self.playergame_set.all()
        n_players = len(players)
        reinforcements = 35

        if n_players == 4:
            reinforcements = 30
        elif n_players == 5:
            reinforcements = 25
        elif n_players == 6:
            reinforcements = 20

        for player in players:
            player.reinforcements = reinforcements
            player.save()


class PlayerGame(models.Model):
    game = models.ForeignKey("Game")
    player = models.ForeignKey("Player")
    color = models.CharField(max_length=50)
    is_active = models.BooleanField(default=False)
    turn_order = models.IntegerField(default=0)
    reinforcements = models.IntegerField(default=0)

    def owns_continents(self):
        for continent in Continent.objects.all():
            continent_territories = continent.territory_set.count()
            player_territories = self.territorygame_set.filter(
                territory__continent=continent).count()
            if continent_territories == player_territories:
                yield continent

    def get_reinforcements(self):
        pass


class TerritoryGame(models.Model):
    game = models.ForeignKey("Game")
    territory = models.ForeignKey("Territory")
    owner = models.ForeignKey("PlayerGame")
    army = models.IntegerField(default=1)
    new_army = models.IntegerField(default=0)
{'fraction_non_alphanumeric': 0.10135585685707935, 'fraction_numerical': 0.009335407868415203, 'mean_word_length': 3.277566539923954, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '862460', 'n_tokens_mistral': 1362, 'n_tokens_neox': 1305, 'n_words': 239}