/** * @param string $pgnPath * @param array $excludedFens * @return PgnGame * @throws NotFoundResourceException */ public function getRandomPgnGame(string $pgnPath, array $excludedFens = []) : PgnGame { $this->parser = new PgnParser($pgnPath); $availableGames = []; foreach ($this->parser->getGames() as $index => $pgnGame) { if (!in_array($pgnGame->getFen(), $excludedFens)) { $availableGames[] = $pgnGame; } } if (empty($availableGames)) { throw new NotFoundResourceException(); } return $availableGames[mt_rand(0, count($availableGames) - 1)]; }
/** * @param string $fileName * @return int */ public function import(string $fileName) : int { if (!file_exists($fileName)) { throw new FileNotFoundException(); } $parser = new PgnParser($fileName); $count = 0; foreach ($parser->getGames() as $pgnGame) { if ($pgnGame->getBlack() != '#2' || empty($pgnGame->getMoves())) { continue; } $count++; $problem = (new Problem())->setFen($pgnGame->getFen())->setPgn($pgnGame->getMoves()); $this->manager->persist($problem); } $this->manager->flush(); return $count; }