lila/public/javascripts/music/replay.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-05-27 12:53:59 -06:00
function lichessReplayMusic() {
var orchestra;
2021-02-06 06:26:05 -07:00
lichess.loadScript('javascripts/music/orchestra.js').then(function () {
2016-05-27 12:53:59 -06:00
orchestra = lichessOrchestra();
});
2021-02-06 06:26:05 -07:00
var isPawn = function (san) {
2016-05-28 05:54:41 -06:00
return san[0] === san[0].toLowerCase();
};
2021-02-06 06:26:05 -07:00
var isKing = function (san) {
2016-05-28 05:54:41 -06:00
return san[0] === 'K';
2016-05-27 12:53:59 -06:00
};
2021-02-06 06:26:05 -07:00
var hasCastle = function (san) {
2019-02-28 03:54:37 -07:00
return san.startsWith('O-O');
2016-05-27 13:27:06 -06:00
};
2021-02-06 06:26:05 -07:00
var hasCheck = function (san) {
2019-02-28 03:54:37 -07:00
return san.includes('+');
2016-05-27 12:53:59 -06:00
};
2021-02-06 06:26:05 -07:00
var hasMate = function (san) {
2019-02-28 03:54:37 -07:00
return san.includes('#');
2016-05-27 12:53:59 -06:00
};
2021-02-06 06:26:05 -07:00
var hasCapture = function (san) {
2019-02-28 03:54:37 -07:00
return san.includes('x');
2016-05-27 12:53:59 -06:00
};
// a -> 0
// c -> 2
2021-02-06 06:26:05 -07:00
var fileToInt = function (file) {
2016-05-27 13:27:06 -06:00
return 'abcdefgh'.indexOf(file);
2016-05-27 12:53:59 -06:00
};
// c7 = 2 * 8 + 7 = 23
2021-02-06 06:26:05 -07:00
var keyToInt = function (key) {
2016-05-27 13:27:06 -06:00
return fileToInt(key[0]) * 8 + parseInt(key[1]) - 1;
2016-05-27 12:53:59 -06:00
};
2016-05-27 13:27:06 -06:00
var uciBase = 64;
2021-02-06 06:26:05 -07:00
var keyToPitch = function (key) {
return keyToInt(key) / (uciBase / 23);
2016-05-27 12:53:59 -06:00
};
2021-02-06 06:26:05 -07:00
var jump = function (node) {
2016-05-27 12:53:59 -06:00
if (node.san) {
2016-05-28 05:54:41 -06:00
var pitch = keyToPitch(node.uci.slice(2));
2021-02-06 06:26:05 -07:00
var instrument = isPawn(node.san) || isKing(node.san) ? 'clav' : 'celesta';
2016-05-28 05:54:41 -06:00
orchestra.play(instrument, pitch);
2016-05-27 13:27:06 -06:00
if (hasCastle(node.san)) orchestra.play('swells', pitch);
else if (hasCheck(node.san)) orchestra.play('swells', pitch);
2016-05-28 05:54:41 -06:00
else if (hasCapture(node.san)) {
orchestra.play('swells', pitch);
var capturePitch = keyToPitch(node.uci.slice(0, 2));
orchestra.play(instrument, capturePitch);
2021-02-06 06:26:05 -07:00
} else if (hasMate(node.san)) orchestra.play('swells', pitch);
2016-05-27 12:53:59 -06:00
} else {
orchestra.play('swells', 0);
}
2016-05-27 13:27:06 -06:00
};
2016-05-27 12:53:59 -06:00
return {
2021-02-06 06:26:05 -07:00
jump: function (node) {
2016-05-27 12:53:59 -06:00
if (!orchestra) return;
2016-05-27 13:33:48 -06:00
jump(node);
2021-02-06 06:26:05 -07:00
},
2016-05-27 12:53:59 -06:00
};
2021-02-06 06:26:05 -07:00
}