Fixed bug parsing comments
This commit is contained in:
@@ -1,23 +1,27 @@
|
||||
<?php
|
||||
|
||||
class MoveBuilder {
|
||||
class MoveBuilder
|
||||
{
|
||||
private $moves = array();
|
||||
private $moveReferences = array();
|
||||
private $pointer = 0;
|
||||
private $currentIndex = 0;
|
||||
|
||||
public function __construct(){
|
||||
public function __construct()
|
||||
{
|
||||
$this->moveReferences[0] =& $this->moves;
|
||||
}
|
||||
|
||||
public function addMoves($moveString){
|
||||
public function addMoves($moveString)
|
||||
{
|
||||
$moves = explode(" ", $moveString);
|
||||
foreach ($moves as $move) {
|
||||
$this->addMove($move);
|
||||
}
|
||||
}
|
||||
|
||||
private function addMove($move){
|
||||
private function addMove($move)
|
||||
{
|
||||
if (!$this->isChessMove($move)) {
|
||||
return;
|
||||
}
|
||||
@@ -26,12 +30,14 @@ class MoveBuilder {
|
||||
$this->currentIndex++;
|
||||
}
|
||||
|
||||
private function isChessMove($move){
|
||||
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)) {
|
||||
return;
|
||||
@@ -40,13 +46,16 @@ class MoveBuilder {
|
||||
$this->addComment($comment);
|
||||
}
|
||||
|
||||
public function addComment($comment){
|
||||
public function addComment($comment)
|
||||
{
|
||||
$comment = trim($comment);
|
||||
if (!strlen($comment)) {
|
||||
return;
|
||||
}
|
||||
#$index = max(0,count($this->moveReferences[$this->pointer])-1);
|
||||
$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);
|
||||
@@ -69,11 +78,19 @@ class MoveBuilder {
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
|
||||
private function getActions($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);
|
||||
@@ -119,12 +136,14 @@ class MoveBuilder {
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function toAction($key, $val){
|
||||
private function toAction($key, $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])) {
|
||||
$this->moveReferences[$this->pointer][$index][CHESS_JSON::MOVE_VARIATIONS] = array();
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user