Files
pdga-rating/courses.html
T
Samuel Enocsson c88d092b36 Add user self-registration and implement rate limiting for predictions
Allow users to add themselves to the player database through a web form,
eliminating the need for manual pdga-numbers.txt updates. Implement 24-hour
rate limiting on prediction refreshes to prevent abuse while maintaining
reasonable update frequency.

Key changes:
- Add player self-registration with PDGA number lookup and confirmation
- Store predicted ratings in database for persistence across restarts
- Implement 24-hour rate limit on prediction refresh endpoint
- Make database the single source of truth (text file only for initial seed)
- Remove "Scrape All Layouts" bulk operation button
- Update "Load All" to refresh existing players instead of text file

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-11 18:20:03 +02:00

612 lines
21 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>PDGA Courses - Sweden</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
@media (max-width: 768px) {
body {
padding: 10px;
}
}
.container {
max-width: 1000px;
margin: 0 auto;
background: white;
padding: 30px;
border-radius: 8px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow-x: auto;
}
@media (max-width: 768px) {
.container {
padding: 15px;
border-radius: 0;
box-shadow: none;
}
}
h1 {
color: #333;
text-align: center;
margin-bottom: 30px;
}
@media (max-width: 768px) {
h1 {
font-size: 24px;
margin-bottom: 20px;
}
}
.nav-links {
text-align: center;
margin-bottom: 20px;
}
.nav-links a {
margin: 0 15px;
color: #007bff;
text-decoration: none;
font-weight: bold;
}
.nav-links a:hover {
text-decoration: underline;
}
.loading {
text-align: center;
padding: 20px;
font-size: 18px;
color: #666;
}
.controls {
text-align: right;
margin-bottom: 20px;
}
.btn {
padding: 8px 16px;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-left: 10px;
}
.btn:hover {
background-color: #0056b3;
}
.btn:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
.search-container {
margin-bottom: 20px;
text-align: center;
}
.search-input {
width: 100%;
max-width: 400px;
padding: 10px 15px;
font-size: 16px;
border: 2px solid #ddd;
border-radius: 4px;
outline: none;
transition: border-color 0.2s;
}
.search-input:focus {
border-color: #007bff;
}
.search-results-info {
text-align: center;
margin: 10px 0;
color: #666;
font-size: 14px;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
font-size: 14px;
}
th, td {
padding: 12px;
text-align: left;
border-bottom: 1px solid #ddd;
}
@media (max-width: 768px) {
table {
font-size: 12px;
}
th, td {
padding: 8px 4px;
}
.mobile-hide {
display: none;
}
}
th {
background-color: #f8f9fa;
font-weight: bold;
color: #495057;
}
tr:hover {
background-color: #f5f5f5;
}
.expandable-row {
cursor: pointer;
}
.expandable-row:hover {
background-color: #e3f2fd;
}
.expanded-content {
display: none;
background-color: #f8f9fa;
border-top: 2px solid #007bff;
}
.expanded-content td {
padding: 20px;
}
.layouts-container {
padding: 15px;
}
.layout-item {
padding: 10px;
margin: 5px 0;
background-color: white;
border-radius: 4px;
border: 1px solid #dee2e6;
display: flex;
justify-content: space-between;
align-items: center;
}
.layout-name {
font-weight: bold;
color: #333;
}
.layout-par {
color: #007bff;
font-weight: bold;
}
.no-layouts {
text-align: center;
color: #999;
font-style: italic;
padding: 20px;
}
.inactive-layouts-accordion {
margin-top: 15px;
border: 1px solid #dee2e6;
border-radius: 4px;
background-color: #f8f9fa;
}
.accordion-header {
padding: 12px 15px;
cursor: pointer;
user-select: none;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #e9ecef;
border-radius: 4px;
transition: background-color 0.2s;
}
.accordion-header:hover {
background-color: #dee2e6;
}
.accordion-header-text {
font-weight: 600;
color: #6c757d;
font-size: 14px;
}
.accordion-icon {
transition: transform 0.3s;
color: #6c757d;
}
.accordion-icon.expanded {
transform: rotate(180deg);
}
.accordion-content {
max-height: 0;
overflow: hidden;
transition: max-height 0.3s ease-out;
padding: 0 10px;
}
.accordion-content.expanded {
max-height: 2000px;
padding: 10px;
transition: max-height 0.5s ease-in;
}
.layout-item.inactive {
opacity: 0.7;
}
.refresh-icon {
cursor: pointer;
opacity: 0.6;
margin-left: 8px;
font-size: 13px;
color: #6c757d;
transition: all 0.2s ease;
padding: 2px;
border-radius: 3px;
}
.refresh-icon:hover {
opacity: 1;
color: #007bff;
background-color: rgba(0, 123, 255, 0.1);
transform: scale(1.1);
}
.refresh-icon.spinning {
animation: spin 1s linear infinite;
color: #007bff;
opacity: 1;
background-color: rgba(0, 123, 255, 0.1);
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>
<div class="container">
<h1>PDGA Courses - Sweden</h1>
<div class="nav-links">
<a href="/">Player Ratings</a>
<a href="/courses.html" style="color: #333;">Courses</a>
</div>
<div class="search-container">
<input
type="text"
class="search-input"
id="course-search"
placeholder="Search courses by name or city..."
oninput="searchCourses()"
/>
</div>
<div id="search-results-info" class="search-results-info"></div>
<div class="controls">
<button class="btn" onclick="scrapeCourses()" id="scrape-courses-btn">
<i class="fas fa-sync-alt"></i> Scrape Courses
</button>
</div>
<div id="loading" class="loading" style="display: none;">Loading courses...</div>
<div id="courses-table"></div>
</div>
<script>
let allCourses = [];
async function loadCourses() {
const loading = document.getElementById('loading');
const tableDiv = document.getElementById('courses-table');
loading.style.display = 'block';
tableDiv.innerHTML = '';
try {
const response = await fetch('/api/courses');
allCourses = await response.json();
loading.style.display = 'none';
displayCourses(allCourses);
updateSearchInfo(allCourses.length, allCourses.length);
} catch (error) {
console.error('Error loading courses:', error);
loading.style.display = 'none';
tableDiv.innerHTML = '<p>Error loading courses. Please try again.</p>';
}
}
function searchCourses() {
const searchInput = document.getElementById('course-search');
const searchTerm = searchInput.value.toLowerCase().trim();
if (!searchTerm) {
displayCourses(allCourses);
updateSearchInfo(allCourses.length, allCourses.length);
return;
}
const filtered = allCourses.filter(course => {
return course.name.toLowerCase().includes(searchTerm) ||
course.city.toLowerCase().includes(searchTerm);
});
displayCourses(filtered);
updateSearchInfo(filtered.length, allCourses.length);
}
function updateSearchInfo(showing, total) {
const infoDiv = document.getElementById('search-results-info');
if (showing === total) {
infoDiv.textContent = `Showing all ${total} courses`;
} else {
infoDiv.textContent = `Showing ${showing} of ${total} courses`;
}
}
function displayCourses(courses) {
const tableDiv = document.getElementById('courses-table');
if (courses.length === 0) {
tableDiv.innerHTML = '<p>No courses found. Click "Scrape Courses" to load Swedish courses from PDGA.</p>';
return;
}
let tableHTML = `
<table>
<thead>
<tr>
<th>Course Name</th>
<th class="mobile-hide">City</th>
<th class="mobile-hide">Last Updated</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
`;
courses.forEach(course => {
const lastUpdated = new Date(course.last_updated).toLocaleDateString('en-US', {
year: 'numeric',
month: 'short',
day: 'numeric'
});
tableHTML += `
<tr id="row-${course.id}" class="expandable-row" onclick="toggleCourseLayouts(${course.id})">
<td>
<a href="${course.link}" target="_blank" onclick="event.stopPropagation()">${course.name}</a>
<div class="mobile-only" style="font-size: 11px; color: #999; margin-top: 2px;">${course.city}</div>
</td>
<td class="mobile-hide">${course.city}</td>
<td class="mobile-hide">${lastUpdated}</td>
<td>
<i class="fas fa-sync-alt refresh-icon" onclick="scrapeLayouts(${course.id}, '${course.name.replace(/'/g, "\\'")}'); event.stopPropagation();" title="Scrape layouts for this course"></i>
</td>
</tr>
<tr id="layouts-${course.id}" class="expanded-content">
<td colspan="4">
<div class="layouts-container" id="layouts-container-${course.id}">
<div class="no-layouts">Click to load layouts...</div>
</div>
</td>
</tr>
`;
});
tableHTML += `
</tbody>
</table>
`;
tableDiv.innerHTML = tableHTML;
}
function toggleAccordion(accordionId) {
const content = document.getElementById(accordionId);
const icon = document.getElementById(`${accordionId}-icon`);
if (content.classList.contains('expanded')) {
content.classList.remove('expanded');
icon.classList.remove('expanded');
} else {
content.classList.add('expanded');
icon.classList.add('expanded');
}
}
async function toggleCourseLayouts(courseId) {
const layoutsRow = document.getElementById(`layouts-${courseId}`);
const layoutsContainer = document.getElementById(`layouts-container-${courseId}`);
if (layoutsRow.style.display === 'table-row') {
layoutsRow.style.display = 'none';
return;
}
layoutsRow.style.display = 'table-row';
// Check if layouts are already loaded
if (layoutsContainer.dataset.loaded === 'true') {
return;
}
// Show loading state
layoutsContainer.innerHTML = '<div class="no-layouts">Loading layouts...</div>';
try {
const response = await fetch(`/api/layouts/${courseId}`);
const layouts = await response.json();
if (layouts.length > 0) {
// Calculate date threshold (365 days ago)
const oneYearAgo = new Date();
oneYearAgo.setDate(oneYearAgo.getDate() - 365);
// Separate layouts into active and inactive
const activeLayouts = [];
const inactiveLayouts = [];
layouts.forEach(layout => {
if (layout.last_played) {
const lastPlayedDate = new Date(layout.last_played);
if (lastPlayedDate >= oneYearAgo) {
activeLayouts.push(layout);
} else {
inactiveLayouts.push(layout);
}
} else {
// Never played -> inactive
inactiveLayouts.push(layout);
}
});
let layoutsHTML = '<h4 style="margin-top: 0;">Layouts:</h4>';
// Render active layouts
if (activeLayouts.length > 0) {
activeLayouts.forEach(layout => {
const ratingDisplay = layout.mean_rating ?
`<span style="color: #28a745; font-weight: bold; margin-left: 10px;">Rating: ${layout.mean_rating}</span>` :
'';
const dateDisplay = layout.last_played ?
`<span style="color: #6c757d; font-size: 12px; margin-left: 10px;">Last played: ${new Date(layout.last_played).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' })}</span>` :
'';
layoutsHTML += `
<div class="layout-item">
<div>
<span class="layout-name">${layout.name}</span>
${dateDisplay}
</div>
<span class="layout-par">Par ${layout.par}${ratingDisplay}</span>
</div>
`;
});
}
// Render inactive layouts in accordion
if (inactiveLayouts.length > 0) {
const accordionId = `accordion-${courseId}`;
layoutsHTML += `
<div class="inactive-layouts-accordion">
<div class="accordion-header" onclick="toggleAccordion('${accordionId}')">
<span class="accordion-header-text">Inactive Layouts (${inactiveLayouts.length}) - Not played in last year</span>
<span class="accordion-icon" id="${accordionId}-icon">▼</span>
</div>
<div class="accordion-content" id="${accordionId}">
`;
inactiveLayouts.forEach(layout => {
const ratingDisplay = layout.mean_rating ?
`<span style="color: #28a745; font-weight: bold; margin-left: 10px;">Rating: ${layout.mean_rating}</span>` :
'';
const dateDisplay = layout.last_played ?
`<span style="color: #6c757d; font-size: 12px; margin-left: 10px;">Last played: ${new Date(layout.last_played).toLocaleDateString('en-US', { year: 'numeric', month: 'short', day: 'numeric' })}</span>` :
`<span style="color: #dc3545; font-size: 12px; margin-left: 10px;">Never played</span>`;
layoutsHTML += `
<div class="layout-item inactive">
<div>
<span class="layout-name">${layout.name}</span>
${dateDisplay}
</div>
<span class="layout-par">Par ${layout.par}${ratingDisplay}</span>
</div>
`;
});
layoutsHTML += `
</div>
</div>
`;
}
if (activeLayouts.length === 0 && inactiveLayouts.length === 0) {
layoutsHTML = '<div class="no-layouts">No layouts found. Click the refresh icon to scrape layouts.</div>';
}
layoutsContainer.innerHTML = layoutsHTML;
layoutsContainer.dataset.loaded = 'true';
} else {
layoutsContainer.innerHTML = '<div class="no-layouts">No layouts found. Click the refresh icon to scrape layouts.</div>';
}
} catch (error) {
console.error('Error loading layouts:', error);
layoutsContainer.innerHTML = '<div class="no-layouts">Error loading layouts</div>';
}
}
async function scrapeCourses() {
const btn = document.getElementById('scrape-courses-btn');
btn.disabled = true;
btn.innerHTML = '<i class="fas fa-sync-alt spinning"></i> Scraping...';
try {
const response = await fetch('/api/scrape-courses', {
method: 'POST'
});
const data = await response.json();
if (data.success) {
alert(data.message);
await loadCourses(); // Reload the courses list
searchCourses(); // Reapply search filter if any
} else {
alert('Failed to scrape courses');
}
} catch (error) {
console.error('Error scraping courses:', error);
alert('Error scraping courses');
} finally {
btn.disabled = false;
btn.innerHTML = '<i class="fas fa-sync-alt"></i> Scrape Courses';
}
}
async function scrapeLayouts(courseId, courseName) {
const icon = document.querySelector(`#row-${courseId} .refresh-icon`);
icon.classList.add('spinning');
try {
const response = await fetch(`/api/scrape-layouts/${courseId}`, {
method: 'POST'
});
const data = await response.json();
if (response.status === 409) {
// Scrape already in progress
alert(data.message || 'Scrape already in progress for this course. Please wait.');
} else if (data.success) {
// Reset the loaded state to force reload
const layoutsContainer = document.getElementById(`layouts-container-${courseId}`);
layoutsContainer.dataset.loaded = 'false';
// If the row is expanded, reload layouts
const layoutsRow = document.getElementById(`layouts-${courseId}`);
if (layoutsRow.style.display === 'table-row') {
toggleCourseLayouts(courseId);
// Re-expand to show new data
setTimeout(() => toggleCourseLayouts(courseId), 100);
}
alert(data.message);
} else {
alert('Failed to scrape layouts');
}
} catch (error) {
console.error('Error scraping layouts:', error);
alert('Error scraping layouts');
} finally {
icon.classList.remove('spinning');
}
}
// Load courses on page load
loadCourses();
</script>
</body>
</html>