jM<div>
<button onclick="showGross()">Gross</button>
<button onclick="showNett()">Nett</button>
<button onclick="showStableford()">Stableford</button>
</div>
<div id="leaderboardContainer">
<p>Test message: The leaderboard will display here once data is fetched.</p>
</div>
<script>
let players = [];
// Fetch player data from tourjson folder
function fetchPlayers() {
const playerFiles = [];
for (let i = 1; i <= 20; i++) {
const fileNumber = i.toString().padStart(2, '0'); // Ensure 2-digit format (e.g., 01, 02)
playerFiles.push(`/tourjson/player-${fileNumber}.json`);
}
// Fetch all player files
return Promise.all(playerFiles.map(file => fetch(file).then(response => response.json())));
}
// Function to display Gross leaderboard
function showGross() {
players.sort((a, b) => a.gross - b.gross);
const htmlContent = generateRankingListHtml(players, "Gross", "gross");
displayLeaderboard(htmlContent);
}
// Function to display Nett leaderboard
function showNett() {
players.sort((a, b) => a.nett - b.nett);
const htmlContent = generateRankingListHtml(players, "Nett", "nett");
displayLeaderboard(htmlContent);
}
// Function to display Stableford leaderboard
function showStableford() {
players.sort((a, b) => b.stableford - a.stableford);
const htmlContent = generateRankingListHtml(players, "Stableford", "stableford");
displayLeaderboard(htmlContent);
}
// Function to generate the leaderboard table based on the current ranking type
function generateRankingListHtml(players, rankingType, scoreField) {
let html = `<table>
<tr><th>Position</th><th>Player</th><th>Club Code</th><th>${rankingType}</th></tr>`;
players.forEach((player, index) => {
html += `<tr><td>${index + 1}</td><td>${player.playerName}</td><td>${player.clubCode}</td><td>${player[scoreField]}</td></tr>`;
});
html += `</table>`;
return html;
}
// Function to display the leaderboard in the container
function displayLeaderboard(htmlContent) {
const leaderboardContainer = document.getElementById('leaderboardContainer');
leaderboardContainer.innerHTML = htmlContent;
}
// Fetch players and display the Gross leaderboard by default
window.onload = function() {
fetchPlayers().then(fetchedPlayers => {
players = fetchedPlayers; // Store the fetched players
showGross(); // Show the gross leaderboard initially
}).catch(error => {
console.error('Error fetching player data:', error);
});
}
</script>
https://whatsonchain.com/tx/be3ae34566b40a38a27b5cd307e9b5cadfa11793af4143cae69195a89df45d1d