mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
Merge branch 'adrien' of https://github.com/AudebertAdrien/ft_transcendence into trash
This commit is contained in:
commit
080280f76b
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,3 +3,4 @@ __pycache__/
|
|||||||
data/
|
data/
|
||||||
.env
|
.env
|
||||||
makefile
|
makefile
|
||||||
|
logs/django.log
|
||||||
|
|||||||
@ -1 +0,0 @@
|
|||||||
{"message": "Not Found: /totofaitduvelo", "taskName": null, "status_code": 404, "request": "<ASGIRequest: GET '/totofaitduvelo'>"}
|
|
||||||
@ -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):
|
||||||
@ -214,6 +216,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)
|
||||||
|
|||||||
@ -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
|
||||||
@ -204,7 +204,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
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@ -212,40 +212,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,
|
||||||
@ -280,6 +292,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(
|
||||||
@ -302,4 +318,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
|
||||||
|
|
||||||
|
|||||||
@ -557,9 +557,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const matchListBody = document.querySelector('#match-list tbody');
|
const matchListBody = document.querySelector('#match-list tbody');
|
||||||
matchListBody.innerHTML = '';
|
matchListBody.innerHTML = '';
|
||||||
|
|
||||||
const row = document.createElement('tr');
|
|
||||||
if (matches.length != 0) {
|
if (matches.length != 0) {
|
||||||
matches.forEach(match => {
|
matches.forEach(match => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
<td>${match.id}</td>
|
<td>${match.id}</td>
|
||||||
<td>${match.player1__name}</td>
|
<td>${match.player1__name}</td>
|
||||||
@ -589,9 +589,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const playersListBody = document.querySelector('#player-list tbody');
|
const playersListBody = document.querySelector('#player-list tbody');
|
||||||
playersListBody.innerHTML = '';
|
playersListBody.innerHTML = '';
|
||||||
|
|
||||||
const row = document.createElement('tr');
|
|
||||||
if (players.length != 0) {
|
if (players.length != 0) {
|
||||||
players.forEach(player => {
|
players.forEach(player => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
<td>${player.id}</td>
|
<td>${player.id}</td>
|
||||||
<td>${player.name}</td>
|
<td>${player.name}</td>
|
||||||
@ -622,9 +622,9 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const tournoisListBody = document.querySelector('#tournoi-list tbody');
|
const tournoisListBody = document.querySelector('#tournoi-list tbody');
|
||||||
tournoisListBody.innerHTML = '';
|
tournoisListBody.innerHTML = '';
|
||||||
|
|
||||||
const row = document.createElement('tr');
|
|
||||||
if (tournois.length != 0) {
|
if (tournois.length != 0) {
|
||||||
tournois.forEach(tournoi => {
|
tournois.forEach(tournoi => {
|
||||||
|
const row = document.createElement('tr');
|
||||||
row.innerHTML = `
|
row.innerHTML = `
|
||||||
<td>${tournoi.id}</td>
|
<td>${tournoi.id}</td>
|
||||||
<td>${tournoi.name}</td>
|
<td>${tournoi.name}</td>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user