|
| 1 | +from json import loads as json_loads, dumps as json_dumps |
| 2 | +from competition.utils import sum_methods |
| 3 | +from competition.serializers import EventRegistrationReadSerializer |
| 4 | +from competition.models import Series, Semester, EventRegistration |
| 5 | +from operator import itemgetter |
| 6 | + |
| 7 | + |
| 8 | +class FreezingNotClosedResults(Exception): |
| 9 | + """Snažíš sa zamraziť výsledky série, ktorá nemá opravené všetky riešenia""" |
| 10 | + |
| 11 | + |
| 12 | +def semester_results(self: Semester) -> dict: |
| 13 | + """Vyrobí výsledky semestra""" |
| 14 | + if self.frozen_results is not None: |
| 15 | + return json_loads(self.frozen_results) |
| 16 | + results = [] |
| 17 | + for registration in self.eventregistration_set.all(): |
| 18 | + results.append(_generate_result_row(registration, self)) |
| 19 | + |
| 20 | + results.sort(key=itemgetter('total'), reverse=True) |
| 21 | + results = _rank_results(results) |
| 22 | + return results |
| 23 | + |
| 24 | + |
| 25 | +def freeze_semester_results(semester: Semester): |
| 26 | + if any(not series.complete for series in semester.series_set.all()): |
| 27 | + raise FreezingNotClosedResults() |
| 28 | + |
| 29 | + semester.frozen_results = json_dumps(semester_results(semester)) |
| 30 | + |
| 31 | + |
| 32 | +def series_results(series: Series): |
| 33 | + results = [ |
| 34 | + _generate_result_row(registration, only_series=series) |
| 35 | + for registration in series.semester.eventregistration_set.all() |
| 36 | + ] |
| 37 | + |
| 38 | + results.sort(key=itemgetter('total'), reverse=True) |
| 39 | + |
| 40 | + return _rank_results(results) |
| 41 | + |
| 42 | + |
| 43 | +def freeze_series_results(series: Series): |
| 44 | + if any( |
| 45 | + problem.num_solutions != problem.num_corrected_solutions |
| 46 | + for problem in series.problems.all() |
| 47 | + ): |
| 48 | + raise FreezingNotClosedResults() |
| 49 | + |
| 50 | + series.frozen_results = json_dumps(series.results) |
| 51 | + |
| 52 | + |
| 53 | +def generate_praticipant_invitations( |
| 54 | + results_with_ranking: list[dict], |
| 55 | + number_of_participants: int, |
| 56 | + number_of_substitues: int, |
| 57 | +) -> list[dict]: |
| 58 | + invited_users = [] |
| 59 | + for i, result_row in enumerate(results_with_ranking): |
| 60 | + if i < number_of_participants: |
| 61 | + invited_users.append({ |
| 62 | + 'first_name': result_row['registration']['profile']['first_name'], |
| 63 | + 'last_name': result_row['registration']['profile']['last_name'], |
| 64 | + 'school': result_row['registration']['school'], |
| 65 | + 'is_participant': True |
| 66 | + }) |
| 67 | + elif i < number_of_participants+number_of_substitues: |
| 68 | + invited_users.append({ |
| 69 | + 'first_name': result_row['registration']['profile']['first_name'], |
| 70 | + 'last_name': result_row['registration']['profile']['last_name'], |
| 71 | + 'school': result_row['registration']['school'], |
| 72 | + 'is_participant': False |
| 73 | + }) |
| 74 | + return invited_users |
| 75 | + |
| 76 | + |
| 77 | +def _generate_result_row( |
| 78 | + semester_registration: EventRegistration, |
| 79 | + semester: Semester | None = None, |
| 80 | + only_series: Series | None = None, |
| 81 | +): |
| 82 | + """ |
| 83 | + Vygeneruje riadok výsledku pre používateľa. |
| 84 | + Ak je uvedený only_semester vygenerujú sa výsledky iba sa daný semester |
| 85 | + """ |
| 86 | + user_solutions = semester_registration.solution_set |
| 87 | + series_set = semester.series_set.order_by( |
| 88 | + 'order') if semester is not None else [only_series] |
| 89 | + solutions = [] |
| 90 | + subtotal = [] |
| 91 | + for series in series_set: |
| 92 | + series_solutions = [] |
| 93 | + solution_points = [] |
| 94 | + for problem in series.problems.order_by('order'): |
| 95 | + sol = user_solutions.filter(problem=problem).first() |
| 96 | + |
| 97 | + solution_points.append(sol.score or 0 if sol is not None else 0) |
| 98 | + series_solutions.append( |
| 99 | + { |
| 100 | + 'points': (str(sol.score if sol.score is not None else '?') |
| 101 | + if sol is not None else '-'), |
| 102 | + 'solution_pk': sol.pk if sol is not None else None, |
| 103 | + 'problem_pk': problem.pk, |
| 104 | + 'votes': 0 # TODO: Implement votes sol.vote |
| 105 | + } |
| 106 | + ) |
| 107 | + series_sum_func = getattr(sum_methods, series.sum_method or '', |
| 108 | + sum_methods.series_simple_sum) |
| 109 | + solutions.append(series_solutions) |
| 110 | + subtotal.append( |
| 111 | + series_sum_func(solution_points, semester_registration) |
| 112 | + ) |
| 113 | + return { |
| 114 | + # Poradie - horná hranica, v prípade deleného miesto(napr. 1.-3.) ide o nižšie miesto(1) |
| 115 | + 'rank_start': 0, |
| 116 | + # Poradie - dolná hranica, v prípade deleného miesto(napr. 1.-3.) ide o vyššie miesto(3) |
| 117 | + 'rank_end': 0, |
| 118 | + # Indikuje či sa zmenilo poradie od minulej priečky, slúži na delené miesta |
| 119 | + 'rank_changed': True, |
| 120 | + # primary key riešiteľovej registrácie do semestra |
| 121 | + 'registration': EventRegistrationReadSerializer(semester_registration).data, |
| 122 | + # Súčty bodov po sériách |
| 123 | + 'subtotal': subtotal, |
| 124 | + # Celkový súčet za danú entitu |
| 125 | + 'total': sum(subtotal), |
| 126 | + # Zoznam riešení, |
| 127 | + 'solutions': solutions |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | +def _rank_results(results: list[dict]) -> list[dict]: |
| 132 | + # Spodná hranica |
| 133 | + current_rank = 1 |
| 134 | + n_teams = 1 |
| 135 | + last_points = None |
| 136 | + for res in results: |
| 137 | + if last_points != res['total']: |
| 138 | + current_rank = n_teams |
| 139 | + last_points = res['total'] |
| 140 | + res['rank_changed'] = True |
| 141 | + else: |
| 142 | + res['rank_changed'] = False |
| 143 | + res['rank_start'] = current_rank |
| 144 | + n_teams += 1 |
| 145 | + |
| 146 | + # Horná hranica |
| 147 | + current_rank = len(results) |
| 148 | + n_teams = len(results) |
| 149 | + last_points = None |
| 150 | + for res in reversed(results): |
| 151 | + if last_points != res['total']: |
| 152 | + current_rank = n_teams |
| 153 | + last_points = res['total'] |
| 154 | + res['rank_end'] = current_rank |
| 155 | + n_teams -= 1 |
| 156 | + return results |
0 commit comments