mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
a lot useless fonction, need to add chakib branch and erase useless fonction
This commit is contained in:
parent
2efc9bc962
commit
9e950a361a
@ -1,5 +1,8 @@
|
|||||||
from .models import Player, Tournoi, Match
|
from .models import Player, Tournoi, Match
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
from django.db.models import Max, Sum, F
|
||||||
|
from datetime import timedelta
|
||||||
|
|
||||||
def create_player(
|
def create_player(
|
||||||
name,
|
name,
|
||||||
@ -63,6 +66,82 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_
|
|||||||
match.save()
|
match.save()
|
||||||
return match
|
return match
|
||||||
|
|
||||||
|
|
||||||
|
def player_statistics(player_name):
|
||||||
|
player = get_object_or_404(Player, name=player_name)
|
||||||
|
|
||||||
|
# Filtrer les matchs où le joueur est joueur 1 ou joueur 2
|
||||||
|
matches_as_player1 = Match.objects.filter(player1=player)
|
||||||
|
matches_as_player2 = Match.objects.filter(player2=player)
|
||||||
|
|
||||||
|
# Calculer les statistiques
|
||||||
|
total_match = matches_as_player1.count() + matches_as_player2.count()
|
||||||
|
|
||||||
|
if total_match == 0:
|
||||||
|
# Eviter la division par zéro
|
||||||
|
player.total_match = total_match
|
||||||
|
player.total_win = 0
|
||||||
|
player.p_win = 0
|
||||||
|
player.m_score_match = 0
|
||||||
|
player.m_score_adv_match = 0
|
||||||
|
player.best_score = 0
|
||||||
|
player.m_nbr_ball_touch = 0
|
||||||
|
player.total_duration = timedelta()
|
||||||
|
player.m_duration = timedelta()
|
||||||
|
player.num_participated_tournaments = 0
|
||||||
|
player.num_won_tournaments = 0
|
||||||
|
player.save()
|
||||||
|
return
|
||||||
|
|
||||||
|
won_matches = Match.objects.filter(winner=player)
|
||||||
|
part_tourn_as_p1 = Tournoi.objects.filter(matches__is_tournoi=True, matches__matches_as_player1=player)
|
||||||
|
part_tourn_as_p2 = Tournoi.objects.filter(matches__is_tournoi=True, matches__matches_as_player2=player)
|
||||||
|
won_tourn = Tournoi.objects.filter(winner=player)
|
||||||
|
|
||||||
|
total_score = matches_as_player1.aggregate(Sum('score_player1'))['score_player1__sum'] or 0
|
||||||
|
total_score += matches_as_player2.aggregate(Sum('score_player2'))['score_player2__sum'] or 0
|
||||||
|
|
||||||
|
total_score_adv = matches_as_player1.aggregate(Sum('score_player2'))['score_player2__sum'] or 0
|
||||||
|
total_score_adv += matches_as_player2.aggregate(Sum('score_player1'))['score_player1__sum'] or 0
|
||||||
|
|
||||||
|
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 = matches_as_player1.aggregate(Sum('nbr_ball_touch_p1'))['nbr_ball_touch_p1__sum'] or 0
|
||||||
|
nbr_ball_touch += matches_as_player2.aggregate(Sum('nbr_ball_touch_p2'))['nbr_ball_touch_p2__sum'] or 0
|
||||||
|
m_nbr_ball_touch = nbr_ball_touch / total_match
|
||||||
|
|
||||||
|
total_duration = matches_as_player1.aggregate(Sum('duration'))['duration__sum'] or timedelta()
|
||||||
|
total_duration += matches_as_player2.aggregate(Sum('duration'))['duration__sum'] or timedelta()
|
||||||
|
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 if total_tourn_p else 0
|
||||||
|
|
||||||
|
best_score_as_player1 = matches_as_player1.aggregate(Max('score_player1'))['score_player1__max'] or 0
|
||||||
|
best_score_as_player2 = matches_as_player2.aggregate(Max('score_player2'))['score_player2__max'] or 0
|
||||||
|
best_score = max(best_score_as_player1, best_score_as_player2)
|
||||||
|
|
||||||
|
# Mettre à jour les champs du joueur
|
||||||
|
player.total_match = total_match
|
||||||
|
player.total_win = total_win
|
||||||
|
player.p_win = p_win
|
||||||
|
player.m_score_match = m_score_match
|
||||||
|
player.m_score_adv_match = m_score_adv_match
|
||||||
|
player.best_score = best_score
|
||||||
|
player.m_nbr_ball_touch = m_nbr_ball_touch
|
||||||
|
player.total_duration = total_duration
|
||||||
|
player.m_duration = m_duration
|
||||||
|
player.num_participated_tournaments = total_tourn_p
|
||||||
|
player.num_won_tournaments = total_win_tourn
|
||||||
|
|
||||||
|
player.save()
|
||||||
|
|
||||||
|
|
||||||
""" def complete_match(match_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration):
|
""" def complete_match(match_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration):
|
||||||
try:
|
try:
|
||||||
match = Match.objects.get(id=match_id)
|
match = Match.objects.get(id=match_id)
|
||||||
@ -95,54 +174,6 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_
|
|||||||
tournoi.save()
|
tournoi.save()
|
||||||
return tournoi """
|
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,
|
|
||||||
'mean score per match' : m_score_match,
|
|
||||||
'mean score adv per match' : m_score_adv_match,
|
|
||||||
'best score' : best_score,
|
|
||||||
'mean nbr ball touch' : m_nbr_ball_touch,
|
|
||||||
'total duration played' : total_duration,
|
|
||||||
'mean duration per match' : m_duration,
|
|
||||||
'num_participated_tournaments': num_participated_tournaments,
|
|
||||||
'num_won_tournaments': num_won_tournaments
|
|
||||||
}
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -140,6 +140,7 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
try {
|
try {
|
||||||
const result = await registerUser(nickname, password);
|
const result = await registerUser(nickname, password);
|
||||||
if (result) {
|
if (result) {
|
||||||
|
//await createPlayer(nickname);
|
||||||
registerForm.style.display = 'none';
|
registerForm.style.display = 'none';
|
||||||
gameContainer.style.display = 'flex';
|
gameContainer.style.display = 'flex';
|
||||||
startWebSocketConnection(token);
|
startWebSocketConnection(token);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user