-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathGameParser.php
executable file
·78 lines (62 loc) · 1.89 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
76
77
78
<?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[CHESS_JSON::MOVE_MOVES]);
$this->addParsedProperty();
return $this->game;
}
private function addParsedProperty()
{
$this->game[CHESS_JSON::GAME_METADATA][CHESS_JSON::MOVE_PARSED] = 1;
}
private function parseMoves(&$moves)
{
foreach ($moves as &$move) {
$this->parseAMove($move);
}
}
private function parseAMove(&$move)
{
if (!isset($move[CHESS_JSON::MOVE_NOTATION]) || (isset($move[CHESS_JSON::FEN]) && isset($move[CHESS_JSON::MOVE_FROM]) && isset($move[CHESS_JSON::MOVE_TO]))) {
return;
}
if (strlen($move[CHESS_JSON::MOVE_NOTATION]) < 2) return;
if (isset($move[CHESS_JSON::MOVE_VARIATIONS])) {
$fen = $this->fenParser0x88->getFen();
$this->parseVariations($move[CHESS_JSON::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[CHESS_JSON::FEN];
}
}