diff --git a/django/library/views.py b/django/library/views.py index 11fbfd0..5da7064 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -849,6 +849,7 @@ def question_list(request): def new_question(request): question_type = request.GET.get('type') quiz_id = request.GET.get('quiz_id') + question_data = None if not quiz_id: messages.error(request, 'Quiz ID muss angegeben werden.') @@ -860,7 +861,7 @@ def new_question(request): messages.error(request, 'Quiz nicht gefunden oder keine Berechtigung.') return redirect('library:overview_quiz') - if question_type not in ['multiple_choice', 'true_false','input','order']: + if question_type not in ['multiple_choice', 'true_false','input','order','guess']: base_url = reverse('library:new_question') return redirect(f'{base_url}?type=multiple_choice&quiz_id={quiz_id}') @@ -937,6 +938,24 @@ def new_question(request): 'question': question_text, 'options': options } + + + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': question_text, + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } @@ -1005,6 +1024,24 @@ def new_question(request): ] } standard_time_per_question = 30 + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': '', + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } + standard_time_per_question = 30 + template_name = f'library/question/question_{question_type}.html' context = { @@ -1157,6 +1194,23 @@ def edit_question(request, pk): 'question': question_text, 'options': options } + elif question_type == 'guess': + correct_answer = request.POST.get('correct_answer') + min_value = request.POST.get('min') + max_value = request.POST.get('max') + tolerance = request.POST.get('tolerance') + json_data = { + 'type': question_type, + 'question': question_text, + 'correct_answer': correct_answer, + 'min': min_value, + 'max': max_value, + 'tolerance': tolerance, + 'options': [ + {'value': correct_answer, 'is_correct': True} + ] + } + question.data = json.dumps(json_data) question.time_per_question = request.POST.get('time_per_question', '30') try: diff --git a/django/play/consumers/game.py b/django/play/consumers/game.py index 01b3abc..5500af7 100644 --- a/django/play/consumers/game.py +++ b/django/play/consumers/game.py @@ -210,15 +210,43 @@ class GameConsumer(AsyncWebsocketConsumer): correct_sequence = sorted(question_data["options"], key=lambda x: x["order"]) correct_values = [opt["value"] for opt in correct_sequence] is_correct = (answers_order == correct_values) - + + elif question_type == "guess": + try: + user_val = float(answer) + correct_val = float(question_data['options'][0].get('value', 0)) + tolerance = float(question_data.get('tolerance', 0)) + percentage = min(user_val, correct_val) / (max(user_val, correct_val) + 1e-5) + + + score_percentage= min(round(percentage, 3), 1.0) + if score_percentage>= abs(tolerance/100): + is_correct=True + else: + is_correct=False + + print(score_percentage) + + + except ValueError: + is_correct = False # falls keine Zahl eingegeben wurde + answer_index = 0 if is_correct else 1 + # Score berechnen + try: + if score_percentage==None: + score_percentage=1 + except: + score_percentage=1 + + score = 0 if is_correct: base_score = 1000 time_factor = time_remaining / int(current_question.time_per_question) / 1000 - score = int(base_score * (0.5 + 0.5 * time_factor)) - + score = int(base_score * (0.75 + 0.25 * time_factor))*score_percentage + # Antwort speichern oder aktualisieren answer_obj, created = QuizAnswer.objects.get_or_create( participant=participant, diff --git a/django/templates/library/detail_quiz.html b/django/templates/library/detail_quiz.html index 46312f7..111b946 100644 --- a/django/templates/library/detail_quiz.html +++ b/django/templates/library/detail_quiz.html @@ -115,6 +115,11 @@ class=" mt-1 bg-yellow-100 text-yellow-800 px-4 py-2 rounded-md text-xs lg:text-lg hover:border-blue-600 border-2"> Reihenfolge + + + Schätzen + {% endif %} @@ -135,8 +140,8 @@
{{ question.data.question }}