forked from DHTMLGoodies/chessParser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GameParser.php
75 lines (62 loc) · 1.67 KB
/
GameParser.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
<?php
class GameParser {
private $game;
private $fen;
private $fenParser0x88;
private $shortVersion;
public function __construct() {
$this->fenParser0x88 = new FenParser0x88();
}
/**
* @param array $game
* @param bool $short for only from and to squares
* @return mixed
*/
public function getParsedGame( $game, $short = false ) {
$this->game = $game;
$this->shortVersion = $short;
$this->fen = $this->getStartFen();
$this->fenParser0x88->newGame( $this->fen );
$this->parseMoves( $this->game[ChessJson::MOVE_MOVES] );
$this->addParsedProperty();
return $this->game;
}
private function addParsedProperty() {
$this->game[ChessJson::GAME_METADATA][ChessJson::MOVE_PARSED] = 1;
}
private function parseMoves( &$moves ) {
foreach ( $moves as &$move ) {
$this->parseAMove( $move );
}
}
private function parseAMove( &$move ) {
if (
!isset( $move[ChessJson::MOVE_NOTATION] )
|| (
isset( $move[ChessJson::FEN] )
&& isset( $move[ChessJson::MOVE_FROM] )
&& isset( $move[ChessJson::MOVE_TO] )
)
) {
return;
}
if ( strlen( $move[ChessJson::MOVE_NOTATION] ) < 2 ) { return;
}
if ( isset( $move[ChessJson::MOVE_VARIATIONS] ) ) {
$fen = $this->fenParser0x88->getFen();
$this->parseVariations( $move[ChessJson::MOVE_VARIATIONS] );
$this->fenParser0x88->setFen( $fen );
}
$move = $this->fenParser0x88->getParsed( $move );
}
private function parseVariations( &$variations ) {
foreach ( $variations as &$variation ) {
$fen = $this->fenParser0x88->getFen();
$this->parseMoves( $variation );
$this->fenParser0x88->setFen( $fen );
}
}
private function getStartFen() {
return $this->game[ChessJson::FEN];
}
}