async function loadMatch(container, team1, team2) { const storageKey = `match_${team1}_${team2}`; try { const res = await fetch("https://love-weld-nine.vercel.app/api/espn"); const data = await res.json(); if (!data.events?.length) { loadFromStorage("No matches available"); return; } const match = data.events.find( e => e.competitions?.[0]?.competitors?.some(c => c.team?.name === team1) && e.competitions?.[0]?.competitors?.some(c => c.team?.name === team2) ); if (!match) { loadFromStorage("No upcoming or live match found for selected teams"); return; } localStorage.setItem(storageKey, JSON.stringify(match)); displayMatch(match); if (match.status?.type?.state === "in") { setTimeout(() => loadMatch(container, team1, team2), 30000); } } catch (err) { console.error(err); loadFromStorage("❌ Failed to load match data"); } function loadFromStorage(fallbackMsg) { const stored = localStorage.getItem(storageKey); if (stored) { const match = JSON.parse(stored); displayMatch(match, true); } else { container.innerHTML = fallbackMsg; } } function displayMatch(match, fromStorage = false) { const comp = match.competitions[0]; const home = comp.competitors?.find(c => c.homeAway === "home"); const away = comp.competitors?.find(c => c.homeAway === "away"); const homeName = home?.team?.name || "Home"; const awayName = away?.team?.name || "Away"; const kickoff = new Date(match.date).toLocaleString("en-GB", { timeZone: "Europe/Paris", dateStyle: "short", timeStyle: "short" }); const isLive = match.status?.type?.state === "in"; const isComplete = match.status?.type?.state === "post"; const clock = match.status?.displayClock || match.status?.clock || ""; const liveButton = isLive ? '🔴 LIVE' : isComplete ? '✅ FULL TIME' : '⏰ SCHEDULED'; const homeForm = home?.form ?.split("") .map( f => `${f}` ) .join("") || ""; const awayForm = away?.form ?.split("") .map( f => `${f}` ) .join("") || ""; const homeStats = home?.statistics || []; const awayStats = away?.statistics || []; const statsMap = {}; homeStats.forEach(h => { const key = h.name; const homeVal = h.displayValue || h.value || "-"; const awayObj = awayStats.find(a => a.name === key); const awayVal = awayObj?.displayValue || awayObj?.value || "-"; statsMap[key] = { home: homeVal, away: awayVal }; }); awayStats.forEach(a => { if (!statsMap[a.name]) { const homeObj = homeStats.find(h => h.name === a.name); const homeVal = homeObj?.displayValue || homeObj?.value || "-"; const awayVal = a.displayValue || a.value || "-"; statsMap[a.name] = { home: homeVal, away: awayVal }; } }); let statsHtml = ""; Object.entries(statsMap).forEach(([key, val]) => { statsHtml += `
No match events available
"; container.innerHTML = ` `; } } // Auto-init for all containers document.querySelectorAll(".match-widget-container").forEach(container => { const team1 = container.getAttribute("data-team1"); const team2 = container.getAttribute("data-team2"); loadMatch(container, team1, team2); setInterval(() => loadMatch(container, team1, team2), 30000); });