document.addEventListener('DOMContentLoaded', () => {
const leaderboardBody = document.getElementById('leaderboard-body');
let currentLeaderboard = [];
let previousLeaderboard = [];
// Carica leaderboard precedente
try {
previousLeaderboard = JSON.parse(localStorage.getItem('previousLeaderboard') || '[]');
} catch(e) {
previousLeaderboard = [];
}
// Genera frecce movimento piĆ¹ eleganti
function generateMovementArrow(wallet, currentRank) {
const prevEntry = previousLeaderboard.find(p => p.wallet === wallet);
if (!prevEntry) return '';
const prevRank = previousLeaderboard.findIndex(p => p.wallet === wallet) + 1;
if (currentRank < prevRank) {
return ``;
} else if (currentRank > prevRank) {
return ``;
}
return ``;
}
// Carica leaderboard
async function loadLeaderboard() {
try {
const response = await fetch('wallet_scores.csv');
const csvText = await response.text();
const rows = csvText.trim().split('\n').slice(1);
currentLeaderboard = rows.map((row, index) => {
const [wallet, score, synergies] = row.split(',');
return {
wallet,
score: parseFloat(score),
synergies: parseInt(synergies),
rank: index + 1
};
}).sort((a, b) => b.score - a.score);
// Salva per prossimo confronto
localStorage.setItem('previousLeaderboard', JSON.stringify(currentLeaderboard));
renderLeaderboard();
} catch (error) {
console.error('Errore caricamento leaderboard:', error);
}
}
// Rendering leaderboard
function renderLeaderboard() {
leaderboardBody.innerHTML = currentLeaderboard.map(entry => `
${entry.rank} |
${entry.wallet.substr(0,6)}...${entry.wallet.substr(-4)}
${generateMovementArrow(entry.wallet, entry.rank)}
|
${entry.score.toFixed(2)} |
`).join('');
}
// Mostra dettagli wallet
window.showWalletDetails = function(wallet) {
// Crea un modal personalizzato
const modal = document.createElement('div');
modal.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0,0,0,0.8);
display: flex;
justify-content: center;
align-items: center;
z-index: 1000;
`;
const modalContent = document.createElement('div');
modalContent.style.cssText = `
background: #1a1a1a;
padding: 30px;
border-radius: 10px;
max-width: 400px;
width: 90%;
color: white;
text-align: center;
`;
// Trova i dettagli del wallet
const walletEntry = currentLeaderboard.find(entry => entry.wallet === wallet);
if (walletEntry) {
modalContent.innerHTML = `
Wallet Details
Address: ${wallet}
Rank: ${walletEntry.rank}
Score: ${walletEntry.score.toFixed(2)}
Synergies: ${walletEntry.synergies}
`;
} else {
modalContent.innerHTML = `
Wallet Not Found
`;
}
modal.appendChild(modalContent);
document.body.appendChild(modal);
}
// Funzione di ricerca wallet
window.filterWallet = function() {
const walletInput = document.getElementById('walletInput').value.toLowerCase();
// Se input vuoto, mostra intera leaderboard
if (!walletInput) {
renderLeaderboard();
return;
}
const filteredLeaderboard = currentLeaderboard.filter(entry =>
entry.wallet.toLowerCase().includes(walletInput)
);
// Rendering leaderboard filtrata
leaderboardBody.innerHTML = filteredLeaderboard.map(entry => `
${entry.rank} |
${entry.wallet.substr(0,6)}...${entry.wallet.substr(-4)}
${generateMovementArrow(entry.wallet, entry.rank)}
|
${entry.score.toFixed(2)} |
`).join('');
}
// Carica la leaderboard all'avvio
loadLeaderboard();
// Aggiorna periodicamente la leaderboard (ogni 5 minuti)
setInterval(loadLeaderboard, 5 * 60 * 1000);
});