added message in room on ready to play

This commit is contained in:
Alexander Karpov 2022-06-26 19:00:53 +03:00
parent 46344ad776
commit 7242992b42
2 changed files with 48 additions and 7 deletions

View File

@ -102,7 +102,7 @@ class QueueConsumer(AsyncWebsocketConsumer):
player_score_2=opponent[1], player_score_2=opponent[1],
) )
await channel_layer.send( await self.channel_layer.send(
opponent[0], opponent[0],
{ {
"type": "info", "type": "info",
@ -219,6 +219,14 @@ class RoomConsumer(AsyncWebsocketConsumer):
}, },
) )
) )
if self.scope["opponent_channel"]:
await self.channel_layer.send(
self.scope["opponent_channel"],
{
"type": "channel",
"channel": self.channel_name,
},
)
# Join room group # Join room group
await self.channel_layer.group_add(self.room_group_name, self.channel_name) await self.channel_layer.group_add(self.room_group_name, self.channel_name)
@ -253,13 +261,15 @@ class RoomConsumer(AsyncWebsocketConsumer):
opponent = PlayerInRoom.objects.get(player_id=p_ids[0]) opponent = PlayerInRoom.objects.get(player_id=p_ids[0])
self.scope["opponent"] = opponent.player.id self.scope["opponent"] = opponent.player.id
self.scope["opponent_channel"] = opponent.channel_name
self.scope["opponent_score"] = opponent.score self.scope["opponent_score"] = opponent.score
self.scope["opponent_deck"] = opponent.deck.id self.scope["opponent_deck"] = opponent.deck.id
self.scope["opponent_first"] = opponent.first self.scope["opponent_first"] = opponent.first
self.scope["opponent_online"] = opponent.online self.scope["opponent_online"] = opponent.online
player.online = True player.online = True
player.save(update_fields=["online"]) player.channel_name = self.channel_name
player.save(update_fields=["online", "channel_name"])
return True return True
async def disconnect(self, close_code): async def disconnect(self, close_code):
@ -275,11 +285,37 @@ class RoomConsumer(AsyncWebsocketConsumer):
# Receive message from WebSocket # Receive message from WebSocket
async def receive(self, text_data): async def receive(self, text_data):
# Send message to room group data = None
await self.channel_layer.group_send(
self.room_group_name, try:
{"type": "chat_message", "message": text_data}, data = json.loads(text_data)
except ValueError:
await self.send(
text_data=json.dumps(
{"type": "ERROR", "message": "data is not JSON serializable"}
) )
)
if data:
if data["type"] == "start":
if not await self.start(data):
await self.send(
text_data=json.dumps(
{"type": "ERROR", "message": "opponent is offline"}
)
)
async def start(self, data):
if self.scope["opponent_channel"] and self.scope["opponent_online"]:
await self.channel_layer.send(
self.scope["opponent_channel"],
{
"type": "info",
"message": "opponent is ready to start",
},
)
return True
return False
# info type group message handler # info type group message handler
async def info(self, event): async def info(self, event):
@ -306,3 +342,7 @@ class RoomConsumer(AsyncWebsocketConsumer):
# Send message to WebSocket # Send message to WebSocket
await self.send(text_data=json.dumps({"lot": message})) await self.send(text_data=json.dumps({"lot": message}))
async def channel(self, event):
channel = event["channel"]
self.scope["opponent_channel"] = channel

View File

@ -31,6 +31,7 @@ class PlayerInRoom(models.Model):
score = models.IntegerField(blank=False) score = models.IntegerField(blank=False)
deck = models.ForeignKey(Deck, on_delete=models.CASCADE, related_name="decks") deck = models.ForeignKey(Deck, on_delete=models.CASCADE, related_name="decks")
online = models.BooleanField(default=False) online = models.BooleanField(default=False)
channel_name = models.CharField(max_length=50, blank=True, null=True)
def __str__(self): def __str__(self):
return f"{self.player.name} in room {self.room.slug}" return f"{self.player.name} in room {self.room.slug}"