mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 22:17:48 +01:00
prob synchronicite solved git add .
This commit is contained in:
parent
d533167366
commit
24cc7c57bc
@ -5,6 +5,8 @@ import asyncio
|
|||||||
import random
|
import random
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
from .utils import endfortheouche
|
from .utils import endfortheouche
|
||||||
|
from asgiref.sync import sync_to_async
|
||||||
|
|
||||||
|
|
||||||
class Game:
|
class Game:
|
||||||
def __init__(self, game_id, player1, player2, localgame):
|
def __init__(self, game_id, player1, player2, localgame):
|
||||||
@ -210,6 +212,6 @@ class Game:
|
|||||||
if not self.localgame:
|
if not self.localgame:
|
||||||
await self.player2.send(end_message)
|
await self.player2.send(end_message)
|
||||||
print("save data")
|
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.game_state['player1_score'], self.game_state['player2_score'],
|
||||||
self.bt1, self.bt2, duration, False, None)
|
self.bt1, self.bt2, duration, False, None)
|
||||||
|
|||||||
@ -1,51 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Matches List</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Matches List</h1>
|
|
||||||
<table border="1">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>ID</th>
|
|
||||||
<th>Player 1</th>
|
|
||||||
<th>Player 2</th>
|
|
||||||
<th>Score Player 1</th>
|
|
||||||
<th>Score Player 2</th>
|
|
||||||
<th>Winner</th>
|
|
||||||
<th>Ball Touches Player 1</th>
|
|
||||||
<th>Ball Touches Player 2</th>
|
|
||||||
<th>Duration</th>
|
|
||||||
<th>Date</th>
|
|
||||||
<th>Is Tournament</th>
|
|
||||||
<th>Tournament</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for match in matches %}
|
|
||||||
<tr>
|
|
||||||
<td>{{ match.id }}</td>
|
|
||||||
<td>{{ match.player1.name }}</td>
|
|
||||||
<td>{{ match.player2.name }}</td>
|
|
||||||
<td>{{ match.score_player1 }}</td>
|
|
||||||
<td>{{ match.score_player2 }}</td>
|
|
||||||
<td>{{ match.winner.name }}</td>
|
|
||||||
<td>{{ match.nbr_ball_touch_p1 }}</td>
|
|
||||||
<td>{{ match.nbr_ball_touch_p2 }}</td>
|
|
||||||
<td>{{ match.duration }}</td>
|
|
||||||
<td>{{ match.date }}</td>
|
|
||||||
<td>{{ match.is_tournoi }}</td>
|
|
||||||
<td>{{ match.tournoi.name }}</td>
|
|
||||||
</tr>
|
|
||||||
{% empty %}
|
|
||||||
<tr>
|
|
||||||
<td colspan="12">No matches found.</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@ -1,53 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Players List</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Players List</h1>
|
|
||||||
<table border="1">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>ID</th>
|
|
||||||
<th>Name</th>
|
|
||||||
<th>Total Matches</th>
|
|
||||||
<th>Total Wins</th>
|
|
||||||
<th>Win Percentage</th>
|
|
||||||
<th>Average Match Score</th>
|
|
||||||
<th>Average Opponent Score</th>
|
|
||||||
<th>Best Score</th>
|
|
||||||
<th>Average Ball Touches</th>
|
|
||||||
<th>Total Duration</th>
|
|
||||||
<th>Average Duration</th>
|
|
||||||
<th>Participated Tournaments</th>
|
|
||||||
<th>Won Tournaments</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for player in players %}
|
|
||||||
<tr>
|
|
||||||
<td>{{ player.id }}</td>
|
|
||||||
<td>{{ player.name }}</td>
|
|
||||||
<td>{{ player.total_match }}</td>
|
|
||||||
<td>{{ player.total_win }}</td>
|
|
||||||
<td>{{ player.p_win }}</td>
|
|
||||||
<td>{{ player.m_score_match }}</td>
|
|
||||||
<td>{{ player.m_score_adv_match }}</td>
|
|
||||||
<td>{{ player.best_score }}</td>
|
|
||||||
<td>{{ player.m_nbr_ball_touch }}</td>
|
|
||||||
<td>{{ player.total_duration }}</td>
|
|
||||||
<td>{{ player.m_duration }}</td>
|
|
||||||
<td>{{ player.num_participated_tournaments }}</td>
|
|
||||||
<td>{{ player.num_won_tournaments }}</td>
|
|
||||||
</tr>
|
|
||||||
{% empty %}
|
|
||||||
<tr>
|
|
||||||
<td colspan="13">No players found.</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@ -1,37 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
||||||
<title>Tournaments List</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<h1>Tournaments List</h1>
|
|
||||||
<table border="1">
|
|
||||||
<thead>
|
|
||||||
<tr>
|
|
||||||
<th>ID</th>
|
|
||||||
<th>Name</th>
|
|
||||||
<th>Number of Players</th>
|
|
||||||
<th>Date</th>
|
|
||||||
<th>Winner</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{% for tournoi in tournois %}
|
|
||||||
<tr>
|
|
||||||
<td>{{ tournoi.id }}</td>
|
|
||||||
<td>{{ tournoi.name }}</td>
|
|
||||||
<td>{{ tournoi.nbr_player }}</td>
|
|
||||||
<td>{{ tournoi.date }}</td>
|
|
||||||
<td>{{ tournoi.winner.name }}</td>
|
|
||||||
</tr>
|
|
||||||
{% empty %}
|
|
||||||
<tr>
|
|
||||||
<td colspan="5">No tournaments found.</td>
|
|
||||||
</tr>
|
|
||||||
{% endfor %}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
@ -6,7 +6,7 @@ from datetime import timedelta
|
|||||||
from channels.db import database_sync_to_async
|
from channels.db import database_sync_to_async
|
||||||
#from asgiref.sync 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:
|
try:
|
||||||
print("here endfortheouche §!!!")
|
print("here endfortheouche §!!!")
|
||||||
# Vérification de l'existence des joueurs et création si nécessaire
|
# 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)
|
player = get_object_or_404(Player, name=player_name)
|
||||||
# Retourner la valeur de p_win
|
# Retourner la valeur de p_win
|
||||||
return player.p_win
|
return player.p_win
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -211,40 +211,52 @@ def get_player_p_win(player_name):
|
|||||||
|
|
||||||
|
|
||||||
######## try synchrone version ########
|
######## try synchrone version ########
|
||||||
|
def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
|
||||||
""" def endfortheouche_sync(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
|
|
||||||
try:
|
try:
|
||||||
print("here endfortheouche §!!!")
|
print("here endfortheouche §!!!")
|
||||||
player_1 = get_or_create_player_sync(p1)
|
# Vérification de l'existence des joueurs et création si nécessaire
|
||||||
player_2 = get_or_create_player_sync(p2)
|
player_1 = get_or_create_player(p1)
|
||||||
|
player_2 = get_or_create_player(p2)
|
||||||
|
|
||||||
print("ok")
|
print("ok")
|
||||||
|
|
||||||
print("############# BEFORE MATCH")
|
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")
|
print("############# AFTER DONE")
|
||||||
|
|
||||||
#update_player_statistics_sync(p1)
|
update_player_statistics(p1)
|
||||||
print("############# END STAT P1")
|
print("############# END STAT P1")
|
||||||
#update_player_statistics_sync(p2)
|
update_player_statistics(p2)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error in endfortheouche: {e}")
|
print(f"Error in endfortheouche: {e}")
|
||||||
|
|
||||||
def get_player_sync(name):
|
def get_player_by_name(name):
|
||||||
print(f"Getting player '{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)
|
return Player.objects.get(name=name)
|
||||||
|
|
||||||
|
|
||||||
def get_or_create_player_sync(name):
|
def get_or_create_player(name):
|
||||||
print("here !!")
|
print("here !!")
|
||||||
player_exists = Player.objects.filter(name=name).exists()
|
print(f"Checking existence for player: {name}")
|
||||||
print("search in database!!")
|
player_exists = get_player_by_name(name)
|
||||||
|
print(f"END search in database!! (Player exists: {player_exists})")
|
||||||
if not player_exists:
|
if not player_exists:
|
||||||
print(f"Player '{name}' does not exist, creating new player.")
|
print("Player does not exist, creating player...")
|
||||||
return create_player_sync(name)
|
player = create_player(name)
|
||||||
|
print(f"Player created: {player}")
|
||||||
|
return player
|
||||||
else:
|
else:
|
||||||
print(f"Player '{name}' exists.")
|
print("Player exists, fetching player...")
|
||||||
return get_player_sync(name)
|
player = get_player(name)
|
||||||
|
print(f"Player fetched: {player}")
|
||||||
|
return player
|
||||||
|
|
||||||
|
|
||||||
def create_player(
|
def create_player(
|
||||||
name,
|
name,
|
||||||
@ -279,6 +291,10 @@ def create_player(
|
|||||||
player.save()
|
player.save()
|
||||||
return player
|
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):
|
def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi):
|
||||||
match = Match(
|
match = Match(
|
||||||
@ -301,4 +317,89 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_
|
|||||||
match.winner = None
|
match.winner = None
|
||||||
|
|
||||||
match.save()
|
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
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user