from data.sources import sources from data.playbooks import playbooks from data.labels import labels from data.questions import questions from random import randrange def evaluate_quiz(submission): # Set up dictionaries to count scores results = { 'playbooks' : {}, 'selected_playbooks': {}, 'display_labels': [], 'max_score': 0 } for playbook in playbooks: if playbooks[playbook]['source'] in set.intersection(set(sources), set(submission)): results['playbooks'][playbook] = 0 for answer in submission: if answer.startswith('q'): qno = int(answer[1:]) - 1 ano = int(submission[answer]) - 1 match_list = questions[qno]['answers'][ano]['matches'] for match in match_list: if match in results['playbooks']: results['playbooks'][match] += match_list[match] results['max_score'] += max(match_list.values()) high_score = max(results['playbooks'].values()) for playbook, score in results['playbooks'].items(): if score == high_score: results['selected_playbooks'][playbook] = {} for label in labels: if 'custom' not in labels[label] or not labels[label]['custom']: results['selected_playbooks'][playbook][label] = playbooks[playbook]['labels'][label] if label not in results['display_labels']: results['display_labels'].append(label) else: if playbook in labels[label]['playbooks']: if label in playbooks[playbook]['labels']: results['selected_playbooks'][playbook][label] = playbooks[playbook]['labels'][label] else: results['selected_playbooks'][playbook][label] = labels[label]['default_value'] if label not in results['display_labels']: results['display_labels'].append(label) if 'joined' in results['selected_playbooks']: if len(results['selected_playbooks']) > 1: l = list(results['selected_playbooks']) l.remove('joined') i = randrange(len(l)-1) results['joined_cloned'] = l[i] results['selected_playbooks']['joined'] = results['selected_playbooks'][l[i]].copy() else: d = results['playbooks'].copy() del d['joined'] high_score = max(d.values()) p = [] for playbook, score in d.items(): if score == high_score: p.append(playbook) p_sel = p[randrange(len(p)-1)] if len(p) > 1 else p[0] results['selected_playbooks'][p_sel] = {} results['joined_cloned'] = p_sel for label in labels: if 'custom' not in labels[label] or not labels[label]['custom']: results['selected_playbooks'][p_sel][label] = playbooks[playbook]['labels'][label] if label not in results['display_labels']: results['display_labels'].append(label) else: if playbook in labels[label]['playbooks']: if label in playbooks[playbook]['labels']: results['selected_playbooks'][p_sel][label] = playbooks[playbook]['labels'][label] else: results['selected_playbooks'][p_sel][label] = labels[label]['default_value'] if label not in results['display_labels']: results['display_labels'].append(label) results['selected_playbooks']['joined'] = results['selected_playbooks'][p_sel].copy() for answer in submission: if answer.startswith('q'): qno = int(answer[1:]) - 1 ano = int(submission[answer]) - 1 for increase in questions[qno]['answers'][ano]['increase']: for playbook in results['selected_playbooks']: if increase in results['selected_playbooks'][playbook]: if results['selected_playbooks'][playbook][increase] < 3: results['selected_playbooks'][playbook][increase] += 1 for decrease in questions[qno]['answers'][ano]['decrease']: for playbook in results['selected_playbooks']: if decrease in results['selected_playbooks'][playbook]: if results['selected_playbooks'][playbook][decrease] > -2: results['selected_playbooks'][playbook][decrease] -= 1 return results