const RESULTS_PER_PAGE = 20; let allResults = []; let currentPage = 0; var board = Chessboard("board", { draggable: true, dropOffBoard: "trash", sparePieces: true, pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png", onSnapEnd: updateInfo, }); function updateInfo() { document.getElementById('fen').value = board.fen(); let pos = board.position(); let pieces = Object.keys(pos).length; } function search() { let pos = board.position(); if (Object.keys(pos).length < 3) { document.getElementById("results").innerHTML = "

Add at least 3 pieces to search.

"; return; } document.getElementById("results").innerHTML = "

Searching...

"; fetch("/search", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ fen: board.fen() }), }) .then((r) => r.json()) .then((data) => { allResults = data.results; window.searchTimeMs = data.time_ms; currentPage = 0; displayPage(); }); } // function search() { // let pos = board.position(); // if (Object.keys(pos).length < 3) { // document.getElementById("results").innerHTML = "

Add at least 3 pieces to search.

"; // return; // } // allResults = []; // document.getElementById("results").innerHTML = "

Searching...

"; // const startTime = performance.now(); // fetch("/search", { // method: "POST", // headers: { "Content-Type": "application/json" }, // body: JSON.stringify({ fen: board.fen() }), // }) // .then(r => r.body.getReader()) // .then(reader => { // const decoder = new TextDecoder(); // let buffer = ''; // function read() { // reader.read().then(({done, value}) => { // if (done) { // window.searchTimeMs = performance.now() - startTime; // currentPage = 0; // displayPage(); // return; // } // buffer += decoder.decode(value, {stream: true}); // const lines = buffer.split('\n'); // buffer = lines.pop(); // lines.forEach(line => { // if (line.trim()) { // allResults.push(JSON.parse(line)); // if (allResults.length === 1 || allResults.length % 20 === 0) displayPage(); // } // }); // read(); // }); // } // read(); // }); // } function displayPage() { const start = currentPage * RESULTS_PER_PAGE, end = start + RESULTS_PER_PAGE, pageResults = allResults.slice(start, end), totalPages = Math.ceil(allResults.length / RESULTS_PER_PAGE); const paginationHtml = allResults.length > 0 ? `` : ""; let html = `

Found ${allResults.length} puzzles${allResults.length > 0 ? ` in ${window.searchTimeMs.toFixed(0)}ms (page ${currentPage + 1} of ${totalPages})` : ""}

`; html += paginationHtml; html += `
`; pageResults.forEach((p, i) => { const idx = start + i; html += `

${p.PuzzleId}

Rating: ${p.Rating} | Pop: ${p.Popularity}%

${p.Themes.map((t) => `${t}`).join(" ")}

Move ${p.MatchedMove}

`; }); html += `
`; html += paginationHtml; document.getElementById("results").innerHTML = html; pageResults.forEach((p, i) => Chessboard(`result-board-${start + i}`, { position: p.FEN, pieceTheme: "/static/img/chesspieces/wikipedia/{piece}.png", }) ); } function nextPage() { if ((currentPage + 1) * RESULTS_PER_PAGE < allResults.length) { currentPage++; displayPage(); } } function prevPage() { if (currentPage > 0) { currentPage--; displayPage(); } } updateInfo();