init: tournament
This commit is contained in:
parent
8ed9deab2d
commit
bfcfabb35a
@ -25,6 +25,7 @@ pip install -r requirements.txt
|
|||||||
python manage.py makemigrations games
|
python manage.py makemigrations games
|
||||||
python manage.py makemigrations profiles
|
python manage.py makemigrations profiles
|
||||||
python manage.py makemigrations chat
|
python manage.py makemigrations chat
|
||||||
|
python manage.py makemigrations tournament
|
||||||
python manage.py migrate
|
python manage.py migrate
|
||||||
```
|
```
|
||||||
- Start the developpement server
|
- Start the developpement server
|
||||||
|
19
frontend/static/js/api/tournament/tournament.js
Normal file
19
frontend/static/js/api/tournament/tournament.js
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
import { Client } from "./client.js";
|
||||||
|
|
||||||
|
class Tourmanent
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @param {Client} client
|
||||||
|
*/
|
||||||
|
constructor(client)
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @type {Client}
|
||||||
|
*/
|
||||||
|
this.client = client
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export { Tourmanent }
|
33
frontend/static/js/api/tournament/tournaments.js
Normal file
33
frontend/static/js/api/tournament/tournaments.js
Normal file
@ -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 }
|
0
tournament/__init__.py
Normal file
0
tournament/__init__.py
Normal file
3
tournament/admin.py
Normal file
3
tournament/admin.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.contrib import admin
|
||||||
|
|
||||||
|
# Register your models here.
|
6
tournament/apps.py
Normal file
6
tournament/apps.py
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
from django.apps import AppConfig
|
||||||
|
|
||||||
|
|
||||||
|
class TournamentConfig(AppConfig):
|
||||||
|
default_auto_field = 'django.db.models.BigAutoField'
|
||||||
|
name = 'tournament'
|
47
tournament/models.py
Normal file
47
tournament/models.py
Normal file
@ -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()
|
3
tournament/tests.py
Normal file
3
tournament/tests.py
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
# Create your tests here.
|
10
tournament/urls.py
Normal file
10
tournament/urls.py
Normal file
@ -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("<int:pk>", name="tournament_page"),
|
||||||
|
path("", TournamentsView.as_view(), name="tournaments"),
|
||||||
|
]
|
61
tournament/views.py
Normal file
61
tournament/views.py
Normal file
@ -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)
|
@ -43,6 +43,7 @@ INSTALLED_APPS = [
|
|||||||
'channels',
|
'channels',
|
||||||
'daphne',
|
'daphne',
|
||||||
|
|
||||||
|
'tournament.apps.TournamentConfig',
|
||||||
'matchmaking.apps.MatchmakingConfig',
|
'matchmaking.apps.MatchmakingConfig',
|
||||||
'games.apps.GamesConfig',
|
'games.apps.GamesConfig',
|
||||||
'accounts.apps.AccountsConfig',
|
'accounts.apps.AccountsConfig',
|
||||||
|
@ -22,5 +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('', include('frontend.urls')),
|
path('', include('frontend.urls')),
|
||||||
]
|
]
|
||||||
|
Loading…
Reference in New Issue
Block a user