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,132 +1,151 @@
<?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;
} }
$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->moveReferences[$this->pointer][] = array(CHESS_JSON::MOVE_NOTATION => $move);
$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;
} }
$this->moveReferences[$this->pointer][] = array(); $this->moveReferences[$this->pointer][] = array();
$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 = 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); $clk = preg_replace('/\[%clk[^0-9]*?([0-9\:]+?)[\]]/si', '$1', $comment);
$comment = str_replace('[%clk ' . $clk . ']', '', $comment); $comment = str_replace('[%clk ' . $clk . ']', '', $comment);
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_CLOCK] = $clk; $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_CLOCK] = $clk;
} }
$actions = $this->getActions($comment); $actions = $this->getActions($comment);
if(!empty($actions)){ if (!empty($actions)) {
if(empty($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS])){ if (empty($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS])) {
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS] = array(); $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS] = array();
} }
foreach($actions as $action){ foreach ($actions as $action) {
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_ACTIONS][] = $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_ARROW . '[^\]]+?\]/si', '', $comment);
$comment = preg_replace('/\[%'.CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . '[^\]]+?\]/si', '', $comment ); $comment = preg_replace('/\[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . '[^\]]+?\]/si', '', $comment);
$comment = trim($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){ private function getActions($comment)
$ret = array(); {
if(strstr($comment,'[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW )){ $ret = array();
$arrow = preg_replace('/.*?\[%'. CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment); if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW)) {
$arrows = explode(",", $arrow); $arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_ARROW . ' ([^\]]+?)\].*/si', '$1', $comment);
$arrows = explode(",", $arrow);
foreach($arrows as $arrow){ foreach ($arrows as $arrow) {
$tokens = explode(";", $arrow); $tokens = explode(";", $arrow);
if(strlen($tokens[0]) == 4){ if (strlen($tokens[0]) == 4) {
$action = array( $action = array(
"from" => substr($arrow,0,2), "from" => substr($arrow, 0, 2),
"to" => substr($arrow, 2,2) "to" => substr($arrow, 2, 2)
); );
if(count($tokens) > 1){ if (count($tokens) > 1) {
$action["color"] = $tokens[1]; $action["color"] = $tokens[1];
} }
$ret[] = $this->toAction("arrow", $action); $ret[] = $this->toAction("arrow", $action);
} }
} }
} }
if(strstr($comment,'[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT )){ if (strstr($comment, '[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT)) {
$arrow = preg_replace('/.*?\[%'. CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . ' ([^\]]+?)\].*/si', '$1', $comment); $arrow = preg_replace('/.*?\[%' . CHESS_JSON::PGN_KEY_ACTION_HIGHLIGHT . ' ([^\]]+?)\].*/si', '$1', $comment);
$arrows = explode(",", $arrow); $arrows = explode(",", $arrow);
foreach($arrows as $arrow){ foreach ($arrows as $arrow) {
$tokens = explode(";", $arrow); $tokens = explode(";", $arrow);
if(strlen($tokens[0]) == 2){ if (strlen($tokens[0]) == 2) {
$action = array( $action = array(
"square" => substr($arrow,0,2) "square" => substr($arrow, 0, 2)
); );
if(count($tokens) > 1){ if (count($tokens) > 1) {
$action["color"] = $tokens[1]; $action["color"] = $tokens[1];
} }
$ret[] = $this->toAction("highlight", $action); $ret[] = $this->toAction("highlight", $action);
} }
} }
} }
return $ret; return $ret;
} }
/** /**
* @param string $key * @param string $key
* @param array $val * @param array $val
* *
* @return array * @return array
*/ */
private function toAction($key, $val){ private function toAction($key, $val)
$val["type"] = $key; {
return $val; $val["type"] = $key;
return $val;
} }
public function startVariation(){ public function startVariation()
$index = count($this->moveReferences[$this->pointer])-1; {
if(!isset($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS])){ $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(); $this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS] = array();
} }
$countVariations = count($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS]); $countVariations = count($this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS]);
@@ -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;
} }
} }