From f00815b3291ef921eef495999290e5e76be8f3b1 Mon Sep 17 00:00:00 2001 From: CHIBOUB Chakib Date: Wed, 31 Jul 2024 17:29:19 +0200 Subject: [PATCH 1/4] is working --- .env | 2 +- .gitignore | 2 ++ pong/static/game.js | 81 --------------------------------------------- 3 files changed, 3 insertions(+), 82 deletions(-) diff --git a/.env b/.env index c092b44..41d604b 100644 --- a/.env +++ b/.env @@ -1,7 +1,7 @@ # Django settings SECRET_KEY="FollowTheWhiteRabbit" DEBUG=True -DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] +DJANGO_ALLOWED_HOSTS=['*'] # PostgreSQL settings POSTGRES_DB=players_db diff --git a/.gitignore b/.gitignore index 3adaaf8..6ccca4f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ venv/ __pycache__/ data/ +.env +makefile \ No newline at end of file diff --git a/pong/static/game.js b/pong/static/game.js index cbfb198..eacd1db 100644 --- a/pong/static/game.js +++ b/pong/static/game.js @@ -30,86 +30,6 @@ document.addEventListener('DOMContentLoaded', () => { registerButton.addEventListener('click', handleRegister); loginButton.addEventListener('click', handleLogin); - - /// THEOUCHE NOT CERTAIN /// - async function createPlayer(name, totalMatch = 0, totalWin = 0, pWin = null, mScoreMatch = null, mScoreAdvMatch = null, bestScore = 0, mNbrBallTouch = null, totalDuration = null, mDuration = null, numParticipatedTournaments = 0, numWonTournaments = 0) { - try { - const response = await fetch('/api/create_player/', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ - name, - total_match: totalMatch, - total_win: totalWin, - p_win: pWin, - m_score_match: mScoreMatch, - m_score_adv_match: mScoreAdvMatch, - best_score: bestScore, - m_nbr_ball_touch: mNbrBallTouch, - total_duration: totalDuration, - m_duration: mDuration, - num_participated_tournaments: numParticipatedTournaments, - num_won_tournaments: numWonTournaments - }) - }); - - if (!response.ok) { - const errorData = await response.json(); - throw new Error(errorData.error || 'Network response was not ok'); - } - - const data = await response.json(); - return data; - - } catch (error) { - // Afficher l'erreur avec un message plus spécifique - console.error('Error creating player:', error.message); - alert(`Failed to create player: ${error.message}`); - } - } - - async function createTournoi(name, nbr_player, date, winner_id) { - try { - const response = await fetch('/api/create_tournoi/', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ name, nbr_player, date, winner_id }) - }); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - return data; - } catch (error) { - console.error('Error creating tournoi:', error); - } - } - - async function createMatch(player1_id, player2_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi_id) { - try { - const response = await fetch('/api/create_match/', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - body: JSON.stringify({ player1_id, player2_id, score_player1, score_player2, nbr_ball_touch_p1, nbr_ball_touch_p2, duration, is_tournoi, tournoi_id }) - }); - if (!response.ok) { - throw new Error('Network response was not ok'); - } - const data = await response.json(); - return data; - } catch (error) { - console.error('Error creating match:', error); - } - } - - /// THEOUCHE NOT CERTAIN /// - async function handleCheckNickname() { const nickname = nicknameInput.value.trim(); if (nickname) { @@ -172,7 +92,6 @@ document.addEventListener('DOMContentLoaded', () => { try { const result = await registerUser(nickname, password); if (result) { - //await createPlayer(nickname); registerForm.style.display = 'none'; gameContainer.style.display = 'flex'; formBlock.style.display = 'none'; From 73d095ddc4d68f2249455e855e0479cdeba80f7f Mon Sep 17 00:00:00 2001 From: CHIBOUB Chakib Date: Wed, 31 Jul 2024 18:08:31 +0200 Subject: [PATCH 2/4] theouche merge ok --- pong/game/migrations/0001_initial.py | 2 +- .../migrations/0002_alter_match_winner.py | 19 +++++++++++++++++++ pong/game/models.py | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 pong/game/migrations/0002_alter_match_winner.py diff --git a/pong/game/migrations/0001_initial.py b/pong/game/migrations/0001_initial.py index 3e3a389..2f03168 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 15:16 +# Generated by Django 5.0.7 on 2024-07-31 16:01 import django.db.models.deletion from django.db import migrations, models diff --git a/pong/game/migrations/0002_alter_match_winner.py b/pong/game/migrations/0002_alter_match_winner.py new file mode 100644 index 0000000..fe0f120 --- /dev/null +++ b/pong/game/migrations/0002_alter_match_winner.py @@ -0,0 +1,19 @@ +# Generated by Django 5.0.7 on 2024-07-31 16:04 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('game', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='match', + name='winner', + field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='won_matches', to='game.player'), + ), + ] diff --git a/pong/game/models.py b/pong/game/models.py index a4525b6..2586446 100644 --- a/pong/game/models.py +++ b/pong/game/models.py @@ -37,7 +37,7 @@ class Match(models.Model): player2 = models.ForeignKey('Player', related_name='match_as_player2', on_delete=models.CASCADE) score_player1 = models.PositiveSmallIntegerField() score_player2 = models.PositiveSmallIntegerField() - winner = models.ForeignKey('Player', related_name='won_matches',on_delete=models.CASCADE) + winner = models.ForeignKey('Player', related_name='won_matches',on_delete=models.CASCADE, null=True) nbr_ball_touch_p1 = models.PositiveIntegerField() nbr_ball_touch_p2 = models.PositiveIntegerField() duration = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True) From 4a504545dd2d32f138608efeee1b3df4d68fe0fa Mon Sep 17 00:00:00 2001 From: CHIBOUB Chakib Date: Mon, 5 Aug 2024 17:39:37 +0200 Subject: [PATCH 3/4] some fixes --- .env | 4 ++-- makefile | 2 -- pong/game/game.py | 4 ++++ pong/game/matchmaking.py | 2 +- pong/static/game.js | 7 ++++++- pong/static/index.html | 5 +---- pong/static/styles.css | 8 +++++--- 7 files changed, 19 insertions(+), 13 deletions(-) diff --git a/.env b/.env index 4ecdb09..41d604b 100644 --- a/.env +++ b/.env @@ -11,5 +11,5 @@ POSTGRES_PASSWORD=qwerty DB_HOST=db DB_PORT=5432 -PROJECT_PATH=${PWD}/pong -POSTGRES_DATA_PATH=${PWD}/data/db \ No newline at end of file +PROJECT_PATH=/home/mchiboub/42cursus/transcendence/pong +POSTGRES_DATA_PATH=/home/mchiboub/42cursus/transcendence/data/db \ No newline at end of file diff --git a/makefile b/makefile index b662bde..f84c03f 100644 --- a/makefile +++ b/makefile @@ -28,8 +28,6 @@ destroy: logs: $(COMPOSE) logs -f $(CONTAINER) -re: destroy up - ps: $(COMPOSE) ps diff --git a/pong/game/game.py b/pong/game/game.py index ea2a5a6..5ceecb8 100644 --- a/pong/game/game.py +++ b/pong/game/game.py @@ -126,9 +126,13 @@ class Game: # Check for scoring if self.game_state['ball_position']['x'] <= 10: self.game_state['player2_score'] += 1 + if self.game_state['player2_score'] >= 5: + 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'] >= 5: + self.end_game() self.reset_ball() def reset_ball(self): diff --git a/pong/game/matchmaking.py b/pong/game/matchmaking.py index fe6504c..28b8dda 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 >= 3 and self.waiting_players: + if self.timer >= 30 and self.waiting_players: player1 = self.waiting_players.pop(0) print(f"*** MATCH FOUND: {player1.user.username} vs BOT") self.botgame = True diff --git a/pong/static/game.js b/pong/static/game.js index f5b430d..edc0f62 100644 --- a/pong/static/game.js +++ b/pong/static/game.js @@ -11,7 +11,8 @@ document.addEventListener('DOMContentLoaded', () => { const loginForm = document.getElementById('login-form'); const registerForm = document.getElementById('register-form'); const formBlock = document.getElementById('block-form'); - + const pongElements = document.getElementById('pong-elements'); + const logo = document.querySelector('.logo'); let socket; let token; @@ -95,6 +96,8 @@ document.addEventListener('DOMContentLoaded', () => { registerForm.style.display = 'none'; gameContainer.style.display = 'flex'; formBlock.style.display = 'none'; + logo.style.display = 'none'; + pongElements.style.display = 'none'; startWebSocketConnection(token); } else { alert('Registration failed. Please try again.'); @@ -131,6 +134,8 @@ document.addEventListener('DOMContentLoaded', () => { loginForm.style.display = 'none'; gameContainer.style.display = 'flex'; formBlock.style.display = 'none'; + logo.style.display = 'none'; + pongElements.style.display = 'none'; startWebSocketConnection(token); } else { alert('Authentication failed. Please try again.'); diff --git a/pong/static/index.html b/pong/static/index.html index 63fc79c..4ce7371 100644 --- a/pong/static/index.html +++ b/pong/static/index.html @@ -10,14 +10,13 @@ -
-
+
@@ -71,8 +70,6 @@ star.style.animationDuration = `${Math.random() * 2 + 1}s`; starsContainer.appendChild(star); } - - setInterval(createTrail, 100); diff --git a/pong/static/styles.css b/pong/static/styles.css index 460feb3..3a9abd4 100644 --- a/pong/static/styles.css +++ b/pong/static/styles.css @@ -141,12 +141,14 @@ button:hover { position: absolute; top: 20px; left: 20px; - font-size: 3rem; - color: #00ffff; - text-shadow: 0 0 15px #00ffff; z-index: 20; } +.logo img { + max-width: 100%; + height: auto; +} + .stars { position: absolute; width: 100%; From c8d0a2d1fadce0fe085bc4382211b45f7be18858 Mon Sep 17 00:00:00 2001 From: estellon Date: Mon, 5 Aug 2024 18:08:49 +0200 Subject: [PATCH 4/4] add_pad_color --- pong/static/styles.css | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/pong/static/styles.css b/pong/static/styles.css index 460feb3..04fea73 100644 --- a/pong/static/styles.css +++ b/pong/static/styles.css @@ -123,16 +123,22 @@ button:hover { #player1-pad { left: 10px; + background-color: #00ffff; + border-radius: 10px; + box-shadow: 0 0 15px #00ffff; } #player2-pad { right: 10px; + background-color: #00ffff; + border-radius: 10px; + box-shadow: 0 0 15px #00ffff; } #ball { width: 20px; height: 20px; - background-color: #ffffff; + background-color: #00ffff; border-radius: 50%; position: absolute; }