next-chessground 0.10.2 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.es.js +241 -15
- package/dist/index.js +242 -15
- package/index.js +10 -2
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -5952,14 +5952,24 @@ const fen = {
|
|
|
5952
5952
|
initial: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
|
|
5953
5953
|
empty: '8/8/8/8/8/8/8/8 w - - 0 1'
|
|
5954
5954
|
};
|
|
5955
|
-
const
|
|
5955
|
+
const initialFen = fen.initial;
|
|
5956
|
+
const emptyFen = fen.empty;
|
|
5957
|
+
const constants = {
|
|
5958
|
+
fen,
|
|
5959
|
+
initialFen,
|
|
5960
|
+
emptyFen
|
|
5961
|
+
};
|
|
5956
5962
|
|
|
5957
5963
|
const useChess = props => {
|
|
5958
|
-
const [fen, setFen] = useState(props.fen ||
|
|
5959
|
-
const [chess$1] = useState(new chess.Chess(fen)); // reinitialize when FEN changes from props
|
|
5964
|
+
const [fen, setFen] = useState(props.fen || initialFen);
|
|
5965
|
+
const [chess$1, setChess] = useState(new chess.Chess(fen)); // reinitialize when FEN changes from props
|
|
5960
5966
|
|
|
5961
5967
|
useEffect(() => {
|
|
5962
|
-
|
|
5968
|
+
if (props.fen) {
|
|
5969
|
+
setFen(props.fen);
|
|
5970
|
+
setChess(new chess.Chess(props.fen));
|
|
5971
|
+
setLastMove([]);
|
|
5972
|
+
}
|
|
5963
5973
|
}, [props.fen]);
|
|
5964
5974
|
const [lastMove, setLastMove] = useState([]);
|
|
5965
5975
|
const promotion = props.lastMove && props.lastMove.promotion;
|
|
@@ -6404,16 +6414,16 @@ const Chessboard = (props, ref) => {
|
|
|
6404
6414
|
|
|
6405
6415
|
if (!move) {
|
|
6406
6416
|
show();
|
|
6417
|
+
}
|
|
6418
|
+
|
|
6419
|
+
if (theme.playSounds) {
|
|
6420
|
+
audio(theme.sounds);
|
|
6407
6421
|
} // pass the chess object to callback function
|
|
6408
6422
|
|
|
6409
6423
|
|
|
6410
6424
|
if (typeof props.onMove === 'function') {
|
|
6411
6425
|
await props.onMove(chess);
|
|
6412
6426
|
}
|
|
6413
|
-
|
|
6414
|
-
if (theme.playSounds) {
|
|
6415
|
-
audio(theme.sounds);
|
|
6416
|
-
}
|
|
6417
6427
|
};
|
|
6418
6428
|
|
|
6419
6429
|
return /*#__PURE__*/React.createElement("div", {
|
|
@@ -6830,19 +6840,19 @@ const FenOptions = ({
|
|
|
6830
6840
|
};
|
|
6831
6841
|
|
|
6832
6842
|
const NextEditor = (props, ref) => {
|
|
6833
|
-
const [fen
|
|
6843
|
+
const [fen, setFen] = useState(props.fen || constants.emptyFen);
|
|
6834
6844
|
const [selected, setSelected] = useState({
|
|
6835
6845
|
role: null,
|
|
6836
6846
|
color: null
|
|
6837
6847
|
});
|
|
6838
6848
|
useEffect(() => {
|
|
6839
6849
|
if (typeof props.onSelect === 'function') {
|
|
6840
|
-
props.onSelect(fen
|
|
6850
|
+
props.onSelect(fen);
|
|
6841
6851
|
}
|
|
6842
|
-
}, [fen
|
|
6852
|
+
}, [fen]);
|
|
6843
6853
|
|
|
6844
6854
|
const onSelect = key => {
|
|
6845
|
-
const array = fen
|
|
6855
|
+
const array = fen.split(' ');
|
|
6846
6856
|
array.shift();
|
|
6847
6857
|
const options = array.join(' ');
|
|
6848
6858
|
const position = dropPiece(ref.current.board, selected, key);
|
|
@@ -6851,7 +6861,7 @@ const NextEditor = (props, ref) => {
|
|
|
6851
6861
|
};
|
|
6852
6862
|
|
|
6853
6863
|
const handleChange = options => {
|
|
6854
|
-
const position = fen
|
|
6864
|
+
const position = fen.split(' ')[0];
|
|
6855
6865
|
const withOptions = [position, options, '- 0 1'].join(' ');
|
|
6856
6866
|
setFen(withOptions);
|
|
6857
6867
|
};
|
|
@@ -6865,7 +6875,7 @@ const NextEditor = (props, ref) => {
|
|
|
6865
6875
|
}), /*#__PURE__*/React.createElement(Chessboard$1, _extends({}, props, {
|
|
6866
6876
|
ref: ref,
|
|
6867
6877
|
onSelect: onSelect,
|
|
6868
|
-
fen: fen
|
|
6878
|
+
fen: fen
|
|
6869
6879
|
})), /*#__PURE__*/React.createElement(EditorPieces, {
|
|
6870
6880
|
selected: selected,
|
|
6871
6881
|
selectPiece: setSelected,
|
|
@@ -6877,6 +6887,222 @@ const NextEditor = (props, ref) => {
|
|
|
6877
6887
|
|
|
6878
6888
|
var NextEditor$1 = /*#__PURE__*/forwardRef(NextEditor);
|
|
6879
6889
|
|
|
6890
|
+
class Stockfish {
|
|
6891
|
+
constructor() {
|
|
6892
|
+
if (typeof window === 'undefined') {
|
|
6893
|
+
return false;
|
|
6894
|
+
}
|
|
6895
|
+
|
|
6896
|
+
if (!window.chessEngineWorker) {
|
|
6897
|
+
const worker = process.env.STOCKFISH_PATH;
|
|
6898
|
+
window.chessEngineWorker = new Worker(worker);
|
|
6899
|
+
}
|
|
6900
|
+
|
|
6901
|
+
this.skillLevel = 20;
|
|
6902
|
+
this.resolveTimeout = null;
|
|
6903
|
+
this.isAnalyzing = false;
|
|
6904
|
+
this.fen = constants.initialFen;
|
|
6905
|
+
}
|
|
6906
|
+
|
|
6907
|
+
getTurnFromFen(fen) {
|
|
6908
|
+
return fen.split(' ')[1];
|
|
6909
|
+
}
|
|
6910
|
+
|
|
6911
|
+
normalizeScoreValue(value) {
|
|
6912
|
+
const {
|
|
6913
|
+
fen
|
|
6914
|
+
} = this;
|
|
6915
|
+
const turn = this.getTurnFromFen(fen);
|
|
6916
|
+
|
|
6917
|
+
if (turn === 'b') {
|
|
6918
|
+
return -1 * value;
|
|
6919
|
+
}
|
|
6920
|
+
|
|
6921
|
+
return value;
|
|
6922
|
+
}
|
|
6923
|
+
|
|
6924
|
+
isInfoMessage(message) {
|
|
6925
|
+
if (!message || !message.data) {
|
|
6926
|
+
return false;
|
|
6927
|
+
}
|
|
6928
|
+
|
|
6929
|
+
return message.data.startsWith('info');
|
|
6930
|
+
}
|
|
6931
|
+
|
|
6932
|
+
setSkillLevel() {
|
|
6933
|
+
/**
|
|
6934
|
+
* skill level is 0-20, higher the stronger
|
|
6935
|
+
* skill level maximum error is 0-5000, lower the stronger
|
|
6936
|
+
* skill level probability is 1-1000, the higher the stronger
|
|
6937
|
+
* seems to be working with max value for maxError and min value for probability
|
|
6938
|
+
*/
|
|
6939
|
+
const maxError = 5000;
|
|
6940
|
+
const probability = 1;
|
|
6941
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level value ' + this.skillLevel);
|
|
6942
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level Maximum Error value ' + maxError);
|
|
6943
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level Probability value ' + probability);
|
|
6944
|
+
}
|
|
6945
|
+
|
|
6946
|
+
async init() {
|
|
6947
|
+
await this.use_uci();
|
|
6948
|
+
await this.is_ready();
|
|
6949
|
+
this.setSkillLevel();
|
|
6950
|
+
}
|
|
6951
|
+
|
|
6952
|
+
getScoreFromInfo(msg) {
|
|
6953
|
+
if (msg.startsWith('info depth 0')) {
|
|
6954
|
+
// This happens when game is over.
|
|
6955
|
+
const split = msg.split(' ');
|
|
6956
|
+
const type = split[4];
|
|
6957
|
+
const value = this.normalizeScoreValue(Number(split[5]));
|
|
6958
|
+
this.isAnalyzing = false;
|
|
6959
|
+
return {
|
|
6960
|
+
type,
|
|
6961
|
+
value
|
|
6962
|
+
};
|
|
6963
|
+
} else if (msg.startsWith('info depth ')) {
|
|
6964
|
+
const split = msg.split(' ');
|
|
6965
|
+
const type = split[8];
|
|
6966
|
+
const value = this.normalizeScoreValue(Number(split[9]));
|
|
6967
|
+
return {
|
|
6968
|
+
type,
|
|
6969
|
+
value
|
|
6970
|
+
};
|
|
6971
|
+
}
|
|
6972
|
+
|
|
6973
|
+
return {
|
|
6974
|
+
type: 'cp',
|
|
6975
|
+
value: 0
|
|
6976
|
+
};
|
|
6977
|
+
}
|
|
6978
|
+
|
|
6979
|
+
parseData(data) {
|
|
6980
|
+
const depth = data.split(' ')[2];
|
|
6981
|
+
let pv = '';
|
|
6982
|
+
|
|
6983
|
+
if (data.indexOf(' pv ') > -1) {
|
|
6984
|
+
pv = data.split(' pv ')[1].split(' bmc ')[0];
|
|
6985
|
+
}
|
|
6986
|
+
|
|
6987
|
+
const score = this.getScoreFromInfo(data);
|
|
6988
|
+
return {
|
|
6989
|
+
depth,
|
|
6990
|
+
pv,
|
|
6991
|
+
score
|
|
6992
|
+
};
|
|
6993
|
+
}
|
|
6994
|
+
|
|
6995
|
+
use_uci() {
|
|
6996
|
+
return new Promise(resolve => {
|
|
6997
|
+
window.chessEngineWorker.postMessage('uci');
|
|
6998
|
+
|
|
6999
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7000
|
+
if (message.data === 'uciok') {
|
|
7001
|
+
resolve(message);
|
|
7002
|
+
}
|
|
7003
|
+
};
|
|
7004
|
+
});
|
|
7005
|
+
}
|
|
7006
|
+
|
|
7007
|
+
is_ready() {
|
|
7008
|
+
return new Promise(resolve => {
|
|
7009
|
+
window.chessEngineWorker.postMessage('isready');
|
|
7010
|
+
|
|
7011
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7012
|
+
if (message.data === 'readyok') {
|
|
7013
|
+
resolve(message);
|
|
7014
|
+
}
|
|
7015
|
+
};
|
|
7016
|
+
});
|
|
7017
|
+
}
|
|
7018
|
+
|
|
7019
|
+
set_position(fen) {
|
|
7020
|
+
return new Promise(resolve => {
|
|
7021
|
+
this.fen = fen;
|
|
7022
|
+
window.chessEngineWorker.postMessage('position fen ' + fen);
|
|
7023
|
+
resolve();
|
|
7024
|
+
});
|
|
7025
|
+
}
|
|
7026
|
+
|
|
7027
|
+
set_multipv(numPv) {
|
|
7028
|
+
window.chessEngineWorker.postMessage('setoption name MultiPV value ' + numPv);
|
|
7029
|
+
}
|
|
7030
|
+
|
|
7031
|
+
get_score(fen, depth = 15) {
|
|
7032
|
+
return new Promise(resolve => {
|
|
7033
|
+
window.chessEngineWorker.postMessage('position fen ' + fen);
|
|
7034
|
+
window.chessEngineWorker.postMessage('go depth 15');
|
|
7035
|
+
|
|
7036
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7037
|
+
if (message.data.startsWith('info depth ' + depth)) {
|
|
7038
|
+
const split = message.data.split(' ');
|
|
7039
|
+
const type = split[8];
|
|
7040
|
+
const value = Number(split[9]);
|
|
7041
|
+
resolve({
|
|
7042
|
+
type,
|
|
7043
|
+
value
|
|
7044
|
+
});
|
|
7045
|
+
} else if (message.data.startsWith('info depth 0')) {
|
|
7046
|
+
// This happens when game is over.
|
|
7047
|
+
const split = message.data.split(' ');
|
|
7048
|
+
const type = split[4];
|
|
7049
|
+
const value = Number(split[5]);
|
|
7050
|
+
resolve({
|
|
7051
|
+
type,
|
|
7052
|
+
value
|
|
7053
|
+
});
|
|
7054
|
+
}
|
|
7055
|
+
};
|
|
7056
|
+
});
|
|
7057
|
+
}
|
|
7058
|
+
|
|
7059
|
+
go_depth(depth) {
|
|
7060
|
+
return new Promise(resolve => {
|
|
7061
|
+
window.chessEngineWorker.postMessage('go depth ' + depth);
|
|
7062
|
+
|
|
7063
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7064
|
+
if (message.data.startsWith('bestmove')) {
|
|
7065
|
+
resolve(message.data);
|
|
7066
|
+
}
|
|
7067
|
+
};
|
|
7068
|
+
});
|
|
7069
|
+
}
|
|
7070
|
+
|
|
7071
|
+
go_time(time) {
|
|
7072
|
+
return new Promise(resolve => {
|
|
7073
|
+
window.chessEngineWorker.postMessage('go movetime ' + time);
|
|
7074
|
+
|
|
7075
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7076
|
+
if (message.data.startsWith('bestmove')) {
|
|
7077
|
+
resolve(message.data);
|
|
7078
|
+
}
|
|
7079
|
+
};
|
|
7080
|
+
});
|
|
7081
|
+
}
|
|
7082
|
+
|
|
7083
|
+
stop() {
|
|
7084
|
+
return new Promise(resolve => {
|
|
7085
|
+
if (!this.isAnalyzing) {
|
|
7086
|
+
resolve();
|
|
7087
|
+
}
|
|
7088
|
+
|
|
7089
|
+
window.chessEngineWorker.postMessage('stop');
|
|
7090
|
+
|
|
7091
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7092
|
+
if (message.data.startsWith('bestmove')) {
|
|
7093
|
+
this.isAnalyzing = false;
|
|
7094
|
+
resolve(message.data);
|
|
7095
|
+
}
|
|
7096
|
+
};
|
|
7097
|
+
});
|
|
7098
|
+
}
|
|
7099
|
+
|
|
7100
|
+
quit() {
|
|
7101
|
+
window.chessEngineWorker.postMessage('quit');
|
|
7102
|
+
}
|
|
7103
|
+
|
|
7104
|
+
}
|
|
7105
|
+
|
|
6880
7106
|
function styleInject(css, ref) {
|
|
6881
7107
|
if (ref === void 0) ref = {};
|
|
6882
7108
|
var insertAt = ref.insertAt;
|
|
@@ -6931,4 +7157,4 @@ var css_248z$7 = ".flex {\n display: flex;\n}\n.flex-col {\n flex-direction: c
|
|
|
6931
7157
|
styleInject(css_248z$7);
|
|
6932
7158
|
|
|
6933
7159
|
export default NextChessground$1;
|
|
6934
|
-
export { NextChessground$1 as NextChessground, NextEditor$1 as NextEditor,
|
|
7160
|
+
export { NextChessground$1 as NextChessground, NextEditor$1 as NextEditor, Stockfish, constants, useChess, useChessground };
|
package/dist/index.js
CHANGED
|
@@ -5961,14 +5961,24 @@ const fen = {
|
|
|
5961
5961
|
initial: 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1',
|
|
5962
5962
|
empty: '8/8/8/8/8/8/8/8 w - - 0 1'
|
|
5963
5963
|
};
|
|
5964
|
-
const
|
|
5964
|
+
const initialFen = fen.initial;
|
|
5965
|
+
const emptyFen = fen.empty;
|
|
5966
|
+
const constants = {
|
|
5967
|
+
fen,
|
|
5968
|
+
initialFen,
|
|
5969
|
+
emptyFen
|
|
5970
|
+
};
|
|
5965
5971
|
|
|
5966
5972
|
const useChess = props => {
|
|
5967
|
-
const [fen, setFen] = React.useState(props.fen ||
|
|
5968
|
-
const [chess$1] = React.useState(new chess.Chess(fen)); // reinitialize when FEN changes from props
|
|
5973
|
+
const [fen, setFen] = React.useState(props.fen || initialFen);
|
|
5974
|
+
const [chess$1, setChess] = React.useState(new chess.Chess(fen)); // reinitialize when FEN changes from props
|
|
5969
5975
|
|
|
5970
5976
|
React.useEffect(() => {
|
|
5971
|
-
|
|
5977
|
+
if (props.fen) {
|
|
5978
|
+
setFen(props.fen);
|
|
5979
|
+
setChess(new chess.Chess(props.fen));
|
|
5980
|
+
setLastMove([]);
|
|
5981
|
+
}
|
|
5972
5982
|
}, [props.fen]);
|
|
5973
5983
|
const [lastMove, setLastMove] = React.useState([]);
|
|
5974
5984
|
const promotion = props.lastMove && props.lastMove.promotion;
|
|
@@ -6413,16 +6423,16 @@ const Chessboard = (props, ref) => {
|
|
|
6413
6423
|
|
|
6414
6424
|
if (!move) {
|
|
6415
6425
|
show();
|
|
6426
|
+
}
|
|
6427
|
+
|
|
6428
|
+
if (theme.playSounds) {
|
|
6429
|
+
audio(theme.sounds);
|
|
6416
6430
|
} // pass the chess object to callback function
|
|
6417
6431
|
|
|
6418
6432
|
|
|
6419
6433
|
if (typeof props.onMove === 'function') {
|
|
6420
6434
|
await props.onMove(chess);
|
|
6421
6435
|
}
|
|
6422
|
-
|
|
6423
|
-
if (theme.playSounds) {
|
|
6424
|
-
audio(theme.sounds);
|
|
6425
|
-
}
|
|
6426
6436
|
};
|
|
6427
6437
|
|
|
6428
6438
|
return /*#__PURE__*/React__default['default'].createElement("div", {
|
|
@@ -6839,19 +6849,19 @@ const FenOptions = ({
|
|
|
6839
6849
|
};
|
|
6840
6850
|
|
|
6841
6851
|
const NextEditor = (props, ref) => {
|
|
6842
|
-
const [fen
|
|
6852
|
+
const [fen, setFen] = React.useState(props.fen || constants.emptyFen);
|
|
6843
6853
|
const [selected, setSelected] = React.useState({
|
|
6844
6854
|
role: null,
|
|
6845
6855
|
color: null
|
|
6846
6856
|
});
|
|
6847
6857
|
React.useEffect(() => {
|
|
6848
6858
|
if (typeof props.onSelect === 'function') {
|
|
6849
|
-
props.onSelect(fen
|
|
6859
|
+
props.onSelect(fen);
|
|
6850
6860
|
}
|
|
6851
|
-
}, [fen
|
|
6861
|
+
}, [fen]);
|
|
6852
6862
|
|
|
6853
6863
|
const onSelect = key => {
|
|
6854
|
-
const array = fen
|
|
6864
|
+
const array = fen.split(' ');
|
|
6855
6865
|
array.shift();
|
|
6856
6866
|
const options = array.join(' ');
|
|
6857
6867
|
const position = dropPiece(ref.current.board, selected, key);
|
|
@@ -6860,7 +6870,7 @@ const NextEditor = (props, ref) => {
|
|
|
6860
6870
|
};
|
|
6861
6871
|
|
|
6862
6872
|
const handleChange = options => {
|
|
6863
|
-
const position = fen
|
|
6873
|
+
const position = fen.split(' ')[0];
|
|
6864
6874
|
const withOptions = [position, options, '- 0 1'].join(' ');
|
|
6865
6875
|
setFen(withOptions);
|
|
6866
6876
|
};
|
|
@@ -6874,7 +6884,7 @@ const NextEditor = (props, ref) => {
|
|
|
6874
6884
|
}), /*#__PURE__*/React__default['default'].createElement(Chessboard$1, _extends({}, props, {
|
|
6875
6885
|
ref: ref,
|
|
6876
6886
|
onSelect: onSelect,
|
|
6877
|
-
fen: fen
|
|
6887
|
+
fen: fen
|
|
6878
6888
|
})), /*#__PURE__*/React__default['default'].createElement(EditorPieces, {
|
|
6879
6889
|
selected: selected,
|
|
6880
6890
|
selectPiece: setSelected,
|
|
@@ -6886,6 +6896,222 @@ const NextEditor = (props, ref) => {
|
|
|
6886
6896
|
|
|
6887
6897
|
var NextEditor$1 = /*#__PURE__*/React.forwardRef(NextEditor);
|
|
6888
6898
|
|
|
6899
|
+
class Stockfish {
|
|
6900
|
+
constructor() {
|
|
6901
|
+
if (typeof window === 'undefined') {
|
|
6902
|
+
return false;
|
|
6903
|
+
}
|
|
6904
|
+
|
|
6905
|
+
if (!window.chessEngineWorker) {
|
|
6906
|
+
const worker = process.env.STOCKFISH_PATH;
|
|
6907
|
+
window.chessEngineWorker = new Worker(worker);
|
|
6908
|
+
}
|
|
6909
|
+
|
|
6910
|
+
this.skillLevel = 20;
|
|
6911
|
+
this.resolveTimeout = null;
|
|
6912
|
+
this.isAnalyzing = false;
|
|
6913
|
+
this.fen = constants.initialFen;
|
|
6914
|
+
}
|
|
6915
|
+
|
|
6916
|
+
getTurnFromFen(fen) {
|
|
6917
|
+
return fen.split(' ')[1];
|
|
6918
|
+
}
|
|
6919
|
+
|
|
6920
|
+
normalizeScoreValue(value) {
|
|
6921
|
+
const {
|
|
6922
|
+
fen
|
|
6923
|
+
} = this;
|
|
6924
|
+
const turn = this.getTurnFromFen(fen);
|
|
6925
|
+
|
|
6926
|
+
if (turn === 'b') {
|
|
6927
|
+
return -1 * value;
|
|
6928
|
+
}
|
|
6929
|
+
|
|
6930
|
+
return value;
|
|
6931
|
+
}
|
|
6932
|
+
|
|
6933
|
+
isInfoMessage(message) {
|
|
6934
|
+
if (!message || !message.data) {
|
|
6935
|
+
return false;
|
|
6936
|
+
}
|
|
6937
|
+
|
|
6938
|
+
return message.data.startsWith('info');
|
|
6939
|
+
}
|
|
6940
|
+
|
|
6941
|
+
setSkillLevel() {
|
|
6942
|
+
/**
|
|
6943
|
+
* skill level is 0-20, higher the stronger
|
|
6944
|
+
* skill level maximum error is 0-5000, lower the stronger
|
|
6945
|
+
* skill level probability is 1-1000, the higher the stronger
|
|
6946
|
+
* seems to be working with max value for maxError and min value for probability
|
|
6947
|
+
*/
|
|
6948
|
+
const maxError = 5000;
|
|
6949
|
+
const probability = 1;
|
|
6950
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level value ' + this.skillLevel);
|
|
6951
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level Maximum Error value ' + maxError);
|
|
6952
|
+
window.chessEngineWorker.postMessage('setoption name Skill Level Probability value ' + probability);
|
|
6953
|
+
}
|
|
6954
|
+
|
|
6955
|
+
async init() {
|
|
6956
|
+
await this.use_uci();
|
|
6957
|
+
await this.is_ready();
|
|
6958
|
+
this.setSkillLevel();
|
|
6959
|
+
}
|
|
6960
|
+
|
|
6961
|
+
getScoreFromInfo(msg) {
|
|
6962
|
+
if (msg.startsWith('info depth 0')) {
|
|
6963
|
+
// This happens when game is over.
|
|
6964
|
+
const split = msg.split(' ');
|
|
6965
|
+
const type = split[4];
|
|
6966
|
+
const value = this.normalizeScoreValue(Number(split[5]));
|
|
6967
|
+
this.isAnalyzing = false;
|
|
6968
|
+
return {
|
|
6969
|
+
type,
|
|
6970
|
+
value
|
|
6971
|
+
};
|
|
6972
|
+
} else if (msg.startsWith('info depth ')) {
|
|
6973
|
+
const split = msg.split(' ');
|
|
6974
|
+
const type = split[8];
|
|
6975
|
+
const value = this.normalizeScoreValue(Number(split[9]));
|
|
6976
|
+
return {
|
|
6977
|
+
type,
|
|
6978
|
+
value
|
|
6979
|
+
};
|
|
6980
|
+
}
|
|
6981
|
+
|
|
6982
|
+
return {
|
|
6983
|
+
type: 'cp',
|
|
6984
|
+
value: 0
|
|
6985
|
+
};
|
|
6986
|
+
}
|
|
6987
|
+
|
|
6988
|
+
parseData(data) {
|
|
6989
|
+
const depth = data.split(' ')[2];
|
|
6990
|
+
let pv = '';
|
|
6991
|
+
|
|
6992
|
+
if (data.indexOf(' pv ') > -1) {
|
|
6993
|
+
pv = data.split(' pv ')[1].split(' bmc ')[0];
|
|
6994
|
+
}
|
|
6995
|
+
|
|
6996
|
+
const score = this.getScoreFromInfo(data);
|
|
6997
|
+
return {
|
|
6998
|
+
depth,
|
|
6999
|
+
pv,
|
|
7000
|
+
score
|
|
7001
|
+
};
|
|
7002
|
+
}
|
|
7003
|
+
|
|
7004
|
+
use_uci() {
|
|
7005
|
+
return new Promise(resolve => {
|
|
7006
|
+
window.chessEngineWorker.postMessage('uci');
|
|
7007
|
+
|
|
7008
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7009
|
+
if (message.data === 'uciok') {
|
|
7010
|
+
resolve(message);
|
|
7011
|
+
}
|
|
7012
|
+
};
|
|
7013
|
+
});
|
|
7014
|
+
}
|
|
7015
|
+
|
|
7016
|
+
is_ready() {
|
|
7017
|
+
return new Promise(resolve => {
|
|
7018
|
+
window.chessEngineWorker.postMessage('isready');
|
|
7019
|
+
|
|
7020
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7021
|
+
if (message.data === 'readyok') {
|
|
7022
|
+
resolve(message);
|
|
7023
|
+
}
|
|
7024
|
+
};
|
|
7025
|
+
});
|
|
7026
|
+
}
|
|
7027
|
+
|
|
7028
|
+
set_position(fen) {
|
|
7029
|
+
return new Promise(resolve => {
|
|
7030
|
+
this.fen = fen;
|
|
7031
|
+
window.chessEngineWorker.postMessage('position fen ' + fen);
|
|
7032
|
+
resolve();
|
|
7033
|
+
});
|
|
7034
|
+
}
|
|
7035
|
+
|
|
7036
|
+
set_multipv(numPv) {
|
|
7037
|
+
window.chessEngineWorker.postMessage('setoption name MultiPV value ' + numPv);
|
|
7038
|
+
}
|
|
7039
|
+
|
|
7040
|
+
get_score(fen, depth = 15) {
|
|
7041
|
+
return new Promise(resolve => {
|
|
7042
|
+
window.chessEngineWorker.postMessage('position fen ' + fen);
|
|
7043
|
+
window.chessEngineWorker.postMessage('go depth 15');
|
|
7044
|
+
|
|
7045
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7046
|
+
if (message.data.startsWith('info depth ' + depth)) {
|
|
7047
|
+
const split = message.data.split(' ');
|
|
7048
|
+
const type = split[8];
|
|
7049
|
+
const value = Number(split[9]);
|
|
7050
|
+
resolve({
|
|
7051
|
+
type,
|
|
7052
|
+
value
|
|
7053
|
+
});
|
|
7054
|
+
} else if (message.data.startsWith('info depth 0')) {
|
|
7055
|
+
// This happens when game is over.
|
|
7056
|
+
const split = message.data.split(' ');
|
|
7057
|
+
const type = split[4];
|
|
7058
|
+
const value = Number(split[5]);
|
|
7059
|
+
resolve({
|
|
7060
|
+
type,
|
|
7061
|
+
value
|
|
7062
|
+
});
|
|
7063
|
+
}
|
|
7064
|
+
};
|
|
7065
|
+
});
|
|
7066
|
+
}
|
|
7067
|
+
|
|
7068
|
+
go_depth(depth) {
|
|
7069
|
+
return new Promise(resolve => {
|
|
7070
|
+
window.chessEngineWorker.postMessage('go depth ' + depth);
|
|
7071
|
+
|
|
7072
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7073
|
+
if (message.data.startsWith('bestmove')) {
|
|
7074
|
+
resolve(message.data);
|
|
7075
|
+
}
|
|
7076
|
+
};
|
|
7077
|
+
});
|
|
7078
|
+
}
|
|
7079
|
+
|
|
7080
|
+
go_time(time) {
|
|
7081
|
+
return new Promise(resolve => {
|
|
7082
|
+
window.chessEngineWorker.postMessage('go movetime ' + time);
|
|
7083
|
+
|
|
7084
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7085
|
+
if (message.data.startsWith('bestmove')) {
|
|
7086
|
+
resolve(message.data);
|
|
7087
|
+
}
|
|
7088
|
+
};
|
|
7089
|
+
});
|
|
7090
|
+
}
|
|
7091
|
+
|
|
7092
|
+
stop() {
|
|
7093
|
+
return new Promise(resolve => {
|
|
7094
|
+
if (!this.isAnalyzing) {
|
|
7095
|
+
resolve();
|
|
7096
|
+
}
|
|
7097
|
+
|
|
7098
|
+
window.chessEngineWorker.postMessage('stop');
|
|
7099
|
+
|
|
7100
|
+
window.chessEngineWorker.onmessage = message => {
|
|
7101
|
+
if (message.data.startsWith('bestmove')) {
|
|
7102
|
+
this.isAnalyzing = false;
|
|
7103
|
+
resolve(message.data);
|
|
7104
|
+
}
|
|
7105
|
+
};
|
|
7106
|
+
});
|
|
7107
|
+
}
|
|
7108
|
+
|
|
7109
|
+
quit() {
|
|
7110
|
+
window.chessEngineWorker.postMessage('quit');
|
|
7111
|
+
}
|
|
7112
|
+
|
|
7113
|
+
}
|
|
7114
|
+
|
|
6889
7115
|
function styleInject(css, ref) {
|
|
6890
7116
|
if (ref === void 0) ref = {};
|
|
6891
7117
|
var insertAt = ref.insertAt;
|
|
@@ -6941,7 +7167,8 @@ styleInject(css_248z$7);
|
|
|
6941
7167
|
|
|
6942
7168
|
exports.NextChessground = NextChessground$1;
|
|
6943
7169
|
exports.NextEditor = NextEditor$1;
|
|
7170
|
+
exports.Stockfish = Stockfish;
|
|
7171
|
+
exports.constants = constants;
|
|
6944
7172
|
exports.default = NextChessground$1;
|
|
6945
|
-
exports.fen = fen;
|
|
6946
7173
|
exports.useChess = useChess;
|
|
6947
7174
|
exports.useChessground = useChessground;
|
package/index.js
CHANGED
|
@@ -2,7 +2,8 @@ import NextChessground from './components/NextChessground';
|
|
|
2
2
|
import NextEditor from './components/NextEditor';
|
|
3
3
|
import useChess from './hooks/use-chess';
|
|
4
4
|
import useChessground from './hooks/use-chessground';
|
|
5
|
-
import
|
|
5
|
+
import constants from './utils/constants';
|
|
6
|
+
import Stockfish from './utils/stockfish';
|
|
6
7
|
|
|
7
8
|
import './assets/css/board.css';
|
|
8
9
|
import './assets/css/chess.css';
|
|
@@ -15,4 +16,11 @@ import './assets/css/tailwind.css';
|
|
|
15
16
|
|
|
16
17
|
export default NextChessground;
|
|
17
18
|
|
|
18
|
-
export {
|
|
19
|
+
export {
|
|
20
|
+
NextChessground,
|
|
21
|
+
NextEditor,
|
|
22
|
+
Stockfish,
|
|
23
|
+
useChess,
|
|
24
|
+
useChessground,
|
|
25
|
+
constants,
|
|
26
|
+
};
|