diff --git a/README.md b/README.md index 255ae1f..d96ff83 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ pip install -r requirements.txt python manage.py makemigrations games python manage.py makemigrations profiles python manage.py makemigrations chat +python manage.py makemigrations tournament python manage.py migrate ``` - Start the developpement server diff --git a/frontend/static/js/api/tournament/tournament.js b/frontend/static/js/api/tournament/tournament.js new file mode 100644 index 0000000..7ecce9c --- /dev/null +++ b/frontend/static/js/api/tournament/tournament.js @@ -0,0 +1,19 @@ +import { Client } from "./client.js"; + +class Tourmanent +{ + /** + * @param {Client} client + */ + constructor(client) + { + /** + * @type {Client} + */ + this.client = client + } + + +} + +export { Tourmanent } \ No newline at end of file diff --git a/frontend/static/js/api/tournament/tournaments.js b/frontend/static/js/api/tournament/tournaments.js new file mode 100644 index 0000000..60a92a7 --- /dev/null +++ b/frontend/static/js/api/tournament/tournaments.js @@ -0,0 +1,33 @@ +import { Client } from "./client.js"; + +class Tourmanents +{ + /** + * @param {Client} client + */ + constructor(client) + { + /** + * @type {Client} + */ + this.client = client + } + + async createTournament(nb_players, online) + { + if (online) + return "offline"; + let response = await this.client._post("/api/tournaments/", {numbers_of_players: nb_players}); + let response_data = await response.json(); + + if (JSON.stringify(response_data) === JSON.stringify({'detail': 'Authentication credentials were not provided.'})) + { + this.client._update_logged(false); + return null; + } + return response_data.tournament_id; + } + +} + +export { Tourmanents } \ No newline at end of file diff --git a/tournament/__init__.py b/tournament/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tournament/admin.py b/tournament/admin.py new file mode 100644 index 0000000..8c38f3f --- /dev/null +++ b/tournament/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/tournament/apps.py b/tournament/apps.py new file mode 100644 index 0000000..15ea9fb --- /dev/null +++ b/tournament/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class TournamentConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'tournament' diff --git a/tournament/models.py b/tournament/models.py new file mode 100644 index 0000000..7b4ce71 --- /dev/null +++ b/tournament/models.py @@ -0,0 +1,47 @@ +from django.db import models + +from games.models import GameModel + +# Create your models here.tu +class TournamentModel(models.Model): + + name = models.CharField() + nb_players = models.IntegerField() + nb_players_by_game = models.IntegerField() + level = models.IntegerField() + 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 + return self.save() + + def create_game(self, users_id): + game_id = GameModel.create(users_id=users_id) + TournamentGamesModel(game_id=game_id, tournament_id=self.pk).save() + return game_id + + def get_games_id_by_level(self, level): + return list(TournamentGamesModel.objects.filter(tournament_id=self.pk, tournament_level=level)) + + def get_games_id(self): + return list(TournamentGamesModel.objects.filter(tournament_id=self.pk)) + + def get_players_id(self): + lst: [int] = [] + for game_id in self.get_games_id(): + lst.append(GameMembersModel.objects.filter(game_id=game_id)) + return lst + +class TournamentGamesModel(models.Model): + + tournament_id = models.IntegerField() + tournament_level = models.IntegerField() + game_id = models.IntegerField() \ No newline at end of file diff --git a/tournament/tests.py b/tournament/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/tournament/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/tournament/urls.py b/tournament/urls.py new file mode 100644 index 0000000..4451812 --- /dev/null +++ b/tournament/urls.py @@ -0,0 +1,10 @@ +from django.urls import path +from django.conf import settings +from django.conf.urls.static import static + +from .views import TournamentsView, TournamentView + +urlpatterns = [ + path("", name="tournament_page"), + path("", TournamentsView.as_view(), name="tournaments"), +] \ No newline at end of file diff --git a/tournament/views.py b/tournament/views.py new file mode 100644 index 0000000..f055d22 --- /dev/null +++ b/tournament/views.py @@ -0,0 +1,61 @@ +from rest_framework.views import APIView +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 matchmaking.models import in_matchmaking +from .models import TournamentModel + +# Create your views here. +class TournamentsView(APIView): + + permission_classes = (permissions.IsAuthenticated,) + authentication_classes = (SessionAuthentication,) + + def post(self, request: HttpRequest): + + data: dict = request.data + + 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) + + 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(users_id=users_id, nb_players=nb_players) + + return Response({"tournament_id": tournament_id}, status=status.HTTP_201_CREATED) + +class TournamentView(APIView): + + permission_classes = (permissions.IsAuthenticated,) + authentication_classes = (SessionAuthentication,) + + def get(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) \ No newline at end of file diff --git a/trancendence/settings.py b/trancendence/settings.py index 411aca1..3080286 100644 --- a/trancendence/settings.py +++ b/trancendence/settings.py @@ -43,6 +43,7 @@ INSTALLED_APPS = [ 'channels', 'daphne', + 'tournament.apps.TournamentConfig', 'matchmaking.apps.MatchmakingConfig', 'games.apps.GamesConfig', 'accounts.apps.AccountsConfig', diff --git a/trancendence/urls.py b/trancendence/urls.py index 46a790f..0c9cd6d 100644 --- a/trancendence/urls.py +++ b/trancendence/urls.py @@ -22,5 +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('', include('frontend.urls')), ]