From 8ed7cf6419aed4350c4f1363718c9bdb741b626d Mon Sep 17 00:00:00 2001 From: Theouche Date: Wed, 31 Jul 2024 17:26:20 +0200 Subject: [PATCH] It worked git add .git add .! --- pong/game/game.py | 2 ++ pong/game/matchmaking.py | 5 ++++- pong/game/migrations/0001_initial.py | 4 ++-- pong/game/models.py | 2 +- pong/game/utils.py | 24 ++++++++++++++++++++---- pong/static/game.js | 3 ++- 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/pong/game/game.py b/pong/game/game.py index 9504552..ea2a5a6 100644 --- a/pong/game/game.py +++ b/pong/game/game.py @@ -89,6 +89,7 @@ class Game: #await self.bot_player.update_bot_position() await self.update_bot_position() #print('is it ok ?? ') + await self.handle_pad_movement() self.update_game_state() await self.send_game_state() await asyncio.sleep(1/60) # Around 60 FPS @@ -165,6 +166,7 @@ class Game: if self.ended: return if player == self.player1: + print(f"Key press: {key}") if key == 'arrowup': self.p1_mov = -1 #self.game_state['player1_position'] = max(self.game_state['player1_position'] - 25, 0) diff --git a/pong/game/matchmaking.py b/pong/game/matchmaking.py index ab6b21e..fe6504c 100644 --- a/pong/game/matchmaking.py +++ b/pong/game/matchmaking.py @@ -40,7 +40,7 @@ class MatchMaker: await asyncio.sleep(1) self.timer += 1 # Waiting for more than 30s -> BOT game - if self.timer >= 30 and self.waiting_players: + if self.timer >= 3 and self.waiting_players: player1 = self.waiting_players.pop(0) print(f"*** MATCH FOUND: {player1.user.username} vs BOT") self.botgame = True @@ -98,3 +98,6 @@ class MatchMaker: # Instance of the class match_maker = MatchMaker() + +# to get what you want use get_player_p_win(player_name) !! (voir utils.py) +# \ No newline at end of file diff --git a/pong/game/migrations/0001_initial.py b/pong/game/migrations/0001_initial.py index ceac39a..3e3a389 100644 --- a/pong/game/migrations/0001_initial.py +++ b/pong/game/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 5.0.7 on 2024-07-31 13:42 +# Generated by Django 5.0.7 on 2024-07-31 15:16 import django.db.models.deletion from django.db import migrations, models @@ -24,7 +24,7 @@ class Migration(migrations.Migration): ('m_score_adv_match', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)), ('best_score', models.PositiveSmallIntegerField(default=0)), ('m_nbr_ball_touch', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)), - ('total_duration', models.DurationField(blank=True, null=True)), + ('total_duration', models.DecimalField(blank=True, decimal_places=2, max_digits=7, null=True)), ('m_duration', models.DecimalField(blank=True, decimal_places=2, max_digits=5, null=True)), ('num_participated_tournaments', models.PositiveSmallIntegerField(default=0)), ('num_won_tournaments', models.PositiveSmallIntegerField(default=0)), diff --git a/pong/game/models.py b/pong/game/models.py index a7b3993..a4525b6 100644 --- a/pong/game/models.py +++ b/pong/game/models.py @@ -15,7 +15,7 @@ class Player(models.Model): m_score_adv_match = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) best_score = models.PositiveSmallIntegerField(default=0) m_nbr_ball_touch = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) - total_duration = models.DurationField(null=True, blank=True) + total_duration = models.DecimalField(max_digits=7, decimal_places=2, null=True, blank=True) m_duration = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) num_participated_tournaments = models.PositiveSmallIntegerField(default=0) num_won_tournaments = models.PositiveSmallIntegerField(default=0) diff --git a/pong/game/utils.py b/pong/game/utils.py index 07dc265..e260e81 100644 --- a/pong/game/utils.py +++ b/pong/game/utils.py @@ -9,20 +9,26 @@ async def endfortheouche(p1, p2, s_p1, s_p2, bt_p1, bt_2, dur, is_tournoi, name_ # Check if player p1 exists, if not create 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) # Check if player p2 exists, if not create 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) # create Match + 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") # Update data p1 et p2 + await uptdate_player_statistics(p1) + print("############# END STAT P1") await uptdate_player_statistics(p2) @database_sync_to_async @@ -92,11 +98,14 @@ def create_match(player1, player2, score_player1, score_player2, nbr_ball_touch_ @database_sync_to_async def uptdate_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() @@ -118,9 +127,9 @@ def uptdate_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_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) + 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 @@ -138,8 +147,8 @@ def uptdate_player_statistics(player_name): 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'] - total_duration += matches_as_player2.aggregate(Sum('duration'))['duration__sum'] + 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() @@ -164,6 +173,13 @@ def uptdate_player_statistics(player_name): 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 """ def complete_match(match_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration): diff --git a/pong/static/game.js b/pong/static/game.js index bf44d3f..d857501 100644 --- a/pong/static/game.js +++ b/pong/static/game.js @@ -205,14 +205,15 @@ document.addEventListener('DOMContentLoaded', () => { } function handleKeyDown(event) { - console.log('Key press: ', event.key); if (event.key === 'ArrowUp' || event.key === 'ArrowDown') { + console.log('Key press: ', event.key); sendKeyPress(event.key.toLowerCase()); } } function sendKeyPress(key) { if (socket.readyState === WebSocket.OPEN) { + console.log('Key sent: ', key); socket.send(JSON.stringify({ type: 'key_press', key })); } }