diff --git a/logs/django.log b/logs/django.log deleted file mode 100644 index 444904d..0000000 --- a/logs/django.log +++ /dev/null @@ -1,9 +0,0 @@ -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} -{"message": "Not Found: /favicon.ico", "taskName": null, "status_code": 404, "request": ""} diff --git a/pong/game/game.py b/pong/game/game.py index 56fe7ed..0f30c73 100644 --- a/pong/game/game.py +++ b/pong/game/game.py @@ -244,12 +244,9 @@ class Game: if not self.localgame: await self.player2.send(end_message) if hasattr(self, 'tournament'): - len_tournament = await sync_to_async(getlen)() - name_tournament = self.tournament.name + " #" + str(len_tournament + 1) - print(f"- Saving match game #{self.game_id} of tournament: {name_tournament}") - await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'], + await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'], self.game_state['player1_score'], self.game_state['player2_score'], - self.bt1, self.bt2, duration, True, name_tournament) + self.bt1, self.bt2, duration, True, self.tournament.tournoi_reg) else: await sync_to_async(handle_game_data)(self.game_state['player1_name'], self.game_state['player2_name'], self.game_state['player1_score'], self.game_state['player2_score'], diff --git a/pong/game/migrations/0003_alter_tournoi_date.py b/pong/game/migrations/0003_alter_tournoi_date.py new file mode 100644 index 0000000..6bfda6c --- /dev/null +++ b/pong/game/migrations/0003_alter_tournoi_date.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.1 on 2024-09-10 14:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0002_alter_match_winner'), + ] + + operations = [ + migrations.AlterField( + model_name='tournoi', + name='date', + field=models.DateField(auto_now_add=True), + ), + ] diff --git a/pong/game/models.py b/pong/game/models.py index d72178f..4f63102 100644 --- a/pong/game/models.py +++ b/pong/game/models.py @@ -25,7 +25,7 @@ class Player(models.Model): class Tournoi(models.Model): name = models.CharField(max_length=200) nbr_player = models.PositiveSmallIntegerField() - date = models.DateField() + date = models.DateField(auto_now_add=True) winner = models.ForeignKey('Player', on_delete=models.SET_NULL, null=True) def __str__(self): diff --git a/pong/game/tournament.py b/pong/game/tournament.py index c2b944c..b0c6c1b 100644 --- a/pong/game/tournament.py +++ b/pong/game/tournament.py @@ -6,6 +6,16 @@ from django.template.loader import render_to_string import random from .matchmaking import match_maker from .game import Game +from .models import Tournoi +from .utils import create_tournament, update_tournament, getlen +from asgiref.sync import sync_to_async + + +TOURNAMENT_NAMES = [ + "Champions Clash", "Ultimate Showdown", "Battle Royale", + "Victory Cup", "Legends Tournament", "Elite Series", "Clash of 42", + "Shibuya incident", "Cunning Game", "Elite of the Stars" +] TOURNAMENT_NAMES = [ "Champion's Clash", "Ultimate Showdown", "Battle Royale", @@ -39,6 +49,8 @@ class TournamentMatchMaker: self.games = 0 self.tournament_state = "waiting" #Can be "waiting", "in_progress", or "ended" self.name = random.choice(TOURNAMENT_NAMES) + self.final_name = "" + self.tournoi_reg = None async def add_player(self, player): if self.tournament_state == "waiting" and player not in self.waiting_players: @@ -85,6 +97,9 @@ class TournamentMatchMaker: self.tournament_state = "in_progress" random.shuffle(self.waiting_players) self.current_round = 0 + len_tournament = await sync_to_async(getlen)() + self.final_name = self.name + " #" + str(len_tournament + 1) + self.tournoi_reg = await sync_to_async(create_tournament)(self.final_name, len(self.waiting_players)) await self.advance_tournament() return True @@ -191,6 +206,8 @@ class TournamentMatchMaker: 'type': 'tournament_end', 'winner': winner_username }) + + await sync_to_async(update_tournament)(self.final_name, winner_username) # Reset tournament state self.waiting_players = [] self.matches = [] diff --git a/pong/game/utils.py b/pong/game/utils.py index 8e9c2db..16921d0 100644 --- a/pong/game/utils.py +++ b/pong/game/utils.py @@ -172,5 +172,25 @@ def get_player_p_win(player_name): player = get_object_or_404(Player, name=player_name) return player.p_win +def create_tournament(name, nbr_player): + print("tournoi created!!!") + tournoi=Tournoi(name=name, nbr_player=nbr_player, winner=None) + tournoi.save() + print(f"tournoi name : {tournoi.name} *******!*!*!*!**!*!**!*!*!*!*!*!*!*!*!*") + return tournoi + +def update_tournament(name_tournoi, winner_name): + tournoi = get_object_or_404(Tournoi, name=name_tournoi) + winner_p = get_object_or_404(Player, name=winner_name) + print(f"in update tourna - tournoi name : {tournoi.name} *******!*!*!*!**!*!**!*!*!*!*!*!*!*!*!*") + print(f"in update tourna - winner is : {winner_p.name} *******!*!*!*!**!*!**!*!*!*!*!*!*!*!*!*") + + tournoi.winner = winner_p + print(f"in update tourna - TOURNOI winner is : {tournoi.winner.name} *******!*!*!*!**!*!**!*!*!*!*!*!*!*!*!*") + tournoi.save() + + + + def getlen(): return Tournoi.objects.count() diff --git a/pong/static/burger.js b/pong/static/burger.js index a98343c..aa2139a 100644 --- a/pong/static/burger.js +++ b/pong/static/burger.js @@ -103,10 +103,10 @@ document.addEventListener('DOMContentLoaded', () => { console.log('Displaying matches:'); const matchListBody = document.querySelector('#match-list tbody'); matchListBody.innerHTML = ''; + const row = document.createElement('tr'); if (matches.length != 0) { matches.forEach(match => { - const row = document.createElement('tr'); row.innerHTML = ` ${match.id} ${match.player1__name} @@ -135,10 +135,10 @@ document.addEventListener('DOMContentLoaded', () => { console.log('Displaying players:'); const playersListBody = document.querySelector('#player-list tbody'); playersListBody.innerHTML = ''; + const row = document.createElement('tr'); if (players.length != 0) { players.forEach(player => { - const row = document.createElement('tr'); row.innerHTML = ` ${player.id} ${player.name} @@ -168,16 +168,16 @@ document.addEventListener('DOMContentLoaded', () => { console.log('Displaying tournois:'); const tournoisListBody = document.querySelector('#tournoi-list tbody'); tournoisListBody.innerHTML = ''; + const row = document.createElement('tr'); if (tournois.length != 0) { tournois.forEach(tournoi => { - const row = document.createElement('tr'); row.innerHTML = ` ${tournoi.id} ${tournoi.name} ${tournoi.nbr_player} ${tournoi.date} - ${tournoi.winner.name} + ${tournoi.winner ? tournoi.winner.name : 'None'} `; tournoisListBody.appendChild(row); }); diff --git a/pong/static/game.js b/pong/static/game.js index 1b5a53c..8dcc898 100644 --- a/pong/static/game.js +++ b/pong/static/game.js @@ -349,6 +349,9 @@ document.addEventListener('DOMContentLoaded', () => { } function startTournament() { + saveData = { + type: 'tournoi' + } tournamentContainer.style.display = 'flex'; logo.style.display = 'none'; pongElements.style.display = 'none'; @@ -440,7 +443,7 @@ document.addEventListener('DOMContentLoaded', () => { function updateGameState(newState) { gameState = newState; renderGame(); - //checkForWinner(); + checkForWinner(); } function renderGame() { @@ -500,12 +503,14 @@ document.addEventListener('DOMContentLoaded', () => { function checkForWinner() { if (gameState.player1_score === 3 || gameState.player2_score === 3) { - gameControls.style.display = 'flex'; - homeButton.style.display = 'block'; - replayButton.style.display = 'none'; - console.log(saveData.type); - if (saveData.type === 'local'){ - replayButton.style.display = 'block'; + if (saveData.type != "tournoi"){ + gameControls.style.display = 'flex'; + homeButton.style.display = 'block'; + replayButton.style.display = 'none'; + console.log(saveData.type); + if (saveData.type === 'local'){ + replayButton.style.display = 'block'; + } } } }