Reihenfolge_Fragetyp(und Lizenzenz_Dateien)
This commit is contained in:
@@ -112,7 +112,6 @@ class GameConsumer(AsyncWebsocketConsumer):
|
||||
await self.send(text_data=json.dumps({
|
||||
'type': 'answer_stats',
|
||||
'stats': stats,
|
||||
'answer': answer,
|
||||
}))
|
||||
|
||||
elif message_type == 'update_participants':
|
||||
@@ -175,93 +174,122 @@ class GameConsumer(AsyncWebsocketConsumer):
|
||||
return False
|
||||
|
||||
@database_sync_to_async
|
||||
def save_answer(self, participant_id, answer_index, time_remaining):
|
||||
try:
|
||||
participant = QuizGameParticipant.objects.get(
|
||||
quiz_game__join_code=self.join_code,
|
||||
participant_id=participant_id
|
||||
)
|
||||
quiz_game = participant.quiz_game
|
||||
current_question = quiz_game.quiz_id.questions.all()[quiz_game.current_question_index]
|
||||
|
||||
# Check if answer is correct
|
||||
is_correct = False
|
||||
question_data = json.loads(current_question.data)
|
||||
print(answer_index)
|
||||
try:
|
||||
if answer_index >= 0 : # -1 means timeout
|
||||
is_correct = question_data['options'][answer_index]['is_correct']
|
||||
print(is_correct)
|
||||
except:
|
||||
user_input = str(answer_index).strip().lower().replace(" ", "")
|
||||
correct_value = str(question_data['options'][0].get('value', '')).strip().lower().replace(" ", "")
|
||||
print("Richtig",correct_value)
|
||||
print(user_input)
|
||||
if user_input == correct_value:
|
||||
is_correct=True
|
||||
answer_index=int(0)
|
||||
print(is_correct)
|
||||
else:
|
||||
answer_index=int(1)
|
||||
def save_answer(self, participant_id, answer, time_remaining):
|
||||
try:
|
||||
participant = QuizGameParticipant.objects.get(
|
||||
quiz_game__join_code=self.join_code,
|
||||
participant_id=participant_id
|
||||
)
|
||||
quiz_game = participant.quiz_game
|
||||
current_question = quiz_game.quiz_id.questions.all()[quiz_game.current_question_index]
|
||||
|
||||
question_data = json.loads(current_question.data)
|
||||
question_type = question_data.get("type")
|
||||
|
||||
|
||||
|
||||
|
||||
is_correct = False
|
||||
answer_index = None
|
||||
answers_order = None
|
||||
answer_text = None
|
||||
|
||||
if question_type in ["multiple_choice", "true_false"]:
|
||||
# answer ist hier eine Integer-Antwort (Index)
|
||||
answer_index = int(answer)
|
||||
if answer_index >= 0:
|
||||
is_correct = question_data['options'][answer_index].get('is_correct', False)
|
||||
|
||||
elif question_type == "input":
|
||||
user_input = str(answer).strip().lower().replace(" ", "")
|
||||
correct_value = str(question_data['options'][0].get('value', '')).strip().lower().replace(" ", "")
|
||||
is_correct = (user_input == correct_value)
|
||||
answer_text = user_input
|
||||
answer_index = 0 if is_correct else 1
|
||||
|
||||
elif question_type == "order":
|
||||
# answer ist hier eine Liste von Strings mit der Reihenfolge
|
||||
answers_order = answer
|
||||
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)
|
||||
|
||||
# Calculate score based on correctness and time
|
||||
score = 0
|
||||
if is_correct:
|
||||
base_score = 1000
|
||||
time_factor = time_remaining / int(current_question.time_per_question)/int(1000)
|
||||
score = int(base_score * (0.5 + 0.5 * time_factor))
|
||||
|
||||
# Update or create answer in QuizAnswer table
|
||||
answer_obj, created = QuizAnswer.objects.get_or_create(
|
||||
participant=participant,
|
||||
question_index=quiz_game.current_question_index,
|
||||
defaults={
|
||||
'answer_index': answer_index,
|
||||
'is_correct': is_correct,
|
||||
'score': score,
|
||||
'time_remaining': time_remaining
|
||||
}
|
||||
)
|
||||
|
||||
if not created:
|
||||
answer_obj.answer_index = answer_index
|
||||
answer_obj.is_correct = is_correct
|
||||
answer_obj.score = score
|
||||
answer_obj.time_remaining = time_remaining
|
||||
answer_obj.save()
|
||||
|
||||
participant.last_score = score
|
||||
participant.score += score
|
||||
participant.last_answer_correct = is_correct
|
||||
participant.save()
|
||||
|
||||
return True
|
||||
except (QuizGameParticipant.DoesNotExist, IndexError):
|
||||
return False
|
||||
|
||||
# Score berechnen
|
||||
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))
|
||||
|
||||
# Antwort speichern oder aktualisieren
|
||||
answer_obj, created = QuizAnswer.objects.get_or_create(
|
||||
participant=participant,
|
||||
question_index=quiz_game.current_question_index,
|
||||
defaults={
|
||||
'answer_index': answer_index,
|
||||
'answers_order': answers_order,
|
||||
'is_correct': is_correct,
|
||||
'score': score,
|
||||
'time_remaining': time_remaining
|
||||
}
|
||||
)
|
||||
if not created:
|
||||
answer_obj.answer_index = answer_index
|
||||
answer_obj.answers_order = answers_order
|
||||
answer_obj.is_correct = is_correct
|
||||
answer_obj.score = score
|
||||
answer_obj.time_remaining = time_remaining
|
||||
answer_obj.save()
|
||||
|
||||
# Teilnehmer aktualisieren
|
||||
participant.last_score = score
|
||||
participant.score += score
|
||||
participant.last_answer=answer_index
|
||||
participant.last_answer_correct = is_correct
|
||||
participant.save()
|
||||
|
||||
return True
|
||||
except (QuizGameParticipant.DoesNotExist, IndexError):
|
||||
return False
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def get_answer_stats(self):
|
||||
try:
|
||||
quiz_game = QuizGame.objects.get(join_code=self.join_code)
|
||||
try:
|
||||
quiz_game = QuizGame.objects.get(join_code=self.join_code)
|
||||
answers = QuizAnswer.objects.filter(
|
||||
participant__quiz_game=quiz_game,
|
||||
question_index=quiz_game.current_question_index
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
current_question = quiz_game.quiz_id.questions.all()[quiz_game.current_question_index]
|
||||
|
||||
question_data = json.loads(current_question.data)
|
||||
question_type = question_data.get("type")
|
||||
stats = {}
|
||||
|
||||
for single_answer in answers:
|
||||
|
||||
# Count answers for each option in the current question
|
||||
stats = {}
|
||||
answers = QuizAnswer.objects.filter(
|
||||
participant__quiz_game=quiz_game,
|
||||
question_index=quiz_game.current_question_index
|
||||
)
|
||||
|
||||
for answer in answers:
|
||||
if answer.answer_index >= 0: # Ignore timeouts (-1)
|
||||
stats[answer.answer_index] = stats.get(answer.answer_index, 0) + 1
|
||||
|
||||
return stats
|
||||
except QuizGame.DoesNotExist:
|
||||
return {}
|
||||
if question_type == "order":
|
||||
correct_answer=[str(opt["value"]) for opt in sorted(question_data["options"], key=lambda x: x["order"])]
|
||||
|
||||
if single_answer.answers_order== correct_answer:
|
||||
key = 0 # richtig
|
||||
else:
|
||||
key = 1 # falsch
|
||||
stats[key] = stats.get(key, 0) + 1
|
||||
else:
|
||||
key = str(single_answer.answer_index if single_answer.answer_index is not None else "unanswered")
|
||||
stats[key] = stats.get(key, 0) + 1
|
||||
return stats
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@database_sync_to_async
|
||||
def save_rating(self, participant_id, rating):
|
||||
|
||||
Reference in New Issue
Block a user