chess-moments 1.1.3 → 1.2.1
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/functions/extras/add-moment-to-tree.js +15 -0
- package/functions/extras/find-inserted-moment.js +17 -0
- package/functions/extras/get-next-moments.js +21 -14
- package/functions/extras/index.js +2 -0
- package/functions/helpers/insert-moment-into-tree.js +3 -0
- package/index.js +2 -0
- package/package.json +1 -1
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
const { flatten, cloneDeep } = require('lodash');
|
|
2
2
|
const { insertMomentIntoTree } = require('../helpers');
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* Adds a new chess moment to the tree structure.
|
|
6
|
+
* @param {*} tree - The current tree structure.
|
|
7
|
+
* @param {*} newMoment - The new moment to add.
|
|
8
|
+
* @returns {*} - The updated tree structure.
|
|
9
|
+
*/
|
|
4
10
|
const addMomentToTree = (tree, newMoment) => {
|
|
5
11
|
if (!newMoment || !newMoment.san || !newMoment.before) {
|
|
6
12
|
return tree;
|
|
@@ -52,6 +58,15 @@ const addMomentToTree = (tree, newMoment) => {
|
|
|
52
58
|
}
|
|
53
59
|
}
|
|
54
60
|
|
|
61
|
+
// Remove all new flags from existing moments
|
|
62
|
+
for (const line of clonedTree) {
|
|
63
|
+
for (const moment of line) {
|
|
64
|
+
if (moment.new) {
|
|
65
|
+
delete moment.new;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
55
70
|
// Insert the moment into the tree
|
|
56
71
|
const newTree = insertMomentIntoTree(clonedTree, point, newMoment);
|
|
57
72
|
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Finds the most recently inserted moment in the tree.
|
|
3
|
+
* @param {*} tree - The current tree structure.
|
|
4
|
+
* @returns {Object|null} - The inserted moment or null if none found.
|
|
5
|
+
*/
|
|
6
|
+
const findInsertedMoment = (tree) => {
|
|
7
|
+
for (const line of tree) {
|
|
8
|
+
for (const moment of line) {
|
|
9
|
+
if (moment.new) {
|
|
10
|
+
return moment;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
module.exports = findInsertedMoment;
|
|
@@ -62,29 +62,36 @@ const getNextMoments = (moments, current) => {
|
|
|
62
62
|
// Add fullmove number to the current moment
|
|
63
63
|
current.fullmove = Number(current.fen.split(' ')[5]);
|
|
64
64
|
|
|
65
|
-
// Keep only slim moments
|
|
66
|
-
const slim = moments.filter((moment) => {
|
|
67
|
-
const fullmove = Number(moment.fen.split(' ')[5]);
|
|
68
|
-
return (
|
|
69
|
-
moment.index > current.index + 1 &&
|
|
70
|
-
fullmove <= current.fullmove + 1 &&
|
|
71
|
-
moment.move // Also filter out moments without a move
|
|
72
|
-
);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
65
|
// Active color after current move (who moves next)
|
|
76
66
|
const activeColorAfterCurrent = current.fen.split(' ')[1];
|
|
77
67
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
//
|
|
81
|
-
if (moment.
|
|
68
|
+
// Process moments in order and stop when we leave the current branch
|
|
69
|
+
for (const moment of moments) {
|
|
70
|
+
// Skip moments that are at or before the current moment's index
|
|
71
|
+
if (moment.index <= current.index + 1) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
// Stop if we hit a moment with lower depth (we've left this branch)
|
|
75
|
+
if (moment.depth < current.depth) {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
// Skip moments without moves
|
|
79
|
+
if (!moment.move) {
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
// Only consider moments at current depth or one level deeper
|
|
83
|
+
if (moment.depth > current.depth + 1) {
|
|
82
84
|
continue;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
87
|
const activeColorAfterMoment = moment.fen.split(' ')[1];
|
|
86
88
|
const fullmove = Number(moment.fen.split(' ')[5]);
|
|
87
89
|
|
|
90
|
+
// Only consider moments within the valid move range
|
|
91
|
+
if (fullmove > current.fullmove + 1) {
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
|
|
88
95
|
// Check if this is a valid next move based on color and fullmove
|
|
89
96
|
const isValidWhiteMove =
|
|
90
97
|
activeColorAfterCurrent === 'w' &&
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
const addMomentToTree = require('./add-moment-to-tree');
|
|
2
|
+
const findInsertedMoment = require('./find-inserted-moment');
|
|
2
3
|
const getNextMoments = require('./get-next-moments');
|
|
3
4
|
const getPrevMoment = require('./get-prev-moment');
|
|
4
5
|
const momentsToPgn = require('./moments-to-pgn');
|
|
@@ -6,6 +7,7 @@ const moveTrainer = require('./move-trainer');
|
|
|
6
7
|
|
|
7
8
|
module.exports = {
|
|
8
9
|
addMomentToTree,
|
|
10
|
+
findInsertedMoment,
|
|
9
11
|
getNextMoments,
|
|
10
12
|
getPrevMoment,
|
|
11
13
|
momentsToPgn,
|
|
@@ -12,6 +12,9 @@ const insertMomentIntoTree = (tree, point, newMoment) => {
|
|
|
12
12
|
// Prepare the moment to be inserted with only the properties that match normal moments
|
|
13
13
|
const momentToInsert = prepareMoment(tree, point, newMoment);
|
|
14
14
|
|
|
15
|
+
// Add flag to indicate this is a new moment
|
|
16
|
+
momentToInsert.new = true;
|
|
17
|
+
|
|
15
18
|
// If no matching position found, always create a new line
|
|
16
19
|
if (!point) {
|
|
17
20
|
tree.push([momentToInsert]);
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const { fen, pgn, flat, tree, mainline } = require('./functions');
|
|
2
2
|
const {
|
|
3
3
|
addMomentToTree,
|
|
4
|
+
findInsertedMoment,
|
|
4
5
|
getNextMoments,
|
|
5
6
|
getPrevMoment,
|
|
6
7
|
momentsToPgn,
|
|
@@ -14,6 +15,7 @@ module.exports = {
|
|
|
14
15
|
tree,
|
|
15
16
|
mainline,
|
|
16
17
|
addMomentToTree,
|
|
18
|
+
findInsertedMoment,
|
|
17
19
|
getNextMoments,
|
|
18
20
|
getPrevMoment,
|
|
19
21
|
momentsToPgn,
|