From 231622a2c23ada428483bd37fda1fe1a7f3d6329 Mon Sep 17 00:00:00 2001 From: starnakin Date: Thu, 28 Dec 2023 12:02:39 +0100 Subject: [PATCH] game: init: consumer --- games/consumers.py | 42 ++++++++++++++++++++++++++++++++++++++++++ games/models.py | 8 +++++++- games/routing.py | 6 ++++++ tournament/routing.py | 2 +- transcendence/asgi.py | 4 +++- 5 files changed, 59 insertions(+), 3 deletions(-) create mode 100644 games/consumers.py create mode 100644 games/routing.py diff --git a/games/consumers.py b/games/consumers.py new file mode 100644 index 0000000..473e2da --- /dev/null +++ b/games/consumers.py @@ -0,0 +1,42 @@ +from channels.generic.websocket import AsyncWebsocketConsumer + +from django.contrib.auth.models import User + +from games.models import GameModel + +import json + +from .models import game_room_manager + +class GameWebSocket(AsyncWebsocketConsumer): + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.channel_name = "games" + self.group_name = "games" + + def connect(self): + + self.user: User = self.scope["user"] + if (self.user.is_anonymous or not self.user.is_authenticated): + return + + self.channel_layer.group_add(self.group_name, self.channel_name) + + self.game_id = int(self.scope['url_route']['kwargs']['game_id']) + + self.room = game_room_manager.get(self.game_id) + + if (self.room is None): + self.member.send("Tournament not found") + self.disconnect(1017) + + self.room.append(self.member) + + def receive(self, text_data: str = None, bytes_data: bytes = None): + self.member.receive(text_data, bytes_data) + + def disconnect(self, close_code): + member = self.room.get_member_by_socket(self) + if (member is not None): + self.room.remove(self.member, close_code) \ No newline at end of file diff --git a/games/models.py b/games/models.py index 36d5394..a504972 100644 --- a/games/models.py +++ b/games/models.py @@ -1,5 +1,9 @@ from django.db import models +from .objects.GameRoomManager import GameRoomManager + +from channels.generic.websocket import AsyncWebsocketConsumer + # Create your models here. class GameModel(models.Model): @@ -18,4 +22,6 @@ class GameModel(models.Model): class GameMembersModel(models.Model): game_id = models.IntegerField() - player_id = models.IntegerField() \ No newline at end of file + player_id = models.IntegerField() + +game_room_manager: GameRoomManager = GameRoomManager() \ No newline at end of file diff --git a/games/routing.py b/games/routing.py new file mode 100644 index 0000000..7271972 --- /dev/null +++ b/games/routing.py @@ -0,0 +1,6 @@ +from django.urls import re_path +from . import consumers + +websocket_urlpatterns = [ + re_path(r'ws/tournaments/(?P\d+)$', consumers.TournamentWebConsumer.as_asgi()) +] diff --git a/tournament/routing.py b/tournament/routing.py index 7271972..eed3b1c 100644 --- a/tournament/routing.py +++ b/tournament/routing.py @@ -2,5 +2,5 @@ from django.urls import re_path from . import consumers websocket_urlpatterns = [ - re_path(r'ws/tournaments/(?P\d+)$', consumers.TournamentWebConsumer.as_asgi()) + re_path(r'ws/games/(?P\d+)$', consumers.GameWebSocket.as_asgi()) ] diff --git a/transcendence/asgi.py b/transcendence/asgi.py index 939a8ed..cb8ac86 100644 --- a/transcendence/asgi.py +++ b/transcendence/asgi.py @@ -14,6 +14,7 @@ from channels.auth import AuthMiddlewareStack import chat.routing import matchmaking.routing import tournament.routing +import games.routing from django.core.asgi import get_asgi_application @@ -25,7 +26,8 @@ application = ProtocolTypeRouter({ URLRouter( chat.routing.websocket_urlpatterns + matchmaking.routing.websocket_urlpatterns + - tournament.routing.websocket_urlpatterns + tournament.routing.websocket_urlpatterns + + games.routing.websocket_urlpatterns ) ) })