Print function correction + player analysis fix and player stats fix

This commit is contained in:
bl3kunja-FW
2025-08-10 18:22:22 +02:00
parent 054c81e78e
commit 33758e7340
7 changed files with 1678 additions and 383 deletions
+540 -38
View File
@@ -48,14 +48,14 @@
background: #f8f9fa;
border: 2px solid #e9ecef;
cursor: pointer;
padding: 8px 16px;
padding: 12px 20px;
border-radius: 8px;
transition: all 0.2s ease;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
color: #333;
text-decoration: none;
font-weight: bold;
font-size: 0.85rem;
font-size: 0.9rem;
}
.nav-btn:hover {
@@ -87,7 +87,7 @@
overflow: hidden;
}
/* Top Section - Stats and Chart */
/* Top Section - Stats and Charts */
.top-section {
display: grid;
grid-template-columns: 320px 1fr;
@@ -146,8 +146,8 @@
font-weight: 500;
}
/* Chart Panel */
.chart-panel {
/* Charts Panel */
.charts-panel {
background: white;
border-radius: 12px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
@@ -156,10 +156,151 @@
flex-direction: column;
}
/* Tournament Type Buttons */
.tournament-type-buttons {
display: flex;
gap: 10px;
margin-bottom: 15px;
flex-wrap: wrap;
}
.type-btn {
background: #f8f9fa;
border: 2px solid #e9ecef;
padding: 10px 16px;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
font-size: 0.9rem;
font-weight: 600;
color: #333;
display: flex;
align-items: center;
gap: 6px;
}
.type-btn:hover {
border-color: #007bff;
transform: translateY(-1px);
}
.type-btn.active {
color: white;
transform: translateY(-1px);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
}
.type-btn.active.targets-40 {
background: #dc3545;
border-color: #dc3545;
}
.type-btn.active.targets-20 {
background: #28a745;
border-color: #28a745;
}
.type-btn.active.targets-4 {
background: #007bff;
border-color: #007bff;
}
/* Chart Info Header */
.chart-info {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding: 12px;
background: #f8f9fa;
border-radius: 8px;
border: 1px solid #e9ecef;
}
.chart-stats {
display: flex;
gap: 20px;
font-size: 0.85rem;
}
.chart-stat {
display: flex;
flex-direction: column;
align-items: center;
text-align: center;
}
.chart-stat-value {
font-size: 1.1rem;
font-weight: bold;
color: #333;
}
.chart-stat-label {
color: #666;
font-size: 0.75rem;
text-transform: uppercase;
}
/* Shot Accuracy Stats */
.accuracy-stats {
display: flex;
flex-wrap: wrap;
gap: 8px;
font-size: 0.8rem;
justify-content: center;
}
.accuracy-stat {
display: flex;
flex-direction: column;
align-items: center;
padding: 4px 8px;
background: white;
border-radius: 6px;
border: 1px solid #e9ecef;
min-width: 35px;
transition: transform 0.2s ease;
}
.accuracy-stat:hover {
transform: scale(1.05);
box-shadow: 0 2px 6px rgba(0,0,0,0.1);
}
.accuracy-value {
font-weight: bold;
color: #333;
font-size: 0.9rem;
}
.accuracy-label {
color: #666;
font-size: 0.65rem;
font-weight: 500;
}
.chart-container {
position: relative;
flex: 1;
min-height: 180px;
min-height: 200px;
background: white;
border-radius: 6px;
border: 1px solid #e9ecef;
padding: 10px;
}
.no-data {
display: flex;
align-items: center;
justify-content: center;
height: 200px;
color: #999;
font-size: 0.9rem;
font-style: italic;
background: #f8f9fa;
border-radius: 6px;
border: 1px dashed #dee2e6;
}
/* Bottom Section - History */
@@ -412,7 +553,7 @@
</div>
<div class="container">
<!-- Top Section - Stats and Chart -->
<!-- Top Section - Stats and Charts -->
<div class="top-section">
<!-- Stats Panel -->
<div class="stats-panel">
@@ -445,11 +586,92 @@
</div>
</div>
<!-- Performance Chart -->
<div class="chart-panel">
<div class="panel-title">📈 Performance Trend</div>
<!-- Performance Charts -->
<div class="charts-panel">
<div class="panel-title">📈 Performance by Tournament Type</div>
<!-- Tournament Type Buttons -->
<div class="tournament-type-buttons">
<button class="type-btn active targets-40" data-type="40 Targets">
<span>💪</span> 40 Targets
</button>
<button class="type-btn targets-20" data-type="20 Targets">
<span></span> 20 Targets
</button>
<button class="type-btn targets-4" data-type="4 Targets">
<span>🎯</span> 4 Targets
</button>
</div>
<!-- Chart Info and Stats -->
<div class="chart-info" id="chartInfo">
<div class="chart-stats">
<div class="chart-stat">
<div class="chart-stat-value" id="gameCount">0</div>
<div class="chart-stat-label">Games</div>
</div>
<div class="chart-stat">
<div class="chart-stat-value" id="avgScore">0</div>
<div class="chart-stat-label">Average</div>
</div>
<div class="chart-stat">
<div class="chart-stat-value" id="bestScore">0</div>
<div class="chart-stat-label">Best</div>
</div>
</div>
<!-- Shot Accuracy Stats -->
<div class="accuracy-stats" id="accuracyStats">
<div class="accuracy-stat">
<div class="accuracy-value" id="tens">0</div>
<div class="accuracy-label">10s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="nines">0</div>
<div class="accuracy-label">9s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="eights">0</div>
<div class="accuracy-label">8s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="sevens">0</div>
<div class="accuracy-label">7s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="sixes">0</div>
<div class="accuracy-label">6s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="fives">0</div>
<div class="accuracy-label">5s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="fours">0</div>
<div class="accuracy-label">4s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="threes">0</div>
<div class="accuracy-label">3s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="twos">0</div>
<div class="accuracy-label">2s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="ones">0</div>
<div class="accuracy-label">1s</div>
</div>
<div class="accuracy-stat">
<div class="accuracy-value" id="zeros">0</div>
<div class="accuracy-label">0s</div>
</div>
</div>
</div>
<!-- Single Chart Container -->
<div class="chart-container">
<canvas id="performanceChart"></canvas>
<canvas id="tournamentChart"></canvas>
</div>
</div>
</div>
@@ -532,42 +754,283 @@
// Player data from Flask
const playerStats = {{ stats|tojson }};
// Tournament type configuration
const tournamentTypes = {
'40 Targets': { color: '#dc3545', icon: '💪', class: 'targets-40' },
'20 Targets': { color: '#28a745', icon: '⚡', class: 'targets-20' },
'4 Targets': { color: '#007bff', icon: '🎯', class: 'targets-4' }
};
let currentChart = null;
let currentTournamentType = '40 Targets'; // Will be updated based on available data
let tournamentsByType = {};
let shotAccuracyData = {};
// Initialize page
function initializePage() {
createPerformanceChart();
groupTournamentsByType();
setupEventListeners();
loadShotAccuracyData();
updateChart();
}
// Create performance trend chart
function createPerformanceChart() {
const ctx = document.getElementById('performanceChart').getContext('2d');
const scores = playerStats.tournament_scores || [];
// Load shot accuracy data from archive
async function loadShotAccuracyData() {
try {
const playerId = '{{ player.id }}'; // Get player ID from template
const response = await fetch(`/api/archive/player/${playerId}/shot-accuracy`);
const result = await response.json();
if (result.status === 'success') {
shotAccuracyData = result.data;
console.log('Shot accuracy data loaded:', shotAccuracyData);
// Auto-select the first tournament type with data
const availableTypes = Object.keys(shotAccuracyData);
if (availableTypes.length > 0) {
// Update current type to first available type
const firstAvailableType = availableTypes[0];
currentTournamentType = firstAvailableType;
// Update button states
updateActiveButton();
}
updateChart(); // Refresh chart with accuracy data
} else {
console.log('No shot accuracy data available:', result.message);
// Still try to show basic tournament data without shot accuracy
updateChart();
}
} catch (error) {
console.error('Error loading shot accuracy data:', error);
console.log('API endpoints may not be set up yet. Showing basic tournament data only.');
updateChart();
}
}
// Update active button based on current tournament type
function updateActiveButton() {
const typeButtons = document.querySelectorAll('.type-btn');
typeButtons.forEach(btn => {
btn.classList.remove('active');
if (btn.dataset.type === currentTournamentType) {
btn.classList.add('active');
}
});
}
// Setup event listeners
function setupEventListeners() {
const typeButtons = document.querySelectorAll('.type-btn');
typeButtons.forEach(btn => {
btn.addEventListener('click', () => {
// Update active button
typeButtons.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
// Update current type and chart
currentTournamentType = btn.dataset.type;
updateChart();
});
});
}
// Group tournaments by type based on tournament_type field or shots fired
function groupTournamentsByType() {
const tournamentHistory = playerStats.tournament_history || [];
tournamentsByType = {};
tournamentHistory.forEach(tournament => {
let type;
// First try to use the tournament_type field if it exists
if (tournament.tournament_type) {
const typeField = tournament.tournament_type.toLowerCase();
if (typeField.includes('40') || typeField.includes('forty')) {
type = '40 Targets';
} else if (typeField.includes('20') || typeField.includes('twenty')) {
type = '20 Targets';
} else if (typeField.includes('4') || typeField.includes('four')) {
type = '4 Targets';
}
}
// If we couldn't determine from tournament_type, fall back to shots_fired
if (!type) {
const shots = tournament.shots_fired;
if (shots >= 30) {
type = '40 Targets';
} else if (shots >= 10 && shots <= 29) {
type = '20 Targets';
} else if (shots <= 9) {
type = '4 Targets';
}
}
// Skip tournaments that don't match our types
if (!type) {
console.log('Could not categorize tournament:', tournament);
return;
}
if (!tournamentsByType[type]) {
tournamentsByType[type] = [];
}
tournamentsByType[type].push(tournament);
});
// Sort each type by date
Object.keys(tournamentsByType).forEach(type => {
tournamentsByType[type].sort((a, b) => new Date(a.date) - new Date(b.date));
});
console.log('Tournaments grouped by type:', tournamentsByType);
console.log('Available tournament types from database:', Object.keys(tournamentsByType));
if (scores.length === 0) {
ctx.font = '14px Arial';
ctx.fillStyle = '#666';
ctx.textAlign = 'center';
ctx.fillText('No tournament data available', ctx.canvas.width / 2, ctx.canvas.height / 2);
// If no 40 Targets tournaments, try to default to an available type
if (!tournamentsByType['40 Targets'] && Object.keys(tournamentsByType).length > 0) {
const availableTypes = Object.keys(tournamentsByType);
currentTournamentType = availableTypes[0];
console.log(`No 40 Targets tournaments found. Defaulting to: ${currentTournamentType}`);
updateActiveButton();
}
}
// Update chart and statistics for current tournament type
function updateChart() {
const tournaments = tournamentsByType[currentTournamentType] || [];
updateChartInfo(tournaments);
createChart(tournaments);
}
// Update chart information and statistics
function updateChartInfo(tournaments) {
const gameCount = tournaments.length;
const scores = tournaments.map(t => t.score);
const avgScore = gameCount > 0 ? Math.round(scores.reduce((a, b) => a + b, 0) / gameCount) : 0;
const bestScore = gameCount > 0 ? Math.max(...scores) : 0;
// Update basic stats
document.getElementById('gameCount').textContent = gameCount;
document.getElementById('avgScore').textContent = avgScore;
document.getElementById('bestScore').textContent = bestScore;
// Update shot accuracy stats (if available in tournament data)
updateAccuracyStats(tournaments);
}
// Update shot accuracy statistics
function updateAccuracyStats(tournaments) {
let tens = 0, nines = 0, eights = 0, sevens = 0, sixes = 0;
let fives = 0, fours = 0, threes = 0, twos = 0, ones = 0, zeros = 0;
let hasData = false;
tournaments.forEach(tournament => {
// Check if we have shot breakdown data for this tournament
if (tournament.shot_breakdown) {
hasData = true;
const breakdown = tournament.shot_breakdown;
tens += breakdown.tens || 0;
nines += breakdown.nines || 0;
eights += breakdown.eights || 0;
sevens += breakdown.sevens || 0;
sixes += breakdown.sixes || 0;
fives += breakdown.fives || 0;
fours += breakdown.fours || 0;
threes += breakdown.threes || 0;
twos += breakdown.twos || 0;
ones += breakdown.ones || 0;
zeros += breakdown.zeros || 0;
}
});
// Also check if we have aggregated shot accuracy data for this tournament type
if (shotAccuracyData && shotAccuracyData[currentTournamentType]) {
hasData = true;
const typeData = shotAccuracyData[currentTournamentType];
tens = typeData.tens || 0;
nines = typeData.nines || 0;
eights = typeData.eights || 0;
sevens = typeData.sevens || 0;
sixes = typeData.sixes || 0;
fives = typeData.fives || 0;
fours = typeData.fours || 0;
threes = typeData.threes || 0;
twos = typeData.twos || 0;
ones = typeData.ones || 0;
zeros = typeData.zeros || 0;
}
const accuracyStatsDiv = document.getElementById('accuracyStats');
if (!hasData) {
// Hide the accuracy stats section if no data is available
accuracyStatsDiv.style.display = 'none';
console.log(`No shot accuracy data available for ${currentTournamentType}`);
return;
}
// Show the section if data is available
accuracyStatsDiv.style.display = 'flex';
document.getElementById('tens').textContent = tens;
document.getElementById('nines').textContent = nines;
document.getElementById('eights').textContent = eights;
document.getElementById('sevens').textContent = sevens;
document.getElementById('sixes').textContent = sixes;
document.getElementById('fives').textContent = fives;
document.getElementById('fours').textContent = fours;
document.getElementById('threes').textContent = threes;
document.getElementById('twos').textContent = twos;
document.getElementById('ones').textContent = ones;
document.getElementById('zeros').textContent = zeros;
console.log(`Shot accuracy for ${currentTournamentType}:`, {
tens, nines, eights, sevens, sixes, fives, fours, threes, twos, ones, zeros
});
}
// Create chart for current tournament type
function createChart(tournaments) {
const canvas = document.getElementById('tournamentChart');
const ctx = canvas.getContext('2d');
new Chart(ctx, {
// Destroy existing chart if it exists
if (currentChart) {
currentChart.destroy();
}
if (tournaments.length === 0) {
// Show no data message
ctx.font = '16px Arial';
ctx.fillStyle = '#666';
ctx.textAlign = 'center';
ctx.fillText(`No ${currentTournamentType} tournaments found`, canvas.width / 2, canvas.height / 2);
return;
}
const typeConfig = tournamentTypes[currentTournamentType];
const scores = tournaments.map(t => t.score);
currentChart = new Chart(ctx, {
type: 'line',
data: {
labels: scores.map((_, i) => `T${i + 1}`),
labels: scores.map((_, i) => `${i + 1}`),
datasets: [{
label: 'Tournament Score',
label: `${currentTournamentType} Score`,
data: scores,
borderColor: '#28a745',
backgroundColor: 'rgba(40, 167, 69, 0.15)',
borderColor: typeConfig.color,
backgroundColor: typeConfig.color + '20',
borderWidth: 3,
fill: true,
tension: 0.4,
pointRadius: 5,
pointHoverRadius: 8,
pointBackgroundColor: '#28a745',
pointBackgroundColor: typeConfig.color,
pointBorderColor: '#fff',
pointBorderWidth: 2,
pointHoverBackgroundColor: '#1e7e34',
pointHoverBackgroundColor: typeConfig.color,
pointHoverBorderColor: '#fff'
}]
},
@@ -577,17 +1040,52 @@
plugins: {
legend: { display: false },
tooltip: {
backgroundColor: 'rgba(40, 167, 69, 0.9)',
backgroundColor: typeConfig.color + 'E6',
titleColor: '#fff',
bodyColor: '#fff',
borderColor: '#28a745',
borderColor: typeConfig.color,
borderWidth: 2,
callbacks: {
title: function(context) {
return `Tournament ${context[0].dataIndex + 1}`;
const tournament = tournaments[context[0].dataIndex];
return `Game ${context[0].dataIndex + 1} - ${tournament.date.split(' ')[0]}`;
},
label: function(context) {
return `Score: ${context.parsed.y}`;
const tournament = tournaments[context.dataIndex];
const labels = [`Score: ${context.parsed.y}`, `Shots: ${tournament.shots_fired}`];
// Add tournament type if available
if (tournament.tournament_type) {
labels.push(`Type: ${tournament.tournament_type.replace('_', ' ')}`);
}
// Add shot breakdown if available
if (tournament.shot_breakdown) {
const breakdown = tournament.shot_breakdown;
const shots = [];
if (breakdown.tens) shots.push(`10s: ${breakdown.tens}`);
if (breakdown.nines) shots.push(`9s: ${breakdown.nines}`);
if (breakdown.eights) shots.push(`8s: ${breakdown.eights}`);
if (breakdown.sevens) shots.push(`7s: ${breakdown.sevens}`);
if (breakdown.sixes) shots.push(`6s: ${breakdown.sixes}`);
if (breakdown.fives) shots.push(`5s: ${breakdown.fives}`);
if (breakdown.fours) shots.push(`4s: ${breakdown.fours}`);
if (breakdown.threes) shots.push(`3s: ${breakdown.threes}`);
if (breakdown.twos) shots.push(`2s: ${breakdown.twos}`);
if (breakdown.ones) shots.push(`1s: ${breakdown.ones}`);
if (breakdown.zeros) shots.push(`0s: ${breakdown.zeros}`);
if (shots.length > 0) {
// Show only top scoring shots to keep tooltip readable
const topShots = shots.slice(0, 5);
labels.push(topShots.join(', '));
if (shots.length > 5) {
labels.push('+ more...');
}
}
}
return labels;
}
}
}
@@ -595,17 +1093,21 @@
scales: {
y: {
beginAtZero: true,
grid: { color: 'rgba(40, 167, 69, 0.1)' },
grid: {
color: typeConfig.color + '20'
},
ticks: {
color: '#28a745',
font: { size: 11, weight: 'bold' }
color: typeConfig.color,
font: { size: 12, weight: 'bold' }
}
},
x: {
grid: { color: 'rgba(40, 167, 69, 0.1)' },
grid: {
color: typeConfig.color + '20'
},
ticks: {
color: '#28a745',
font: { size: 11, weight: 'bold' }
color: typeConfig.color,
font: { size: 12, weight: 'bold' }
}
}
},