Rafs-an09002's picture
Create static/script.js
4d8520a verified
// ১. ইনিশিয়ালাইজেশন
var board = null;
var game = new Chess();
var $status = $('#status');
var $pgn = $('#pgn-output');
// ২. কাস্টম পিস পাথ (আপনার আপলোড করা SVG ফাইল অনুযায়ী)
function pieceTheme(piece) {
// piece name format: 'wK', 'bQ', etc.
return '/static/pieces/' + piece + '.svg';
}
// ৩. অডিও ফাংশন (MP3)
function playSound(type) {
let sound;
if (type === 'move') sound = document.getElementById('soundMove');
if (type === 'capture') sound = document.getElementById('soundCapture');
if (type === 'check') sound = document.getElementById('soundCheck');
if (sound) {
sound.currentTime = 0;
sound.play().catch(e => console.log("Audio play blocked"));
}
}
// ৪. ব্যাকএন্ড থেকে এআই চাল এবং মেট্রিক্স আনা
async function getAIMove() {
$status.html('<i class="fas fa-spinner fa-spin"></i> Synapse-Edge is thinking...');
const response = await fetch('/get_move', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ fen: game.fen() })
});
const data = await response.json();
if (data.move) {
// এআই এর চাল দেওয়া
const move = game.move(data.move, { sloppy: true });
board.position(game.fen());
// সাউন্ড এফেক্ট
if (move.captured) playSound('capture');
else if (game.in_check()) playSound('check');
else playSound('move');
// ৫. মেট্রিক্স আপডেট (Flagship Model Heads)
updateDashboard(data);
updateStatus();
}
}
// ৬. ড্যাশবোর্ড আপডেট করা
function updateDashboard(data) {
// Value Head: [-1, 1] কে পার্সেন্টেজে রূপান্তর (সাদার জন্য)
const evalScore = data.value.toFixed(2);
const evalPercent = ((data.value + 1) / 2) * 100;
$('#eval-value').text(evalScore);
$('#eval-bar').css('width', evalPercent + '%');
// Tactical Head: [0, 1] শার্পনেস
const tacticalPercent = (data.tactical * 100).toFixed(0);
$('#tactical-value').text(tacticalPercent + '%');
$('#tactical-bar').css('width', tacticalPercent + '%');
// Phase Head: [0: Opening, 1: Middle, 2: Endgame]
const phases = ["Opening Stage", "Middlegame", "Endgame Territory"];
$('#game-phase').text(phases[data.phase] || "Unknown");
}
// ৭. ইউজার চাল হ্যান্ডেল করা
function onDrop(source, target) {
var move = game.move({
from: source,
to: target,
promotion: 'q' // সহজ করার জন্য সবসময় কুইন প্রমোশন
});
if (move === null) return 'snapback';
// সাউন্ড
if (move.captured) playSound('capture');
else playSound('move');
updateStatus();
// ইউজারের চালের পর এআই এর চাল কল করা (১ সেকেন্ড ডিলে সহ)
window.setTimeout(getAIMove, 500);
}
// ৮. স্ট্যাটাস এবং পিজিএন আপডেট
function updateStatus() {
var status = '';
var moveColor = (game.turn() === 'b') ? 'Black' : 'White';
if (game.in_checkmate()) status = 'Game Over, ' + moveColor + ' is in checkmate.';
else if (game.in_draw()) status = 'Game Over, Draw position.';
else {
status = moveColor + ' to move';
if (game.in_check()) status += ', ' + moveColor + ' is in check';
}
$status.html(status);
$pgn.html(game.pgn());
}
// ৯. বোর্ড কনফিগারেশন
var config = {
draggable: true,
position: 'start',
pieceTheme: pieceTheme,
onDrop: onDrop,
onSnapEnd: function () {
board.position(game.fen());
}
};
board = Chessboard('board', config);
// ১০. কন্ট্রোল বাটন লজিক
$('#resetBtn').on('click', function () {
game.reset();
board.start();
updateStatus();
$('#eval-bar').css('width', '50%');
$('#tactical-bar').css('width', '0%');
});
$('#flipBtn').on('click', board.flip);
updateStatus();