Compare commits

..

No commits in common. "9bdd07d59e0274498d9afeef91cf2e4389faa1ec" and "56cfd563d77c42781cdb83e3c51f2e8271013875" have entirely different histories.

4 changed files with 30 additions and 18 deletions

View File

@ -12,6 +12,19 @@ class TournamentModel(models.Model):
started = models.BooleanField(default=False)
finished = models.BooleanField(default=False)
def create(self, nb_players: int, nb_players_by_game: int, name: str = ""):
self.level = 1
number: int = nb_players
while (number != nb_players_by_game):
number = number // 2 + (number % 2)
self.level += 1
self.nb_players = nb_players
self.nb_players_by_game = nb_players_by_game
self.name = name
self.save()
return self.pk
def create_game(self, users_id):
game_id = GameModel.create(users_id=users_id)
TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save()

View File

@ -3,14 +3,11 @@ from .models import TournamentModel
class TournamentSerializer(serializers.ModelSerializer):
levels = serializers.SerializerMethodField(read_only=True, required=False)
level = serializers.ReadOnlyField()
started = serializers.ReadOnlyField()
finished = serializers.ReadOnlyField()
levels = serializers.SerializerMethodField()
class Meta:
model = TournamentModel
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id"]
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels"]
def get_levels(self, instance):
levels: [[int]] = []

View File

@ -5,6 +5,6 @@ from django.conf.urls.static import static
from .viewset import TournamentViewSet
urlpatterns = [
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve"}), name="tournament_page"),
re_path(r"search/(?P<state>\w*)", TournamentViewSet.as_view({"get": "list", "post": "create"}), 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"),
]

View File

@ -19,19 +19,21 @@ class TournamentViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,)
def perform_create(self, serializer: TournamentSerializer):
def perform_create(self, request: HttpRequest):
nb_players = serializer.validated_data["nb_players"]
nb_players_by_game = serializer.validated_data["nb_players_by_game"]
level = 1
number: int = nb_players
while (number != nb_players_by_game):
number = number // 2 + (number % 2)
level += 1
data: dict = request.data
tournament = serializer.save(level = level)
nb_players_by_game = data.get("nb_players_by_game")
if (nb_players_by_game is None):
return Response("nb_player_by_game is required.", status=status.HTTP_400_BAD_REQUEST)
return Response(self.serializer_class(tournament).data, status=status.HTTP_201_CREATED)
nb_players = data.get("nb_players")
if (nb_players is None):
return Response("nb_players is required.", status=status.HTTP_400_BAD_REQUEST)
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
@ -40,7 +42,7 @@ class TournamentViewSet(viewsets.ModelViewSet):
query = TournamentModel.objects.filter(started=True, finished=False)
case "finished":
query = TournamentModel.objects.filter(finished=True)
case "waiting":
case "currently":
query = TournamentModel.objects.filter(started=False, finished=False)
case _:
query = TournamentModel.objects.all()