119 lines
5.3 KiB
JavaScript
Executable File
119 lines
5.3 KiB
JavaScript
Executable File
|
|
Object.keys(giocatori).forEach(numero => {
|
|
let giocatore = giocatori[numero];
|
|
document.getElementById('elencoGiocatori').innerHTML += `<div class="giocatore" onclick="window.location='giocatore.php?giocatore=${numero}'">${numero} ${giocatore.nome} ${giocatore.cognome}</div>`;
|
|
});
|
|
Object.keys(squadre).forEach(squadra => {
|
|
document.getElementById('elencoSquadre').innerHTML += `<option value="${squadra}">${squadra}</option>`;
|
|
});
|
|
|
|
elencoSquadre.addEventListener('change', (event) => {
|
|
let squadra = event.target.value;
|
|
giocatoriSquadra.innerHTML = '';
|
|
squadre[squadra].forEach(numero => {
|
|
let giocatore = giocatori[numero];
|
|
giocatoriSquadra.innerHTML += `<div class="giocatore" onclick="window.location='giocatore.php?giocatore=${numero}'">${numero} ${giocatore.nome} ${giocatore.cognome}</div>`;
|
|
});
|
|
});
|
|
|
|
function generaSquadre() {
|
|
// Chiedi conferma prima di procedere
|
|
if (!confirm('Sei sicuro di voler generare nuove squadre? Le squadre attuali verranno sovrascritte.')) {
|
|
return;
|
|
}
|
|
|
|
// Crea un nuovo oggetto squadre per non modificare quello globale prima del salvataggio
|
|
const giocatoriForti = shuffleArray([15, 17, 19]); // Capitani
|
|
tuttiGiocatori = shuffleArray(Object.keys(giocatori).filter(giocatore => !giocatoriForti.includes(parseInt(giocatore))));
|
|
const nuoveSquadre = {
|
|
"squadra1": [giocatoriForti[0], ...tuttiGiocatori.slice(0, 5)],
|
|
"squadra2": [giocatoriForti[1], ...tuttiGiocatori.slice(5, 10)],
|
|
"squadra3": [giocatoriForti[2], ...tuttiGiocatori.slice(10, 15)]
|
|
};
|
|
|
|
// Invia i dati al server per il salvataggio
|
|
fetch('salva_squadre.php', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
body: JSON.stringify(nuoveSquadre)
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
alert(data.message); // Mostra il messaggio di successo o errore
|
|
if (data.success) {
|
|
// Ricarica la pagina per visualizzare le nuove squadre salvate
|
|
window.location.reload();
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Errore durante il salvataggio delle squadre:', error);
|
|
alert('Si è verificato un errore di rete durante il salvataggio.');
|
|
});
|
|
}
|
|
function shuffleArray(array) {
|
|
for (let i = array.length - 1; i > 0; i--) {
|
|
const j = Math.floor(Math.random() * (i + 1));
|
|
[array[i], array[j]] = [array[j], array[i]];
|
|
}
|
|
return array;
|
|
}
|
|
|
|
// Logica di Import
|
|
const fileInput = document.getElementById('file-input');
|
|
let importType = '';
|
|
|
|
document.getElementById('import-giocatori').addEventListener('click', () => {
|
|
importType = 'giocatori';
|
|
fileInput.click();
|
|
});
|
|
|
|
document.getElementById('import-voti').addEventListener('click', () => {
|
|
importType = 'voti';
|
|
fileInput.click();
|
|
});
|
|
|
|
fileInput.addEventListener('change', (event) => {
|
|
const file = event.target.files[0];
|
|
if (!file) {
|
|
return;
|
|
}
|
|
|
|
// Validazione del nome del file
|
|
let expectedFileName = '';
|
|
if (importType === 'giocatori') {
|
|
expectedFileName = 'giocatori.csv';
|
|
} else if (importType === 'voti') {
|
|
expectedFileName = 'voti.csv';
|
|
}
|
|
|
|
if (!file.name.toLowerCase().endsWith(expectedFileName)) {
|
|
alert(`Per favore, seleziona un file chiamato "${expectedFileName}".`);
|
|
event.target.value = ''; // Reset file input
|
|
return;
|
|
}
|
|
|
|
const formData = new FormData();
|
|
formData.append('csv-file', file);
|
|
formData.append('type', importType);
|
|
|
|
fetch('upload.php', {
|
|
method: 'POST',
|
|
body: formData
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
alert(data.message);
|
|
if (data.success) {
|
|
window.location.reload();
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Errore:', error);
|
|
alert('Si è verificato un errore durante il caricamento.');
|
|
});
|
|
|
|
// Reset file input
|
|
event.target.value = '';
|
|
}); |