Compare commits

...

3 Commits

Author SHA1 Message Date
231622a2c2 game: init: consumer 2023-12-28 12:02:39 +01:00
f11af8336a game: init: class 2023-12-28 12:01:52 +01:00
2e8eebc788 game: allow any to get game data 2023-12-28 11:34:18 +01:00
9 changed files with 104 additions and 4 deletions

42
games/consumers.py Normal file
View 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)

View File

@ -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()

View File

@ -0,0 +1,17 @@
from channels.generic.websocket import AsyncWebsocketConsumer
from transcendence.abstract.AbstractRoomMember import AbstractRoomMember
class GameMember(AbstractRoomMember):
def __init__(self, user_id: int, socket: AsyncWebsocketConsumer):
super().__init__(user_id, socket)
self.is_a_player = False
def receive(self, data: dict):
if (not self.is_a_player):
self.send("You are not a player.")
return
def send_ball(self, ball):
pass

View File

@ -0,0 +1,9 @@
from transcendence.abstract.AbstractRoom import AbstractRoom
from .GameRoomManager import GameRoomManager
class GameRoom(AbstractRoom):
def __init__(self, game_room_manager: GameRoomManager, game_id: int):
super().__init__(game_room_manager)
self.game_id = game_id

View File

@ -0,0 +1,18 @@
from transcendence.abstract.AbstractRoomManager import AbstractRoomManager
from ..models import GameModel
class GameRoomManager(AbstractRoomManager):
def get(self, game_id: int):
for room in self._room_list:
if (room.game_id == game_id):
return room
if (GameModel.objects.filter(pk = tournament_id).exists()):
room = TournamentRoom(self, game_id)
self.append(room)
return room
return None

6
games/routing.py Normal file
View 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())
]

View File

@ -14,7 +14,7 @@ class GameViewSet(viewsets.ModelViewSet):
queryset = GameModel.objects queryset = GameModel.objects
serializer_class = GameSerializer serializer_class = GameSerializer
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.AllowAny,)
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
def retrieve(self, request: HttpRequest, pk): def retrieve(self, request: HttpRequest, pk):

View File

@ -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())
] ]

View File

@ -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
) )
) )
}) })