jMl$<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Enter Tournaments</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 900px;
margin: auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
h2 {
text-align: center;
margin-bottom: 20px;
}
.input-row, .player-entry-row {
display: flex;
gap: 10px;
margin-bottom: 15px;
}
.input-row div, .player-entry-row div {
flex: 1;
}
label {
font-weight: bold;
margin-bottom: 5px;
display: block;
}
input[type="text"], input[type="number"] {
width: 100%;
padding: 8px;
box-sizing: border-box;
border: 1px solid #ccc;
border-radius: 4px;
}
#submitPlayer {
padding: 10px 20px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-top: 10px;
}
#submitPlayer:hover {
background-color: #45a049;
}
</style>
</head>
<body>
<div class="container">
<h2>Available Tournaments</h2>
<!-- Country and Club Inputs in a Row -->
<div class="input-row">
<div>
<label for="country">Country Code:</label>
<input type="text" id="country" value="064">
</div>
<div>
<label for="club">Club Code:</label>
<input type="text" id="club" value="271">
</div>
<div>
<label> </label> <!-- Placeholder to align the button with the inputs -->
<button id="loadFiles">Select Tournament From List</button>
</div>
</div>
<h2>Player Entry</h2>
<div class="player-entry-row">
<div>
<label for="tournamentName">Tournament Name</label>
<input type="text" id="tournamentName" placeholder="Select a tournament" readonly>
</div>
<div>
<label for="playerName">Player Name</label>
<input type="text" id="playerName" placeholder="Enter player name">
</div>
<div>
<label for="playerNumber">Player Number</label>
<input type="text" id="playerNumber" placeholder="Enter player number">
</div>
<div>
<label for="teamName">Team Name</label>
<input type="text" id="teamName" placeholder="Enter team name">
</div>
<div>
<label for="handicap">Handicap</label>
<input type="number" id="handicap" value="0">
</div>
</div>
<button id="submitPlayer">Submit Player</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
const loadButton = document.getElementById('loadFiles');
const fileList = document.getElementById('fileList');
const tournamentNameInput = document.getElementById('tournamentName');
const submitButton = document.getElementById('submitPlayer');
const countryInput = document.getElementById('country');
const clubInput = document.getElementById('club');
let selectedTournament = null;
loadButton.addEventListener('click', async () => {
await fetchAndDisplayFiles();
});
// Fetch and display the list of tournaments, filtering for Player Number 0
async function fetchAndDisplayFiles() {
try {
const country = countryInput.value.trim();
const club = clubInput.value.trim();
const response = await fetch(`https://bsvgolf.com:8030/${country}/${club}/list-files`);
if (!response.ok) {
console.error(`Failed to fetch file list: ${response.status}`);
fileList.textContent = 'Failed to load files.';
return;
}
const { files } = await response.json();
const tourFiles = files.filter(file => {
const parts = file.split('.');
return parts.length >= 3 && parts[2] === '0';
});
displayFiles(tourFiles, country, club);
} catch (error) {
console.error('Error fetching files:', error);
fileList.textContent = 'Error loading files.';
}
}
// Display the list of tournaments with only the tournament name
function displayFiles(files, country, club) {
fileList.innerHTML = '';
if (files.length === 0) {
fileList.textContent = 'No TOUR tournaments found.';
return;
}
files.forEach(file => {
const div = document.createElement('div');
div.className = 'file-item';
// Extract only the tournament name (first segment of filename)
const tournamentName = file.split('.')[0];
const fileNameSpan = document.createElement('span');
fileNameSpan.textContent = tournamentName;
const selectButton = document.createElement('button');
selectButton.textContent = 'Select';
selectButton.addEventListener('click', () => selectTournament(file, country, club));
div.appendChild(fileNameSpan);
div.appendChild(selectButton);
fileList.appendChild(div);
});
}
// Store the selected tournament and populate the tournament name field
function selectTournament(file, country, club) {
selectedTournament = { file, country, club };
tournamentNameInput.value = file.split('.')[0]; // Populate tournament name
}
// Handle player submission
submitButton.addEventListener('click', async () => {
if (!selectedTournament) {
alert('Please select a tournament first.');
return;
}
const playerBlob = createPlayerBlob();
if (!playerBlob) {
alert('Please fill in all required player details.');
return;
}
await postPlayerData(playerBlob);
});
// Create player blob from input fields
function createPlayerBlob() {
const playerName = document.getElementById('playerName').value.trim();
const playerNumber = document.getElementById('playerNumber').value.trim();
const teamName = document.getElementById('teamName').value.trim();
const handicap = parseInt(document.getElementById('handicap').value) || 0;
const matchName = tournamentNameInput.value;
if (!playerName || !playerNumber || !matchName) return null;
return {
metadata: {
playerName,
playerNumber,
teamName,
handicap,
matchName,
country: countryInput.value.trim(),
club: clubInput.value.trim()
},
dataBlob: {
scores: Array(18).fill(0),
totals: {
grossTotal: 0,
nettTotal: 0,
stablefordTotal: 0
}
}
};
}
// Post player data to the backend, using the tournament creation endpoint
async function postPlayerData(playerData) {
const { country, club } = selectedTournament;
try {
const response = await fetch(`https://bsvgolf.com:8030/${country}/${club}/comp`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(playerData)
});
if (response.ok) {
alert('Player submitted successfully!');
console.log(`Player ${playerData.metadata.playerName} saved successfully!`);
} else {
const error = await response.json();
console.error(`Failed to submit player ${playerData.metadata.playerName}:`, error);
alert(`Error: ${error.message}`);
}
} catch (error) {
console.error('Network error submitting player:', error);
alert('Network error. Please try again.');
}
}
});
</script>
</body>
</html>
https://whatsonchain.com/tx/727e1cc4b8d0c435636d427f3f93d1b4fc10efd6ea2775a9b508c731e4fb9242