From 6db8d3806d3c113f3de1747a9c16b786532cf112 Mon Sep 17 00:00:00 2001 From: DHTMLGoodies Date: Sat, 2 Sep 2017 13:39:20 +0200 Subject: [PATCH] Fixed bug parsing comments --- MoveBuilder.php | 177 +++++++++++++++++++++++++++--------------------- 1 file changed, 99 insertions(+), 78 deletions(-) diff --git a/MoveBuilder.php b/MoveBuilder.php index 894bf42..fcc0baa 100755 --- a/MoveBuilder.php +++ b/MoveBuilder.php @@ -1,132 +1,151 @@ moveReferences[0] =& $this->moves; } - public function addMoves($moveString){ + public function addMoves($moveString) + { $moves = explode(" ", $moveString); - foreach($moves as $move){ + foreach ($moves as $move) { $this->addMove($move); } } - private function addMove($move){ - if(!$this->isChessMove($move)){ + private function addMove($move) + { + if (!$this->isChessMove($move)) { return; } - $move = preg_replace("/^([a-h])([18])([QRNB])$/", "$1$2=$3", $move ); + $move = preg_replace("/^([a-h])([18])([QRNB])$/", "$1$2=$3", $move); $this->moveReferences[$this->pointer][] = array(CHESS_JSON::MOVE_NOTATION => $move); - $this->currentIndex ++; + $this->currentIndex++; } - private function isChessMove($move){ - if($move == '--')return true; + private function isChessMove($move) + { + 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); } - public function addCommentBeforeFirstMove($comment){ + public function addCommentBeforeFirstMove($comment) + { $comment = trim($comment); - if(!strlen($comment)){ + if (!strlen($comment)) { return; } $this->moveReferences[$this->pointer][] = array(); $this->addComment($comment); } - public function addComment($comment){ + public function addComment($comment) + { $comment = trim($comment); - if(!strlen($comment)){ + if (!strlen($comment)) { return; } - $index = count($this->moveReferences[$this->pointer])-1; + #$index = max(0,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); $comment = str_replace('[%clk ' . $clk . ']', '', $comment); $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_CLOCK] = $clk; } $actions = $this->getActions($comment); - if(!empty($actions)){ - if(empty($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS])){ - $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS] = array(); - } - foreach($actions as $action){ - $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS][] = $action; - } + if (!empty($actions)) { + if (empty($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS])) { + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS] = array(); + } + foreach ($actions as $action) { + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS][] = $action; + } } - $comment = preg_replace('/\[%'.CHESS_JSON::PGN_KEY_ACTION_ARROW . '[^\]]+?\]/si', '', $comment ); - $comment = preg_replace('/\[%'.CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . '[^\]]+?\]/si', '', $comment ); - $comment = trim($comment); + $comment = preg_replace('/\[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW . '[^\]]+?\]/si', '', $comment); + $comment = preg_replace('/\[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . '[^\]]+?\]/si', '', $comment); + $comment = trim($comment); - if(empty($comment))return; + if (empty($comment)) return; + if ($index === -1) { + $index = 0; + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment; + $this->currentIndex++; + } else { + $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment; + + } - $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_COMMENT] = $comment; } - private function getActions($comment){ - $ret = array(); - if(strstr($comment,'[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW )){ - $arrow = preg_replace('/.*?\[%'. CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment); - $arrows = explode(",", $arrow); + private function getActions($comment) + { + $ret = array(); + if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW)) { + $arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment); + $arrows = explode(",", $arrow); - foreach($arrows as $arrow){ - $tokens = explode(";", $arrow); - if(strlen($tokens[0]) == 4){ - $action = array( - "from" => substr($arrow,0,2), - "to" => substr($arrow, 2,2) - ); - if(count($tokens) > 1){ - $action["color"] = $tokens[1]; - } - $ret[] = $this->toAction("arrow", $action); - } - } - } - if(strstr($comment,'[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT )){ - $arrow = preg_replace('/.*?\[%'. CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . ' ([^\]]+?)\].*/si', '$1', $comment); - $arrows = explode(",", $arrow); + foreach ($arrows as $arrow) { + $tokens = explode(";", $arrow); + if (strlen($tokens[0]) == 4) { + $action = array( + "from" => substr($arrow, 0, 2), + "to" => substr($arrow, 2, 2) + ); + if (count($tokens) > 1) { + $action["color"] = $tokens[1]; + } + $ret[] = $this->toAction("arrow", $action); + } + } + } + if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT)) { + $arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . ' ([^\]]+?)\].*/si', '$1', $comment); + $arrows = explode(",", $arrow); - foreach($arrows as $arrow){ - $tokens = explode(";", $arrow); - if(strlen($tokens[0]) == 2){ - $action = array( - "square" => substr($arrow,0,2) - ); - if(count($tokens) > 1){ - $action["color"] = $tokens[1]; - } - $ret[] = $this->toAction("highlight", $action); - } - } - } - return $ret; + foreach ($arrows as $arrow) { + $tokens = explode(";", $arrow); + if (strlen($tokens[0]) == 2) { + $action = array( + "square" => substr($arrow, 0, 2) + ); + if (count($tokens) > 1) { + $action["color"] = $tokens[1]; + } + $ret[] = $this->toAction("highlight", $action); + } + } + } + return $ret; } - /** - * @param string $key - * @param array $val - * - * @return array - */ - private function toAction($key, $val){ - $val["type"] = $key; - return $val; + /** + * @param string $key + * @param array $val + * + * @return array + */ + private function toAction($key, $val) + { + $val["type"] = $key; + return $val; } - public function startVariation(){ - $index = count($this->moveReferences[$this->pointer])-1; - if(!isset($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS])){ + public function startVariation() + { + $index = count($this->moveReferences[$this->pointer]) - 1; + if (!isset($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS])) { $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS] = array(); } $countVariations = count($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS]); @@ -135,12 +154,14 @@ class MoveBuilder { $this->pointer++; } - public function endVariation(){ + public function endVariation() + { array_pop($this->moveReferences); $this->pointer--; } - public function getMoves(){ + public function getMoves() + { return $this->moves; } }