Skip to content

Commit 784ec0c

Browse files
Refactor dotenv setup and improve PGN output
Refactor dotenv configuration and enhance PGN generation with additional game result details.
1 parent 0d802a1 commit 784ec0c

1 file changed

Lines changed: 41 additions & 5 deletions

File tree

microchess/microchess.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
require('dotenv').config();
2-
const fs = require('fs');
31
const path = require('path');
2+
require('dotenv').config({ path: path.resolve(__dirname, '.env') });
3+
const fs = require('fs');
44
const winston = require('winston');
55
const { Chess } = require('../dist/cjs/chess.js');
66

@@ -10,7 +10,6 @@ const OUTPUT_FILE = path.join(__dirname, 'randomchess.json');
1010
// To customize, change the value below (in bytes), e.g. 2 * 1024 * 1024 for 2MB.
1111
const MAX_SIZE_BYTES = 1 * 1024 * 1024;
1212

13-
1413
/*
1514
| MICROCHESS_GENERATOR_INTERVAL (ms) | Interval | Example use |
1615
|------------------------------------|------------------|-----------------------|
@@ -65,6 +64,7 @@ function generateRandomChessGame() {
6564
const chess = new Chess();
6665
let moves = [];
6766
let moveNumber = 1;
67+
const startTime = new Date();
6868
while (!chess.isGameOver() && moveNumber <= 100) {
6969
const legalMoves = chess.moves();
7070
if (legalMoves.length === 0) break;
@@ -73,18 +73,54 @@ function generateRandomChessGame() {
7373
moves.push(move);
7474
moveNumber++;
7575
}
76-
// Create a PGN-like string
76+
const endTime = new Date();
77+
// PGN moves
7778
let pgnMoves = '';
7879
for (let i = 0; i < moves.length; i++) {
7980
if (i % 2 === 0) pgnMoves += `${Math.floor(i / 2) + 1}. `;
8081
pgnMoves += moves[i] + ' ';
8182
}
8283
pgnMoves = pgnMoves.trim();
84+
85+
// Determine result and reason
86+
let result = '*', reason = '';
87+
if (chess.isCheckmate()) {
88+
result = chess.turn() === 'w' ? '0-1' : '1-0';
89+
reason = 'Checkmate';
90+
} else if (chess.isStalemate()) {
91+
result = '1/2-1/2';
92+
reason = 'Draw by stalemate';
93+
} else if (chess.isThreefoldRepetition()) {
94+
result = '1/2-1/2';
95+
reason = 'Draw by threefold repetition';
96+
} else if (chess.isInsufficientMaterial()) {
97+
result = '1/2-1/2';
98+
reason = 'Draw by insufficient material';
99+
} else if (chess.isDraw()) {
100+
result = '1/2-1/2';
101+
reason = 'Draw';
102+
}
103+
104+
// Build PGN with headers
105+
const pgnHeaders = [
106+
`[Event "Random Game"]`,
107+
`[Site "microchess"]`,
108+
`[Date "${startTime.toISOString().slice(0, 10).replace(/-/g, ".")}"]`,
109+
`[Result "${result}"]`,
110+
`[PlyCount "${moves.length}"]`
111+
].join('\n');
112+
const pgn = `${pgnHeaders}\n\n${pgnMoves} ${result}`;
113+
83114
return {
84115
process: 'microchess',
85116
message: `Random Game Of Chess: ${pgnMoves}`,
86117
status: 'generated',
87-
timestamp: new Date().toISOString()
118+
timestamp: endTime.toISOString(),
119+
move_count: moves.length,
120+
result,
121+
reason,
122+
final_fen: chess.fen(),
123+
pgn
88124
};
89125
}
90126

0 commit comments

Comments
 (0)