game: init: consumer
This commit is contained in:
parent
f11af8336a
commit
231622a2c2
42
games/consumers.py
Normal file
42
games/consumers.py
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||||
|
|
||||||
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
from games.models import GameModel
|
||||||
|
|
||||||
|
import json
|
||||||
|
|
||||||
|
from .models import game_room_manager
|
||||||
|
|
||||||
|
class GameWebSocket(AsyncWebsocketConsumer):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.channel_name = "games"
|
||||||
|
self.group_name = "games"
|
||||||
|
|
||||||
|
def connect(self):
|
||||||
|
|
||||||
|
self.user: User = self.scope["user"]
|
||||||
|
if (self.user.is_anonymous or not self.user.is_authenticated):
|
||||||
|
return
|
||||||
|
|
||||||
|
self.channel_layer.group_add(self.group_name, self.channel_name)
|
||||||
|
|
||||||
|
self.game_id = int(self.scope['url_route']['kwargs']['game_id'])
|
||||||
|
|
||||||
|
self.room = game_room_manager.get(self.game_id)
|
||||||
|
|
||||||
|
if (self.room is None):
|
||||||
|
self.member.send("Tournament not found")
|
||||||
|
self.disconnect(1017)
|
||||||
|
|
||||||
|
self.room.append(self.member)
|
||||||
|
|
||||||
|
def receive(self, text_data: str = None, bytes_data: bytes = None):
|
||||||
|
self.member.receive(text_data, bytes_data)
|
||||||
|
|
||||||
|
def disconnect(self, close_code):
|
||||||
|
member = self.room.get_member_by_socket(self)
|
||||||
|
if (member is not None):
|
||||||
|
self.room.remove(self.member, close_code)
|
@ -1,5 +1,9 @@
|
|||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
from .objects.GameRoomManager import GameRoomManager
|
||||||
|
|
||||||
|
from channels.generic.websocket import AsyncWebsocketConsumer
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
class GameModel(models.Model):
|
class GameModel(models.Model):
|
||||||
|
|
||||||
@ -18,4 +22,6 @@ class GameModel(models.Model):
|
|||||||
|
|
||||||
class GameMembersModel(models.Model):
|
class GameMembersModel(models.Model):
|
||||||
game_id = models.IntegerField()
|
game_id = models.IntegerField()
|
||||||
player_id = models.IntegerField()
|
player_id = models.IntegerField()
|
||||||
|
|
||||||
|
game_room_manager: GameRoomManager = GameRoomManager()
|
6
games/routing.py
Normal file
6
games/routing.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from django.urls import re_path
|
||||||
|
from . import consumers
|
||||||
|
|
||||||
|
websocket_urlpatterns = [
|
||||||
|
re_path(r'ws/tournaments/(?P<tournament_id>\d+)$', consumers.TournamentWebConsumer.as_asgi())
|
||||||
|
]
|
@ -2,5 +2,5 @@ from django.urls import re_path
|
|||||||
from . import consumers
|
from . import consumers
|
||||||
|
|
||||||
websocket_urlpatterns = [
|
websocket_urlpatterns = [
|
||||||
re_path(r'ws/tournaments/(?P<tournament_id>\d+)$', consumers.TournamentWebConsumer.as_asgi())
|
re_path(r'ws/games/(?P<game_id>\d+)$', consumers.GameWebSocket.as_asgi())
|
||||||
]
|
]
|
||||||
|
@ -14,6 +14,7 @@ from channels.auth import AuthMiddlewareStack
|
|||||||
import chat.routing
|
import chat.routing
|
||||||
import matchmaking.routing
|
import matchmaking.routing
|
||||||
import tournament.routing
|
import tournament.routing
|
||||||
|
import games.routing
|
||||||
|
|
||||||
from django.core.asgi import get_asgi_application
|
from django.core.asgi import get_asgi_application
|
||||||
|
|
||||||
@ -25,7 +26,8 @@ application = ProtocolTypeRouter({
|
|||||||
URLRouter(
|
URLRouter(
|
||||||
chat.routing.websocket_urlpatterns +
|
chat.routing.websocket_urlpatterns +
|
||||||
matchmaking.routing.websocket_urlpatterns +
|
matchmaking.routing.websocket_urlpatterns +
|
||||||
tournament.routing.websocket_urlpatterns
|
tournament.routing.websocket_urlpatterns +
|
||||||
|
games.routing.websocket_urlpatterns
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user