forked from DHTMLGoodies/chessParser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPgnGameParser.php
executable file
·178 lines (144 loc) · 5.82 KB
/
PgnGameParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
class PgnGameParser{
private $pgnGame;
private $defaultFen = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
private $gameData = array();
private $specialMetadata = array(
'event','site','white','black','result','plycount','eco','fen',
'timecontrol','round','date','annotator','termination'
);
public function __construct($pgnGame = null){
if(isset($pgnGame)){
$this->pgnGame = trim($pgnGame);
$this->moveBuilder = new MoveBuilder();
}
}
public function setPgn($pgnGame){
$this->pgnGame = trim($pgnGame);
$this->gameData = array();
$this->moveBuilder = new MoveBuilder();
}
public function getParsedData(){
$this->gameData = $this->getMetadata();
$this->gameData[CHESS_JSON::MOVE_MOVES] = $this->getMoves();
return $this->gameData;
}
public function getMetadata(){
$ret = array(
CHESS_JSON::GAME_METADATA=>array()
);
// TODO set lastmoves property by reading last 3-4 moves in moves array
$lines = explode("\n", $this->pgnGame);
foreach($lines as $line){
$line = trim($line);
if(substr($line, 0, 1) === '[' && substr($line, strlen($line)-1, 1) === ']'){
$metadata = $this->getMetadataKeyAndValue($line);
if(in_array($metadata['key'], $this->specialMetadata)){
$ret[$metadata['key']] = $metadata['value'];
}else{
$ret[CHESS_JSON::GAME_METADATA][$metadata['key']] = $metadata['value'];
}
}
}
if(!isset($ret[CHESS_JSON::FEN])) {
$ret[CHESS_JSON::FEN] = $this->defaultFen;
}
return $ret;
}
public function getMetadataKeyAndValue($metadataString){
$metadataString = preg_replace("/[\[\]]/s", "", $metadataString);
$metadataString = str_replace('"', '', $metadataString);
$tokens = explode(" ", $metadataString);
$key = $tokens[0];
$value = implode(" ", array_slice($tokens, 1));
$ret = array('key' => $this->getValidKey($key), 'value' => $value );
return $ret;
}
public function getValidKey($key){
$key = strtolower($key);
return $key;
}
public function getMoves(){
$parts = $this->getMovesAndComments();
for($i=0, $count = count($parts); $i<$count; $i++){
$move = trim($parts[$i]);
switch($move){
case '{':
if($i==0){
$this->moveBuilder->addCommentBeforeFirstMove($parts[$i+1]);
}else{
$this->moveBuilder->addComment($parts[$i+1]);
}
$i+=2;
break;
default:
$moves = $this->getMovesAndVariationFromString($move);
foreach($moves as $move){
switch($move){
case '(':
$this->moveBuilder->startVariation();
break;
case ')':
$this->moveBuilder->endVariation();
break;
default:
$this->moveBuilder->addMoves($move);
}
}
}
}
return $this->moveBuilder->getMoves();
}
public function addGameComment($comment){
$this->gameData[CHESS_JSON::GAME_METADATA][CHESS_JSON::MOVE_COMMENT] = $comment;
}
public function getMovesAndComments(){
$ret = preg_split("/({|})/s", $this->getMoveString(), 0, PREG_SPLIT_DELIM_CAPTURE);
if(!$ret[0]){
$ret = array_slice($ret, 1);
}
return $ret;
}
public function getMovesAndVariationFromString($string){
$string = " ". $string;
$string = preg_replace("/[0-9]+?\./s", "", $string);
$string = str_replace(" ..", "", $string);
$string = str_replace(" ", " ", $string);
$string = trim($string);
return preg_split("/(\(|\))/s", $string, 0, PREG_SPLIT_DELIM_CAPTURE);
}
public function getMoveString() {
$tokens = preg_split("/\]\n\n/s", $this->pgnGame);
// try again, this time making an allowance for extra whitespace (chess.com)
if (count($tokens) < 2) {
$tokens = preg_split("/\]\n(\s+)\n/s", $this->pgnGame);
}
if (count($tokens) < 2) {
return '';
}
$gameData = $tokens[1];
$gameData = str_replace("\n", " ", $gameData);
$gameData = preg_replace("/(\s+)/", " ", $gameData);
$gameData = $this->fixPgnInlineComments($gameData);
return trim($gameData);
}
/**
* https://stackoverflow.com/questions/22487483/regular-expression-to-add-curly-brackets-around-the-comments-in-a-pgn-chess
*/
public function fixPgnInlineComments($str) {
$re = '/((?:\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}(?:\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2})?\s?[()]?\s?[()]?\s?)+)|((?:(?!\s?[\(\)]?\s?[\(\)]?\s?[0-9]{1,3}\.{1,3}\s[NBRQK]?[a-h1-8]?x?[a-hO][1-8-][O-]{0,3}[!?+#=]{0,2}[NBRQ]?[!?+#]{0,2}).)+)/';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
$reconstructedPgn = '';
if (count($matches) === 1) {
return $matches[0][0];
}
foreach ($matches as $match) {
if (count($match) === 3) {
$reconstructedPgn .= '{' . $match[0] . '}';
} else {
$reconstructedPgn .= $match[0];
}
}
return $reconstructedPgn;
}
}