diff --git a/server/app/tools/quiz.py b/server/app/tools/quiz.py new file mode 100644 index 0000000..ccbdd67 --- /dev/null +++ b/server/app/tools/quiz.py @@ -0,0 +1,29 @@ +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 \ No newline at end of file diff --git a/server/app/views.py b/server/app/views.py index 3e7b947..2bf37bb 100644 --- a/server/app/views.py +++ b/server/app/views.py @@ -1,6 +1,7 @@ -from .tools.data import check_file, load, render_questions +from .tools.data import check_file, load +from .tools.quiz import compile_results, evaluate_answers, render_questions -from flask import Blueprint +from flask import Blueprint, jsonify, request from flask.helpers import abort views = Blueprint( @@ -14,10 +15,13 @@ def _fetch(data_type): if data_type == 'questions': return render_questions() return load(f'{data_type}.json') -@views.route('/submit', methods=['POST']) +@views.route('/submit/', methods=['POST']) def _submit(): - pass + answers = request.json + scores = evaluate_answers(answers) + results = compile_results(results=scores) + return jsonify(results) -@views.route('/results') +@views.route('/results/') def _results(): pass \ No newline at end of file