mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2026-02-04 11:40:25 +01:00
update
This commit is contained in:
commit
02c15e349f
@ -100,12 +100,14 @@ class Game:
|
||||
self.game_state['player2_score'] += 1
|
||||
if self.game_state['player2_score'] > 2:
|
||||
print("Here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
||||
await self.send_game_state()
|
||||
await self.end_game()
|
||||
self.reset_ball()
|
||||
elif self.game_state['ball_position']['x'] >= 790:
|
||||
self.game_state['player1_score'] += 1
|
||||
if self.game_state['player1_score'] > 2:
|
||||
print("Here !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
|
||||
await self.send_game_state()
|
||||
await self.end_game()
|
||||
self.reset_ball()
|
||||
|
||||
|
||||
@ -6,24 +6,15 @@ 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):
|
||||
try:
|
||||
print("here endfortheouche §!!!")
|
||||
if not await database_sync_to_async(Player.objects.filter(name=p1).exists)():
|
||||
player_1 = await create_player(p1)
|
||||
print("############# PLAYER DONE")
|
||||
else:
|
||||
player_1 = await database_sync_to_async(Player.objects.get)(name=p1)
|
||||
# Vérification de l'existence des joueurs et création si nécessaire
|
||||
player_1 = await get_or_create_player(p1)
|
||||
player_2 = await get_or_create_player(p2)
|
||||
|
||||
print("ok")
|
||||
|
||||
if not await database_sync_to_async(Player.objects.filter(name=p2).exists)():
|
||||
player_2 = await create_player(p2)
|
||||
print("############# PLAYER DONE")
|
||||
else:
|
||||
player_2 = await database_sync_to_async(Player.objects.get)(name=p2)
|
||||
|
||||
print("############# BEFORE MATCH")
|
||||
await create_match(player_1, player_2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament)
|
||||
print("############# AFTER DONE")
|
||||
@ -34,6 +25,36 @@ async def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_
|
||||
except Exception as e:
|
||||
print(f"Error in endfortheouche: {e}")
|
||||
|
||||
@database_sync_to_async
|
||||
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
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def get_player(name):
|
||||
return Player.objects.get(name=name)
|
||||
|
||||
|
||||
async def get_or_create_player(name):
|
||||
print("here !!")
|
||||
print(f"Checking existence for player: {name}")
|
||||
player_exists = await get_player_by_name(name)
|
||||
print(f"END search in database!! (Player exists: {player_exists})")
|
||||
if not player_exists:
|
||||
print("Player does not exist, creating player...")
|
||||
player = await create_player(name)
|
||||
print(f"Player created: {player}")
|
||||
return player
|
||||
else:
|
||||
print("Player exists, fetching player...")
|
||||
player = await get_player(name)
|
||||
print(f"Player fetched: {player}")
|
||||
return player
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def create_player(
|
||||
name,
|
||||
@ -50,8 +71,6 @@ def create_player(
|
||||
num_won_tournaments=0
|
||||
):
|
||||
print("create player !!!")
|
||||
""" if database_sync_to_async(Player.objects.filter(name=name).exists)():
|
||||
raise ValueError(f"A player with the name '{name}' already exists.") """
|
||||
|
||||
player = Player(
|
||||
name=name,
|
||||
@ -131,9 +150,9 @@ def update_player_statistics(player_name):
|
||||
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) """
|
||||
#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
|
||||
@ -155,10 +174,10 @@ def update_player_statistics(player_name):
|
||||
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
|
||||
"""
|
||||
#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)
|
||||
@ -173,8 +192,8 @@ def update_player_statistics(player_name):
|
||||
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.num_participated_tournaments = total_tourn_p
|
||||
#player.num_won_tournaments = total_win_tourn
|
||||
|
||||
player.save()
|
||||
print("CHAKU IS THE BEST")
|
||||
@ -186,4 +205,100 @@ def get_player_p_win(player_name):
|
||||
return player.p_win
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
######## try synchrone version ########
|
||||
|
||||
""" def endfortheouche_sync(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)
|
||||
|
||||
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)
|
||||
print("############# AFTER DONE")
|
||||
|
||||
#update_player_statistics_sync(p1)
|
||||
print("############# END STAT P1")
|
||||
#update_player_statistics_sync(p2)
|
||||
except Exception as e:
|
||||
print(f"Error in endfortheouche: {e}")
|
||||
|
||||
def get_player_sync(name):
|
||||
print(f"Getting player '{name}'")
|
||||
return Player.objects.get(name=name)
|
||||
|
||||
|
||||
def get_or_create_player_sync(name):
|
||||
print("here !!")
|
||||
player_exists = Player.objects.filter(name=name).exists()
|
||||
print("search in database!!")
|
||||
if not player_exists:
|
||||
print(f"Player '{name}' does not exist, creating new player.")
|
||||
return create_player_sync(name)
|
||||
else:
|
||||
print(f"Player '{name}' exists.")
|
||||
return get_player_sync(name)
|
||||
|
||||
def create_player(
|
||||
name,
|
||||
total_match=0,
|
||||
total_win=0,
|
||||
p_win= None,
|
||||
m_score_match= None,
|
||||
m_score_adv_match= None,
|
||||
best_score=0,
|
||||
m_nbr_ball_touch= None,
|
||||
total_duration= None,
|
||||
m_duration= None,
|
||||
num_participated_tournaments=0,
|
||||
num_won_tournaments=0
|
||||
):
|
||||
print("create player !!!")
|
||||
|
||||
player = Player(
|
||||
name=name,
|
||||
total_match=total_match,
|
||||
total_win=total_win,
|
||||
p_win=p_win,
|
||||
m_score_match=m_score_match,
|
||||
m_score_adv_match=m_score_adv_match,
|
||||
best_score=best_score,
|
||||
m_nbr_ball_touch=m_nbr_ball_touch,
|
||||
total_duration=total_duration,
|
||||
m_duration=m_duration,
|
||||
num_participated_tournaments=num_participated_tournaments,
|
||||
num_won_tournaments=num_won_tournaments
|
||||
)
|
||||
player.save()
|
||||
return player
|
||||
|
||||
|
||||
def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi):
|
||||
match = Match(
|
||||
player1=player1,
|
||||
player2=player2,
|
||||
score_player1=score_player1,
|
||||
score_player2=score_player2,
|
||||
nbr_ball_touch_p1=nbr_ball_touch_p1,
|
||||
nbr_ball_touch_p2=nbr_ball_touch_p2,
|
||||
duration=duration,
|
||||
is_tournoi=is_tournoi,
|
||||
tournoi=tournoi
|
||||
)
|
||||
|
||||
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 """
|
||||
|
||||
@ -86,8 +86,8 @@ def tournoi_list(request):
|
||||
|
||||
from django.http import JsonResponse
|
||||
|
||||
async def match_list_json(request):
|
||||
matches = await Match.objects.all()
|
||||
def match_list_json(request):
|
||||
matches = Match.objects.all()
|
||||
data = {
|
||||
'matches': list(matches.values(
|
||||
'id', 'player1__name', 'player2__name', 'score_player1', 'score_player2',
|
||||
@ -97,9 +97,9 @@ async def match_list_json(request):
|
||||
}
|
||||
return JsonResponse(data)
|
||||
|
||||
async def player_list_json(request):
|
||||
def player_list_json(request):
|
||||
# Récupère tous les joueurs
|
||||
players = await Player.objects.all()
|
||||
players = Player.objects.all()
|
||||
|
||||
# Crée un dictionnaire avec les informations des joueurs
|
||||
data = {
|
||||
@ -114,9 +114,9 @@ async def player_list_json(request):
|
||||
# Renvoie les données en JSON
|
||||
return JsonResponse(data)
|
||||
|
||||
async def tournoi_list_json(request):
|
||||
def tournoi_list_json(request):
|
||||
# Récupère tous les joueurs
|
||||
tournois = await Tournoi.objects.all()
|
||||
tournois = Tournoi.objects.all()
|
||||
|
||||
# Crée un dictionnaire avec les informations des joueurs
|
||||
data = {
|
||||
|
||||
@ -144,7 +144,7 @@
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% if maches %}
|
||||
{% if matches %}
|
||||
{% for match in matches %}
|
||||
<tr>
|
||||
<td>{{ match.id }}</td>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user