diff --git a/index.html b/index.html index e4b3441..ae07936 100644 --- a/index.html +++ b/index.html @@ -380,6 +380,33 @@

Openings Repertoire

+
+
+

Deep Game Analysis Tool

+

Enter your Chess.com or Lichess username below for a quick evaluation of your latest games.

+ +
+
+ + + + + +
+ +
+

Ready for analysis...

+
+
+
+
+

Student Inquiry Form

@@ -443,6 +470,98 @@

Student Inquiry F

+ // --- Start Lichess Analysis Functionality --- + +document.addEventListener('DOMContentLoaded', () => { + const analyzeButton = document.getElementById('analyze-button'); + const usernameInput = document.getElementById('username-input'); + const platformSelect = document.getElementById('platform-select'); + const resultsContainer = document.getElementById('analysis-results'); + const statusElement = document.getElementById('analysis-status'); + + if (analyzeButton) { + analyzeButton.addEventListener('click', handleAnalysis); + } + + async function handleAnalysis() { + const username = usernameInput.value.trim(); + const platform = platformSelect.value; + resultsContainer.innerHTML = ''; + + if (!username) { + statusElement.textContent = "Please enter a username."; + return; + } + + statusElement.textContent = `Analyzing ${username} on ${platform}...`; + + if (platform === 'lichess') { + await fetchLichessData(username); + } else { + statusElement.textContent = "Chess.com analysis is currently unavailable. Please use Lichess."; + // Note: Chess.com's API requires more complex rate limits/setup. + } + } + + async function fetchLichessData(username) { + try { + // Fetch the user's profile and ratings + const userResponse = await fetch(`https://lichess.org/api/user/${username}`); + if (!userResponse.ok) { + statusElement.textContent = "Error: Lichess user not found or profile is private."; + return; + } + const userData = await userResponse.json(); + + // Fetch recent 5 games (in PGN format) + // Lichess API allows fetching games in PGN, which we will parse for simple stats. + const gamesResponse = await fetch(`https://lichess.org/api/games/user/${username}?max=5&perfType=rapid,blitz&rated=true`); + + if (!gamesResponse.ok) { + statusElement.textContent = "Error: Could not retrieve recent games."; + return; + } + + const gamesText = await gamesResponse.text(); + + // 1. Display Current Ratings + let ratingsHTML = ` +

Current Ratings:

+ + `; + + // 2. Simple Game Summary (Parsing PGN is complex, so we'll do a simple match count) + const wins = (gamesText.match(/1-0/g) || []).length; + const draws = (gamesText.match(/1\/2-1\/2/g) || []).length; + const losses = (gamesText.match(/0-1/g) || []).length; + + ratingsHTML += ` +

Latest 5 Games:

+
+ Wins: ${wins} + Draws: ${draws} + Losses: ${losses} +
+ `; + + + resultsContainer.innerHTML = ratingsHTML; + statusElement.textContent = "Analysis complete!"; + + } catch (error) { + console.error("Lichess API Error:", error); + statusElement.textContent = "A technical error occurred during analysis."; + } + } + + // Note: If you want this to work, ensure the handleAnalysis function is defined + // before the DOMContentLoaded listener attempts to assign it to the button's click event. +}); +// --- End Lichess Analysis Functionality --- +