Esempio n. 1
0
 /**
  * @depends testCreateAndGetGameCoinById
  */
 public function testResolveByPerson()
 {
     Yii::app()->user->userModel = User::getByUsername('steven');
     $gameCoin = GameCoin::resolveByPerson(Yii::app()->user->userModel);
     $this->assertEquals(20, $gameCoin->value);
     $this->assertEquals(Yii::app()->user->userModel, $gameCoin->person);
     $this->assertTrue($gameCoin->id > 0);
     Yii::app()->user->userModel = User::getByUsername('super');
     $gameCoin = GameCoin::resolveByPerson(Yii::app()->user->userModel);
     $this->assertEquals(0, $gameCoin->value);
     $this->assertEquals(Yii::app()->user->userModel, $gameCoin->person);
     $this->assertTrue($gameCoin->id < 0);
 }
 public function testRemoveDuplicatesByModelsNonGameCollection()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $models = array();
     $gameCoin = new GameCoin();
     $gameCoin->person = $super;
     $gameCoin->value = 34;
     $gameCoin->save();
     $models[] = $gameCoin;
     $gameCoin2 = new GameCoin();
     $gameCoin2->person = $super;
     $gameCoin2->value = 56;
     $gameCoin2->save();
     $models[] = $gameCoin2;
     $messageContent = null;
     $this->assertEquals(2, count(GameCoin::getAll()));
     GamificationUtil::removeDuplicatesByModels($models, $messageContent);
     $gameCoins = GameCoin::getAll();
     $this->assertEquals(1, count($gameCoins));
     //Ensure it deleted the smaller value (Bank error in your favor)
     $this->assertEquals(56, $gameCoins[0]->value);
     $this->assertNotNull($messageContent);
 }
 public function testRedeemReward()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $super->primaryEmail->emailAddress = '*****@*****.**';
     $super->save();
     Yii::app()->user->userModel = $super;
     $gameRewards = GameReward::getByName('myNewGameReward');
     //not enough coins
     $this->setGetArray(array('id' => $gameRewards[0]->id));
     $content = $this->runControllerWithExitExceptionAndGetContent('gameRewards/default/redeemReward');
     $this->assertContains('You do not have enough coins to redeem this reward', $content);
     //enough coins
     $gameCoin = new GameCoin();
     $gameCoin->person = $super;
     $gameCoin->value = 100;
     $this->assertTrue($gameCoin->save());
     $notifications = Notification::getAll();
     //check for no notification
     $this->assertEquals(0, EmailMessage::getCount());
     $this->assertEquals(0, count($notifications));
     $this->setGetArray(array('id' => $gameRewards[0]->id));
     $content = $this->runControllerWithExitExceptionAndGetContent('gameRewards/default/redeemReward');
     $this->assertContains('myNewGameReward has been redeemed.', $content);
     //check for notification
     $notifications = Notification::getAll();
     $this->assertEquals(1, count($notifications));
     //email content
     $this->assertContains('myNewGameReward was redeemed by Clark Kent.', $notifications[0]->notificationMessage->htmlContent);
     //check url
     $this->assertContains('/gameRewards/default/details?id=13', $notifications[0]->notificationMessage->htmlContent);
     // Not Coding Standard
     //check for email notification
     $emailMessages = EmailMessage::getAll();
     $this->assertCount(1, $emailMessages);
     $this->assertContains('myNewGameReward was redeemed by Clark Kent.', $emailMessages[0]->content->htmlContent);
     $this->assertContains('myNewGameReward was redeemed by Clark Kent.', $emailMessages[0]->content->textContent);
 }
 protected function getGameCoinForCurrentUser()
 {
     return GameCoin::resolveByPerson(Yii::app()->user->userModel);
 }
 public function actionRefreshGameDashboardCoinContainer($id)
 {
     $user = User::getById((int) $id);
     $gameCoin = GameCoin::resolveByPerson($user);
     echo UserGameDashboardView::renderCoinsContent($gameCoin->value, $user);
 }
 public function testRefreshGameDashboardCoinContainer()
 {
     $super = $this->logoutCurrentUserLoginNewUserAndGetByUsername('super');
     $gameCoin = GameCoin::resolveByPerson($super);
     $gameCoin->addValue(1);
     $saved = $gameCoin->save();
     $this->assertEquals(1, $gameCoin->value);
     $gameCoin = GameCoin::resolveByPerson($super);
     $this->assertEquals(1, $gameCoin->value);
     $this->setGetArray(array('id' => $super->id));
     $content = $this->runControllerWithNoExceptionsAndGetContent('gamification/default/refreshGameDashboardCoinContainer');
     $this->assertContains('1 coin', $content);
 }
 protected function resolveFilteredByMetadataBeforeMakingDataProvider($searchForm, &$metadata)
 {
     if ($searchForm->filteredBy == GameRewardsSearchForm::FILTERED_BY_CAN_REDEEM) {
         $gameCoin = GameCoin::resolveByPerson(Yii::app()->user->userModel);
         $totalCoinsForUser = (int) $gameCoin->value;
         $clauseNumber = count($metadata['clauses']) + 1;
         $metadata['clauses'][$clauseNumber] = array('attributeName' => 'cost', 'operatorType' => 'lessThanOrEqualTo', 'value' => $totalCoinsForUser);
         if ($metadata['structure'] == '') {
             $metadata['structure'] = $clauseNumber;
         } else {
             $metadata['structure'] .= ' AND ' . $clauseNumber;
         }
     }
 }
 protected function getGameCoinForUser()
 {
     return GameCoin::resolveByPerson($this->user);
 }
