-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
169 lines (154 loc) · 4.19 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
const { Chess } = require("chess.js");
const ChessWebAPI = require("chess-web-api");
module.exports = class Chesster {
constructor(chessId) {
this.chessId = chessId;
this.chessAPI = new ChessWebAPI();
this.startingChessBoard =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
}
async initialize() {
if (!this.chessId || this.chessId == "")
throw new Error("Chess game ID is required");
this.chessData = await this.chessAPI.getGameByID(this.chessId);
this.gameData = this.chessData.body.game;
}
async getFen() {
const { startingChessBoard, gameData } = this;
const chess = new Chess();
const newChessBoard = new Chess(startingChessBoard);
chess.loadPgn(gameData.pgn, { sloppy: true });
let moves = chess.history();
let fenArray = [];
for (let move of moves) {
newChessBoard.move(move);
fenArray.push(newChessBoard.fen());
}
return {
fen: fenArray,
};
}
async getMoves() {
const chess = new Chess();
chess.loadPgn(this.gameData.pgn, { sloppy: true });
let moves = chess.history();
return {
moves,
};
}
async getPGN() {
const { pgn } = this.gameData;
return { pgn };
}
async getElo() {
const { WhiteElo: whiteElo, BlackElo: blackElo } = this.gameData.pgnHeaders;
return {
whiteElo,
blackElo,
};
}
async getPlayerUsernames() {
const { White: whitePlayer, Black: blackPlayer } = this.gameData.pgnHeaders;
return {
whitePlayer,
blackPlayer,
};
}
async getPlayerData() {
const { top, bottom } = this.chessData.body.players;
return { top, bottom };
}
async getAverageElo() {
const { WhiteElo, BlackElo } = this.gameData.pgnHeaders;
const averageElo = (WhiteElo + BlackElo) / 2;
return {
averageElo,
};
}
async getWinner() {
const { colorOfWinner } = this.gameData;
return {
winner: colorOfWinner ? colorOfWinner : "draw",
};
}
async getResultMessage() {
const { resultMessage } = this.gameData;
return {
resultMessage,
};
}
async getAll() {
const chess = new Chess();
const newChessBoard = new Chess(this.startingChessBoard);
chess.loadPgn(this.gameData.pgn, { sloppy: true });
let moves = chess.history();
let fenArray = [];
for (let move of moves) {
newChessBoard.move(move);
fenArray.push(newChessBoard.fen());
}
let resultArray = [];
const splittedMoveTimestamps = this.gameData.moveTimestamps.split(",");
let formattedTimestampsArray = [];
const convertSecondsToHMS = (sec) => {
let sec_num = parseInt(sec, 10) / 10;
let hours = Math.floor(sec_num / 3600);
let minutes = Math.floor((sec_num - hours * 3600) / 60);
let seconds =
Math.round((sec_num - hours * 3600 - minutes * 60) * 10) / 10;
if (hours < 10) hours = "0" + hours;
if (minutes < 10) minutes = "0" + minutes;
if (seconds < 10) seconds = "0" + seconds;
return hours + ":" + minutes + ":" + seconds;
};
for (let j = 0; j < splittedMoveTimestamps.length; j++)
formattedTimestampsArray.push(
convertSecondsToHMS(splittedMoveTimestamps[j])
);
for (let i = 0; i < fenArray.length; i++) {
resultArray.push({
fen: fenArray[i],
move: moves[i],
time: formattedTimestampsArray[i],
moveBy: i % 2 == 0 ? "white" : "black",
});
}
const {
White: whitePlayer,
Black: blackPlayer,
WhiteElo: whiteElo,
BlackElo: blackElo,
} = this.gameData.pgnHeaders;
const { top, bottom } = this.chessData.body.players;
const {
isAbortable,
isAnalyzable,
isCheckmate,
isStalemate,
isFinished,
isRated,
isResignable,
colorOfWinner,
resultMessage,
} = this.gameData;
return {
game: resultArray,
whitePlayer,
blackPlayer,
isAbortable,
isAnalyzable,
isCheckmate,
isStalemate,
isFinished,
isRated,
isResignable,
whiteElo,
blackElo,
resultMessage,
top,
bottom,
averageElo: (whiteElo + blackElo) / 2,
winner: colorOfWinner ? colorOfWinner : "draw",
};
}
};