Die Datenstruktur für die Inning-Ergebnisse (`inning_results`) war in der `record_results`-View (`calendars/views.py`) und der `team_statistics`-View (`team_stats/views.py`) inkonsistent. In `calendars/views.py` wurde eine verschachtelte Struktur mit `inning_` als Schlüssel verwendet, während in `team_stats/views.py` eine Struktur mit 'home'- und 'away'-Listen erwartet wurde. Diese Inkonsistenz führte dazu, dass die Inning-Ergebnisse in der Team-Statistik nicht korrekt angezeigt wurden. Der Fix vereinheitlicht die Datenstruktur, sodass die `record_results`-View die Ergebnisse in dem von der `team_statistics`-View erwarteten Format speichert. Zudem wurde die Logik zum Laden der Formulardaten in `record_results` angepasst.
99 lines
3.1 KiB
Python
99 lines
3.1 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from django.contrib.auth.decorators import login_required
|
|
from django.http import HttpResponseForbidden
|
|
from clubs.models import Team
|
|
from calendars.models import Game, GameResult
|
|
|
|
@login_required
|
|
def team_statistics(request, team_id):
|
|
team = get_object_or_404(Team, pk=team_id)
|
|
|
|
# Check if the user is the head coach of the team
|
|
if request.user != team.head_coach:
|
|
return HttpResponseForbidden("You are not authorized to view this page.")
|
|
|
|
games = Game.objects.filter(team=team, result__isnull=False).order_by('start_time')
|
|
|
|
wins = 0
|
|
losses = 0
|
|
runs_scored = 0
|
|
runs_allowed = 0
|
|
inning_runs = {i: 0 for i in range(1, 10)}
|
|
streak_counter = 0
|
|
current_streak_type = None
|
|
last_game_result = None
|
|
|
|
for game in games:
|
|
result = game.result
|
|
print("DEBUGGER:"+str(result))
|
|
|
|
home_innings = [inning for inning in result.inning_results.get('home', []) if isinstance(inning, int)]
|
|
away_innings = [inning for inning in result.inning_results.get('away', []) if isinstance(inning, int)]
|
|
home_score = sum(home_innings)
|
|
away_score = sum(away_innings)
|
|
print("HOMEINNING:"+str(home_innings))
|
|
if game.is_home_game:
|
|
team_score = home_score
|
|
opponent_score = away_score
|
|
else:
|
|
team_score = away_score
|
|
opponent_score = home_score
|
|
|
|
runs_scored += team_score
|
|
runs_allowed += opponent_score
|
|
|
|
# W-L Record and Streak
|
|
if team_score > opponent_score:
|
|
wins += 1
|
|
if last_game_result == 'win':
|
|
streak_counter += 1
|
|
else:
|
|
streak_counter = 1
|
|
last_game_result = 'win'
|
|
elif team_score < opponent_score:
|
|
losses += 1
|
|
if last_game_result == 'loss':
|
|
streak_counter += 1
|
|
else:
|
|
streak_counter = 1
|
|
last_game_result = 'loss'
|
|
# Inning Heatmap
|
|
team_innings = home_innings if game.is_home_game else away_innings
|
|
for i, runs in enumerate(team_innings):
|
|
if i + 1 in inning_runs:
|
|
inning_runs[i + 1] += runs
|
|
|
|
# Winning Percentage (PCT)
|
|
total_games = wins + losses
|
|
pct = (wins / total_games) * 100 if total_games > 0 else 0
|
|
|
|
# Streak
|
|
if last_game_result == 'win':
|
|
streak_str = f"Won {streak_counter}"
|
|
elif last_game_result == 'loss':
|
|
streak_str = f"Lost {streak_counter}"
|
|
else:
|
|
streak_str = "N/A"
|
|
|
|
# Pythagorean Winning Percentage
|
|
if runs_scored > 0 or runs_allowed > 0:
|
|
pythagorean_pct = (runs_scored**2 / (runs_scored**2 + runs_allowed**2)) * 100
|
|
else:
|
|
pythagorean_pct = 0
|
|
|
|
|
|
context = {
|
|
'team': team,
|
|
'wins': wins,
|
|
'losses': losses,
|
|
'pct': pct,
|
|
'streak': streak_str,
|
|
'runs_scored': runs_scored,
|
|
'runs_allowed': runs_allowed,
|
|
'pythagorean_pct': pythagorean_pct,
|
|
'inning_runs': inning_runs,
|
|
}
|
|
print("DebuggeR:"+str(context))
|
|
|
|
return render(request, 'team_stats/team_statistics.html', context)
|