From 5505a28b1df5698ca16105ce04e2d297e1cb0d47 Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Mon, 30 Jan 2017 13:42:28 +0100 Subject: [PATCH] Parsing out clock %clk comments --- CHESS_JSON.php | 1 + MoveBuilder.php | 9 +++++++++ PgnParser.php | 1 - test/ParserTest.php | 23 +++++++++++++++++++++++ test/pgn/lcc2016.pgn | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 68 insertions(+), 1 deletion(-) create mode 100755 test/pgn/lcc2016.pgn diff --git a/CHESS_JSON.php b/CHESS_JSON.php index 0f027c1..70ad476 100755 --- a/CHESS_JSON.php +++ b/CHESS_JSON.php @@ -7,6 +7,7 @@ class CHESS_JSON { const MOVE_NOTATION = 'm'; const FEN = 'fen'; const MOVE_COMMENT = 'comment'; + const MOVE_CLOCK = 'clock'; const MOVE_ACTION = 'action'; const MOVE_VARIATIONS = 'variations'; const MOVE_MOVES = 'moves'; diff --git a/MoveBuilder.php b/MoveBuilder.php index f83d284..f1055c5 100755 --- a/MoveBuilder.php +++ b/MoveBuilder.php @@ -45,6 +45,15 @@ class MoveBuilder { return; } $index = count($this->moveReferences[$this->pointer])-1; + + if(strstr($comment,'[%clk' )){ + $clk = preg_replace('/\[%clk[^0-9]*?([0-9\:]+?)[\]]/si', '$1', $comment); + $comment = str_replace('[%clk ' . $clk . ']', '', $comment); + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_CLOCK] = $clk; + } + if(empty($comment))return; + + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment; } diff --git a/PgnParser.php b/PgnParser.php index 89b7fb0..3dc16fc 100755 --- a/PgnParser.php +++ b/PgnParser.php @@ -18,7 +18,6 @@ class PgnParser if(!file_exists($this->pgnFile)){ throw new Exception("File not found: ". $this->pgnFile); } - } $this->_fullParsing = $fullParsing; diff --git a/test/ParserTest.php b/test/ParserTest.php index f322dd2..a65425b 100755 --- a/test/ParserTest.php +++ b/test/ParserTest.php @@ -2026,6 +2026,29 @@ Rc8 Ne6+ 72. Kf6 d2 73. c5+ Kd7 0-1'; // then $this->assertEquals((36*2)+1, count($game['moves'])); } + + /** + * @test + */ + public function shouldParseClockComments(){ + // given + + $pgnParser = new PgnParser("pgn/lcc2016.pgn"); + + // when + $game = $pgnParser->getGameByIndex(0); + + // then + $this->assertNotEmpty($game['white'], $game); + $this->assertNotEmpty($game['moves'][0]); + $m = json_encode($game['moves'][0]); + echo $m; + $this->assertNotEmpty($game['moves'][0]['clock'], "Move: ". $m); + $this->assertEquals('1:59:56',$game['moves'][0]['clock']); + + + } + /** * @test */ diff --git a/test/pgn/lcc2016.pgn b/test/pgn/lcc2016.pgn new file mode 100755 index 0000000..4fb139a --- /dev/null +++ b/test/pgn/lcc2016.pgn @@ -0,0 +1,35 @@ +[Event "London Chess Classic 2016"] +[Site "London"] +[Date "2016.12.18"] +[Round "9.1"] +[White "So, Wesley"] +[Black "Vachier-Lagrave, Maxime"] +[Result "1/2-1/2"] +[BlackElo "2804"] +[WhiteElo "2794"] +[LiveChessVersion "1.4.8"] +[ECO "A04"] + +1. Nf3 {[%clk 1:59:56]} c5 {[%clk 1:59:51]} 2. c4 {[%clk 1:59:51]} Nc6 +{[%clk 1:59:48]} 3. Nc3 {[%clk 1:59:45]} e5 {[%clk 1:59:45]} 4. e3 +{[%clk 1:59:33]} Nf6 {[%clk 1:59:40]} 5. Be2 {[%clk 1:59:26]} d5 {[%clk 1:59:14]} +6. d4 {[%clk 1:59:18]} cxd4 {[%clk 1:58:32]} 7. exd4 {[%clk 1:59:11]} e4 +{[%clk 1:58:29]} 8. Ne5 {[%clk 1:59:04]} dxc4 {[%clk 1:58:26]} 9. Bxc4 +{[%clk 1:58:42]} Nxe5 {[%clk 1:58:21]} 10. dxe5 {[%clk 1:58:38]} Qxd1+ +{[%clk 1:58:18]} 11. Kxd1 {[%clk 1:58:31]} Ng4 {[%clk 1:58:15]} 12. e6 +{[%clk 1:58:24]} fxe6 {[%clk 1:58:11]} 13. Nxe4 {[%clk 1:57:17]} Bd7 +{[%clk 1:57:19]} 14. f3 {[%clk 1:56:56]} Ne5 {[%clk 1:53:13]} 15. Bb3 +{[%clk 1:56:50]} Rd8 {[%clk 1:42:01]} 16. Bd2 {[%clk 1:56:12]} Nd3 +{[%clk 1:41:54]} 17. Kc2 {[%clk 1:49:59]} Nb4+ {[%clk 1:41:47]} 18. Bxb4 +{[%clk 1:49:55]} Bxb4 {[%clk 1:41:40]} 19. Nc3 {[%clk 1:49:48]} Ke7 +{[%clk 1:36:22]} 20. Rhe1 {[%clk 1:48:59]} Bxc3 {[%clk 1:36:16]} 21. Kxc3 +{[%clk 1:46:18]} Rc8+ {[%clk 1:33:40]} 22. Kd2 {[%clk 1:46:11]} Rhd8 +{[%clk 1:33:36]} 23. Ke3 {[%clk 1:46:06]} e5 {[%clk 1:29:17]} 24. Rad1 +{[%clk 1:43:07]} Bc6 {[%clk 1:28:23]} 25. h4 {[%clk 1:41:49]} h6 {[%clk 1:27:41]} +26. a3 {[%clk 1:40:10]} Rxd1 {[%clk 1:22:21]} 27. Rxd1 {[%clk 1:40:05]} Rf8 +{[%clk 1:21:39]} 28. Rf1 {[%clk 1:37:25]} Rf4 {[%clk 1:20:34]} 29. g3 +{[%clk 1:37:12]} Rd4 {[%clk 1:20:28]} 30. Rd1 {[%clk 1:36:36]} Rxd1 +{[%clk 1:20:24]} 31. Bxd1 {[%clk 1:36:34]} g5 {[%clk 1:19:44]} 32. hxg5 +{[%clk 1:36:30]} hxg5 {[%clk 1:19:39]} 33. f4 {[%clk 1:36:26]} gxf4+ +{[%clk 1:19:36]} 34. gxf4 {[%clk 1:36:23]} exf4+ {[%clk 1:19:34]} 35. Kxf4 +{[%clk 1:36:19]} 1/2-1/2