diff --git a/django/src/games/pong.py b/django/src/games/pong.py index 02fbf7c9..1f802c9d 100644 --- a/django/src/games/pong.py +++ b/django/src/games/pong.py @@ -1,7 +1,6 @@ import asyncio import json import random -import uuid import redis.asyncio as redis import logging from typing import Any, List @@ -44,9 +43,8 @@ async def initialize_game(self): query_params = parse_qs(self.scope["query_string"].decode()) self.mode = self.check_missing_param(query_params, "mode") player_needed = self.check_missing_param(query_params, "player_needed") - room_id = query_params.get("room_id", [None])[0] - self.host = room_id is None - self.room_id = room_id or str(uuid.uuid4()) + self.room_id = self.check_missing_param(query_params, "room_id") + self.host = self.check_missing_param(query_params, "host") == "True" self.group_name = f"pong_{self.room_id}" self.pad_n = "pad_1" if self.host else "pad_2" @@ -116,10 +114,6 @@ async def game_join(self, event): self.info.players.append(event["content"]["user"]) if len(self.info.players) == self.info.player_needed: await self.redis.set(self.room_id, 1) - await self.send_message("group", "game_info", self.info.__dict__) - - async def game_info(self, event): - await self.send_message("client", args=event) async def game_ready(self, event): if self.host: diff --git a/django/src/games/templates/pong.html b/django/src/games/templates/pong.html index 2334fd7e..972ae7be 100644 --- a/django/src/games/templates/pong.html +++ b/django/src/games/templates/pong.html @@ -1,278 +1,61 @@ {% extends "./extends/base.html" %} {% block content %} -
-
- - +
+
+ +
-
- - +
+ Play with a Friend + Play with an AI
-
- - - +
+ Create a Room + + Join a Tournament
-
-
- - +
+ + +
-
-
-
- -
- - - +
{% endblock %} diff --git a/django/src/games/templates/pong_game.html b/django/src/games/templates/pong_game.html new file mode 100644 index 00000000..19c00197 --- /dev/null +++ b/django/src/games/templates/pong_game.html @@ -0,0 +1,196 @@ +{% extends "./extends/base.html" %} + +{% block content %} +
+
+

Roomd Id: {{room_id}}

+ +
+ + + +
+ + +{% endblock %} diff --git a/django/src/games/tests.py b/django/src/games/tests.py index 1365bb1d..709fc22f 100644 --- a/django/src/games/tests.py +++ b/django/src/games/tests.py @@ -39,15 +39,13 @@ async def test_websocket_connect(self): Test if the websocket consumer connects successfully. """ user = await sync_to_async(get_user_model().objects.create_user)(username='testuser', password='password123') - communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1") + communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1&host=True&room_id=1") communicator.scope['user'] = user connected, _ = await communicator.connect() self.assertTrue(connected) response = await communicator.receive_json_from() self.assertEqual(response["type"], "game_pad") - response = await communicator.receive_json_from() - self.assertEqual(response["type"], "game_info") await communicator.disconnect() @@ -56,15 +54,13 @@ async def test_invalid_message(self): Test if the consumer handles invalid messages properly. """ user = await sync_to_async(get_user_model().objects.create_user)(username='testuser', password='password123') - communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1") + communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1&host=True&room_id=1") communicator.scope['user'] = user connected, _ = await communicator.connect() self.assertTrue(connected) response = await communicator.receive_json_from() self.assertEqual(response["type"], "game_pad") - response = await communicator.receive_json_from() - self.assertEqual(response["type"], "game_info") await communicator.send_json_to({}) response = await communicator.receive_json_from() @@ -88,15 +84,13 @@ async def test_websocket_receive_game_ready(self): Test if the consumer can send and receive messages, such as 'game_ready'. """ user = await sync_to_async(get_user_model().objects.create_user)(username='testuser', password='password123') - communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1") + communicator = WebsocketCommunicator(Consumer.as_asgi(), "/ws/pong/?mode=local&player_needed=1&host=True&room_id=1") communicator.scope['user'] = user connected, _ = await communicator.connect() self.assertTrue(connected) response = await communicator.receive_json_from() self.assertEqual(response["type"], "game_pad") - response = await communicator.receive_json_from() - self.assertEqual(response["type"], "game_info") await communicator.send_json_to({"type": "game_ready", "content": {}}) response = await communicator.receive_json_from() diff --git a/django/src/pages/urls.py b/django/src/pages/urls.py index afca1a70..b6ce2471 100644 --- a/django/src/pages/urls.py +++ b/django/src/pages/urls.py @@ -4,6 +4,9 @@ urlpatterns = [ path('index/', views.index), path('pong/', views.pong), + path('pong/local/', views.pong_local), + path('pong/online/', views.pong_online), + path('pong/online//', views.pong_online), path('login/', views.login), path('register/', views.register), path('authorize/', views.authorize), diff --git a/django/src/pages/views.py b/django/src/pages/views.py index 2e085598..8d42820d 100644 --- a/django/src/pages/views.py +++ b/django/src/pages/views.py @@ -1,3 +1,4 @@ +from uuid import uuid4 from django.views.decorators.http import require_GET from django.http import JsonResponse, HttpRequest from django.template.loader import get_template @@ -27,6 +28,34 @@ def index(request): def pong(request): return create_response(request, 'pong.html', title="Pong", need_authentication=True) +@require_GET +def pong_local(request): + return create_response( + request=request, + template_name='pong_game.html', + title="Local Pong", + context={ + "mode": "local", + "room_id": str(uuid4()), + "host": True, + }, + need_authentication=True, + ) + +@require_GET +def pong_online(request, id=None): + return create_response( + request=request, + template_name='pong_game.html', + title="Local Pong", + context={ + "mode": "online", + "room_id": str(uuid4()) if id == None else id, + "host": True if id == None else False, + }, + need_authentication=True, + ) + @require_GET def login(request: HttpRequest): if (request.user.is_authenticated):