chess-moments 0.11.0 → 0.11.2
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/README.md +5 -0
- package/functions/split-by-move.js +21 -0
- package/functions/train.js +44 -10
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -42,6 +42,11 @@ The `depth` key delimits between variants and subvariants played in the chess ga
|
|
|
42
42
|
Returns a two level deep array of chess moments.
|
|
43
43
|
The first level splits between variants and subvariants played in the chess game.
|
|
44
44
|
|
|
45
|
+
### .train()
|
|
46
|
+
|
|
47
|
+
Returns a two level deep array of chess moments.
|
|
48
|
+
It is useful for hiding future moves in chess training.
|
|
49
|
+
|
|
45
50
|
## JSON output
|
|
46
51
|
|
|
47
52
|
The basic PGN file `1. e4 e5 *` will generated the following chess moments in JSON format:
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
function splitByMoveIndex(inputData, current) {
|
|
2
|
+
const before = [];
|
|
3
|
+
const after = [];
|
|
4
|
+
|
|
5
|
+
inputData.forEach((innerArray) => {
|
|
6
|
+
// Check if every element in the inner array has an index less than moveIndex
|
|
7
|
+
if (innerArray.every((item) => item.index < current.index)) {
|
|
8
|
+
if (innerArray.length) {
|
|
9
|
+
before.push(innerArray);
|
|
10
|
+
}
|
|
11
|
+
} else {
|
|
12
|
+
if (innerArray.length) {
|
|
13
|
+
after.push(innerArray);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
return { before, after };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = splitByMoveIndex;
|
package/functions/train.js
CHANGED
|
@@ -1,21 +1,55 @@
|
|
|
1
|
-
const
|
|
1
|
+
const { flatten } = require('lodash');
|
|
2
|
+
const splitByMoveIndex = require('./split-by-move');
|
|
3
|
+
const tree = require('./tree');
|
|
2
4
|
|
|
3
|
-
const train = (sloppyPgn,
|
|
4
|
-
|
|
5
|
+
const train = (sloppyPgn, fen) => {
|
|
6
|
+
// Tree is an array of array depths
|
|
7
|
+
const moments = tree(sloppyPgn);
|
|
8
|
+
|
|
9
|
+
// Similar structure to the tree, but with only the training data
|
|
10
|
+
const training = [];
|
|
5
11
|
|
|
6
12
|
// Remove sidelines (depth > 1) when no move index is specified
|
|
7
|
-
if (!
|
|
8
|
-
|
|
13
|
+
if (!fen) {
|
|
14
|
+
for (const innerArray of moments) {
|
|
15
|
+
const hasDepth = innerArray.filter((moment) => moment.depth === 1);
|
|
16
|
+
if (hasDepth.length) {
|
|
17
|
+
training.push(innerArray);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return training;
|
|
9
21
|
}
|
|
10
22
|
|
|
23
|
+
// Get the current move from the move index
|
|
24
|
+
const current = flatten(moments).find((moment) => moment.fen === fen);
|
|
25
|
+
|
|
11
26
|
// Split the moments into two groups
|
|
12
|
-
const before = moments
|
|
13
|
-
const
|
|
27
|
+
const { before, after } = splitByMoveIndex(moments, current);
|
|
28
|
+
for (const innerArray of before) {
|
|
29
|
+
training.push(innerArray);
|
|
30
|
+
}
|
|
14
31
|
|
|
15
|
-
//
|
|
16
|
-
const
|
|
32
|
+
// Only future moves remaining
|
|
33
|
+
for (const innerArray of after) {
|
|
34
|
+
// Remove every inner array with a depth not equal than the depth of the move index
|
|
35
|
+
const filtered = innerArray.filter((moment) => {
|
|
36
|
+
return moment.depth === current.depth;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
// Add hidden prop to filtered moves with index greater than the move index
|
|
40
|
+
filtered.forEach((moment) => {
|
|
41
|
+
if (moment.index > current.index) {
|
|
42
|
+
moment.hidden = true;
|
|
43
|
+
// Also remove comments and shapes for hidden moves
|
|
44
|
+
delete moment.comment;
|
|
45
|
+
delete moment.shapes;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
training.push(filtered);
|
|
50
|
+
}
|
|
17
51
|
|
|
18
|
-
return
|
|
52
|
+
return training;
|
|
19
53
|
};
|
|
20
54
|
|
|
21
55
|
module.exports = train;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "chess-moments",
|
|
3
|
-
"version": "0.11.
|
|
4
|
-
"description": "PGN
|
|
3
|
+
"version": "0.11.2",
|
|
4
|
+
"description": "PGN parser that transforms PGN files into chess \"moments\"",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"files": [
|
|
7
7
|
"index.js",
|