First Commit
This commit is contained in:
commit
ce0758fbbb
496 changed files with 52062 additions and 0 deletions
0
games/__init__.py
Normal file
0
games/__init__.py
Normal file
4
games/admin.py
Normal file
4
games/admin.py
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
from django.contrib import admin
|
||||
from .models import LittleBacCategories
|
||||
|
||||
admin.site.register(LittleBacCategories)
|
||||
6
games/apps.py
Normal file
6
games/apps.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class GamesConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'games'
|
||||
64
games/migrations/0001_initial.py
Normal file
64
games/migrations/0001_initial.py
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 09:48
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='LittleBacCategories',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=100)),
|
||||
('description', models.TextField(default='')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='LittleBacGames',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('name', models.CharField(max_length=155)),
|
||||
('status', models.CharField(choices=[('waiting', 'En attente'), ('in_progress', 'En cours'), ('finished', 'Terminée')], default='waiting', max_length=20)),
|
||||
('created', models.DateTimeField(auto_now_add=True)),
|
||||
('updated', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='LittleBacRounds',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('letter', models.CharField(max_length=1)),
|
||||
('round_counter', models.IntegerField(default=1)),
|
||||
('game', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_rounds', to='games.littlebacgames')),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='LittleBacPlayers',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('score', models.IntegerField()),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_players', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='LittleBacAnswers',
|
||||
fields=[
|
||||
('id', models.AutoField(primary_key=True, serialize=False)),
|
||||
('answer', models.CharField(max_length=100)),
|
||||
('is_valid', models.BooleanField(default=False)),
|
||||
('point', models.IntegerField(default=0)),
|
||||
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_answers', to='games.littlebaccategories')),
|
||||
('player', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_answers', to='games.littlebacplayers')),
|
||||
('round', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_answers', to='games.littlebacrounds')),
|
||||
],
|
||||
),
|
||||
]
|
||||
20
games/migrations/0002_littlebacplayers_game.py
Normal file
20
games/migrations/0002_littlebacplayers_game.py
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 10:51
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacplayers',
|
||||
name='game',
|
||||
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_players', to='games.littlebacgames'),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
22
games/migrations/0003_littlebacgames_author.py
Normal file
22
games/migrations/0003_littlebacgames_author.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 12:10
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('games', '0002_littlebacplayers_game'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='author',
|
||||
field=models.ForeignKey(default=1, on_delete=django.db.models.deletion.CASCADE, related_name='little_bac_games', to=settings.AUTH_USER_MODEL),
|
||||
preserve_default=False,
|
||||
),
|
||||
]
|
||||
18
games/migrations/0004_littlebacplayers_is_ready.py
Normal file
18
games/migrations/0004_littlebacplayers_is_ready.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 12:45
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0003_littlebacgames_author'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacplayers',
|
||||
name='is_ready',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
18
games/migrations/0005_littlebacgames_players.py
Normal file
18
games/migrations/0005_littlebacgames_players.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 15:24
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0004_littlebacplayers_is_ready'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='players',
|
||||
field=models.ManyToManyField(related_name='little_bac_games', to='games.littlebacplayers'),
|
||||
),
|
||||
]
|
||||
17
games/migrations/0006_remove_littlebacgames_players.py
Normal file
17
games/migrations/0006_remove_littlebacgames_players.py
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 15:37
|
||||
|
||||
from django.db import migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0005_littlebacgames_players'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RemoveField(
|
||||
model_name='littlebacgames',
|
||||
name='players',
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 19:24
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
('games', '0006_remove_littlebacgames_players'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='countdown_started',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='countdown_time',
|
||||
field=models.IntegerField(default=0),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacanswers',
|
||||
name='category',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='answers', to='games.littlebaccategories'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacanswers',
|
||||
name='player',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='answers', to='games.littlebacplayers'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacanswers',
|
||||
name='round',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='answers', to='games.littlebacrounds'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacgames',
|
||||
name='author',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='games', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacplayers',
|
||||
name='game',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to='games.littlebacgames'),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacplayers',
|
||||
name='user',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='players', to=settings.AUTH_USER_MODEL),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='littlebacrounds',
|
||||
name='game',
|
||||
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='rounds', to='games.littlebacgames'),
|
||||
),
|
||||
]
|
||||
18
games/migrations/0008_littlebacgames_countdown_start_time.py
Normal file
18
games/migrations/0008_littlebacgames_countdown_start_time.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-30 19:46
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0007_littlebacgames_countdown_started_and_more'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='countdown_start_time',
|
||||
field=models.DateTimeField(blank=True, null=True),
|
||||
),
|
||||
]
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-31 08:54
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0008_littlebacgames_countdown_start_time'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='littlebacgames',
|
||||
name='countdown_start_time',
|
||||
field=models.DateTimeField(blank=True, default=None, null=True),
|
||||
),
|
||||
]
|
||||
18
games/migrations/0010_littlebacplayers_status.py
Normal file
18
games/migrations/0010_littlebacplayers_status.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2024-12-31 12:39
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0009_alter_littlebacgames_countdown_start_time'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacplayers',
|
||||
name='status',
|
||||
field=models.CharField(choices=[('playing', 'Joue'), ('overed', 'A fini')], default='playing', max_length=20),
|
||||
),
|
||||
]
|
||||
18
games/migrations/0011_littlebacgames_current_phase.py
Normal file
18
games/migrations/0011_littlebacgames_current_phase.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 4.2.17 on 2025-01-02 13:49
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('games', '0010_littlebacplayers_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='littlebacgames',
|
||||
name='current_phase',
|
||||
field=models.CharField(blank=True, default='ready_game', max_length=60, null=True),
|
||||
),
|
||||
]
|
||||
0
games/migrations/__init__.py
Normal file
0
games/migrations/__init__.py
Normal file
49
games/models.py
Normal file
49
games/models.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
from django.db import models
|
||||
from users.models import User
|
||||
|
||||
class LittleBacGames(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='games')
|
||||
name = models.CharField(max_length=155)
|
||||
status = models.CharField(max_length=20 ,choices=[
|
||||
('waiting', 'En attente'),
|
||||
('in_progress', 'En cours'),
|
||||
('finished', 'Terminée')
|
||||
], default='waiting')
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
updated = models.DateTimeField(auto_now=True)
|
||||
countdown_started = models.BooleanField(default=False)
|
||||
countdown_time = models.IntegerField(default=0)
|
||||
countdown_start_time = models.DateTimeField(default=None, null=True, blank=True)
|
||||
current_phase = models.CharField(max_length=60, default="ready_game", null=True, blank=True)
|
||||
|
||||
class LittleBacPlayers(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='players')
|
||||
game = models.ForeignKey(LittleBacGames, on_delete=models.CASCADE, related_name='players')
|
||||
score = models.IntegerField()
|
||||
is_ready = models.BooleanField(default=False)
|
||||
status = models.CharField(max_length=20, choices=[
|
||||
('playing', 'Joue'),
|
||||
('overed', 'A fini')
|
||||
], default="playing")
|
||||
|
||||
class LittleBacCategories(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
name = models.CharField(max_length=100)
|
||||
description = models.TextField(default="")
|
||||
|
||||
class LittleBacRounds(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
game = models.ForeignKey(LittleBacGames, on_delete=models.CASCADE, related_name='rounds')
|
||||
letter = models.CharField(max_length=1)
|
||||
round_counter = models.IntegerField(default=1)
|
||||
|
||||
class LittleBacAnswers(models.Model):
|
||||
id = models.AutoField(primary_key=True)
|
||||
round = models.ForeignKey(LittleBacRounds, on_delete=models.CASCADE, related_name='answers')
|
||||
player = models.ForeignKey(LittleBacPlayers, on_delete=models.CASCADE, related_name='answers')
|
||||
category = models.ForeignKey(LittleBacCategories, on_delete=models.CASCADE, related_name='answers')
|
||||
answer = models.CharField(max_length=100)
|
||||
is_valid = models.BooleanField(default=False)
|
||||
point = models.IntegerField(default=0)
|
||||
7
games/templatetags/custom_filters.py
Normal file
7
games/templatetags/custom_filters.py
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
from django import template
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@register.filter
|
||||
def get_item(dictionary, key):
|
||||
return dictionary.get(key)
|
||||
3
games/tests.py
Normal file
3
games/tests.py
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
25
games/urls.py
Normal file
25
games/urls.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
from django.urls import path
|
||||
from django.conf.urls.static import static
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
# API REST
|
||||
path('api/<int:game_id>/start_countdown', views.game_start_countdown, name='game_start_countdown'),
|
||||
path('api/<int:game_id>/countdown_status', views.game_countdown_status, name='game_countdown_status'),
|
||||
path('api/bac/<int:game_id>/info', views.game_infos_little_bac, name='game_infos_little_bac'),
|
||||
path('api/bac/<int:game_id>/info_party', views.party_infos_little_bac, name='party_infos_little_bac'),
|
||||
path('api/bac/<int:game_id>/players', views.game_players_little_bac, name='game_players_little_bac'),
|
||||
path('api/bac/<int:game_id>/end_game', views.game_liitle_bac_end_game, name='end_game'),
|
||||
path('api/bac/<int:game_id>/player/<int:player_id>/toggle_ready', views.toggle_ready_status_little_bac, name='toggle_ready_status_little_bac'),
|
||||
|
||||
|
||||
path("", views.portal, name="portal_games"),
|
||||
path("bac", views.little_bac_home, name="bac_games"),
|
||||
path("bac/party", views.little_bac_start, name="bac_start_games"),
|
||||
path("bac/party/<str:party_id>", views.little_bac_party, name="bac_party_games"),
|
||||
path("bac/party/<str:party_id>/join", views.little_bac_party_join, name="bac_party_join_games"),
|
||||
path("bac/party/<str:party_id>/play", views.little_bac_party_play, name="bac_party_play_games"),
|
||||
path("bac/party/<str:party_id>/results", views.game_little_bac_results, name="bac_party_results_games"),
|
||||
path('party/<int:game_id>/new_round/', views.game_little_bac_start_new_round, name='bac_start_new_round'),
|
||||
|
||||
]
|
||||
433
games/views.py
Normal file
433
games/views.py
Normal file
|
|
@ -0,0 +1,433 @@
|
|||
from django.shortcuts import render, redirect, get_object_or_404
|
||||
from django.http import JsonResponse
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from .models import *
|
||||
from users.models import UserLevel
|
||||
from django.utils.timezone import now
|
||||
from django.contrib import messages
|
||||
from django.db.models import Count, Sum, F
|
||||
from django.db.models.functions import Lower
|
||||
|
||||
def portal(request):
|
||||
last_party = LittleBacGames.objects.filter().last()
|
||||
nb_parties = LittleBacGames.objects.filter(status="finished").count()
|
||||
|
||||
if not request.user.is_authenticated:
|
||||
return render(request, 'games/portal.html', {'last_party': last_party, 'nb_parties': nb_parties})
|
||||
games = LittleBacGames.objects.filter(author=request.user, status='waiting')
|
||||
|
||||
return render(request, 'games/portal.html', {'games': games, 'last_party': last_party, 'nb_parties': nb_parties})
|
||||
|
||||
def little_bac_home(request):
|
||||
return render(request, 'games/littlebac/home.html')
|
||||
|
||||
@login_required()
|
||||
def little_bac_start(request):
|
||||
import random
|
||||
import string
|
||||
|
||||
game = LittleBacGames.objects.create(name=f"Partie de {request.user.username}", author=request.user)
|
||||
LittleBacPlayers.objects.create(
|
||||
user=request.user,
|
||||
game=game,
|
||||
score=0
|
||||
)
|
||||
|
||||
# Liste des lettres de l'alphabet
|
||||
alphabet = string.ascii_uppercase # 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||
|
||||
# Sélection aléatoire
|
||||
random_letter = random.choice(alphabet)
|
||||
|
||||
LittleBacRounds.objects.create(
|
||||
game=game,
|
||||
letter=random_letter,
|
||||
round_counter=1
|
||||
)
|
||||
|
||||
return redirect('bac_party_games', party_id=game.id)
|
||||
|
||||
@login_required()
|
||||
def little_bac_party(request, party_id):
|
||||
game = LittleBacGames.objects.get(id=party_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
rounds = LittleBacRounds.objects.filter(game=game)
|
||||
|
||||
if players.filter(user=request.user).exists():
|
||||
current_round = rounds.last()
|
||||
return render(request, 'games/littlebac/game.html', {'game': game, 'players': players, 'round': current_round})
|
||||
else:
|
||||
return redirect('bac_games')
|
||||
|
||||
@login_required()
|
||||
def little_bac_party_join(request, party_id):
|
||||
game = LittleBacGames.objects.get(id=party_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
|
||||
if players.filter(user=request.user).exists():
|
||||
return redirect('bac_games')
|
||||
|
||||
LittleBacPlayers.objects.create(
|
||||
user=request.user,
|
||||
game=game,
|
||||
score=0
|
||||
)
|
||||
|
||||
return redirect('bac_party_games', party_id=game.id)
|
||||
|
||||
@login_required()
|
||||
def little_bac_party_play(request, party_id):
|
||||
game = LittleBacGames.objects.get(id=party_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
|
||||
if not players.filter(user=request.user).exists():
|
||||
return redirect('bac_games')
|
||||
|
||||
# Vérifie si la partie est en attente et met à jour le statut
|
||||
if game.status == 'waiting':
|
||||
game.status = 'in_progress'
|
||||
game.save()
|
||||
|
||||
# Récupère le round actuel (dernier round)
|
||||
current_round = LittleBacRounds.objects.filter(game=game).last()
|
||||
categories = LittleBacCategories.objects.all()
|
||||
player = players.get(user=request.user)
|
||||
|
||||
if request.method == "POST":
|
||||
print("POST")
|
||||
# On vérifie que les réponses commencent par la lettre du round
|
||||
round_letter = current_round.letter.upper()
|
||||
all_valid = True
|
||||
for category in categories:
|
||||
answer = request.POST.get(f"col-{category.id}", "").strip()
|
||||
if answer and answer[0].upper() != round_letter:
|
||||
all_valid = False
|
||||
break
|
||||
|
||||
if all_valid:
|
||||
player = LittleBacPlayers.objects.get(game=game, user_id=request.user.id)
|
||||
player.status = "overed"
|
||||
player.save()
|
||||
|
||||
# Récupérer les réponses soumises par le joueur
|
||||
answers = {
|
||||
f"col-{category.id}": request.POST.get(f"col-{category.id}")
|
||||
for category in categories
|
||||
}
|
||||
|
||||
responses = []
|
||||
# Enregistre chaque réponse en base de données
|
||||
for category in categories:
|
||||
answer = answers.get(f"col-{category.id}", "").strip() # Récupère la réponse ou une chaîne vide
|
||||
if answer: # Vérifie si une réponse est fournie
|
||||
response = LittleBacAnswers.objects.create(
|
||||
round=current_round,
|
||||
player=players.get(user=request.user),
|
||||
category=category,
|
||||
answer=answer,
|
||||
is_valid=False
|
||||
)
|
||||
responses.append(response)
|
||||
|
||||
return render(request, 'games/littlebac/finish.html', {
|
||||
'responses': responses,
|
||||
'round': current_round,
|
||||
'categories': categories,
|
||||
'player': player
|
||||
})
|
||||
else:
|
||||
messages.error(request, "Les réponses doivent commencer par la lettre du tour.")
|
||||
return render(request, 'games/littlebac/play.html', {
|
||||
'game': game,
|
||||
'round': current_round,
|
||||
'categories': categories,
|
||||
'countdown_remaining': countdown_remaining,
|
||||
})
|
||||
|
||||
# Passe les informations du décompte au template
|
||||
countdown_remaining = max(
|
||||
0, game.countdown_time - int((now() - game.countdown_start_time).total_seconds())
|
||||
) if game.countdown_started else None
|
||||
|
||||
return render(request, 'games/littlebac/play.html', {
|
||||
'game': game,
|
||||
'round': current_round,
|
||||
'categories': categories,
|
||||
'countdown_remaining': countdown_remaining,
|
||||
'player': player
|
||||
})
|
||||
|
||||
@login_required()
|
||||
def game_little_bac_results(request, party_id):
|
||||
game = get_object_or_404(LittleBacGames, id=party_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
rounds = LittleBacRounds.objects.filter(game=game)
|
||||
categories = LittleBacCategories.objects.all()
|
||||
|
||||
all_organized_answers = {}
|
||||
scores_by_round = {round.id: {} for round in rounds}
|
||||
total_scores = {player.id: 0 for player in players}
|
||||
|
||||
for round in rounds:
|
||||
answers = LittleBacAnswers.objects.filter(round=round)
|
||||
|
||||
# On détermine qu'un mot est valide s'il est unique pour une catégorie donnée
|
||||
for category in categories:
|
||||
valid_answers = answers.filter(category=category).annotate(
|
||||
lower_answer=Lower('answer')
|
||||
).values('lower_answer').annotate(
|
||||
count=Count('lower_answer')
|
||||
).filter(count=1)
|
||||
for answer in valid_answers:
|
||||
answers.filter(category=category, answer__iexact=answer['lower_answer']).update(is_valid=True, point=5)
|
||||
|
||||
# Marquer les réponses dupliquées et leur attribuer 1 point
|
||||
duplicate_answers = answers.filter(category=category).annotate(
|
||||
lower_answer=Lower('answer')
|
||||
).values('lower_answer').annotate(
|
||||
count=Count('lower_answer')
|
||||
).filter(count__gt=1)
|
||||
for answer in duplicate_answers:
|
||||
answers.filter(category=category, answer__iexact=answer['lower_answer']).update(is_valid=False, point=1)
|
||||
|
||||
# Calcule des points pour chaque joueur pour ce round
|
||||
for player in players:
|
||||
player_score = answers.filter(
|
||||
player=player
|
||||
).aggregate(
|
||||
total=Sum('point')
|
||||
)['total'] or 0
|
||||
|
||||
|
||||
scores_by_round[round.id][player.id] = player_score
|
||||
total_scores[player.id] += player_score
|
||||
|
||||
# On ajoute X experience au joueur (X: score du round)
|
||||
user_level = UserLevel.objects.get(user=player.user)
|
||||
last_round_updated = request.session.get(f'last_round_updated_{player.user.id}', 0)
|
||||
if last_round_updated < round.id:
|
||||
user_level.experience += player_score
|
||||
user_level.save()
|
||||
request.session[f'last_round_updated_{player.user.id}'] = round.id
|
||||
|
||||
# Organiser les réponses par joueur et par catégorie pour chaque round
|
||||
for round in rounds:
|
||||
answers = LittleBacAnswers.objects.filter(round=round)
|
||||
organized_answers = {}
|
||||
for player in players:
|
||||
organized_answers[player.id] = {}
|
||||
for category in categories:
|
||||
answer = answers.filter(player=player, category=category).first()
|
||||
if answer:
|
||||
organized_answers[player.id][category.id] = answer.answer
|
||||
else:
|
||||
organized_answers[player.id][category.id] = ""
|
||||
all_organized_answers[round.id] = organized_answers
|
||||
|
||||
# Mettre à jour les scores totaux des joueurs
|
||||
for player in players:
|
||||
player.score = total_scores[player.id]
|
||||
player.save()
|
||||
|
||||
return render(request, 'games/littlebac/results.html', {
|
||||
'game': game,
|
||||
'players': players,
|
||||
'rounds': rounds,
|
||||
'categories': categories,
|
||||
'all_organized_answers': all_organized_answers,
|
||||
'scores_by_round': scores_by_round,
|
||||
'total_scores': total_scores
|
||||
})
|
||||
|
||||
login_required()
|
||||
def game_little_bac_start_new_round(request, game_id):
|
||||
import random
|
||||
import string
|
||||
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
if game.author != request.user:
|
||||
return redirect('bac_party_games', party_id=game_id)
|
||||
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
|
||||
game.status = 'waiting'
|
||||
game.countdown_started = False
|
||||
game.countdown_start_time = None
|
||||
game.countdown_time = 0
|
||||
game.current_phase = "ready_game"
|
||||
game.save()
|
||||
|
||||
players.update(is_ready=False, status='playing')
|
||||
|
||||
# Sélectionne une lettre aléatoire pour le nouveau round
|
||||
alphabet = string.ascii_uppercase
|
||||
letter = random.choice(alphabet)
|
||||
|
||||
# Détermine le numéro du nouveau round
|
||||
round_counter = game.rounds.count() + 1
|
||||
|
||||
# Crée un nouveau round
|
||||
new_round = LittleBacRounds.objects.create(
|
||||
game=game,
|
||||
letter=letter,
|
||||
round_counter=round_counter
|
||||
)
|
||||
|
||||
return redirect('bac_party_games', party_id=game_id)
|
||||
|
||||
# API REST DES JEUX
|
||||
@login_required()
|
||||
def game_players_little_bac(request, game_id):
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
players_list = [{"id": player.id, "username": player.user.username, "score": player.score} for player in players]
|
||||
return JsonResponse({"game_id": game_id, "players": players_list})
|
||||
except LittleBacGames.DoesNotExist:
|
||||
return JsonResponse({"error": "Game not found"}, status=404)
|
||||
|
||||
@login_required()
|
||||
def toggle_ready_status_little_bac(request, game_id, player_id):
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
player = LittleBacPlayers.objects.get(game=game, user_id=player_id)
|
||||
|
||||
player.is_ready = not player.is_ready
|
||||
player.save()
|
||||
|
||||
# Vérifie si tous les joueurs de la partie sont prêts
|
||||
game = player.game
|
||||
print("Joueurs de la partie",game)
|
||||
all_ready = LittleBacPlayers.objects.filter(game=game, is_ready=True).count()
|
||||
print(all_ready)
|
||||
|
||||
return JsonResponse({"is_ready": player.is_ready, "all_ready": all_ready})
|
||||
except LittleBacPlayers.DoesNotExist:
|
||||
return JsonResponse({"error": "Player not found"}, status=404)
|
||||
|
||||
def game_infos_little_bac(request, game_id):
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
|
||||
return JsonResponse({
|
||||
"name": game.name,
|
||||
"status": game.status,
|
||||
"created": game.created,
|
||||
"all_ready": game.players.filter(is_ready=True).count()
|
||||
})
|
||||
except LittleBacGames.DoesNotExist:
|
||||
return JsonResponse({"error": "Party not found"}, status=404)
|
||||
|
||||
@login_required()
|
||||
def party_infos_little_bac(request, game_id):
|
||||
from django.utils.timezone import now
|
||||
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
players = LittleBacPlayers.objects.filter(game=game)
|
||||
players_data = [
|
||||
{"id": player.id, "username": player.user.username, "status": player.status, "score": player.score}
|
||||
for player in players
|
||||
]
|
||||
|
||||
countdown_remaining = None
|
||||
if game.countdown_started and game.countdown_start_time:
|
||||
elapsed_time = (now() - game.countdown_start_time).total_seconds()
|
||||
countdown_remaining = max(0, game.countdown_time - int(elapsed_time))
|
||||
|
||||
print(game.status)
|
||||
|
||||
return JsonResponse({
|
||||
"game_status": game.status,
|
||||
"players": players_data,
|
||||
"countdown_time": countdown_remaining,
|
||||
"countdown_started": game.countdown_started,
|
||||
"current_phase": game.current_phase if hasattr(game, 'current_phase') else None
|
||||
})
|
||||
except LittleBacGames.DoesNotExist:
|
||||
return JsonResponse({"error": "Game not found"}, status=404)
|
||||
|
||||
@login_required()
|
||||
def game_start_countdown(request, game_id):
|
||||
from django.utils.timezone import now
|
||||
|
||||
# Récupère le type de décompte (ready_game ou finish_game)
|
||||
countdown_type = request.GET.get("type", "ready_game")
|
||||
print(f"Type de décompte reçu: {countdown_type}") # Ajout de cette ligne pour vérifier le type de décompte
|
||||
|
||||
if countdown_type not in ["ready_game", "finish_game"]:
|
||||
return JsonResponse({"success": False, "error": "Type de décompte invalide."}, status=400)
|
||||
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
print(f"Statut du jeu: {game.status}") # Ajout de cette ligne pour vérifier le statut du jeu
|
||||
|
||||
# Vérification des conditions pour chaque décompte
|
||||
if countdown_type == "ready_game" and not game.countdown_started and game.status == "waiting":
|
||||
game.countdown_started = True
|
||||
game.countdown_start_time = now()
|
||||
game.countdown_time = 5
|
||||
game.save()
|
||||
|
||||
elif countdown_type == "finish_game" and game.status == "in_progress":
|
||||
print("Finish game")
|
||||
# Vérifie si un décompte précédent n'est pas actif
|
||||
elapsed_time = (now() - game.countdown_start_time).total_seconds()
|
||||
if not game.countdown_started or elapsed_time >= game.countdown_time:
|
||||
print("Start countdown")
|
||||
game.countdown_started = True
|
||||
game.countdown_start_time = now()
|
||||
game.countdown_time = 60
|
||||
game.current_phase = "finish_game"
|
||||
game.save()
|
||||
else:
|
||||
return JsonResponse({"success": False, "error": "Décompte déjà en cours."}, status=400)
|
||||
|
||||
else:
|
||||
return JsonResponse({"success": False, "error": "Condition de décompte non remplie."}, status=400)
|
||||
|
||||
return JsonResponse({
|
||||
"success": True,
|
||||
"countdown_started": game.countdown_started,
|
||||
"countdown_time": game.countdown_time
|
||||
})
|
||||
except LittleBacGames.DoesNotExist:
|
||||
return JsonResponse({"success": False, "error": "Game not found"}, status=404)
|
||||
|
||||
def game_countdown_status(request, game_id):
|
||||
from django.utils.timezone import now
|
||||
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
|
||||
if game.countdown_started and game.countdown_start_time:
|
||||
# Temps écoulé en secondes
|
||||
elapsed_time = (now() - game.countdown_start_time).total_seconds()
|
||||
remaining_time = max(0, game.countdown_time - int(elapsed_time))
|
||||
print(f"Elapsed time: {elapsed_time}, Remaining time: {remaining_time}") # Vérification
|
||||
else:
|
||||
print("Ouuups")
|
||||
remaining_time = game.countdown_time
|
||||
|
||||
return JsonResponse({
|
||||
"countdown_started": game.countdown_started,
|
||||
"countdown_time": remaining_time
|
||||
})
|
||||
|
||||
@login_required()
|
||||
def game_liitle_bac_end_game(request, game_id):
|
||||
from django.utils.timezone import now
|
||||
|
||||
try:
|
||||
game = LittleBacGames.objects.get(id=game_id)
|
||||
|
||||
# Mettre à jour l'état du jeu à "finished"
|
||||
game.status = 'finished'
|
||||
game.updated = now()
|
||||
game.save()
|
||||
|
||||
# Optionnel : Mettez à jour tous les joueurs pour les marquer comme ayant terminé
|
||||
LittleBacPlayers.objects.filter(game=game).update(status='overed')
|
||||
|
||||
return JsonResponse({"success": True, "message": "Game ended successfully."})
|
||||
except LittleBacGames.DoesNotExist:
|
||||
return JsonResponse({"error": "Game not found"}, status=404)
|
||||
Loading…
Add table
Add a link
Reference in a new issue