Support for arrow and highlight actions in pgn

This commit is contained in:
DHTMLGoodies
2017-03-29 18:48:07 +02:00
parent 126dc9b599
commit 40a19a7551
5 changed files with 634 additions and 3 deletions

View File

@@ -52,12 +52,78 @@ class MoveBuilder {
$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;
}
}
$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;
$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);
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;
}
/**
* @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])){