diff --git a/chess_backend/settings.py b/chess_backend/settings.py index 2e204bc..f9604f8 100644 --- a/chess_backend/settings.py +++ b/chess_backend/settings.py @@ -12,16 +12,16 @@ DEBUG = True ALLOWED_HOSTS = [] INSTALLED_APPS = [ + "django.contrib.sessions", "django.contrib.auth", "django.contrib.contenttypes", "django.contrib.messages", - "django.contrib.staticfiles", # Packages "rest_framework", "channels", # Apps "game", - "room" + "room", ] MIDDLEWARE = [ @@ -45,6 +45,7 @@ CHANNEL_LAYERS = { }, } + WSGI_APPLICATION = "chess_backend.wsgi.application" diff --git a/common/generators.py b/common/generators.py index b81f420..cd01384 100644 --- a/common/generators.py +++ b/common/generators.py @@ -1,8 +1,14 @@ import string import secrets +from random import randint + def generate_charset(length: int): return "".join( secrets.choice(string.digits + string.ascii_letters) for _ in range(length) ) + + +def gen_ton(): + return "".join([str(randint(0, 9)) for _ in range(48)]) diff --git a/game/admin.py b/game/admin.py new file mode 100644 index 0000000..b8635f5 --- /dev/null +++ b/game/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin + +# Register your models here. +from game.models import HeroImageSet + +admin.site.register(HeroImageSet) diff --git a/game/api/v1/views.py b/game/api/v1/views.py index f83cb59..6d6222a 100644 --- a/game/api/v1/views.py +++ b/game/api/v1/views.py @@ -80,8 +80,8 @@ class PlayerCreateView(GenericAPIView, CreateModelMixin): serializer.is_valid(raise_exception=True) instance = self.perform_create(serializer) - access_jwt = sign_jwt({"id": instance.id, "type": "access"}, t_life=3600) - refresh_jwt = sign_jwt({"jit": instance.get_auth_session(), "type": "refresh"}) + access_jwt = instance.get_access_token() + refresh_jwt = instance.get_refresh_token() return Response( { "access_token": access_jwt, diff --git a/game/models.py b/game/models.py index 10bbabe..c640ae3 100644 --- a/game/models.py +++ b/game/models.py @@ -12,6 +12,7 @@ from django.db import models from django.conf import settings from common.generators import generate_charset +from game.services.jwt import sign_jwt HER0_TYPES = [ ("WIZARD", "wizard"), @@ -66,6 +67,12 @@ class Player(models.Model): def get_auth_session(self): return PlayerAuthSession.objects.get(player=self).jit + def get_refresh_token(self): + return sign_jwt({"jit": self.get_auth_session(), "type": "refresh"}) + + def get_access_token(self): + return sign_jwt({"id": self.id, "type": "access"}, t_life=3600) + def __str__(self): return self.name diff --git a/room/consumers.py b/room/consumers.py index c63a224..a672e8e 100644 --- a/room/consumers.py +++ b/room/consumers.py @@ -5,9 +5,11 @@ from channels.generic.websocket import AsyncWebsocketConsumer from channels.layers import get_channel_layer from game.models import Deck -from room.models import PlayerInQueue +from room.models import PlayerInQueue, Room, PlayerInRoom from room.services.room_create import create_room +channel_layer = get_channel_layer() + class QueueConsumer(AsyncWebsocketConsumer): def __init__(self, *args, **kwargs): @@ -91,7 +93,6 @@ class QueueConsumer(AsyncWebsocketConsumer): ) else: # add to group and send message that opponent found to players - channel_layer = get_channel_layer() room = await create_room( deck_id_1=self.scope["deck"], player_id_1=self.scope["player"], @@ -201,13 +202,39 @@ class RoomConsumer(AsyncWebsocketConsumer): self.room_name = None async def connect(self): - self.room_name = self.scope["url_route"]["kwargs"]["room_name"] - self.room_group_name = f"room_{self.room_name}" + await self.accept() + + if not await self.connect_to_room(): + await self.close() # Join room group await self.channel_layer.group_add(self.room_group_name, self.channel_name) - await self.accept() + @sync_to_async + def connect_to_room(self): + slug = self.scope["url_route"]["kwargs"]["room_name"] + + self.room_name = slug + self.room_group_name = f"room_{slug}" + room = Room.objects.filter(slug=slug) + + if not room: + return False + + self.scope["room"] = room + + # check if player can be in a room + if self.scope["player"] not in [x.id for x in room.first().players.all()]: + return False + + # add player info to scope + player = PlayerInRoom.objects.get(player_id=self.scope["player"]) + + self.scope["first"] = player.first + self.scope["score"] = player.score + self.scope["deck"] = player.deck.id + + return True async def disconnect(self, close_code): # Leave room group @@ -221,6 +248,12 @@ class RoomConsumer(AsyncWebsocketConsumer): {"type": "chat_message", "message": text_data}, ) + # info type message handler + async def info(self, event): + message = event["message"] + + await self.send(text_data=json.dumps({"type": "INFO", "message": message})) + # Receive message from room group async def chat_message(self, event): message = event["message"] diff --git a/room/routing.py b/room/routing.py index 9a299c0..db78ff6 100644 --- a/room/routing.py +++ b/room/routing.py @@ -4,5 +4,5 @@ from . import consumers websocket_urlpatterns = [ path("room/", consumers.QueueConsumer.as_asgi()), - path("room//", consumers.RoomConsumer.as_asgi()), + path("room/", consumers.RoomConsumer.as_asgi()), ]