Skip to content

Commit

Permalink
Merge pull request #175 from franciscoBSalgueiro/cutechess-format
Browse files Browse the repository at this point in the history
Add cutechess-style comments support
  • Loading branch information
niklasf authored Feb 7, 2024
2 parents 1342cb6 + 1650aa2 commit d5d54f8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
18 changes: 18 additions & 0 deletions src/pgn.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ test('parse comment', () => {
shapes: [{ from: 0, to: 0, color: 'green' }],
});
expect(parseComment('foo [%bar] [%csl Ga1] [%cal Ra1h1,Gb1b8] [%clk 3:25:45]').text).toBe('foo [%bar]');
expect(parseComment('+M35/33 4.3s')).toEqual({
text: '',
emt: 4.3,
evaluation: { mate: 35, depth: 33 },
shapes: [],
});
expect(parseComment('-0.32/24 1.0s, White loses on time')).toEqual({
emt: 1.0,
evaluation: { pawns: -0.32, depth: 24 },
text: 'White loses on time',
shapes: [],
});
expect(parseComment('+M1/245, 0.003s, nodes,9071, seldepth, 2, nps, 2267750, Komodo got checkmated')).toEqual({
emt: 0.003,
evaluation: { mate: 1, depth: 245 },
text: 'nodes,9071, seldepth, 2, nps, 2267750, Komodo got checkmated',
shapes: [],
});
});

test('make comment', () => {
Expand Down
11 changes: 11 additions & 0 deletions src/pgn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,17 @@ export const parseComment = (comment: string): Comment => {
return ' ';
},
)
.replace(
/\s?(?:([+-]?M\d{1,5})|([+-]?(?:\d{1,5}|\d{0,5}\.\d{1,2})))(?:\/(\d{1,5}))?,?(?:\s(\d{1,5}\.\d{1,3})s),?\s?/g,
(_, mate, pawns, d, e) => {
const depth = d && parseInt(d, 10);
evaluation = mate ? { mate: parseInt(mate.replace('M', ''), 10), depth } : { pawns: parseFloat(pawns), depth };
if (e) {
emt = parseFloat(e);
}
return ' ';
},
)
.trim();
return {
text,
Expand Down

0 comments on commit d5d54f8

Please sign in to comment.