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); } } }
/** * @depends testResolveByTypeAndPerson */ public function testProcessBonusPointsOnLevelChange() { Yii::app()->user->userModel = User::getByUsername('steven'); $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(0, $gamePoint->value); //Testing a level that does not give bonus points. $gameLevel = new GameLevel(); $gameLevel->person = Yii::app()->user->userModel; $gameLevel->type = GameLevel::TYPE_SALES; $gameLevel->value = 1; $this->assertTrue($gameLevel->save()); GameLevel::processBonusPointsOnLevelChange($gameLevel, Yii::app()->user->userModel); //Test that bonus points were actually received. $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(100, $gamePoint->value); //Now get the GameLevel again, and make sure it works for level 2 $gameLevel = GameLevel::resolveByTypeAndPerson(GameLevel::TYPE_SALES, Yii::app()->user->userModel); $gameLevel->person = Yii::app()->user->userModel; $gameLevel->type = GameLevel::TYPE_SALES; $gameLevel->value = 2; $this->assertTrue($gameLevel->save()); GameLevel::processBonusPointsOnLevelChange($gameLevel, Yii::app()->user->userModel); //Test that bonus points were actually received. $gamePoint = GamePoint::resolveToGetByTypeAndPerson(GamePoint::TYPE_USER_ADOPTION, Yii::app()->user->userModel); $this->assertEquals(GamePoint::TYPE_USER_ADOPTION, $gamePoint->type); $this->assertEquals(210, $gamePoint->value); }