diff --git a/pong/game/game.py b/pong/game/game.py
index 851f0b3..fd8c25c 100644
--- a/pong/game/game.py
+++ b/pong/game/game.py
@@ -5,6 +5,8 @@ import asyncio
import random
from datetime import datetime
from .utils import endfortheouche
+from asgiref.sync import sync_to_async
+
class Game:
def __init__(self, game_id, player1, player2, localgame):
@@ -210,6 +212,6 @@ class Game:
if not self.localgame:
await self.player2.send(end_message)
print("save data")
- await endfortheouche(self.game_state['player1_name'], self.game_state['player2_name'],
+ await sync_to_async(endfortheouche)(self.game_state['player1_name'], self.game_state['player2_name'],
self.game_state['player1_score'], self.game_state['player2_score'],
self.bt1, self.bt2, duration, False, None)
diff --git a/pong/game/templates/pong/match_list.html b/pong/game/templates/pong/match_list.html
deleted file mode 100644
index 72c1315..0000000
--- a/pong/game/templates/pong/match_list.html
+++ /dev/null
@@ -1,51 +0,0 @@
-
-
-
-
-
- Matches List
-
-
- Matches List
-
-
-
- | ID |
- Player 1 |
- Player 2 |
- Score Player 1 |
- Score Player 2 |
- Winner |
- Ball Touches Player 1 |
- Ball Touches Player 2 |
- Duration |
- Date |
- Is Tournament |
- Tournament |
-
-
-
- {% for match in matches %}
-
- | {{ match.id }} |
- {{ match.player1.name }} |
- {{ match.player2.name }} |
- {{ match.score_player1 }} |
- {{ match.score_player2 }} |
- {{ match.winner.name }} |
- {{ match.nbr_ball_touch_p1 }} |
- {{ match.nbr_ball_touch_p2 }} |
- {{ match.duration }} |
- {{ match.date }} |
- {{ match.is_tournoi }} |
- {{ match.tournoi.name }} |
-
- {% empty %}
-
- | No matches found. |
-
- {% endfor %}
-
-
-
-
diff --git a/pong/game/templates/pong/player_list.html b/pong/game/templates/pong/player_list.html
deleted file mode 100644
index b9fbb42..0000000
--- a/pong/game/templates/pong/player_list.html
+++ /dev/null
@@ -1,53 +0,0 @@
-
-
-
-
-
- Players List
-
-
- Players List
-
-
-
- | ID |
- Name |
- Total Matches |
- Total Wins |
- Win Percentage |
- Average Match Score |
- Average Opponent Score |
- Best Score |
- Average Ball Touches |
- Total Duration |
- Average Duration |
- Participated Tournaments |
- Won Tournaments |
-
-
-
- {% for player in players %}
-
- | {{ player.id }} |
- {{ player.name }} |
- {{ player.total_match }} |
- {{ player.total_win }} |
- {{ player.p_win }} |
- {{ player.m_score_match }} |
- {{ player.m_score_adv_match }} |
- {{ player.best_score }} |
- {{ player.m_nbr_ball_touch }} |
- {{ player.total_duration }} |
- {{ player.m_duration }} |
- {{ player.num_participated_tournaments }} |
- {{ player.num_won_tournaments }} |
-
- {% empty %}
-
- | No players found. |
-
- {% endfor %}
-
-
-
-
diff --git a/pong/game/templates/pong/tournoi_list.html b/pong/game/templates/pong/tournoi_list.html
deleted file mode 100644
index e5718f5..0000000
--- a/pong/game/templates/pong/tournoi_list.html
+++ /dev/null
@@ -1,37 +0,0 @@
-
-
-
-
-
- Tournaments List
-
-
- Tournaments List
-
-
-
- | ID |
- Name |
- Number of Players |
- Date |
- Winner |
-
-
-
- {% for tournoi in tournois %}
-
- | {{ tournoi.id }} |
- {{ tournoi.name }} |
- {{ tournoi.nbr_player }} |
- {{ tournoi.date }} |
- {{ tournoi.winner.name }} |
-
- {% empty %}
-
- | No tournaments found. |
-
- {% endfor %}
-
-
-
-
diff --git a/pong/game/utils.py b/pong/game/utils.py
index ccad1dc..7ef52e9 100644
--- a/pong/game/utils.py
+++ b/pong/game/utils.py
@@ -6,7 +6,7 @@ from datetime import timedelta
from channels.db import database_sync_to_async
#from asgiref.sync import database_sync_to_async
-async def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
+""" async def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
try:
print("here endfortheouche §!!!")
# Vérification de l'existence des joueurs et création si nécessaire
@@ -203,7 +203,7 @@ def get_player_p_win(player_name):
player = get_object_or_404(Player, name=player_name)
# Retourner la valeur de p_win
return player.p_win
-
+ """
@@ -211,40 +211,52 @@ def get_player_p_win(player_name):
######## try synchrone version ########
-
-""" def endfortheouche_sync(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
+def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
try:
print("here endfortheouche §!!!")
- player_1 = get_or_create_player_sync(p1)
- player_2 = get_or_create_player_sync(p2)
+ # Vérification de l'existence des joueurs et création si nécessaire
+ player_1 = get_or_create_player(p1)
+ player_2 = get_or_create_player(p2)
print("ok")
print("############# BEFORE MATCH")
- create_match_sync(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
+ create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
print("############# AFTER DONE")
- #update_player_statistics_sync(p1)
+ update_player_statistics(p1)
print("############# END STAT P1")
- #update_player_statistics_sync(p2)
+ update_player_statistics(p2)
except Exception as e:
print(f"Error in endfortheouche: {e}")
-def get_player_sync(name):
- print(f"Getting player '{name}'")
+def get_player_by_name(name):
+ print(f"Checking if player '{name}' exists")
+ exists = Player.objects.filter(name=name).exists()
+ print(f"Player exists: {exists}")
+ return exists
+
+
+def get_player(name):
return Player.objects.get(name=name)
-def get_or_create_player_sync(name):
+def get_or_create_player(name):
print("here !!")
- player_exists = Player.objects.filter(name=name).exists()
- print("search in database!!")
+ print(f"Checking existence for player: {name}")
+ player_exists = get_player_by_name(name)
+ print(f"END search in database!! (Player exists: {player_exists})")
if not player_exists:
- print(f"Player '{name}' does not exist, creating new player.")
- return create_player_sync(name)
+ print("Player does not exist, creating player...")
+ player = create_player(name)
+ print(f"Player created: {player}")
+ return player
else:
- print(f"Player '{name}' exists.")
- return get_player_sync(name)
+ print("Player exists, fetching player...")
+ player = get_player(name)
+ print(f"Player fetched: {player}")
+ return player
+
def create_player(
name,
@@ -279,6 +291,10 @@ def create_player(
player.save()
return player
+def create_tournoi(name, nbr_player, date, winner):
+ tournoi = Tournoi(name=name, nbr_player=nbr_player, date=date, winner=winner)
+ tournoi.save()
+ return tournoi
def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi):
match = Match(
@@ -301,4 +317,89 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_
match.winner = None
match.save()
- return match """
+ return match
+
+def update_player_statistics(player_name):
+ print("############# BEG STAT P")
+ player = get_object_or_404(Player, name=player_name)
+
+ # Filtrer les matchs où le joueur est joueur 1 ou joueur 2
+ print("############# HERE")
+ matches_as_player1 = Match.objects.filter(player1=player)
+ matches_as_player2 = Match.objects.filter(player2=player)
+ print("############# ACTUALLY, IT'S GOOD")
+
+ # 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 = 0
+ player.m_duration = 0
+ 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 0
+ total_duration += matches_as_player2.aggregate(Sum('duration'))['duration__sum'] or 0
+ 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()
+ print("CHAKU IS THE BEST")
+
+def get_player_p_win(player_name):
+ # Rechercher le joueur par son nom
+ player = get_object_or_404(Player, name=player_name)
+ # Retourner la valeur de p_win
+ return player.p_win
+