Fixed bug parsing comments

This commit is contained in:
DHTMLGoodies
2017-09-02 13:39:20 +02:00
parent 88b0b3fbf0
commit 6db8d3806d

View File

@@ -1,23 +1,27 @@
<?php <?php
class MoveBuilder { class MoveBuilder
{
private $moves = array(); private $moves = array();
private $moveReferences = array(); private $moveReferences = array();
private $pointer = 0; private $pointer = 0;
private $currentIndex = 0; private $currentIndex = 0;
public function __construct(){ public function __construct()
{
$this->moveReferences[0] =& $this->moves; $this->moveReferences[0] =& $this->moves;
} }
public function addMoves($moveString){ public function addMoves($moveString)
{
$moves = explode(" ", $moveString); $moves = explode(" ", $moveString);
foreach ($moves as $move) { foreach ($moves as $move) {
$this->addMove($move); $this->addMove($move);
} }
} }
private function addMove($move){ private function addMove($move)
{
if (!$this->isChessMove($move)) { if (!$this->isChessMove($move)) {
return; return;
} }
@@ -26,12 +30,14 @@ class MoveBuilder {
$this->currentIndex++; $this->currentIndex++;
} }
private function isChessMove($move){ private function isChessMove($move)
{
if ($move == '--') return true; if ($move == '--') return true;
return preg_match("/([PNBRQK]?[a-h]?[1-8]?x?[a-h][1-8](?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?/s", $move); return preg_match("/([PNBRQK]?[a-h]?[1-8]?x?[a-h][1-8](?:\=[PNBRQK])?|O(-?O){1,2})[\+#]?(\s*[\!\?]+)?/s", $move);
} }
public function addCommentBeforeFirstMove($comment){ public function addCommentBeforeFirstMove($comment)
{
$comment = trim($comment); $comment = trim($comment);
if (!strlen($comment)) { if (!strlen($comment)) {
return; return;
@@ -40,13 +46,16 @@ class MoveBuilder {
$this->addComment($comment); $this->addComment($comment);
} }
public function addComment($comment){ public function addComment($comment)
{
$comment = trim($comment); $comment = trim($comment);
if (!strlen($comment)) { if (!strlen($comment)) {
return; return;
} }
#$index = max(0,count($this->moveReferences[$this->pointer])-1);
$index = count($this->moveReferences[$this->pointer]) - 1; $index = count($this->moveReferences[$this->pointer]) - 1;
if (strstr($comment, '[%clk')) { if (strstr($comment, '[%clk')) {
$clk = preg_replace('/\[%clk[^0-9]*?([0-9\:]+?)[\]]/si', '$1', $comment); $clk = preg_replace('/\[%clk[^0-9]*?([0-9\:]+?)[\]]/si', '$1', $comment);
$comment = str_replace('[%clk ' . $clk . ']', '', $comment); $comment = str_replace('[%clk ' . $clk . ']', '', $comment);
@@ -69,11 +78,19 @@ class MoveBuilder {
if (empty($comment)) return; if (empty($comment)) return;
if ($index === -1) {
$index = 0;
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment; $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment;
$this->currentIndex++;
} else {
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment;
} }
private function getActions($comment){ }
private function getActions($comment)
{
$ret = array(); $ret = array();
if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW)) { if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW)) {
$arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment); $arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment);
@@ -119,12 +136,14 @@ class MoveBuilder {
* *
* @return array * @return array
*/ */
private function toAction($key, $val){ private function toAction($key, $val)
{
$val["type"] = $key; $val["type"] = $key;
return $val; return $val;
} }
public function startVariation(){ public function startVariation()
{
$index = count($this->moveReferences[$this->pointer]) - 1; $index = count($this->moveReferences[$this->pointer]) - 1;
if (!isset($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS])) { if (!isset($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS])) {
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS] = array(); $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS] = array();
@@ -135,12 +154,14 @@ class MoveBuilder {
$this->pointer++; $this->pointer++;
} }
public function endVariation(){ public function endVariation()
{
array_pop($this->moveReferences); array_pop($this->moveReferences);
$this->pointer--; $this->pointer--;
} }
public function getMoves(){ public function getMoves()
{
return $this->moves; return $this->moves;
} }
} }