Files
fantacalcio-4h/partita.js
T

97 lines
3.2 KiB
JavaScript
Executable File

let numeroPartita = 1;
const SQUADRE_PARTITA = {
1: ['squadra1', 'squadra2'],
2: ['squadra1', 'squadra3'],
3: ['squadra2', 'squadra3']
}
function setPartita(n) {
numeroPartita = n;
let s1 = SQUADRE_PARTITA[n][0];
let s2 = SQUADRE_PARTITA[n][1]
document.getElementById('sq1').innerText = s1;
document.getElementById('sq2').innerText = s2;
riempi(s1, sq1);
riempi(s2, sq2);
}
const sq1 = document.getElementById('elenco-sq1');
const sq2 = document.getElementById('elenco-sq2');
function riempi(nome, lista) {
lista.innerHTML = '';
squadre[nome].forEach((n) => {
let li = document.createElement('li');
let giocatore = giocatori[n];
li.innerHTML = `${giocatore.nome} ${giocatore.cognome} | Assist: <input id='assist${n}' type='number' min='0' value='${giocatore.assist}'> | Goal: <input id='goal${n}' type='number' min='0' value='${giocatore.goal}'> | Voto: <span id='voto${n}'>6</span>`;
li.id = 'giocatore' + n;
lista.appendChild(li);
// Aggiungi event listener per aggiornare il voto quando assist o goal cambiano
document.getElementById(`assist${n}`).addEventListener('input', () => {
aggiornaVoto(n);
});
document.getElementById(`goal${n}`).addEventListener('input', () => {
aggiornaVoto(n);
});
});
}
function aggiornaVoto(n) {
let assist = parseInt(document.getElementById(`assist${n}`).value) || 0;
let goal = parseInt(document.getElementById(`goal${n}`).value) || 0;
let voto = 6 + (assist * 1) + (goal * 3);
if (voto>=6){
document.getElementById(`voto${n}`).style.backgroundColor = "#41b776";
} else {
document.getElementById(`voto${n}`).style.backgroundColor = "red";
}
document.getElementById(`voto${n}`).innerText = voto;
}
setPartita(1);
function salvaVoti() {
if (!confirm("Vuoi salvare i voti delle partite?")) return;
// Aggiorna assist e goal
Object.keys(giocatori).forEach(n => {
let assistInput = document.getElementById(`assist${n}`);
let goalInput = document.getElementById(`goal${n}`);
if (assistInput && goalInput) {
giocatori[n].assist = parseInt(assistInput.value) || 0;
giocatori[n].goal = parseInt(goalInput.value) || 0;
giocatori[n].partiteGiocate = (giocatori[n].partiteGiocate || 0) + 1;
}
});
// Aggiunge i voti
Object.keys(voti).forEach(n => {
let votoSpan = document.getElementById(`voto${n}`);
if (!votoSpan) return; // se non esiste lo span, salta
if (!Array.isArray(voti[n])) return; // se non è array, salta
let nuovoVoto = parseFloat(votoSpan.innerText);
if (isNaN(nuovoVoto)) return; // se non è numero, salta
voti[n].push(nuovoVoto);
});
fetch("salva_voti.php", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ voti, giocatori })
})
.then(res => res.json())
.then(data => console.log("Server risponde:", data))
.catch(err => console.error("Errore:", err));
}
function home(){
if (!confirm("Tutti i dati non salvati andranno persi. Vuoi tornare alla home?")) return;
window.location = './';
}