Logo Más Madrid

Herramienta de sorteos

Código JavaScript

document.getElementById('fileInput').addEventListener('change', handleFileSelect);

            function handleFileSelect(event) {
                const file = event.target.files[0];
                if (file) {
                    const reader = new FileReader();
                    reader.onload = function(e) {
                        const content = e.target.result;
                        const emails = processCSV(content);
                        displayEmails(emails);
                        document.getElementById('processButton').style.display = 'block';
                    };
                    reader.readAsText(file);
                }
            }
        
            function processCSV(data) {
                const lines = data.split('\n');
                const header = lines[0].split(',');
                const emailIndex = header.indexOf('Email');
                
                if (emailIndex === -1) {
                    alert("La columna 'Email' no se encuentra en el archivo CSV. Verifica el nombre de la columna.");
                    throw new Error("La columna 'Email' no se encuentra en el archivo CSV.");
                }
                
                const emails = lines.slice(1).map(line => {
                    const columns = line.split(',');
                    return columns[emailIndex];
                }).filter(email => email); // Elimina entradas vacías
        
                return emails.map(email => ocultarEmail(email));
            }
        
            function ocultarEmail(email) {
                const [nombre, dominio] = email.split('@');
                const mitad = Math.floor(nombre.length / 2);
                return nombre.slice(0, mitad) + '*'.repeat(nombre.length - mitad) + '@' + dominio;
            }
        
            function displayEmails(emails) {
                const emailsList = document.getElementById('emailsList');
                emailsList.innerHTML = '';
                emails.forEach(email => {
                    const li = document.createElement('li');
                    li.textContent = email;
                    emailsList.appendChild(li);
                });
            }
        
            function processFile() {
                const emails = Array.from(document.querySelectorAll('#emailsList li')).map(li => li.textContent);
                drawWinners(emails);
            }
        
            function drawWinners(emails) {
                const shuffledEmails = emails.sort(() => Math.random() - 0.5);
                const winners = shuffledEmails.slice(0, 2);
                const substitutes = shuffledEmails.slice(2, 6);
        
                displayResults(winners, substitutes);
            }
        
            function displayResults(winners, substitutes) {
                const resultSection = document.getElementById('result');
                const winnersList = document.getElementById('winnersList');
                const substitutesList = document.getElementById('substitutesList');
        
                winnersList.innerHTML = '';
                substitutesList.innerHTML = '';
        
                winners.forEach(email => {
                    const li = document.createElement('li');
                    li.textContent = email;
                    winnersList.appendChild(li);
                });
        
                substitutes.forEach(email => {
                    const li = document.createElement('li');
                    li.textContent = email;
                    substitutesList.appendChild(li);
                });
        
                resultSection.classList.remove('hidden');
            }