wanderhome-quiz/server/app/tools/quiz.py

29 lines
1.1 KiB
Python
Raw Normal View History

2022-08-29 17:09:07 +01:00
from .data import load
def evaluate_answers(answers:list) -> dict:
playbooks = load('playbooks.json')
questions = load('questions.json')
scores = dict.fromkeys(playbooks,0)
for index, answer in enumerate(answers):
question = questions[index]
if type(answer) is list:
answer = answer[0:question['select']]
for _answer in answer:
for match in question['answers'][int(_answer)]['matches']: scores[match] += 1
else:
for match in question['answers'][int(answer)]['matches']: scores[match] += 1
return scores
def compile_results(results:dict) -> dict:
output = {
'all_playbooks': results.copy(),
'playbooks': [ ],
'score': max(results.values()),
'max_score': 0
}
for question in load('questions.json'):
output['max_score'] += question.pop('max', 1)
playbooks = load('playbooks.json')
for playbook, score in results.items():
if score == output['score']: output['playbooks'].append({playbook: playbooks[playbook].copy()})
return output