~ajk/idpa-results

72ccf05045a429272d8570d13efc160455e437ea — Andrew Kay 2 years ago dfe37d5
Implementing 2017 total time calculation
2 files changed, 15 insertions(+), 6 deletions(-)

M katerina.py
M models.py
M katerina.py => katerina.py +13 -3
@@ 135,7 135,7 @@ def load_match(session, key, correlate_shooters, name_generator):
    
    scores_data = scores_response.json()
    
    load_stage_scores(session, stages, match_entries, match_data["match_penalties"], scores_data)
    load_stage_scores(session, match, stages, match_entries, match_data["match_penalties"], scores_data)

    return match



@@ 165,7 165,9 @@ def load_match_entries(session, match, shooters_data, correlate_shooters, name_g

    return match_entries

def load_stage_scores(session, stages, match_entries, match_penalties, scores_data):
def load_stage_scores(session, match, stages, match_entries, match_penalties, scores_data):
    match_year = match.date.year

    for stage_scores in scores_data["match_scores"]:
        stage = stages[stage_scores["stage_uuid"]]
        


@@ 176,11 178,19 @@ def load_stage_scores(session, stages, match_entries, match_penalties, scores_da
                time = sum([string_time for string_time in score["str"]])
                points_down = sum([target_points_down for target_points_down in score["tpts"]])
                penalties = sum([count * match_penalties[index]["pen_val"] for (index, count) in enumerate(score["pens"])])

                total_time = calculate_total_time(time, points_down, penalties, match_year)
            
                stage_score = StageScore(stage, match_entry, time, points_down, penalties)
                stage_score = StageScore(stage, match_entry, time, points_down, penalties, total_time)

                session.add(stage_score)

def calculate_total_time(time, points_down, penalties, year):
    if year < 2017:
        return time + (points_down * 0.5) + penalties
    else:
        return time + points_down + penalties

def remove_empty_match_entries(session, match):
    session.execute(select([func.remove_empty_match_entries(match.id)]))


M models.py => models.py +2 -3
@@ 101,14 101,13 @@ class StageScore(Base):
    stage = relationship("Stage", innerjoin=True)
    match_entry = relationship("MatchEntry", innerjoin=True)

    def __init__(self, stage, match_entry, time, points_down, penalties):
    def __init__(self, stage, match_entry, time, points_down, penalties, total_time):
        self.stage = stage
        self.match_entry = match_entry
        self.time = time
        self.points_down = points_down
        self.penalties = penalties

        self.total_time = self.time + (self.points_down * 0.5) + self.penalties
        self.total_time = total_time

class MatchScore(Base):
    __tablename__ = "match_scores"