mirror of
https://github.com/AudebertAdrien/ft_transcendence.git
synced 2025-12-16 14:07:49 +01:00
add research bar
This commit is contained in:
parent
86074d359c
commit
4129f927a8
@ -70,13 +70,35 @@ class Game:
|
|||||||
await asyncio.sleep(1/60) # Around 60 FPS
|
await asyncio.sleep(1/60) # Around 60 FPS
|
||||||
|
|
||||||
async def update_bot_position(self):
|
async def update_bot_position(self):
|
||||||
target_y = self.game_state['ball_position']['y']
|
future_ball_position = self.predict_ball_trajectory()
|
||||||
if self.game_state['player2_position'] < target_y < self.game_state['player2_position'] + 80:
|
|
||||||
pass
|
target_y = future_ball_position['y']
|
||||||
elif self.game_state['player2_position'] < target_y:
|
player2_position = self.game_state['player2_position']
|
||||||
self.game_state['player2_position'] = min(self.game_state['player2_position'] + (50 * self.speed), 300)
|
|
||||||
elif self.game_state['player2_position'] + 80 > target_y:
|
# Ajuste la position du bot en fonction de la position prévue de la balle
|
||||||
self.game_state['player2_position'] = max(self.game_state['player2_position'] - (50 * self.speed), 0)
|
if player2_position < target_y < player2_position + 80:
|
||||||
|
pass # Pas besoin de bouger, le bot est déjà bien placé
|
||||||
|
elif player2_position < target_y:
|
||||||
|
self.game_state['player2_position'] = min(player2_position + (50 * self.speed), 300)
|
||||||
|
elif player2_position + 80 > target_y:
|
||||||
|
self.game_state['player2_position'] = max(player2_position - (50 * self.speed), 0)
|
||||||
|
|
||||||
|
def predict_ball_trajectory(self, steps=60):
|
||||||
|
|
||||||
|
future_x = self.game_state['ball_position']['x']
|
||||||
|
future_y = self.game_state['ball_position']['y']
|
||||||
|
velocity_x = self.game_state['ball_velocity']['x']
|
||||||
|
velocity_y = self.game_state['ball_velocity']['y']
|
||||||
|
|
||||||
|
for _ in range(steps):
|
||||||
|
future_x += velocity_x
|
||||||
|
future_y += velocity_y
|
||||||
|
|
||||||
|
# Gérer les rebonds sur les murs
|
||||||
|
if future_y <= 0 or future_y >= 300:
|
||||||
|
velocity_y = -velocity_y # Inverser la direction du mouvement vertical
|
||||||
|
|
||||||
|
return {'x': future_x, 'y': future_y}
|
||||||
|
|
||||||
async def update_game_state(self):
|
async def update_game_state(self):
|
||||||
if self.ended:
|
if self.ended:
|
||||||
|
|||||||
@ -6,211 +6,6 @@ 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):
|
|
||||||
try:
|
|
||||||
print("here endfortheouche §!!!")
|
|
||||||
# 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")
|
|
||||||
|
|
||||||
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")
|
|
||||||
|
|
||||||
await update_player_statistics(p1)
|
|
||||||
print("############# END STAT P1")
|
|
||||||
await update_player_statistics(p2)
|
|
||||||
print("############# END STAT P2")
|
|
||||||
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,
|
|
||||||
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
|
|
||||||
|
|
||||||
@database_sync_to_async
|
|
||||||
def create_tournoi(name, nbr_player, date, winner):
|
|
||||||
tournoi = Tournoi(name=name, nbr_player=nbr_player, date=date, winner=winner)
|
|
||||||
tournoi.save()
|
|
||||||
return tournoi
|
|
||||||
|
|
||||||
@database_sync_to_async
|
|
||||||
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
|
|
||||||
|
|
||||||
@database_sync_to_async
|
|
||||||
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
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
######## 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(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tournament):
|
||||||
try:
|
try:
|
||||||
@ -229,6 +24,7 @@ def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_tourna
|
|||||||
update_player_statistics(p1)
|
update_player_statistics(p1)
|
||||||
print("############# END STAT P1")
|
print("############# END STAT P1")
|
||||||
update_player_statistics(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}")
|
||||||
|
|
||||||
|
|||||||
@ -45,11 +45,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
const quickMatchButton = document.getElementById('quick-match');
|
const quickMatchButton = document.getElementById('quick-match');
|
||||||
const tournamentButton = document.getElementById('tournament');
|
const tournamentButton = document.getElementById('tournament');
|
||||||
|
|
||||||
/* const modal = document.getElementById("myModal");
|
|
||||||
const btn = document.getElementById("myBtn");
|
|
||||||
const span = document.getElementsByClassName("close")[0];
|
|
||||||
const jsonContent = document.getElementById("jsonContent"); */
|
|
||||||
|
|
||||||
let socket;
|
let socket;
|
||||||
let token;
|
let token;
|
||||||
let gameState;
|
let gameState;
|
||||||
@ -646,6 +641,67 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
////////////////////////////// END BURGER BUTTON ////////////////////////////////
|
////////////////////////////// END BURGER BUTTON ////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////// BEG STAT SPE ////////////////////////////////
|
||||||
|
|
||||||
|
document.getElementById('search-player').addEventListener('input', filterPlayers);
|
||||||
|
document.getElementById('search-match-player').addEventListener('input', filterMatches);
|
||||||
|
document.getElementById('search-match-date').addEventListener('input', filterMatches);
|
||||||
|
|
||||||
|
function filterPlayers() {
|
||||||
|
const searchValue = document.getElementById('search-player').value.toLowerCase();
|
||||||
|
const playersListBody = document.querySelector('#player-list tbody');
|
||||||
|
const rows = playersListBody.getElementsByTagName('tr');
|
||||||
|
|
||||||
|
for (let i = 0; i < rows.length; i++) {
|
||||||
|
const nameCell = rows[i].getElementsByTagName('td')[1]; // The 'Name' column
|
||||||
|
if (nameCell) {
|
||||||
|
const nameValue = nameCell.textContent || nameCell.innerText;
|
||||||
|
if (nameValue.toLowerCase().indexof(searchValue) > -1 ) {
|
||||||
|
rows[i].style.display = '';
|
||||||
|
} else {
|
||||||
|
rows[i].style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function filterMatches() {
|
||||||
|
const playerSearchValue = document.getElementById('search-match-player').value.toLowerCase();
|
||||||
|
const dateSearchValue = document.getElementById('search-match-date').value;
|
||||||
|
const matchListBody = document.querySelector('#match-list tbody');
|
||||||
|
const rows = matchListBody.getElementsByTagName('tr');
|
||||||
|
|
||||||
|
for (let i = 0; i < rows.length; i++) {
|
||||||
|
const player1Cell = rows[i].getElementsByTagName('td')[1]; // The 'Player 1' column
|
||||||
|
const player2Cell = rows[i].getElementsByTagName('td')[2]; // The 'Player 2' column
|
||||||
|
const dateCell = rows[i].getElementsByTagName('td')[9]; // The 'Date' column
|
||||||
|
|
||||||
|
let playerMatch = true;
|
||||||
|
if (playerSearchValue) {
|
||||||
|
const player1Value = player1Cell.textContent || player1Cell.innerText;
|
||||||
|
const player2Value = player2Cell.textContent || player2Cell.innerText;
|
||||||
|
playerMatch = player1Value.toLowerCase().indexOf(playerSearchValue) > -1 ||
|
||||||
|
player2Value.toLowerCase().indexOf(playerSearchValue) > -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
let dateMatch = true;
|
||||||
|
if (dateSearchValue) {
|
||||||
|
const dateValue = dateCell.textContent || dateCell.innerText;
|
||||||
|
dateMatch = dateValue.startsWith(dateSearchValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (playerMatch && dateMatch) {
|
||||||
|
rows[i].style.display = '';
|
||||||
|
} else {
|
||||||
|
rows[i].style.display = 'none';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////// END STAT SPE ////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
////////////////////////////// BEG STARS ////////////////////////////////
|
////////////////////////////// BEG STARS ////////////////////////////////
|
||||||
|
|
||||||
const starsContainer = document.getElementById('stars');
|
const starsContainer = document.getElementById('stars');
|
||||||
@ -662,26 +718,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
|
|
||||||
////////////////////////////// END STARS ////////////////////////////////
|
////////////////////////////// END STARS ////////////////////////////////
|
||||||
|
|
||||||
|
|
||||||
/* btn.onclick = function() {
|
|
||||||
fetch('/web3/')
|
|
||||||
.then(response => response.json())
|
|
||||||
.then(data => {
|
|
||||||
console.log('ok here !!');
|
|
||||||
jsonContent.textContent = JSON.stringify(data, null, 2);
|
|
||||||
modal.style.display = "block";
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
span.onclick = function() {
|
|
||||||
modal.style.display = "none";
|
|
||||||
}
|
|
||||||
|
|
||||||
window.onclick = function(event) {
|
|
||||||
if (event.target == modal) {
|
|
||||||
modal.style.display = "none";
|
|
||||||
}
|
|
||||||
} */
|
|
||||||
|
|
||||||
////////////////////////////// BEG LANGAGE ////////////////////////////////
|
////////////////////////////// BEG LANGAGE ////////////////////////////////
|
||||||
const translations = {
|
const translations = {
|
||||||
|
|||||||
@ -128,6 +128,8 @@
|
|||||||
|
|
||||||
<div id="match-list" class="content-list" style="display: none;">
|
<div id="match-list" class="content-list" style="display: none;">
|
||||||
<h1>Matches</h1>
|
<h1>Matches</h1>
|
||||||
|
<input type="text" id="search-match-player" placeholder="Rechercher par nom de joueur">
|
||||||
|
<input type="date" id="search-match-date">
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -152,6 +154,7 @@
|
|||||||
|
|
||||||
<div id="player-list" class="content-list" style="display: none;">
|
<div id="player-list" class="content-list" style="display: none;">
|
||||||
<h1>Players</h1>
|
<h1>Players</h1>
|
||||||
|
<input type="text" id="search-player" placeholder="Rechercher par nom">
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user