-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathccrlExtensions.js
More file actions
417 lines (377 loc) · 13.7 KB
/
Copy pathccrlExtensions.js
File metadata and controls
417 lines (377 loc) · 13.7 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
// ==UserScript==
// @name CCRL Extensions
// @namespace Violentmonkey Scripts
// @match https://ccrl.live/*
// @grant GM.xmlHttpRequest
// @version 1.0
// @author Gediminas Masaitis
// @description Kibitzer and eval graph support for CCRL
// @require https://code.jquery.com/jquery-3.7.0.min.js
// @require https://cdn.jsdelivr.net/npm/chart.js
// @require https://cdnjs.cloudflare.com/ajax/libs/chess.js/0.10.3/chess.min.js
// ==/UserScript==
(function () {
'use strict';
// Utility functions
const formatCompactNumber = (number) => {
if (number < 1000) return number;
const units = ['k', 'M', 'B', 'T'];
let unitIndex = -1;
while (number >= 1000 && unitIndex < units.length - 1) {
number /= 1000;
unitIndex++;
}
return `${number.toFixed(2)}${units[unitIndex]}`;
};
const getFullmoveNumberFromFen = (fen) => {
const parts = fen.split(" ");
return parts.length === 6 ? parseInt(parts[5]) : 1;
};
const convertPVToAlgebraic = (pv, startingFen) => {
if (typeof Chess === 'undefined') {
console.error("Chess.js not loaded");
return pv;
}
const chess = new Chess(startingFen);
const moves = pv.trim().split(/\s+/);
let groups = [];
let currentMoveNumber = getFullmoveNumberFromFen(startingFen);
let isWhiteToMove = chess.turn() === 'w';
moves.forEach((move, i) => {
let moveObj = chess.move(move, { sloppy: true });
if (!moveObj) return;
if (isWhiteToMove) {
groups.push({
moveNumber: currentMoveNumber,
white: moveObj.san,
black: null
});
} else {
if (groups.length > 0) {
groups[groups.length - 1].black = moveObj.san;
} else {
groups.push({
moveNumber: currentMoveNumber,
white: "...", // <-- Place "..." after move number when Black moves first
black: moveObj.san
});
}
currentMoveNumber++;
}
isWhiteToMove = chess.turn() === 'w';
});
return groups.map(group => {
let moveNumberHtml = `<span style="color: #f5c276; font-weight: bold;">${group.moveNumber}.</span>`;
let whiteMove = group.white ? `<span style="color: #ffffff;">${group.white}</span>` : '';
let blackMove = group.black ? `<span style="color: #ffffff;">${group.black}</span>` : '';
return `${moveNumberHtml} ${whiteMove} ${blackMove}`.trim();
}).join(" ");
};
// A helper function to parse the score string into a numeric value and display string
const parseScore = (scoreText) => {
scoreText = scoreText.trim();
// Format: "+MX" where X is a positive integer (mate for white)
if (scoreText.startsWith("+M")) {
const mateMoves = parseInt(scoreText.substring(2));
// Use a high constant (e.g. 1000) so that mate scores are always above normal evals.
const value = 30 - (isNaN(mateMoves) ? 0 : mateMoves * 0.01);
return { value: value, tooltip: scoreText };
}
// Format: "-MX" where X is a positive integer (mate for black)
else if (scoreText.startsWith("-M")) {
const mateMoves = parseInt(scoreText.substring(2));
// Use a low constant so that mate scores are always below normal evals.
const value = -30 + (isNaN(mateMoves) ? 0 : mateMoves * 0.01);
return { value: value, tooltip: scoreText };
}
else {
const numStr = scoreText;
const value = Math.min(Math.max(parseFloat(numStr), -20), 20);
if (!isNaN(value)) {
return { value: value, tooltip: scoreText };
}
}
// If none of the formats match, return null.
return null;
}
// Create page layout
const container = $('.container').css('max-width', '180vh');
// Create a wrapper for the whole page content
const pageWrapper = $('<div id="page-wrapper"></div>').css({
'display': 'flex',
'flex-direction': 'row',
'width': '100%',
'gap': '15px'
});
// Move all existing content from the container to a main content div
const mainContent = $('<div id="main-content"></div>').css({
'flex': '7',
'min-width': '0'
});
// Create the sidebar for analysis
const sidebarAnalysis = $('<div id="sidebar-analysis"></div>').css({
'flex': '3',
'display': 'flex',
'flex-direction': 'column',
'gap': '15px',
'min-width': '0',
'max-width': 'none'
});
// Move existing container children to mainContent
container.children().appendTo(mainContent);
// Clear container and add our new structure
container.empty().append(pageWrapper);
pageWrapper.append(mainContent).append(sidebarAnalysis);
// Create the first kibitzer
const kibitzerInfo1 = $(`<div id="kibitzer1-info"></div>`).css({
'margin-bottom': '15px',
'overflow': 'auto',
'max-height': '30vh'
});
sidebarAnalysis.append(kibitzerInfo1);
kibitzerInfo1
.append(`<h3 id="kibitzer1-name">Kibitzer 1 inactive</h3>`)
.append(`
<div class="card fluid">
<div class="row">
<div class="col-sm">
<div class="row">
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Score</small></p>
<p class="small-margin info-value" id="kibitzer1-score">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Depth</small></p>
<p class="small-margin info-value" id="kibitzer1-depth">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Nodes</small></p>
<p class="small-margin info-value" id="kibitzer1-nodes">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Nps</small></p>
<p class="small-margin info-value" id="kibitzer1-nps">0</p>
</div>
</div>
</div>
<div class="col-sm-3" style="text-align: right">
<h3><small id="kibitzer1-time"><mark>∞</mark></small></h3>
</div>
<div class="col-sm-12">
<p class="pv"><small id="kibitzer1-pv"></small></p>
</div>
</div>
</div>
`)
.append(`<p class="mainline" id="kibitzer1-mainline" style="margin-top: 5px; font-style: italic;"></p>`);
// Setup chart container
const evalChartContainer = $('<div id="eval-chart-container"><canvas id="eval-chart"></canvas></div>').css({
'width': '100%',
'height': '300px',
'margin-bottom': '15px'
});
// Append the chart container to the sidebar
sidebarAnalysis.append(evalChartContainer);
// Create the second kibitzer (placed after the chart)
const kibitzerInfo2 = $(`<div id="kibitzer2-info"></div>`).css({
'margin-bottom': '15px',
'overflow': 'auto',
'max-height': '30vh'
});
sidebarAnalysis.append(kibitzerInfo2);
kibitzerInfo2
.append(`<h3 id="kibitzer2-name">Kibitzer 2 inactive</h3>`)
.append(`
<div class="card fluid">
<div class="row">
<div class="col-sm">
<div class="row">
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Score</small></p>
<p class="small-margin info-value" id="kibitzer2-score">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Depth</small></p>
<p class="small-margin info-value" id="kibitzer2-depth">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Nodes</small></p>
<p class="small-margin info-value" id="kibitzer2-nodes">0</p>
</div>
<div class="col-sm info">
<p class="small-margin"><small class="info-header">Nps</small></p>
<p class="small-margin info-value" id="kibitzer2-nps">0</p>
</div>
</div>
</div>
<div class="col-sm-3" style="text-align: right">
<h3><small id="kibitzer2-time"><mark>∞</mark></small></h3>
</div>
<div class="col-sm-12">
<p class="pv"><small id="kibitzer2-pv"></small></p>
</div>
</div>
</div>
`)
.append(`<p class="mainline" id="kibitzer2-mainline" style="margin-top: 5px; font-style: italic;"></p>`);
const ctx = $("#eval-chart")[0];
const chart = new Chart(ctx, {
type: 'line',
data: {
labels: [],
datasets: [
{
label: 'White',
data: [],
borderColor: 'rgb(220, 220, 220)'
},
{
label: 'Black',
data: [],
borderColor: 'rgb(100, 100, 100)'
},
{
label: 'Kibitzer 1',
data: [],
borderColor: 'rgb(220, 100, 100)'
},
{
label: 'Kibitzer 2',
data: [],
borderColor: 'rgb(100, 100, 220)'
}
]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
},
plugins: {
tooltip: {
callbacks: {
label: (context) => {
const pt = context.raw;
// If pt is an object and has tooltip, return that. Otherwise, use the y value.
return pt.tooltip || pt.y;
}
}
}
}
}
});
// State
let currentFen = "";
let labels = [];
let scores = [{}, {}, {}, {}];
// Update FEN and send data
const getFen = () => $("#fen").text();
const sendFen = () => {
const fen = getFen();
if (!fen || fen === currentFen) return;
currentFen = fen;
if (fen === "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1") {
labels = [];
scores = [{}, {}, {}, {}];
}
GM.xmlHttpRequest({
method: "POST",
url: "http://127.0.0.1:5210/fen",
headers: {
"Content-Type": "application/json"
},
data: JSON.stringify({
fen
}),
onload: (response) => {
const engines = JSON.parse(response.responseText).engines;
if (engines.length > 0) $("#kibitzer1-name").text(engines[0].name);
if (engines.length > 1) $("#kibitzer2-name").text(engines[1].name);
}
});
};
// Register a MutationObserver to look at fen changes
const fenElement = document.getElementById('fen');
if (fenElement) {
const observer = new MutationObserver((mutationsList) => {
// Send the new fen to the backend
sendFen();
});
// Observe the fen textbox
observer.observe(fenElement, {
childList: true,
subtree: true,
characterData: true
});
}
// Update the graph and kibitzers every second
setInterval(() => {
const fen = getFen();
if (!fen) return;
// Correct ply calculation
const fenParts = fen.split(" ");
const fullmoveNumber = parseInt(fenParts[5]) || 1;
const activeColor = fenParts[1];
const ply = (fullmoveNumber - 1) * 2 + (activeColor === 'b' ? 1 : 0);
const plyStr = ply.toString();
if (!labels.includes(plyStr)) {
labels.push(plyStr);
chart.data.labels = labels;
}
GM.xmlHttpRequest({
method: "GET",
url: "http://127.0.0.1:5210/query",
onload: (response) => {
const engineInfos = JSON.parse(response.responseText).engineInfos;
engineInfos.forEach((info, index) => {
const kibitzerNum = index + 1;
$(`#kibitzer${kibitzerNum}-score`).text(parseScore(info.score).tooltip);
$(`#kibitzer${kibitzerNum}-depth`).text(info.depth);
$(`#kibitzer${kibitzerNum}-nodes`).text(formatCompactNumber(info.nodes));
$(`#kibitzer${kibitzerNum}-nps`).text(formatCompactNumber(info.nps));
// Update kibitzer scores and labels for the chart (positions 2 and 3 in the scores array)
const datasetIndex = 2 + index;
scores[datasetIndex][plyStr] = { x: plyStr, y: parseScore(info.score).value, tooltip: parseScore(info.score).tooltip };
chart.data.datasets[datasetIndex].label = info.name;
let multipvHtml = "";
if (info.multipv) {
info.multipv.sort((a, b) => a.orderKey - b.orderKey).forEach((variation) => {
const algebraicPV = convertPVToAlgebraic(variation.pv, currentFen);
multipvHtml += `
<div class="multipv-box" style="border: 1px solid #ccc; margin-bottom: 5px; padding: 5px;">
<div class="multipv-header" style="font-weight: bold;color: #aaa;">
Depth: ${variation.depth} | Eval: ${variation.score}
</div>
<div class="multipv-pv" style="margin-top: 3px;">${algebraicPV}</div>
</div>`;
});
}
$(`#kibitzer${kibitzerNum}-pv`).html(multipvHtml);
});
},
onerror: () => console.log("Failed querying backend")
});
// Handle White and Black scores with validation
const whiteScoreText = $("#white-score").text();
const blackScoreText = $("#black-score").text();
const whiteScore = parseFloat(whiteScoreText);
const blackScore = parseFloat(blackScoreText);
if (!isNaN(whiteScore)) {
scores[0][plyStr] = { x: plyStr, y: whiteScore, tooltip: whiteScore.toString() };
}
if (!isNaN(blackScore)) {
scores[1][plyStr] = { x: plyStr, y: blackScore, tooltip: blackScore.toString() };
}
// Update chart datasets
chart.data.datasets.forEach((dataset, i) => {
dataset.data = labels.map(plyStr => {
const scoreObj = scores[i][plyStr];
return scoreObj ? scoreObj : { x: plyStr, y: null };
});
});
chart.update();
}, 1000);
})();