Compare commits
2 Commits
56cfd563d7
...
9bdd07d59e
Author | SHA1 | Date | |
---|---|---|---|
9bdd07d59e | |||
9523ac4554 |
@ -12,19 +12,6 @@ class TournamentModel(models.Model):
|
|||||||
started = models.BooleanField(default=False)
|
started = models.BooleanField(default=False)
|
||||||
finished = 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):
|
def create_game(self, users_id):
|
||||||
game_id = GameModel.create(users_id=users_id)
|
game_id = GameModel.create(users_id=users_id)
|
||||||
TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save()
|
TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save()
|
||||||
|
@ -3,11 +3,14 @@ from .models import TournamentModel
|
|||||||
|
|
||||||
class TournamentSerializer(serializers.ModelSerializer):
|
class TournamentSerializer(serializers.ModelSerializer):
|
||||||
|
|
||||||
levels = serializers.SerializerMethodField()
|
levels = serializers.SerializerMethodField(read_only=True, required=False)
|
||||||
|
level = serializers.ReadOnlyField()
|
||||||
|
started = serializers.ReadOnlyField()
|
||||||
|
finished = serializers.ReadOnlyField()
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = TournamentModel
|
model = TournamentModel
|
||||||
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels"]
|
fields = ["name", "nb_players", "nb_players_by_game", "level", "started", "finished", "levels", "id"]
|
||||||
|
|
||||||
def get_levels(self, instance):
|
def get_levels(self, instance):
|
||||||
levels: [[int]] = []
|
levels: [[int]] = []
|
||||||
|
@ -5,6 +5,6 @@ from django.conf.urls.static import static
|
|||||||
from .viewset import TournamentViewSet
|
from .viewset import TournamentViewSet
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve", "post": "create"}), name="tournament_page"),
|
path("<int:pk>", TournamentViewSet.as_view({"get": "retrieve"}), name="tournament_page"),
|
||||||
re_path(r"(<str:state>)?", TournamentViewSet.as_view({"get": "list"}), name="tournaments"),
|
re_path(r"search/(?P<state>\w*)", TournamentViewSet.as_view({"get": "list", "post": "create"}), name="tournaments"),
|
||||||
]
|
]
|
@ -19,21 +19,19 @@ class TournamentViewSet(viewsets.ModelViewSet):
|
|||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
authentication_classes = (SessionAuthentication,)
|
authentication_classes = (SessionAuthentication,)
|
||||||
|
|
||||||
def perform_create(self, request: HttpRequest):
|
def perform_create(self, serializer: TournamentSerializer):
|
||||||
|
|
||||||
data: dict = request.data
|
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
|
||||||
|
|
||||||
nb_players_by_game = data.get("nb_players_by_game")
|
tournament = serializer.save(level = level)
|
||||||
if (nb_players_by_game is None):
|
|
||||||
return Response("nb_player_by_game is required.", status=status.HTTP_400_BAD_REQUEST)
|
|
||||||
|
|
||||||
nb_players = data.get("nb_players")
|
return Response(self.serializer_class(tournament).data, status=status.HTTP_201_CREATED)
|
||||||
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 = ""):
|
def list(self, request: HttpRequest, state: str = ""):
|
||||||
query: QuerySet
|
query: QuerySet
|
||||||
@ -42,7 +40,7 @@ class TournamentViewSet(viewsets.ModelViewSet):
|
|||||||
query = TournamentModel.objects.filter(started=True, finished=False)
|
query = TournamentModel.objects.filter(started=True, finished=False)
|
||||||
case "finished":
|
case "finished":
|
||||||
query = TournamentModel.objects.filter(finished=True)
|
query = TournamentModel.objects.filter(finished=True)
|
||||||
case "currently":
|
case "waiting":
|
||||||
query = TournamentModel.objects.filter(started=False, finished=False)
|
query = TournamentModel.objects.filter(started=False, finished=False)
|
||||||
case _:
|
case _:
|
||||||
query = TournamentModel.objects.all()
|
query = TournamentModel.objects.all()
|
||||||
|
Loading…
Reference in New Issue
Block a user