public function onOpen(WebSocketConnection $connection)
 {
     if (count($this->connections) > 100) {
         $connection->close();
         return;
     }
     $this->connections->pushBack($connection);
 }
示例#2
0
 public function __construct($seats = 5)
 {
     if ($seats <= 0) {
         throw new \LogicException('Table must have at least 1 seat');
     }
     $this->seats = new Vector();
     for ($seat = 0; $seat < $seats; $seat++) {
         $this->seats->pushBack(new TableSeat($seat, $this));
     }
     $this->id = md5(uniqid('', true));
 }
 public function accept(Socket $socket)
 {
     if (count($this->connections) >= $this->maxConnections) {
         $socket->write(['alias' => 'capacity_reached']);
         $socket->close();
         throw new \RuntimeException('Server capacity reached');
     }
     $player = $this->playerFactory->newSocketPlayer(sprintf('PlayerBot%d', ++$this->connectionIndex), null, 5000, $socket);
     $connection = $player->getConnection();
     $this->connections->pushBack($connection);
     $this->handleSocketEvents($connection);
     $this->handlePlayerNameChange($connection);
     $this->handlePlayerConnection($connection);
 }
 /**
  * Create a table with a specific number of seats.
  * All seats are assigned a computer player until a real player joins.
  *
  * @param int $seats
  *
  * @return Table
  */
 public function createTable($seats = 5)
 {
     $table = new WebSocketObservableTable($seats);
     $index = 0;
     foreach ($table->getSeats() as $seat) {
         $player = $this->playerFactory->newServerPlayer('Bot' . $index++, $seat, 5000);
         $seat->setPlayer($player);
     }
     $this->logger->info('Created new table', ['id' => $table->getId()]);
     $loop = $this->tableLoopFactory->createLoop($table);
     $this->tableLoopMap->attach($table, $loop);
     $this->tables->pushBack($table);
     return $table;
 }
示例#5
0
 public function restockDeck()
 {
     $this->cards = new Vector();
     $suit = [Card::SUIT_CLUBS, Card::SUIT_SPADES, Card::SUIT_HEARTS, Card::SUIT_DIAMONDS];
     foreach ($suit as $cardSuit) {
         for ($rank = Card::RANK_ACE; $rank <= Card::RANK_KING; $rank++) {
             if ($rank === 11) {
                 continue;
             }
             $this->cards->pushBack(new Card($rank, $cardSuit));
         }
     }
     $cards = clone $this->cards;
     for ($deck = 0; $deck < $this->decks; $deck++) {
         $this->cards->append($cards);
     }
 }
示例#6
0
 /**
  * @param Card $card
  */
 public function addCard(Card $card)
 {
     $this->cards->pushBack($card);
 }
 public function addConnection(WebSocketConnection $connection)
 {
     $this->connections->pushBack($connection);
     $connection->send($this->serializeMessage(new SendStateMessage($this)));
 }