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)
|
||||
started = models.BooleanField(default=False)
|
||||
winner_id = models.IntegerField()
|
||||
winner_id = models.IntegerField(null=True, blank=True)
|
||||
|
||||
def create(self, users_id: [int]):
|
||||
self.save()
|
||||
|
@ -31,9 +31,6 @@ class ProfileViewSet(viewsets.ModelViewSet):
|
||||
profile["avatar_url"] = profile["avatar_url"][profile["avatar_url"].find("static") - 1:]
|
||||
return Response(serializer.data)
|
||||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(user=self.request.user)
|
||||
|
||||
class MyProfileViewSet(viewsets.ModelViewSet):
|
||||
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
|
@ -5,7 +5,7 @@ from games.models import GameModel
|
||||
# Create your models here.tu
|
||||
class TournamentModel(models.Model):
|
||||
|
||||
name = models.CharField()
|
||||
name = models.CharField(max_length=100)
|
||||
nb_players = models.IntegerField()
|
||||
nb_players_by_game = models.IntegerField()
|
||||
level = models.IntegerField()
|
||||
@ -21,7 +21,9 @@ class TournamentModel(models.Model):
|
||||
self.nb_players = nb_players
|
||||
self.nb_players_by_game = nb_players_by_game
|
||||
self.name = name
|
||||
return self.save()
|
||||
self.save()
|
||||
|
||||
return self.pk
|
||||
|
||||
def create_game(self, 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.urls.static import static
|
||||
|
||||
from .views import TournamentsView, TournamentView
|
||||
from .viewset import TournamentViewSet
|
||||
|
||||
urlpatterns = [
|
||||
path("<int:pk>", name="tournament_page"),
|
||||
path("", TournamentsView.as_view(), name="tournaments"),
|
||||
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"),
|
||||
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 import permissions, status
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
from django.http import HttpRequest
|
||||
from django.contrib.auth import login
|
||||
from django.db.models import QuerySet
|
||||
|
||||
from matchmaking.models import in_matchmaking
|
||||
from .models import TournamentModel
|
||||
from .serializers import TournamentSerializer
|
||||
|
||||
# Create your views here.
|
||||
class TournamentsView(APIView):
|
||||
class TournamentViewSet(viewsets.ModelViewSet):
|
||||
|
||||
queryset = TournamentModel.objects.all
|
||||
serializer_class = TournamentSerializer
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
def perform_create(self, request: HttpRequest):
|
||||
|
||||
data: dict = request.data
|
||||
|
||||
@ -27,35 +31,29 @@ class TournamentsView(APIView):
|
||||
if (nb_players is None):
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
class TournamentView(APIView):
|
||||
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def get(self, request: HttpRequest, pk):
|
||||
def retrieve(self, request: HttpRequest, pk):
|
||||
|
||||
if (not TournamentModel.objects.filter(pk=pk).exists()):
|
||||
return Response({"detail": "Tournament not found."}, status=status.HTTP_404_NOT_FOUND)
|
||||
|
||||
tournament = TournamentModel.objects.get(pk=pk)
|
||||
|
||||
levels: [[int]] = []
|
||||
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)
|
||||
return Response(self.serializer_class(tournament).data, status=status.HTTP_200_OK)
|
@ -22,6 +22,6 @@ urlpatterns = [
|
||||
path('api/profiles/', include('profiles.urls')),
|
||||
path('api/accounts/', include('accounts.urls')),
|
||||
path('api/chat/', include('chat.urls')),
|
||||
path('api/tournament', include('tournament.urls')),
|
||||
path('api/tournaments/', include('tournament.urls')),
|
||||
path('', include('frontend.urls')),
|
||||
]
|
||||
|
Loading…
Reference in New Issue
Block a user