예제 #1
0
 public function drop($playerId)
 {
     $t = new Transaction();
     // Award any outstanding match to the opposing player.
     $sql = 'SELECT match_id ' . 'FROM player_match ' . 'WHERE wins IS NULL AND player_id = ' . Q($playerId);
     $matchId = D()->value($sql, null);
     if ($matchId !== null) {
         $sql = 'UPDATE player_match ' . 'SET wins = 0 ' . 'WHERE match_id = ' . Q($matchId) . ' AND player_id = ' . Q($playerId);
         $t->execute($sql);
         $sql = 'UPDATE player_match ' . 'SET wins = 1 ' . 'WHERE match_id = ' . Q($matchId) . ' AND player_id <> ' . Q($playerId);
         $t->execute($sql);
     }
     // Drop player from any unstarted events.
     $sql = 'DELETE pe ' . 'FROM player_event AS pe ' . 'INNER JOIN event AS e ON pe.event_id = e.id ' . 'WHERE NOT started AND player_id = ' . Q($playerId);
     $t->execute($sql);
     // Drop player from any started events.
     $sql = 'UPDATE player_event ' . 'SET dropped = TRUE ' . 'WHERE player_id = ' . Q($playerId) . ' AND event_id IN (SELECT event_id FROM event WHERE NOT finished)';
     $t->execute($sql);
     return $t->commit();
 }
예제 #2
0
 public function report($wins, $opponentWins)
 {
     if (!$this->awaitingResult()) {
         $msg = 'Tried to report when not awaiting result.';
         throw new IllegalStateException($msg);
     }
     $t = new Transaction();
     $entries = [$this->playerId => $wins, $this->opponentId => $opponentWins];
     foreach ($entries as $playerId => $playerWins) {
         $sql = 'UPDATE player_match SET wins = ' . Q($playerWins) . ' WHERE match_id = ' . Q($this->matchId) . ' AND player_id = ' . Q($playerId);
         $t->execute($sql);
     }
     $t->commit();
 }
   /**
    * Test if we get an error back, if we violate a unique constraint
    */
   public function testCreateAndExecuteTransactionWithTransactionErrorUniqueConstraintOnSave()
   {
       if (isCluster($this->connection)) {
           // don't execute this test in a cluster
           return;
       }
       $writeCollections = array($this->collection1->getName());
       $readCollections = array($this->collection2->getName());
       $action = '
 function () {
   var db = require("internal").db;
   db.' . $this->collection1->getName() . '.save({ _key : "hello" });
   db.' . $this->collection1->getName() . '.save({ _key : "hello" });
 }';
       $transaction = new Transaction($this->connection);
       $transaction->setWriteCollections($writeCollections);
       $transaction->setReadCollections($readCollections);
       $transaction->setAction($action);
       $e = null;
       try {
           $transaction->execute();
       } catch (ServerException $e) {
       }
       $details = $e->getDetails();
       $expectedCutDownMessage = "unique constraint violated";
       $this->assertTrue($e->getCode() == 400 && strstr($details['errorMessage'], $expectedCutDownMessage) !== false, 'Did not return code 400 with first part of the message: "' . $expectedCutDownMessage . '", instead returned: ' . $e->getCode() . ' and "' . $details['errorMessage'] . '"');
   }