/**
  * @param User $user
  * @param $levelType
  * @return array
  */
 public static function getStatisticsDataForAGivenLevelType(User $user, $levelType)
 {
     assert('is_string($levelType) && $levelType != null');
     $rulesClassName = $levelType . 'GameLevelRules';
     $currentGameLevel = GameLevel::resolveByTypeAndPerson($levelType, $user);
     $currentPointsData = GamePoint::getSummationPointsDataByLevelTypeAndUser($user, $levelType);
     if ($currentPointsData != null) {
         $currentPoints = $currentPointsData['sum'];
     } else {
         $currentPoints = 0;
     }
     //If the user has not reached level one, the model has not been saved yet
     if ($currentGameLevel->id < 0) {
         $nextLevel = 1;
         $trueCurrentGameLevel = 0;
         $className = $levelType . 'GameLevelRules';
         $nextLevelPointValue = $className::getMinimumPointsForLevel(1);
         $currentLevelMinimumPointValue = 0;
     } else {
         $nextLevelPointValue = GameLevelUtil::getNextLevelPointValueByTypeAndCurrentLevel($levelType, $currentGameLevel);
         $nextLevel = GameLevelUtil::getNextLevelByTypeAndCurrentLevel($levelType, $currentGameLevel);
         $currentLevelMinimumPointValue = $rulesClassName::getMinimumPointsForLevel(intval($currentGameLevel->value));
         $trueCurrentGameLevel = $currentGameLevel->value;
     }
     if ($nextLevel !== false) {
         $pointsCollectedTowardsNextLevel = $currentPoints - $currentLevelMinimumPointValue;
         if ($pointsCollectedTowardsNextLevel == 0) {
             $nextLevelPercentageComplete = 0;
         } else {
             $nextLevelPercentageComplete = $pointsCollectedTowardsNextLevel / ($nextLevelPointValue - $currentLevelMinimumPointValue) * 100;
         }
     } else {
         $nextLevelPercentageComplete = null;
     }
     $rankingData = array('level' => (int) $trueCurrentGameLevel, 'points' => (int) $currentPoints, 'nextLevelPercentageComplete' => round($nextLevelPercentageComplete), 'levelTypeLabel' => $rulesClassName::getDisplayLabel());
     return $rankingData;
 }
示例#2
0
 protected function resolveLevelChangeByType($levelType, GameLevel $currentGameLevel, $pointSumsIndexedByType)
 {
     assert('is_string($levelType) && $levelType != null');
     assert('is_array($pointSumsIndexedByType)');
     //If the user has not reached level one, the model has not been saved yet
     if ($currentGameLevel->id < 0) {
         $className = $levelType . 'GameLevelRules';
         $nextLevelPointValue = $className::getMinimumPointsForLevel(1);
         $nextLevelValue = 1;
     } else {
         $nextLevelPointValue = GameLevelUtil::getNextLevelPointValueByTypeAndCurrentLevel($levelType, $currentGameLevel);
         $nextLevelValue = GameLevelUtil::getNextLevelByTypeAndCurrentLevel($levelType, $currentGameLevel);
     }
     if ($nextLevelValue !== false && static::resolveSummationValueByLevelTypeAndPointSums($levelType, $pointSumsIndexedByType) > $nextLevelPointValue) {
         $currentGameLevel->value = $nextLevelValue;
         $saved = $currentGameLevel->save();
         if (!$saved) {
             throw new FailedToSaveModelException();
         }
         GameLevel::processBonusPointsOnLevelChange($currentGameLevel, Yii::app()->user->userModel);
         if ($levelType == GameLevel::TYPE_GENERAL && $this->modalNotificationsEnabled) {
             static::processLevelChangeGameNotification($nextLevelValue);
         }
     }
 }
示例#3
0
 public function testGetNextLevelPointValueByTypeAndCurrentLevel()
 {
     $super = User::getByUsername('super');
     Yii::app()->user->userModel = $super;
     $gameLevel = new GameLevel();
     $gameLevel->value = 1;
     $this->assertEquals(500, GameLevelUtil::getNextLevelPointValueByTypeAndCurrentLevel(GameLevel::TYPE_GENERAL, $gameLevel));
     $gameLevel->value = 2;
     $this->assertEquals(1000, GameLevelUtil::getNextLevelPointValueByTypeAndCurrentLevel(GameLevel::TYPE_GENERAL, $gameLevel));
 }