diff --git a/django/library/views.py b/django/library/views.py index 5efed6e..03cab19 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -176,23 +176,17 @@ def favorite_quiz(request, pk): statement="Das Quiz '"+ favorite.quiz.name+ "' wurde erfolgreich zu den Favoriten hinzugefügt!" messages.success(request,statement ) + favorite.save() else: favorite.favorite_date = None - - statement="Das Quiz '"+ favorite.quiz.name+ "' wurde erfolgreich von den Favoriten entfernt!" + favorite.delete() + statement="Das Quiz '"+ favorite.quiz.name+ "' wurde erfolgreich von den Favoriten entfernt!" messages.error(request, statement) - favorite.save() + else: QivipQuizFavorite.objects.create(user=request.user, quiz=quiz, favorite=True, favorite_date=timezone.now()) - """ - referer = request.META.get('HTTP_REFERER') - if referer and 'library/' in referer: - - return redirect(request.META.get('HTTP_REFERER', 'library:overview_quiz')) - else: - return overview_quiz(request) - """ + return redirect(request.META.get('HTTP_REFERER', 'library:overview_quiz')) @@ -255,33 +249,7 @@ def detail_quiz(request, pk): 'detail':show_answers, } return render(request, 'library/detail_quiz.html', context) -""" -def modified(request, pk): - original_quiz = get_object_or_404(QivipQuiz, pk=pk) - words=original_quiz.modified_description.split() - word_exist=0 - print(original_quiz.creator.username) - print(request.user.username) - - for word in words: - - if word==request.user.username or word+","==request.user.username: - word_exist=1 - - - if word_exist==0 and len(words)!=0 and request.user.username!=original_quiz.creator.username: - original_quiz.modified_description +=", "+ request.user.username - - - elif word_exist==0 and request.user.username!=original_quiz.creator.username: - original_quiz.modified_description +=" "+ request.user.username - - original_quiz.save() - - return redirect('library:detail_quiz', pk=pk) - -""" @login_required def copy_quiz(request, pk): diff --git a/django/play/urls.py b/django/play/urls.py index df8326c..261b472 100644 --- a/django/play/urls.py +++ b/django/play/urls.py @@ -13,4 +13,5 @@ urlpatterns = [ path('game//scores', views.scores, name='scores'), path('game//finished', views.finished, name='finished'), path('select_mode/', views.select_mode, name='select_mode'), + path('selected_mode/', views.selected_mode, name='selected_mode'), ] \ No newline at end of file diff --git a/django/play/views.py b/django/play/views.py index 253f55e..c2dbc3a 100644 --- a/django/play/views.py +++ b/django/play/views.py @@ -1,3 +1,4 @@ +from django.utils import timezone from django.shortcuts import render from django.shortcuts import render, redirect, get_object_or_404, reverse from play.models import QuizGame, QuizGameParticipant @@ -7,8 +8,12 @@ from django.contrib import messages import json + # Create your views here. def lobby(request, join_code): + + + quiz_game = get_object_or_404(QuizGame, join_code=join_code) quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id) context = { @@ -16,11 +21,19 @@ def lobby(request, join_code): 'quiz_game': quiz_game, } + + mode = request.GET.get('mode','default') + request.session['mode'] = mode if "host_id" in request.COOKIES: host_id = request.COOKIES['host_id'] if host_id == quiz_game.host_id: context['host_id'] = host_id return render(request, 'play/lobby.html', context=context) + elif mode=="learn": + return redirect('play:create_participant', join_code=join_code) + + + if not "participant_id" in request.COOKIES: return redirect('play:create_participant', join_code=join_code) @@ -44,9 +57,11 @@ def select_mode(request,join_code): def create_participant(request, join_code): + mode = request.GET.get('mode','default') + request.session['mode'] = mode quiz_game = get_object_or_404(QuizGame, join_code=join_code) - - if "participant_id" in request.COOKIES: + if mode!="learn": + if "participant_id" in request.COOKIES: # Prüfe ob der Teilnehmer noch existiert participant_id = request.COOKIES['participant_id'] try: @@ -62,7 +77,7 @@ def create_participant(request, join_code): response.delete_cookie('participant_id') return response - if request.method == 'POST': + if request.method == 'POST': display_name = request.POST.get('display_name') if not display_name: return render(request, 'play/initialize_participant.html', { @@ -80,9 +95,12 @@ def create_participant(request, join_code): response.set_cookie('participant_id', participant.participant_id, max_age=3600) return response - return render(request, 'play/initialize_participant.html', {'join_code': join_code}) - + return render(request, 'play/initialize_participant.html', {'join_code': join_code}) + else: + messages.error(request, "Beitritt nicht möglich.") + return render(request, 'play/join_game.html') def create_game(request, quiz_id): + try: quiz = QivipQuiz.objects.get(id=quiz_id) game = QuizGame() @@ -90,6 +108,7 @@ def create_game(request, quiz_id): game.host_id = game.generate_unique_id() game.save() + response = HttpResponseRedirect(reverse('play:select_mode', kwargs={'join_code': game.join_code})) response.set_cookie('host_id', game.host_id, max_age=3600) @@ -133,7 +152,7 @@ def finished(request, join_code): def scores(request, join_code): quiz_game = get_object_or_404(QuizGame, join_code=join_code) - + # Verify game state if quiz_game.current_state != 'scores': return redirect('play:lobby', join_code=join_code) @@ -151,9 +170,18 @@ def scores(request, join_code): current_question = questions[quiz_game.current_question_index] question_data = json.loads(current_question.data) + try: + my_participant = request.session.get('my_participant-participant_id') + participant = QuizGameParticipant.objects.get(participant_id=my_participant) + my_participant=participant + except: + participant = QuizGameParticipant.objects.none() + return render(request, 'play/game/score_overview.html', { 'quiz_game': quiz_game, 'participants': participants, + 'participant': participant, + 'my_participant':my_participant, 'is_host': is_host, 'host_id': quiz_game.host_id if is_host else None, 'is_last_question': quiz_game.current_question_index + 1 >= len(questions), @@ -230,8 +258,11 @@ def question(request, join_code): participant_id = request.COOKIES['participant_id'] try: participant = QuizGameParticipant.objects.get(participant_id=participant_id) + + except QuizGameParticipant.DoesNotExist: return redirect('play:create_participant', join_code=join_code) + request.session['my_participant-participant_id'] = participant.participant_id return render(request, 'play/game/question_participant.html', { 'quiz_game': quiz_game, @@ -263,4 +294,43 @@ def waiting_room(request, join_code): return render(request, 'play/game/wait_for_other_players.html', { 'quiz_game': quiz_game, 'participant': participant - }) \ No newline at end of file + }) + + +def selected_mode(request, join_code): + + + mode = request.GET.get('mode','default') + request.session['mode'] = mode + quiz_game = get_object_or_404(QuizGame, join_code=join_code) + quiz = get_object_or_404(QivipQuiz, pk=quiz_game.quiz_id.id) + QuizGameParticipant.objects.filter(quiz_game=quiz_game).delete() + + context = { + 'quiz': quiz, + 'quiz_game': quiz_game, + } + if "host_id" in request.COOKIES: + host_id = request.COOKIES['host_id'] + if host_id == quiz_game.host_id: + context['host_id'] = host_id + + if mode=="learn": + participant = QuizGameParticipant() + if request.user.is_authenticated: + participant.display_name = request.user.username + else: + participant.display_name = "SIE" + + participant.participant_id=host_id + participant.quiz_game = quiz_game + participant.score = 0 # Initialize score + participant.save() + quiz_game.current_state = "question" + quiz_game.question_start_time = timezone.now() + quiz_game.save() + + return redirect('play:question',join_code) + + + return render(request, 'play/lobby.html', context=context) diff --git a/django/templates/library/detail_quiz.html b/django/templates/library/detail_quiz.html index 17afbc8..f62d2bb 100644 --- a/django/templates/library/detail_quiz.html +++ b/django/templates/library/detail_quiz.html @@ -34,7 +34,8 @@ Quiz kopieren - + + {% if quiz.questions.count != 0 %} @@ -44,7 +45,8 @@ d="M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /> Spielen - + + {% endif %} diff --git a/django/templates/library/overview_quiz.html b/django/templates/library/overview_quiz.html index 4c27130..97ca65a 100644 --- a/django/templates/library/overview_quiz.html +++ b/django/templates/library/overview_quiz.html @@ -53,15 +53,15 @@
- + {{ form.min_amout_questions }}
- + {{ form.max_amout_questions }}
- + {{ form.user }}
@@ -193,6 +193,7 @@
+ {% if quiz.questions.count != 0 %} @@ -201,6 +202,9 @@ Spielen + {% else %} + Fürs Spielen ist min. 1 Frage nötig! + {% endif %}
@@ -377,6 +381,7 @@
+ {% if quiz.questions.count != 0 %} @@ -385,6 +390,10 @@ Spielen + {% else %} + Fürs Spielen ist min. 1 Frage nötig! + {% endif %} +
@@ -565,6 +574,7 @@
+ {% if quiz.questions.count != 0 %} @@ -573,6 +583,9 @@ Spielen + {% else %} + Fürs Spielen ist min. 1 Frage nötig! + {% endif %}
diff --git a/django/templates/play/game/finished.html b/django/templates/play/game/finished.html index b0300e1..9290126 100644 --- a/django/templates/play/game/finished.html +++ b/django/templates/play/game/finished.html @@ -7,7 +7,7 @@

