pgn-manager 1.0.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/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.js +152 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Harsh Kumar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# pgn-manager
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { ParsedPGN, Move, Rav, Header } from "pgn-parser";
|
|
2
|
+
export declare const FEN_START_POSITION = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
3
|
+
export declare const FEN_EMPTY_POSITION = "8/8/8/8/8/8/8/8";
|
|
4
|
+
declare class PGNManager {
|
|
5
|
+
private rawPGN;
|
|
6
|
+
private game;
|
|
7
|
+
private sortedMoves;
|
|
8
|
+
private moveFen;
|
|
9
|
+
private moveParent;
|
|
10
|
+
private ravParent;
|
|
11
|
+
constructor(pgn: string);
|
|
12
|
+
private dfOnGame;
|
|
13
|
+
private dfsOnGame;
|
|
14
|
+
get pgn(): string;
|
|
15
|
+
get parsedPGN(): ParsedPGN;
|
|
16
|
+
get headers(): Array<Header>;
|
|
17
|
+
getMove: (moveNumber: number) => Move;
|
|
18
|
+
getMoveNumber: (move: Move) => number;
|
|
19
|
+
nextMove: (move: Move | undefined) => Move;
|
|
20
|
+
hasNextMove: (move: Move) => boolean;
|
|
21
|
+
previousMove: (move: Move) => Move | undefined;
|
|
22
|
+
getFirstMove: () => Move;
|
|
23
|
+
getLastMove: () => any;
|
|
24
|
+
getMoveFen: (move: Move) => string;
|
|
25
|
+
getParentRav: (move: Move) => Rav | null;
|
|
26
|
+
}
|
|
27
|
+
export default PGNManager;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FEN_EMPTY_POSITION = exports.FEN_START_POSITION = void 0;
|
|
4
|
+
const ChessJS = require("chess.js");
|
|
5
|
+
const Chess = typeof ChessJS === "function" ? ChessJS : ChessJS.Chess;
|
|
6
|
+
const pgn_parser_1 = require("pgn-parser");
|
|
7
|
+
exports.FEN_START_POSITION = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
|
8
|
+
exports.FEN_EMPTY_POSITION = "8/8/8/8/8/8/8/8";
|
|
9
|
+
class PGNManager {
|
|
10
|
+
rawPGN;
|
|
11
|
+
game;
|
|
12
|
+
sortedMoves;
|
|
13
|
+
moveFen;
|
|
14
|
+
moveParent;
|
|
15
|
+
ravParent;
|
|
16
|
+
constructor(pgn) {
|
|
17
|
+
this.rawPGN = pgn;
|
|
18
|
+
this.game = pgn_parser_1.default.parse(pgn + " *")[0];
|
|
19
|
+
this.sortedMoves = [];
|
|
20
|
+
this.moveParent = new Map();
|
|
21
|
+
this.ravParent = new Map();
|
|
22
|
+
this.moveFen = new Map();
|
|
23
|
+
this.dfOnGame(this.game);
|
|
24
|
+
}
|
|
25
|
+
dfOnGame = (game) => {
|
|
26
|
+
this.sortedMoves = [];
|
|
27
|
+
var chessGame = new Chess(exports.FEN_START_POSITION);
|
|
28
|
+
for (let move of game.moves) {
|
|
29
|
+
this.dfsOnGame(move, game, chessGame);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
dfsOnGame = (move, parent, chessGame) => {
|
|
33
|
+
this.sortedMoves.push(move);
|
|
34
|
+
this.moveParent.set(move, parent);
|
|
35
|
+
if (move.ravs) {
|
|
36
|
+
for (let rav of move.ravs) {
|
|
37
|
+
this.ravParent.set(rav, move);
|
|
38
|
+
let newVarChessGame = new Chess(chessGame.fen());
|
|
39
|
+
for (let ravMove of rav.moves) {
|
|
40
|
+
this.dfsOnGame(ravMove, rav, newVarChessGame);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// move only after variations are processed
|
|
45
|
+
if (!chessGame.move(move.move, { sloppy: false }) &&
|
|
46
|
+
!chessGame.move(move.move, { sloppy: true }))
|
|
47
|
+
console.log("Invalid move: " + move.move);
|
|
48
|
+
this.moveFen.set(move, chessGame.fen());
|
|
49
|
+
};
|
|
50
|
+
get pgn() {
|
|
51
|
+
return this.rawPGN;
|
|
52
|
+
}
|
|
53
|
+
get parsedPGN() {
|
|
54
|
+
return this.game;
|
|
55
|
+
}
|
|
56
|
+
get headers() {
|
|
57
|
+
if (!this.game || !this.game.headers)
|
|
58
|
+
return [];
|
|
59
|
+
return this.game.headers;
|
|
60
|
+
}
|
|
61
|
+
getMove = (moveNumber) => {
|
|
62
|
+
let move = this.sortedMoves[moveNumber - 1];
|
|
63
|
+
return move;
|
|
64
|
+
};
|
|
65
|
+
getMoveNumber = (move) => {
|
|
66
|
+
return this.sortedMoves.indexOf(move) + 1;
|
|
67
|
+
};
|
|
68
|
+
nextMove = (move) => {
|
|
69
|
+
if (!move) {
|
|
70
|
+
if (this.sortedMoves.length == 0) {
|
|
71
|
+
throw Error("No moves in game");
|
|
72
|
+
}
|
|
73
|
+
return this.sortedMoves[0];
|
|
74
|
+
}
|
|
75
|
+
if (move == this.game.moves[this.game.moves.length - 1] ||
|
|
76
|
+
move === this.sortedMoves[this.sortedMoves.length - 1]) {
|
|
77
|
+
return move;
|
|
78
|
+
}
|
|
79
|
+
let parentRav = this.moveParent.get(move);
|
|
80
|
+
let tempNextMove = this.getMove(this.getMoveNumber(move) + 1);
|
|
81
|
+
if (parentRav) {
|
|
82
|
+
let indexInParent = parentRav.moves.indexOf(move);
|
|
83
|
+
if (indexInParent == parentRav.moves.length - 1) {
|
|
84
|
+
let superParent = this.ravParent.get(parentRav);
|
|
85
|
+
if (superParent) {
|
|
86
|
+
tempNextMove = this.nextMove(superParent);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
tempNextMove = parentRav.moves[indexInParent + 1];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return tempNextMove;
|
|
94
|
+
};
|
|
95
|
+
hasNextMove = (move) => {
|
|
96
|
+
return !move || this.nextMove(move) !== move;
|
|
97
|
+
};
|
|
98
|
+
previousMove = (move) => {
|
|
99
|
+
if (!move) {
|
|
100
|
+
if (this.sortedMoves.length == 0) {
|
|
101
|
+
throw Error("No moves in game");
|
|
102
|
+
}
|
|
103
|
+
throw Error("Invalid 'move' parameter while getting previous move");
|
|
104
|
+
}
|
|
105
|
+
let currentMoveNumber = this.getMoveNumber(move);
|
|
106
|
+
if (currentMoveNumber == 0) {
|
|
107
|
+
return undefined;
|
|
108
|
+
}
|
|
109
|
+
let parentRav = this.moveParent.get(move);
|
|
110
|
+
let tempPrevMove = this.getMove(currentMoveNumber - 1);
|
|
111
|
+
if (parentRav) {
|
|
112
|
+
let indexInParent = parentRav.moves.indexOf(move);
|
|
113
|
+
if (indexInParent == 0) {
|
|
114
|
+
let superParent = this.ravParent.get(parentRav);
|
|
115
|
+
if (superParent) {
|
|
116
|
+
tempPrevMove = superParent;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
tempPrevMove = parentRav.moves[indexInParent - 1];
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
return tempPrevMove;
|
|
124
|
+
};
|
|
125
|
+
getFirstMove = () => {
|
|
126
|
+
if (this.sortedMoves.length == 0) {
|
|
127
|
+
throw Error("No moves in game");
|
|
128
|
+
}
|
|
129
|
+
return this.sortedMoves[0];
|
|
130
|
+
};
|
|
131
|
+
getLastMove = () => {
|
|
132
|
+
if (this.sortedMoves.length == 0) {
|
|
133
|
+
throw Error("No moves in game");
|
|
134
|
+
}
|
|
135
|
+
return this.game.moves[this.game.moves.length - 1];
|
|
136
|
+
};
|
|
137
|
+
getMoveFen = (move) => {
|
|
138
|
+
if (!move) {
|
|
139
|
+
throw Error("Invalid 'move' parameter while getting fen");
|
|
140
|
+
}
|
|
141
|
+
let moveFen = this.moveFen.get(move);
|
|
142
|
+
return moveFen ? moveFen : exports.FEN_EMPTY_POSITION;
|
|
143
|
+
};
|
|
144
|
+
getParentRav = (move) => {
|
|
145
|
+
if (!move) {
|
|
146
|
+
throw Error("Invalid 'move' parameter while getting parent rav");
|
|
147
|
+
}
|
|
148
|
+
let parentRav = this.moveParent.get(move);
|
|
149
|
+
return parentRav ? parentRav : null;
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
exports.default = PGNManager;
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pgn-manager",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Libraray built on top of chess.js and pgn-parser to load and process PGN files in typescript.",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"typings": "dist/index",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
+
"prepublishOnly": "npm run compile",
|
|
10
|
+
"compile": "npm run clean && tsc -p .",
|
|
11
|
+
"watch": "tsc -w -p .",
|
|
12
|
+
"clean": "rm -rf dist"
|
|
13
|
+
},
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/hadron43/pgn-manager.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"Chess",
|
|
20
|
+
"PGN",
|
|
21
|
+
"Portal",
|
|
22
|
+
"Game",
|
|
23
|
+
"Notation"
|
|
24
|
+
],
|
|
25
|
+
"author": "Harsh Kumar <hadron43@yahoo.com>",
|
|
26
|
+
"license": "MIT",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/hadron43/pgn-manager/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/hadron43/pgn-manager#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"chess.js": "0.12.0",
|
|
33
|
+
"pgn-parser": "2.1.0",
|
|
34
|
+
"typescript": "4.5.2"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist/index.js",
|
|
38
|
+
"dist/index.d.ts",
|
|
39
|
+
"dist/lib"
|
|
40
|
+
]
|
|
41
|
+
}
|