Esempio n. 9
0
 /**
  * @param int $nextLevelValue
  * @throws FailedToSaveModelException
  */
 protected static function processLevelChangeGameNotification($nextLevelValue)
 {
     assert('is_int($nextLevelValue)');
     $coinsValue = GameCoinRules::getCoinsByLevel((int) $nextLevelValue);
     $gameNotification = new GameNotification();
     $gameNotification->user = Yii::app()->user->userModel;
     $gameNotification->setLevelChangeByNextLevelValue($nextLevelValue, $coinsValue);
     $saved = $gameNotification->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
     $gameCoin = GameCoin::resolveByPerson(Yii::app()->user->userModel);
     $gameCoin->addValue($coinsValue);
     $saved = $gameCoin->save();
     if (!$saved) {
         throw new FailedToSaveModelException();
     }
 }
 /**
  * @see GamificationUtil::logAndNotifyOnDuplicateGameModel($logContent for an explanation of why you would
  * use this method.
  */
 public function actionRepairGamification()
 {
     $duplicateModelsData = array();
     //Check GameCoin for duplication person models
     $gameCoinDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByPersonKey(GameCoin::getTableName());
     $duplicateModelsData['GameCoin'] = $gameCoinDuplicateData;
     //Check GameCollection, GameLevel, GamePoint, and GameScore for duplicate type/person models
     $gameCollectionDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameCollection::getTableName());
     $duplicateModelsData['GameCollection'] = $gameCollectionDuplicateData;
     $gameLevelDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameLevel::getTableName());
     $duplicateModelsData['GameLevel'] = $gameLevelDuplicateData;
     $gamePointDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GamePoint::getTableName());
     $duplicateModelsData['GamePoint'] = $gamePointDuplicateData;
     $gameScoreDuplicateData = GamificationUtil::findGameTableRowsThatAreDuplicatedByTypePersonKey(GameScore::getTableName());
     $duplicateModelsData['GameScore'] = $gameScoreDuplicateData;
     foreach ($duplicateModelsData as $modelClassName => $duplicatesData) {
         if (empty($duplicatesData)) {
             echo 'No duplicates found for ' . $modelClassName . "<BR>";
         } else {
             echo 'Duplicates discovered for ' . $modelClassName . "<BR>";
             foreach ($duplicatesData as $typePersonKeyDuplicateData) {
                 $searchAttributeData = array();
                 if ($modelClassName == 'GameCoin') {
                     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id']));
                     $searchAttributeData['structure'] = '1';
                 } else {
                     $searchAttributeData['clauses'] = array(1 => array('attributeName' => 'type', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['type']), 2 => array('attributeName' => 'person', 'relatedAttributeName' => 'id', 'operatorType' => 'equals', 'value' => $typePersonKeyDuplicateData['person_item_id']));
                     $searchAttributeData['structure'] = '1 and 2';
                 }
                 $joinTablesAdapter = new RedBeanModelJoinTablesQueryAdapter($modelClassName);
                 $where = RedBeanModelDataProvider::makeWhere($modelClassName, $searchAttributeData, $joinTablesAdapter);
                 $models = $modelClassName::getSubset($joinTablesAdapter, null, null, $where, null);
                 if ($modelClassName == 'GameCoin') {
                     echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . "<BR>";
                 } else {
                     echo $modelClassName . ' --- Quantity of duplicates: ' . count($models) . ' --- for person_item_id: ' . $typePersonKeyDuplicateData['person_item_id'] . ' with type: ' . $typePersonKeyDuplicateData['type'] . "<BR>";
                 }
                 $messageContent = null;
                 GamificationUtil::removeDuplicatesByModels($models, $messageContent);
                 echo $messageContent;
             }
         }
     }
     echo "<BR>" . 'Repair complete.' . "<BR>";
 }
 public function getAvailableCoinsForCurrentUser()
 {
     $gameCoin = GameCoin::resolveByPerson(Yii::app()->user->userModel);
     return (int) $gameCoin->value;
 }