Quiz beendet!

-

Vielen Dank fürs Mitspielen!

+

Vielen Dank fürs Spielen!

{% if not is_host %} diff --git a/django/templates/play/game/question_host.html b/django/templates/play/game/question_host.html index 777dd88..8345b06 100644 --- a/django/templates/play/game/question_host.html +++ b/django/templates/play/game/question_host.html @@ -1,7 +1,14 @@ {% extends 'base.html' %} - {% block content %} + +{% if request.session.mode == "learn" %} +
+
+ +
{% endif %}

Frage {{ question_index|add:1 }} von {{ total_questions }}

{{ question_data.question }}

@@ -12,29 +19,44 @@
{% endif %} + {% if request.session.mode == "learn" %} +
+ {% else %}
+ {% endif %} + {% if request.session.mode != "learn" %}
-

Verbleibende Zeit

+

Verbleibende Zeit

30

+ {% endif %} +

Antworten

0/0

-
+
{% for option in question_data.options %} -
+
+ {% endfor %}
+ + +
{% endblock %} + + {% block extra_js %} {% include 'play/game/common_scripts.html' %} {% endblock %} \ No newline at end of file diff --git a/django/templates/play/game/score_overview.html b/django/templates/play/game/score_overview.html index ae201ac..e8960d0 100644 --- a/django/templates/play/game/score_overview.html +++ b/django/templates/play/game/score_overview.html @@ -10,33 +10,43 @@

{{ question_data.question }}

{% for option in question_data.options %} -
+

{{ option.value }}

0 Antworten

{% endfor %}
- + {% if request.session.mode != "learn" %} +

Punktestand

{% for participant in participants %} + {% if participant == my_participant or is_host %} +
-
-

{{ participant.display_name }}

-

{{ participant.score }} Punkte

-
+
+

{{ participant.display_name }}

+

{{ participant.score }} Punkte

+
{% if participant.last_answer_correct %} {% elif participant.last_answer_correct == False %} {% endif %}
+ {% else %} + + + {% endif %} + + + {% endfor %}
- + {% endif %} {% if is_host %} {% if is_last_question %}