mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
Merge branch 'theouche' of github.com:AudebertAdrien/ft_transcendence into test
This commit is contained in:
commit
2f6fa89adf
2
.env
2
.env
@ -6,7 +6,7 @@ DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
|
|||||||
# PostgreSQL settings
|
# PostgreSQL settings
|
||||||
POSTGRES_DB=players_db
|
POSTGRES_DB=players_db
|
||||||
POSTGRES_USER=42student
|
POSTGRES_USER=42student
|
||||||
POSTGRES_PASSWORD=qwerty#42
|
POSTGRES_PASSWORD=qwerty
|
||||||
|
|
||||||
DB_HOST=db
|
DB_HOST=db
|
||||||
DB_PORT=5432
|
DB_PORT=5432
|
||||||
|
|||||||
49
pong/game/migrations/0001_initial.py
Normal file
49
pong/game/migrations/0001_initial.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
# Generated by Django 5.0.7 on 2024-07-23 16:04
|
||||||
|
|
||||||
|
import django.db.models.deletion
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
initial = True
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Player',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=100)),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Tournoi',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('name', models.CharField(max_length=200)),
|
||||||
|
('nbr_player', models.PositiveSmallIntegerField()),
|
||||||
|
('date', models.DateField()),
|
||||||
|
('winner', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='game.player')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Match',
|
||||||
|
fields=[
|
||||||
|
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('score_player1', models.PositiveSmallIntegerField()),
|
||||||
|
('score_player2', models.PositiveSmallIntegerField()),
|
||||||
|
('nbr_ball_touch_p1', models.PositiveIntegerField()),
|
||||||
|
('nbr_ball_touch_p2', models.PositiveIntegerField()),
|
||||||
|
('duration', models.DurationField()),
|
||||||
|
('date', models.DateField(auto_now_add=True)),
|
||||||
|
('is_tournoi', models.BooleanField()),
|
||||||
|
('player1', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='match_as_player1', to='game.player')),
|
||||||
|
('player2', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='match_as_player2', to='game.player')),
|
||||||
|
('winner', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='won_matches', to='game.player')),
|
||||||
|
('tournoi', models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='matches', to='game.tournoi')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
0
pong/game/migrations/__init__.py
Normal file
0
pong/game/migrations/__init__.py
Normal file
@ -3,4 +3,49 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
User.add_to_class('auth_token', models.CharField(max_length=100, null=True, blank=True, unique=True))
|
User.add_to_class('auth_token', models.CharField(max_length=100, null=True, blank=True, unique=True))
|
||||||
|
# Create your models here.
|
||||||
|
|
||||||
|
class Player(models.Model):
|
||||||
|
name = models.CharField(max_length=100)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
class Tournoi(models.Model):
|
||||||
|
name = models.CharField(max_length=200)
|
||||||
|
nbr_player = models.PositiveSmallIntegerField()
|
||||||
|
date = models.DateField()
|
||||||
|
winner = models.ForeignKey('Player', on_delete=models.SET_NULL, null=True)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
class Match(models.Model):
|
||||||
|
player1 = models.ForeignKey('Player', related_name='match_as_player1', on_delete=models.CASCADE)
|
||||||
|
player2 = models.ForeignKey('Player', related_name='match_as_player2', on_delete=models.CASCADE)
|
||||||
|
score_player1 = models.PositiveSmallIntegerField()
|
||||||
|
score_player2 = models.PositiveSmallIntegerField()
|
||||||
|
winner = models.ForeignKey('Player', related_name='won_matches',on_delete=models.CASCADE)
|
||||||
|
nbr_ball_touch_p1 = models.PositiveIntegerField()
|
||||||
|
nbr_ball_touch_p2 = models.PositiveIntegerField()
|
||||||
|
duration = models.DurationField()
|
||||||
|
date = models.DateField(auto_now_add=True)
|
||||||
|
is_tournoi = models.BooleanField()
|
||||||
|
tournoi = models.ForeignKey('Tournoi', related_name='matches', on_delete=models.SET_NULL, null=True)
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
if self.score_player1 < 0 or self.score_player2 < 0:
|
||||||
|
raise ValidationError('Les scores doivent être positifs.')
|
||||||
|
if self.score_player1 > self.score_player2 and self.winner != self.player1:
|
||||||
|
raise ValidationError('Le gagnant ne correspond pas aux scores.')
|
||||||
|
if self.score_player2 > self.score_player1 and self.winner != self.player2:
|
||||||
|
raise ValidationError('Le gagnant ne correspond pas aux scores.')
|
||||||
|
super().clean()
|
||||||
|
|
||||||
|
def save(self, *args, **kwargs):
|
||||||
|
self.full_clean() # Appel de la méthode clean() avant d'enregistrer
|
||||||
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return f"{self.player1.name} vs {self.player2.name}"
|
||||||
106
pong/game/utils.py
Normal file
106
pong/game/utils.py
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
from myapp.models import Player, Tournoi, Match
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
|
def create_player(name):
|
||||||
|
player = Player(name=name)
|
||||||
|
player.save()
|
||||||
|
return player
|
||||||
|
|
||||||
|
def create_tournoi(name, nbr_player, date, winner=None):
|
||||||
|
tournoi = Tournoi(name=name, nbr_player=nbr_player, date=date, winner=winner)
|
||||||
|
tournoi.save()
|
||||||
|
return tournoi
|
||||||
|
|
||||||
|
def create_match(player1, player2, score_player1=0, score_player2=0, winner=None, nbr_ball_touch_p1=0, nbr_ball_touch_p2=0, duration=None, is_tournoi=False, tournoi=None):
|
||||||
|
match = Match(
|
||||||
|
player1=player1,
|
||||||
|
player2=player2,
|
||||||
|
score_player1=score_player1,
|
||||||
|
score_player2=score_player2,
|
||||||
|
winner=winner,
|
||||||
|
nbr_ball_touch_p1=nbr_ball_touch_p1,
|
||||||
|
nbr_ball_touch_p2=nbr_ball_touch_p2,
|
||||||
|
duration=duration,
|
||||||
|
is_tournoi=is_tournoi,
|
||||||
|
tournoi=tournoi
|
||||||
|
)
|
||||||
|
match.save()
|
||||||
|
return match
|
||||||
|
|
||||||
|
def complete_match(match_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration):
|
||||||
|
try:
|
||||||
|
match = Match.objects.get(id=match_id)
|
||||||
|
except Match.DoesNotExist:
|
||||||
|
raise ValidationError(f"Match with id {match_id} does not exist")
|
||||||
|
|
||||||
|
match.score_player1 = score_player1
|
||||||
|
match.score_player2 = score_player2
|
||||||
|
match.nbr_ball_touch_p1 = nbr_ball_touch_p1
|
||||||
|
match.nbr_ball_touch_p2 = nbr_ball_touch_p2
|
||||||
|
match.duration = duration
|
||||||
|
|
||||||
|
if score_player1 > score_player2:
|
||||||
|
match.winner = match.player1
|
||||||
|
elif score_player2 > score_player1:
|
||||||
|
match.winner = match.player2
|
||||||
|
else:
|
||||||
|
match.winner = None
|
||||||
|
|
||||||
|
match.save()
|
||||||
|
return match
|
||||||
|
|
||||||
|
def complete_tournoi(tournoi_id, player):
|
||||||
|
try:
|
||||||
|
tournoi = Tournoi.objects.get(id = tournoi_id)
|
||||||
|
except Tournoi.DoesNotExist:
|
||||||
|
raise ValidationError(f"Tournoi with id {tournoi_id} does not exist")
|
||||||
|
|
||||||
|
tournoi.winner = player
|
||||||
|
tournoi.save()
|
||||||
|
return tournoi
|
||||||
|
|
||||||
|
def player_statistics(request, player_name):
|
||||||
|
player = get_object_or_404(Player, nam = player_name)
|
||||||
|
|
||||||
|
#filtre on tab
|
||||||
|
matches_as_player1 = Match.objects.filter(player1=player)
|
||||||
|
matches_as_player2 = Match.objects.filter(player2=player)
|
||||||
|
won_matches = Match.objects.filter(winner=player)
|
||||||
|
part_tourn_as_p1 = Tournoi.objects.filter(matches__is_tournoi=True, matches__player1=player)
|
||||||
|
part_tourn_as_p2 = Tournoi.objects.filter(matches__is_tournoi=True, matches__player2=player)
|
||||||
|
won_tourn = Tournoi.objects.filter(winner=player)
|
||||||
|
|
||||||
|
# calulate stat
|
||||||
|
total_match = match_as_player1.count() + match_as_player2.count()
|
||||||
|
total_score = sum([match.score_player1 for match in matches_as_player1 ]) + sum([match.score_player2 for match in matches_as_player2])
|
||||||
|
total_score_adv = sum([match.score_player2 for match in matches_as_player1 ]) + sum([match.score_player1 for match in matches_as_player2])
|
||||||
|
total_win = won_matches.count()
|
||||||
|
p_win = (total_win / total_match) * 100
|
||||||
|
m_score_match = total_score / total_match
|
||||||
|
m_score_adv_match = total_score_adv / total_match
|
||||||
|
nbr_ball_touch = sum([match.nbr_ball_touch_p1 for match in matches_as_player1]) + sum([match.nbr_ball_touch_p2 for match in matches_as_player2])
|
||||||
|
m_nbr_ball_touch = nbr_ball_touch / total_match
|
||||||
|
total_duration = sum([match.duration for match in matches_as_player1]) + sum(match.duration for match in matches_as_player2)
|
||||||
|
m_duration = total_duration / total_match
|
||||||
|
total_tourn_p = part_tourn_as_p1.count() + part_tourn_as_p2.count()
|
||||||
|
total_win_tourn = won_tourn.count()
|
||||||
|
p_win_tourn = (total_win_tourn / total_tourn_p) * 100
|
||||||
|
|
||||||
|
best_score_as_player1 = matches_as_player1.aggregate(Max('score_player1'))['score_player1__max']
|
||||||
|
best_score_as_player2 = matches_as_player2.aggregate(Max('score_player2'))['score_player2__max']
|
||||||
|
best_score = max(best_score_as_player1, best_score_as_player2)
|
||||||
|
|
||||||
|
data = {
|
||||||
|
'player_name': player.name,
|
||||||
|
'number of match played' : total_match
|
||||||
|
'number of win (matches)' : total_win
|
||||||
|
'pourcentage of victory' : p_win
|
||||||
|
'num_participated_tournaments': num_participated_tournaments,
|
||||||
|
'num_won_tournaments': num_won_tournaments
|
||||||
|
}
|
||||||
|
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user