Beispiel #1
0
 protected function getGameUrl(Game $game)
 {
     if (null === $this->urlGenerator) {
         return 'http://lichess.org/';
     }
     return $this->urlGenerator->generate('lichess_pgn_viewer', array('id' => $game->getId()), true);
 }
Beispiel #2
0
 public function expandGame(Game $game)
 {
     return sprintf('game(%s,%s,%s,%s,%d,%s)', $game->getVariantName(), $game->getClockName(), $game->getIsRated() ? 'rated' : 'casual', $game->getIsPlayable() ? 'playable' : $game->getStatusMessage(), $game->getTurns(), $this->generator->generate('lichess_game', array('id' => $game->getId()), true));
 }
Beispiel #3
0
 /**
  * @return string
  */
 public function getFullId()
 {
     return $this->game->getId() . $this->getId();
 }
Beispiel #4
0
 public function findSimilar(Game $game, \DateTime $since)
 {
     return $this->createQueryBuilder()->field('id')->notEqual($game->getId())->field('updatedAt')->gt(new \MongoDate($since->getTimestamp()))->field('status')->equals(Game::STARTED)->field('turns')->equals($game->getTurns())->field('pgnMoves')->equals($game->getPgnMoves())->hint(array('updatedAt' => -1))->getQuery()->execute();
 }
Beispiel #5
0
 public static function diffToMove(Game $game, $forsyth)
 {
     $moves = array('from' => array(), 'to' => array());
     $x = 1;
     $y = 8;
     $board = $game->getBoard();
     $forsyth = str_replace('/', '', preg_replace('#\\s*([\\w\\d/]+)\\s.+#i', '$1', $forsyth));
     for ($itForsyth = 0, $forsythLen = strlen($forsyth); $itForsyth < $forsythLen; $itForsyth++) {
         $letter = $forsyth[$itForsyth];
         $key = Board::posToKey($x, $y);
         if (is_numeric($letter)) {
             for ($x = $x, $max = $x + intval($letter); $x < $max; $x++) {
                 $_key = Board::posToKey($x, $y);
                 if (!$board->getSquareByKey($_key)->isEmpty()) {
                     $moves['from'][] = $_key;
                 }
             }
         } else {
             $color = ctype_lower($letter) ? 'black' : 'white';
             switch (strtolower($letter)) {
                 case 'p':
                     $class = 'Pawn';
                     break;
                 case 'r':
                     $class = 'Rook';
                     break;
                 case 'n':
                     $class = 'Knight';
                     break;
                 case 'b':
                     $class = 'Bishop';
                     break;
                 case 'q':
                     $class = 'Queen';
                     break;
                 case 'k':
                     $class = 'King';
                     break;
             }
             if ($piece = $board->getPieceByKey($key)) {
                 if ($class != $piece->getClass() || $color !== $piece->getColor()) {
                     $moves['to'][] = $key;
                 }
             } else {
                 $moves['to'][] = $key;
             }
             ++$x;
         }
         if ($x > 8) {
             $x = 1;
             --$y;
         }
     }
     if (empty($moves['from'])) {
         return null;
     } elseif (1 === count($moves['from']) && 1 === count($moves['to'])) {
         $from = $moves['from'][0];
         $to = $moves['to'][0];
     } elseif (2 === count($moves['from']) && 2 === count($moves['to'])) {
         if ($board->getPieceByKey($moves['from'][0])->isClass('King')) {
             $from = $moves['from'][0];
         } else {
             $from = $moves['from'][1];
         }
         if (in_array($board->getSquareByKey($moves['to'][0])->getX(), array(3, 7))) {
             $to = $moves['to'][0];
         } else {
             $to = $moves['to'][1];
         }
     } elseif (2 === count($moves['from']) && 1 === count($moves['to']) && $board->getPieceByKey($moves['from'][0])->isClass('Pawn') && $board->getPieceByKey($moves['from'][1])->isClass('Pawn') && !$board->hasPieceByKey($moves['to'][0])) {
         if ($moves['from'][0][0] === $moves['to'][0][0]) {
             $from = $moves['from'][1];
         } else {
             $from = $moves['from'][0];
         }
         $to = $moves['to'][0];
     } else {
         throw new \RuntimeException(sprintf('Forsyth:diffToMove game:%s, variant:%s, moves: %s, forsyth:%s', $game->getId(), $game->getVariantName(), str_replace("\n", " ", var_export($moves, true)), $forsyth));
     }
     return $from . ' ' . $to;
 }
Beispiel #6
0
 public function findByGame(Game $game)
 {
     return $this->createQueryBuilder()->field('game.$id')->equals($game->getId())->getQuery()->execute();
 }
Beispiel #7
0
 public function talk(Game $game, $message)
 {
     $this->post('talk/' . $game->getId(), $message);
 }
Beispiel #8
0
 public function updateElo(User $user, $elo, Game $game)
 {
     $user->setElo($elo);
     $ts = date_create()->getTimestamp();
     $this->historyRepository->findOneByUserOrCreate($user)->addGame($ts, $elo, $game->getId());
 }
 public function renderGameFen(Game $game, User $user = null)
 {
     $player = $game->getPlayerByUserOrCreator($user);
     $authUser = $this->container->get('security.context')->getToken()->getUser();
     if ($authUser instanceof User && ($authPlayer = $game->getPlayerByUser($authUser))) {
         $gameUrl = $this->getUrlGenerator()->generate('lichess_player', array('id' => $authPlayer->getFullId()));
     } else {
         $gameUrl = $this->getUrlGenerator()->generate('lichess_game', array('id' => $game->getId(), 'color' => $player->getColor()));
     }
     return sprintf('<a href="%s" title="%s" class="mini_board parse_fen" data-color="%s" data-fen="%s"></a>', $gameUrl, $this->getTranslator()->trans('View in full size'), $player->getColor(), Forsyth::export($game));
 }