-
-
-
+
-
{% 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):