File size: 4,421 Bytes
4d8520a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// ১. ইনিশিয়ালাইজেশন
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();