mirror of
https://github.com/evgen-app/chess_rpg_backend.git
synced 2024-11-22 01:27:00 +03:00
added room create for match
This commit is contained in:
parent
e188de29a0
commit
7a14089d10
|
@ -1,11 +1,12 @@
|
|||
import json
|
||||
|
||||
from asgiref.sync import sync_to_async, async_to_sync
|
||||
from asgiref.sync import sync_to_async
|
||||
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.services.room_create import create_room
|
||||
|
||||
|
||||
class QueueConsumer(AsyncWebsocketConsumer):
|
||||
|
@ -91,12 +92,21 @@ 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"],
|
||||
player_score_1=self.scope["score"],
|
||||
deck_id_2=opponent[2],
|
||||
player_id_2=opponent[3],
|
||||
player_score_2=opponent[1],
|
||||
)
|
||||
|
||||
await channel_layer.send(
|
||||
opponent[0],
|
||||
{
|
||||
"type": "info",
|
||||
"message": f"user found, with score {self.scope['score']}",
|
||||
"room": room,
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -105,6 +115,7 @@ class QueueConsumer(AsyncWebsocketConsumer):
|
|||
{
|
||||
"type": "INFO",
|
||||
"message": f"user found, with score {opponent[1]}",
|
||||
"room": room,
|
||||
}
|
||||
)
|
||||
)
|
||||
|
@ -132,7 +143,7 @@ class QueueConsumer(AsyncWebsocketConsumer):
|
|||
for el in PlayerInQueue.objects.all():
|
||||
if el.player_id != self.scope["player"]:
|
||||
if s_min <= el.score <= s_max:
|
||||
return el.channel_name, el.score
|
||||
return el.channel_name, el.score, el.deck.id, el.player.id
|
||||
return False
|
||||
|
||||
@sync_to_async
|
||||
|
@ -156,16 +167,22 @@ class QueueConsumer(AsyncWebsocketConsumer):
|
|||
except PlayerInQueue.DoesNotExist:
|
||||
queue = PlayerInQueue.objects.create(
|
||||
player_id=self.scope["player"],
|
||||
deck=deck,
|
||||
score=deck.score(),
|
||||
channel_name=self.channel_name,
|
||||
)
|
||||
|
||||
self.scope["queue"] = queue.id
|
||||
self.scope["deck"] = deck.id
|
||||
self.scope["score"] = queue.score
|
||||
|
||||
async def info(self, event):
|
||||
message = event["message"]
|
||||
await self.send(text_data=json.dumps({"type": "INFO", "message": message}))
|
||||
msg = {"type": "INFO", "message": message}
|
||||
if "room" in event:
|
||||
msg["room"] = event["room"]
|
||||
|
||||
await self.send(text_data=json.dumps(msg))
|
||||
|
||||
async def check_origin(self):
|
||||
if not self.scope["player"]:
|
||||
|
|
|
@ -1,14 +1,35 @@
|
|||
from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
from game.models import Player
|
||||
from game.models import Player, Deck
|
||||
|
||||
|
||||
class PlayerInQueue(models.Model):
|
||||
# TODO use redis for storing
|
||||
player = models.OneToOneField(Player, unique=True, on_delete=models.CASCADE)
|
||||
score = models.IntegerField()
|
||||
channel_name = models.CharField(max_length=50, blank=False)
|
||||
deck = models.ForeignKey(Deck, on_delete=models.CASCADE)
|
||||
score = models.IntegerField()
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.player.name} in que with score {self.score}"
|
||||
|
||||
|
||||
class Room(models.Model):
|
||||
slug = models.SlugField(max_length=16, unique=True)
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
ended = models.BooleanField(default=False)
|
||||
|
||||
def __str__(self):
|
||||
return f"room with slug {self.slug}"
|
||||
|
||||
|
||||
class PlayerInRoom(models.Model):
|
||||
player = models.OneToOneField(Player, unique=True, on_delete=models.CASCADE)
|
||||
room = models.ForeignKey(Room, on_delete=models.CASCADE, related_name="players")
|
||||
first = models.BooleanField()
|
||||
score = models.IntegerField(blank=False)
|
||||
deck = models.ForeignKey(Deck, on_delete=models.CASCADE, related_name="decks")
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.player.name} in room {self.room.slug}"
|
||||
|
|
0
room/services/__init__.py
Normal file
0
room/services/__init__.py
Normal file
39
room/services/room_create.py
Normal file
39
room/services/room_create.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
from asgiref.sync import sync_to_async
|
||||
from random import randint
|
||||
|
||||
from common.generators import generate_charset
|
||||
from game.models import Player
|
||||
from room.models import Room, PlayerInRoom
|
||||
|
||||
|
||||
@sync_to_async
|
||||
def create_room(
|
||||
deck_id_1: int,
|
||||
player_id_1: int,
|
||||
player_score_1: int,
|
||||
deck_id_2: int,
|
||||
player_id_2: int,
|
||||
player_score_2: int,
|
||||
) -> str:
|
||||
room = Room.objects.create(slug=generate_charset(16))
|
||||
player_1 = Player.objects.get(id=player_id_1)
|
||||
player_2 = Player.objects.get(id=player_id_2)
|
||||
|
||||
first_player = randint(1, 2)
|
||||
|
||||
PlayerInRoom.objects.create(
|
||||
player=player_1,
|
||||
room=room,
|
||||
score=player_score_1,
|
||||
deck_id=deck_id_1,
|
||||
first=first_player == 1,
|
||||
)
|
||||
|
||||
PlayerInRoom.objects.create(
|
||||
player=player_2,
|
||||
room=room,
|
||||
score=player_score_2,
|
||||
deck_id=deck_id_2,
|
||||
first=first_player == 2,
|
||||
)
|
||||
return room.slug
|
Loading…
Reference in New Issue
Block a user