2022-06-21 11:36:55 +03:00
|
|
|
import json
|
2022-06-21 20:26:32 +03:00
|
|
|
|
2022-06-24 01:04:35 +03:00
|
|
|
from asgiref.sync import sync_to_async
|
2022-06-21 11:36:55 +03:00
|
|
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
2022-06-23 14:45:34 +03:00
|
|
|
from channels.layers import get_channel_layer
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
from game.models import Deck
|
2022-06-25 12:36:47 +03:00
|
|
|
from room.models import PlayerInQueue, Room, PlayerInRoom
|
2022-06-24 01:04:35 +03:00
|
|
|
from room.services.room_create import create_room
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-25 12:36:47 +03:00
|
|
|
channel_layer = get_channel_layer()
|
|
|
|
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
class QueueConsumer(AsyncWebsocketConsumer):
|
2022-06-23 14:45:34 +03:00
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(args, kwargs)
|
|
|
|
self.room_group_name = None
|
|
|
|
|
2022-06-21 11:36:55 +03:00
|
|
|
async def connect(self):
|
2022-06-21 20:26:32 +03:00
|
|
|
self.room_group_name = "queue"
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
await self.accept()
|
2022-06-21 20:26:32 +03:00
|
|
|
await self.check_origin()
|
|
|
|
|
|
|
|
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
async def disconnect(self, close_code):
|
2022-06-23 14:45:34 +03:00
|
|
|
await self.delete_user_in_queue()
|
2022-06-21 11:36:55 +03:00
|
|
|
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
|
|
|
|
|
|
|
|
# Receive message from WebSocket
|
|
|
|
async def receive(self, text_data):
|
2022-06-21 20:26:32 +03:00
|
|
|
data = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = json.loads(text_data)
|
|
|
|
except ValueError:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "data is not JSON serializable"}
|
|
|
|
)
|
|
|
|
)
|
2022-06-22 00:11:52 +03:00
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
if data:
|
|
|
|
# TODO move to external function/class
|
|
|
|
if "type" not in data:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "incorrect data typing"}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
if data["type"] == "connect":
|
|
|
|
if "deck_id" not in data:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "deck id is not provided"}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
deck = None
|
|
|
|
# validate deck and check user originality
|
|
|
|
try:
|
|
|
|
deck_id = int(data["deck_id"])
|
|
|
|
deck = await self.check_user_deck(deck_id)
|
|
|
|
except ValueError:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "deck id is incorrect"}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if deck:
|
2022-06-23 14:45:34 +03:00
|
|
|
# add to que, start finding players
|
2022-06-21 20:26:32 +03:00
|
|
|
await self.queue_connector(deck)
|
2022-06-22 00:11:52 +03:00
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"type": "INFO",
|
|
|
|
"message": f"added to queue deck with score {self.scope['score']}",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2022-06-23 14:45:34 +03:00
|
|
|
opponent = await self.find_user_by_score()
|
|
|
|
|
|
|
|
if not opponent:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"type": "INFO",
|
|
|
|
"message": "no user found, awaiting in queue",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
# add to group and send message that opponent found to players
|
2022-06-24 01:04:35 +03:00
|
|
|
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],
|
|
|
|
)
|
2022-06-23 14:45:34 +03:00
|
|
|
|
2022-06-26 19:00:53 +03:00
|
|
|
await self.channel_layer.send(
|
2022-06-23 14:45:34 +03:00
|
|
|
opponent[0],
|
|
|
|
{
|
|
|
|
"type": "info",
|
|
|
|
"message": f"user found, with score {self.scope['score']}",
|
2022-06-24 01:04:35 +03:00
|
|
|
"room": room,
|
2022-06-23 14:45:34 +03:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"type": "INFO",
|
|
|
|
"message": f"user found, with score {opponent[1]}",
|
2022-06-24 01:04:35 +03:00
|
|
|
"room": room,
|
2022-06-23 14:45:34 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2022-06-21 20:26:32 +03:00
|
|
|
else:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"type": "ERROR",
|
|
|
|
"message": "such deck doesn't exist",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2022-06-23 14:45:34 +03:00
|
|
|
@sync_to_async
|
|
|
|
def delete_user_in_queue(self):
|
|
|
|
try:
|
|
|
|
PlayerInQueue.objects.get(player_id=self.scope["player"]).delete()
|
|
|
|
except PlayerInQueue.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@sync_to_async
|
|
|
|
def find_user_by_score(self):
|
|
|
|
s_min = self.scope["score"] * 0.95
|
|
|
|
s_max = self.scope["score"] * 1.05
|
|
|
|
for el in PlayerInQueue.objects.all():
|
|
|
|
if el.player_id != self.scope["player"]:
|
|
|
|
if s_min <= el.score <= s_max:
|
2022-06-24 01:04:35 +03:00
|
|
|
return el.channel_name, el.score, el.deck.id, el.player.id
|
2022-06-23 14:45:34 +03:00
|
|
|
return False
|
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
@sync_to_async
|
|
|
|
def check_user_deck(self, deck_id: int):
|
|
|
|
try:
|
|
|
|
deck = Deck.objects.get(id=deck_id)
|
|
|
|
if deck.player.id != self.scope["player"]:
|
|
|
|
return False
|
|
|
|
return deck
|
|
|
|
except Deck.DoesNotExist:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@sync_to_async
|
|
|
|
def queue_connector(self, deck):
|
|
|
|
try:
|
2022-06-22 00:11:52 +03:00
|
|
|
queue = PlayerInQueue.objects.get(player_id=self.scope["player"])
|
|
|
|
queue.score = deck.score()
|
2022-06-23 14:45:34 +03:00
|
|
|
queue.channel_name = self.channel_name
|
|
|
|
queue.save()
|
|
|
|
|
2022-06-21 20:26:32 +03:00
|
|
|
except PlayerInQueue.DoesNotExist:
|
|
|
|
queue = PlayerInQueue.objects.create(
|
2022-06-23 14:45:34 +03:00
|
|
|
player_id=self.scope["player"],
|
2022-06-24 01:04:35 +03:00
|
|
|
deck=deck,
|
2022-06-23 14:45:34 +03:00
|
|
|
score=deck.score(),
|
|
|
|
channel_name=self.channel_name,
|
2022-06-21 20:26:32 +03:00
|
|
|
)
|
|
|
|
|
2022-06-22 00:11:52 +03:00
|
|
|
self.scope["queue"] = queue.id
|
2022-06-24 01:04:35 +03:00
|
|
|
self.scope["deck"] = deck.id
|
2022-06-22 00:11:52 +03:00
|
|
|
self.scope["score"] = queue.score
|
2022-06-21 20:26:32 +03:00
|
|
|
|
2022-06-23 14:45:34 +03:00
|
|
|
async def info(self, event):
|
|
|
|
message = event["message"]
|
2022-06-24 01:04:35 +03:00
|
|
|
msg = {"type": "INFO", "message": message}
|
|
|
|
if "room" in event:
|
|
|
|
msg["room"] = event["room"]
|
|
|
|
|
|
|
|
await self.send(text_data=json.dumps(msg))
|
2022-06-21 20:26:32 +03:00
|
|
|
|
|
|
|
async def check_origin(self):
|
|
|
|
if not self.scope["player"]:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "token is incorrect or expired"}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
await self.close()
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
|
|
|
|
class RoomConsumer(AsyncWebsocketConsumer):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.room_group_name = None
|
|
|
|
self.room_name = None
|
|
|
|
|
|
|
|
async def connect(self):
|
2022-06-25 12:36:47 +03:00
|
|
|
await self.accept()
|
2022-06-26 19:23:18 +03:00
|
|
|
await self.check_origin()
|
2022-06-25 12:36:47 +03:00
|
|
|
|
|
|
|
if not await self.connect_to_room():
|
|
|
|
await self.close()
|
2022-06-26 18:04:09 +03:00
|
|
|
else:
|
|
|
|
await self.send(
|
|
|
|
json.dumps(
|
|
|
|
{
|
|
|
|
"type": "INFO",
|
|
|
|
"message": f"welcome to room {self.room_name}",
|
|
|
|
"opponent_score": self.scope["opponent_score"],
|
|
|
|
"opponent_deck": self.scope["opponent_deck"],
|
|
|
|
"opponent_online": self.scope["opponent_online"],
|
|
|
|
"first": self.scope["first"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
2022-06-26 19:23:18 +03:00
|
|
|
if "opponent_channel" in self.scope and self.scope["opponent_channel"]:
|
2022-06-26 19:00:53 +03:00
|
|
|
await self.channel_layer.send(
|
|
|
|
self.scope["opponent_channel"],
|
|
|
|
{
|
|
|
|
"type": "channel",
|
|
|
|
"channel": self.channel_name,
|
|
|
|
},
|
|
|
|
)
|
2022-06-26 19:23:18 +03:00
|
|
|
await self.channel_layer.send(
|
|
|
|
self.scope["opponent_channel"],
|
|
|
|
{"type": "connection_info", "online": True},
|
|
|
|
)
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-26 18:04:09 +03:00
|
|
|
# Join room group
|
|
|
|
await self.channel_layer.group_add(self.room_group_name, self.channel_name)
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-25 12:36:47 +03:00
|
|
|
@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
|
2022-06-26 18:04:09 +03:00
|
|
|
p_ids = [x.player.id for x in room.first().players.all()]
|
2022-06-26 13:34:10 +03:00
|
|
|
if self.scope["player"] not in p_ids:
|
2022-06-25 12:36:47 +03:00
|
|
|
return False
|
|
|
|
|
|
|
|
# add player info to scope
|
|
|
|
player = PlayerInRoom.objects.get(player_id=self.scope["player"])
|
|
|
|
|
2022-06-26 13:34:10 +03:00
|
|
|
self.scope["player_in_room"] = player
|
2022-06-25 12:36:47 +03:00
|
|
|
self.scope["first"] = player.first
|
|
|
|
self.scope["score"] = player.score
|
|
|
|
self.scope["deck"] = player.deck.id
|
|
|
|
|
2022-06-26 13:34:10 +03:00
|
|
|
p_ids.remove(player.player.id)
|
|
|
|
opponent = PlayerInRoom.objects.get(player_id=p_ids[0])
|
|
|
|
|
|
|
|
self.scope["opponent"] = opponent.player.id
|
2022-06-26 19:00:53 +03:00
|
|
|
self.scope["opponent_channel"] = opponent.channel_name
|
2022-06-26 13:34:10 +03:00
|
|
|
self.scope["opponent_score"] = opponent.score
|
|
|
|
self.scope["opponent_deck"] = opponent.deck.id
|
|
|
|
self.scope["opponent_first"] = opponent.first
|
|
|
|
self.scope["opponent_online"] = opponent.online
|
|
|
|
|
2022-06-26 18:04:09 +03:00
|
|
|
player.online = True
|
2022-06-26 19:00:53 +03:00
|
|
|
player.channel_name = self.channel_name
|
|
|
|
player.save(update_fields=["online", "channel_name"])
|
2022-06-25 12:36:47 +03:00
|
|
|
return True
|
2022-06-21 11:36:55 +03:00
|
|
|
|
|
|
|
async def disconnect(self, close_code):
|
|
|
|
# Leave room group
|
|
|
|
await self.channel_layer.group_discard(self.room_group_name, self.channel_name)
|
2022-06-26 18:04:09 +03:00
|
|
|
await self.disconnect_player()
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-26 19:23:18 +03:00
|
|
|
if "opponent_channel" in self.scope and self.scope["opponent_channel"]:
|
|
|
|
await self.channel_layer.send(
|
|
|
|
self.scope["opponent_channel"],
|
|
|
|
{"type": "connection_info", "online": False},
|
|
|
|
)
|
|
|
|
|
2022-06-26 13:34:10 +03:00
|
|
|
@sync_to_async
|
|
|
|
def disconnect_player(self):
|
2022-06-26 18:04:09 +03:00
|
|
|
if "player_in_room" in self.scope:
|
|
|
|
self.scope["player_in_room"].online = False
|
2022-06-26 19:23:18 +03:00
|
|
|
self.scope["player_in_room"].channel_name = None
|
|
|
|
self.scope["player_in_room"].save(update_fields=["online", "channel_name"])
|
2022-06-26 13:34:10 +03:00
|
|
|
|
2022-06-21 11:36:55 +03:00
|
|
|
# Receive message from WebSocket
|
|
|
|
async def receive(self, text_data):
|
2022-06-26 19:00:53 +03:00
|
|
|
data = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
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
|
2022-06-21 11:36:55 +03:00
|
|
|
|
2022-06-26 18:04:09 +03:00
|
|
|
# info type group message handler
|
2022-06-25 12:36:47 +03:00
|
|
|
async def info(self, event):
|
|
|
|
message = event["message"]
|
2022-06-26 13:34:10 +03:00
|
|
|
msg = {"type": "INFO", "message": message}
|
|
|
|
|
|
|
|
if "opponent_score" in event:
|
|
|
|
msg["opponent_score"] = event["opponent_score"]
|
|
|
|
|
|
|
|
if "opponent_deck" in event:
|
|
|
|
msg["opponent_deck"] = event["opponent_deck"]
|
|
|
|
|
|
|
|
if "opponent_online" in event:
|
|
|
|
msg["opponent_online"] = event["opponent_online"]
|
|
|
|
|
|
|
|
if "first" in event:
|
|
|
|
msg["first"] = event["first"]
|
2022-06-25 12:36:47 +03:00
|
|
|
|
2022-06-26 18:04:09 +03:00
|
|
|
await self.send(text_data=json.dumps(msg))
|
2022-06-25 12:36:47 +03:00
|
|
|
|
2022-06-21 11:36:55 +03:00
|
|
|
# Receive message from room group
|
|
|
|
async def chat_message(self, event):
|
|
|
|
message = event["message"]
|
|
|
|
|
|
|
|
# Send message to WebSocket
|
|
|
|
await self.send(text_data=json.dumps({"lot": message}))
|
2022-06-26 19:00:53 +03:00
|
|
|
|
|
|
|
async def channel(self, event):
|
|
|
|
channel = event["channel"]
|
|
|
|
self.scope["opponent_channel"] = channel
|
2022-06-26 19:23:18 +03:00
|
|
|
|
|
|
|
async def connection_info(self, event):
|
|
|
|
status = event["online"]
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{
|
|
|
|
"type": "INFO",
|
|
|
|
"message": "opponent is online"
|
|
|
|
if status
|
|
|
|
else "opponent is offline",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self.scope["opponent_online"] = status
|
|
|
|
|
|
|
|
async def check_origin(self):
|
|
|
|
if not self.scope["player"]:
|
|
|
|
await self.send(
|
|
|
|
text_data=json.dumps(
|
|
|
|
{"type": "ERROR", "message": "token is incorrect or expired"}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
await self.close()
|