Example #1
0
 /**
  * Create and position pieces of the game for the forsyth string
  *
  * @param Game $game
  * @param string $forsyth
  * @return Game $game
  */
 public static function import(Game $game, $forsyth)
 {
     static $classes = array('p' => 'Pawn', 'r' => 'Rook', 'n' => 'Knight', 'b' => 'Bishop', 'q' => 'Queen', 'k' => 'King');
     $x = 1;
     $y = 8;
     $board = $game->getBoard();
     $forsyth = str_replace('/', '', preg_replace('#\\s*([\\w\\d/]+)\\s.+#i', '$1', $forsyth));
     $pieces = array('white' => array(), 'black' => array());
     for ($itForsyth = 0, $forsythLen = strlen($forsyth); $itForsyth < $forsythLen; $itForsyth++) {
         $letter = $forsyth[$itForsyth];
         if (is_numeric($letter)) {
             $x += intval($letter);
         } else {
             $color = ctype_lower($letter) ? 'black' : 'white';
             $pieces[$color][] = new Piece($x, $y, $classes[strtolower($letter)]);
             ++$x;
         }
         if ($x > 8) {
             $x = 1;
             --$y;
         }
     }
     foreach ($game->getPlayers() as $player) {
         $player->setPieces($pieces[$player->getColor()]);
     }
     $game->ensureDependencies();
 }
Example #2
0
 /**
  * @depends testGameCreationStandard
  */
 public function testGamePlayers(Document\Game $game)
 {
     $this->assertEquals(2, count($game->getPlayers()));
     $player = $game->getPlayer('white');
     $this->assertTrue($player instanceof Document\Player);
     $this->assertEquals('white', $player->getColor());
     $this->assertEquals(false, $player->getIsWinner());
     $this->assertEquals(false, $player->getIsAi());
     $this->assertSame($game, $player->getGame());
     $this->assertSame($game->getPlayer('black'), $player->getOpponent());
     $this->assertSame($player, $player->getOpponent()->getOpponent());
 }
Example #3
0
 protected function addMessage(Game $game, $author, $message)
 {
     $author = (string) $author;
     $message = (string) $message;
     if ('' === $message) {
         throw new \InvalidArgumentException('Messenger: Can not add empty message');
     }
     if (mb_strlen($message) > 140) {
         throw new \InvalidArgumentException('Messenger: message is too long');
     }
     if ($game->addRoomMessage($author, $message)) {
         $sayEvent = array('type' => 'message', 'message' => array($author, $message));
         foreach ($game->getPlayers() as $player) {
             $player->addEventToStack($sayEvent);
         }
     }
 }
Example #4
0
 private function encodeLobbyEntry(Game $game)
 {
     $entry = array();
     foreach ($game->getPlayers() as $player) {
         $entry[] = $player->hasUser() ? $player->getUsername() : null;
         $entry[] = $player->getUsernameWithElo();
     }
     return implode("\$", $entry);
 }