mirror of
https://github.com/evgen-app/chess_rpg_backend.git
synced 2024-11-10 19:57:12 +03:00
added connection to room algorithm, minor changes for player model
This commit is contained in:
parent
7a14089d10
commit
e1c8b2fb74
|
@ -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"
|
||||
|
||||
|
||||
|
|
|
@ -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)])
|
||||
|
|
6
game/admin.py
Normal file
6
game/admin.py
Normal file
|
@ -0,0 +1,6 @@
|
|||
from django.contrib import admin
|
||||
|
||||
# Register your models here.
|
||||
from game.models import HeroImageSet
|
||||
|
||||
admin.site.register(HeroImageSet)
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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"]
|
||||
|
|
|
@ -4,5 +4,5 @@ from . import consumers
|
|||
|
||||
websocket_urlpatterns = [
|
||||
path("room/", consumers.QueueConsumer.as_asgi()),
|
||||
path("room/<str:room_name>/", consumers.RoomConsumer.as_asgi()),
|
||||
path("room/<str:room_name>", consumers.RoomConsumer.as_asgi()),
|
||||
]
|
||||
|
|
Loading…
Reference in New Issue
Block a user