TLAquarius / HCMUS_SE_Project_Football_Management

0 stars 1 forks source link

Implement head-to-head comparison (could be wrong) #1

Open nahoang1104 opened 3 months ago

nahoang1104 commented 3 months ago

Implement head-to-head comparison

def head_to_head(team1, team2):
    # Retrieve the matches between the two teams
    matches = Match.query.filter_by(season_id=team1.season_id, host_team_id=team1.team_id, guest_team_id=team2.team_id).all()
    matches += Match.query.filter_by(season_id=team1.season_id, host_team_id=team2.team_id, guest_team_id=team1.team_id).all()

    # Calculate the head-to-head score
    team1_score = sum(match.host_score for match in matches if match.host_team_id == team1.team_id) + \
                  sum(match.guest_score for match in matches if match.guest_team_id == team1.team_id)
    team2_score = sum(match.host_score for match in matches if match.host_team_id == team2.team_id) + \
                  sum(match.guest_score for match in matches if match.guest_team_id == team2.team_id)

    # Return the head-to-head comparison result
    if team1_score > team2_score:
        return 1
    elif team1_score < team2_score:
        return -1
    else:
        return 0

def update_team_rankings(self):
    rankings = TeamRanking.query.filter_by(season_id=self.season_id).all()

    # Sort the teams based on ranking criteria
    rankings.sort(key=lambda r: (
        r.total_points,
        r.win_loss_difference,
        r.total_wins, #implement total_goals, not total_wins
        # Additional head-to-head comparison should be implemented if needed
    ), reverse=True)

    #head-to-head
    for i in range(len(rankings) - 1):
        max = i
        for j in range(i+1, len(rankings)):
            if (rankings[max].total_points == rankings[j].total_points and 
                rankings[max].win_loss_difference == rankings[j].win_loss_difference and 
                rankings[max].total_wins == rankings[j].total_wins):
                head_to_head_result = self.head_to_head(rankings[max], rankings[j])
                if head_to_head_result == -1:
                    max = j
            else:
                break
        rankings[i], rankings[max] = rankings[max], rankings[i]

    # Update the rank attribute based on the sorted order
    for rank, team_ranking in enumerate(rankings, start=1):
        team_ranking.ranking = rank

    db.session.commit()
nahoang1104 commented 3 months ago

Add total_goals column to team_ranking class:

total_goals = db.Column(db.Integer, default=0)

Implement update_total_goals:

def update_total_goals(self):
    matches = Match.query.filter_by(host_team_id=self.team_id).all()
    matches += Match.query.filter_by(guest_team_id=self.team_id).all()
    self.total_goals = sum(match.host_score for match in matches if match.host_team_id == self.team_id) + \
                       sum(match.guest_score for match in matches if match.guest_team_id == self.team_id)
    db.session.commit()

also replace r.total_wins with r.total_goals in update_team_rankings pls