Compare commits
4 Commits
9714918de9
...
56cfd563d7
Author | SHA1 | Date | |
---|---|---|---|
56cfd563d7 | |||
288252ab37 | |||
adf0c5ceed | |||
1032a8fd98 |
@ -5,7 +5,7 @@ class GameModel(models.Model):
|
|||||||
|
|
||||||
finished = models.BooleanField(default=False)
|
finished = models.BooleanField(default=False)
|
||||||
started = models.BooleanField(default=False)
|
started = models.BooleanField(default=False)
|
||||||
winner_id = models.IntegerField()
|
winner_id = models.IntegerField(null=True, blank=True)
|
||||||
|
|
||||||
def create(self, users_id: [int]):
|
def create(self, users_id: [int]):
|
||||||
self.save()
|
self.save()
|
||||||
|
@ -31,9 +31,6 @@ class ProfileViewSet(viewsets.ModelViewSet):
|
|||||||
profile["avatar_url"] = profile["avatar_url"][profile["avatar_url"].find("static") - 1:]
|
profile["avatar_url"] = profile["avatar_url"][profile["avatar_url"].find("static") - 1:]
|
||||||
return Response(serializer.data)
|
return Response(serializer.data)
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
|
||||||
serializer.save(user=self.request.user)
|
|
||||||
|
|
||||||
class MyProfileViewSet(viewsets.ModelViewSet):
|
class MyProfileViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
@ -5,7 +5,7 @@ from games.models import GameModel
|
|||||||
# Create your models here.tu
|
# Create your models here.tu
|
||||||
class TournamentModel(models.Model):
|
class TournamentModel(models.Model):
|
||||||
|
|
||||||
name = models.CharField()
|
name = models.CharField(max_length=100)
|
||||||
nb_players = models.IntegerField()
|
nb_players = models.IntegerField()
|
||||||
nb_players_by_game = models.IntegerField()
|
nb_players_by_game = models.IntegerField()
|
||||||
level = models.IntegerField()
|
level = models.IntegerField()
|
||||||
@ -21,7 +21,9 @@ class TournamentModel(models.Model):
|
|||||||
self.nb_players = nb_players
|
self.nb_players = nb_players
|
||||||
self.nb_players_by_game = nb_players_by_game
|
self.nb_players_by_game = nb_players_by_game
|
||||||
self.name = name
|
self.name = name
|
||||||
return self.save()
|
self.save()
|
||||||
|
|
||||||
|
return self.pk
|
||||||
|
|
||||||
def create_game(self, users_id):
|
def create_game(self, users_id):
|
||||||
game_id = GameModel.create(users_id=users_id)
|
game_id = GameModel.create(users_id=users_id)
|
||||||
|
19
tournament/serializers.py
Normal file
19
tournament/serializers.py
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
from .models import TournamentModel
|
||||||
|
|
||||||
|
class TournamentSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
|
levels = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = TournamentModel
|
||||||
|
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels"]
|
||||||
|
|
||||||
|
def get_levels(self, instance):
|
||||||
|
levels: [[int]] = []
|
||||||
|
for i in range(instance.level):
|
||||||
|
level: [int] = instance.get_games_id_by_level(i)
|
||||||
|
if (level == []):
|
||||||
|
break
|
||||||
|
levels.append(level)
|
||||||
|
return levels
|
@ -1,10 +1,10 @@
|
|||||||
from django.urls import path
|
from django.urls import path, re_path
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
|
|
||||||
from .views import TournamentsView, TournamentView
|
from .viewset import TournamentViewSet
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("<int:pk>", name="tournament_page"),
|
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"),
|
||||||
path("", TournamentsView.as_view(), name="tournaments"),
|
re_path(r"(<str:state>)?", TournamentViewSet.as_view({"get": "list"}), name="tournaments"),
|
||||||
]
|
]
|
@ -1,21 +1,25 @@
|
|||||||
from rest_framework.views import APIView
|
from rest_framework import viewsets
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
from rest_framework import permissions, status
|
from rest_framework import permissions, status
|
||||||
from rest_framework.authentication import SessionAuthentication
|
from rest_framework.authentication import SessionAuthentication
|
||||||
|
|
||||||
from django.http import HttpRequest
|
from django.http import HttpRequest
|
||||||
from django.contrib.auth import login
|
from django.contrib.auth import login
|
||||||
|
from django.db.models import QuerySet
|
||||||
|
|
||||||
from matchmaking.models import in_matchmaking
|
from matchmaking.models import in_matchmaking
|
||||||
from .models import TournamentModel
|
from .models import TournamentModel
|
||||||
|
from .serializers import TournamentSerializer
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
class TournamentsView(APIView):
|
class TournamentViewSet(viewsets.ModelViewSet):
|
||||||
|
|
||||||
|
queryset = TournamentModel.objects.all
|
||||||
|
serializer_class = TournamentSerializer
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
authentication_classes = (SessionAuthentication,)
|
authentication_classes = (SessionAuthentication,)
|
||||||
|
|
||||||
def post(self, request: HttpRequest):
|
def perform_create(self, request: HttpRequest):
|
||||||
|
|
||||||
data: dict = request.data
|
data: dict = request.data
|
||||||
|
|
||||||
@ -27,35 +31,29 @@ class TournamentsView(APIView):
|
|||||||
if (nb_players is None):
|
if (nb_players is None):
|
||||||
return Response("nb_players is required.", status=status.HTTP_400_BAD_REQUEST)
|
return Response("nb_players is required.", status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
tournament_id: int = TournamentModel.create(users_id=users_id, nb_players=nb_players)
|
tournament_id: int = TournamentModel().create(nb_players=nb_players, nb_players_by_game=nb_players_by_game)
|
||||||
|
|
||||||
return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED)
|
return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED)
|
||||||
|
|
||||||
class TournamentView(APIView):
|
def list(self, request: HttpRequest, state: str = ""):
|
||||||
|
query: QuerySet
|
||||||
|
match state:
|
||||||
|
case "started":
|
||||||
|
query = TournamentModel.objects.filter(started=True, finished=False)
|
||||||
|
case "finished":
|
||||||
|
query = TournamentModel.objects.filter(finished=True)
|
||||||
|
case "currently":
|
||||||
|
query = TournamentModel.objects.filter(started=False, finished=False)
|
||||||
|
case _:
|
||||||
|
query = TournamentModel.objects.all()
|
||||||
|
serializer = TournamentSerializer(query, many=True)
|
||||||
|
return Response(serializer.data)
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
def retrieve(self, request: HttpRequest, pk):
|
||||||
authentication_classes = (SessionAuthentication,)
|
|
||||||
|
|
||||||
def get(self, request: HttpRequest, pk):
|
|
||||||
|
|
||||||
if (not TournamentModel.objects.filter(pk=pk).exists()):
|
if (not TournamentModel.objects.filter(pk=pk).exists()):
|
||||||
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
|
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
|
||||||
tournament = TournamentModel.objects.get(pk=pk)
|
tournament = TournamentModel.objects.get(pk=pk)
|
||||||
|
|
||||||
levels: [[int]] = []
|
return Response(self.serializer_class(tournament).data, status=status.HTTP_200_OK)
|
||||||
level: [int] = tournament.get_games_id_by_level
|
|
||||||
while level != []:
|
|
||||||
levels.append(level)
|
|
||||||
level: [int] = tournament.get_games_id_by_level
|
|
||||||
|
|
||||||
data = {
|
|
||||||
"name": tournament.name,
|
|
||||||
"finished": tournament.finished,
|
|
||||||
"started": tournament.finished,
|
|
||||||
"nb_players": tournament.nb_players,
|
|
||||||
"nb_players_by_game": tournament.nb_players_by_game,
|
|
||||||
"levels": levels
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response(data, status=status.HTTP_200_OK)
|
|
@ -22,6 +22,6 @@ urlpatterns = [
|
|||||||
path('api/profiles/', include('profiles.urls')),
|
path('api/profiles/', include('profiles.urls')),
|
||||||
path('api/accounts/', include('accounts.urls')),
|
path('api/accounts/', include('accounts.urls')),
|
||||||
path('api/chat/', include('chat.urls')),
|
path('api/chat/', include('chat.urls')),
|
||||||
path('api/tournament', include('tournament.urls')),
|
path('api/tournaments/', include('tournament.urls')),
|
||||||
path('', include('frontend.urls')),
|
path('', include('frontend.urls')),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user