public function testCreateAndGetGamePointById() { $user = UserTestHelper::createBasicUser('Steven'); $gamePoint = new GamePoint(); $gamePoint->person = $user; $gamePoint->type = 'SomeType'; $gamePoint->addValue(10); $this->assertTrue($gamePoint->save()); $id = $gamePoint->id; unset($gamePoint); $gamePoint = GamePoint::getById($id); $this->assertEquals('SomeType', $gamePoint->type); $this->assertEquals(10, $gamePoint->value); $this->assertEquals($user, $gamePoint->person); $this->assertEquals(1, $gamePoint->transactions->count()); $gamePoint->addValue(50, false); $this->assertTrue($gamePoint->save()); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, 50); $this->assertEquals(60, $gamePoint->value); $gamePoint::forgetAll(); $gamePoint = GamePoint::getById($id); $this->assertEquals(60, $gamePoint->value); $this->assertEquals(2, $gamePoint->transactions->count()); $this->assertEquals(10, $gamePoint->transactions[0]->value); $this->assertEquals(50, $gamePoint->transactions[1]->value); }
/** * Given a user and a gameBadge, process the bonus points, if applicable for the badge. This will also * process grade change points for the given badge. * @param GameBadge $gameBadge * @param User $user * @param $gradeChangeOrNewBadge * @throws NotSupportedException */ public static function processBonusPoints(GameBadge $gameBadge, User $user, $gradeChangeOrNewBadge) { assert('$gameBadge->id > 0'); assert('$gradeChangeOrNewBadge == "GradeChange" || $gradeChangeOrNewBadge == "NewBadge"'); $gameBadgeRulesClassName = $gameBadge->type . 'GameBadgeRules'; $gamePoint = null; if ($gradeChangeOrNewBadge == 'NewBadge' && $gameBadgeRulesClassName::hasBonusPointsOnCreation()) { $type = $gameBadgeRulesClassName::getNewBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameBadgeRulesClassName::getNewBonusPointValue(); } elseif ($gradeChangeOrNewBadge == 'GradeChange' && $gameBadgeRulesClassName::hasBonusPointsOnGradeChange()) { $type = $gameBadgeRulesClassName::getGradeBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameBadgeRulesClassName::getGradeBonusPointValue($gameBadge->grade); } if ($gamePoint != null && $value > 0) { $gamePoint->addValue($value, false); $saved = $gamePoint->save(); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, $value); if (!$saved) { throw new NotSupportedException(); } } }
/** * Given a user and a gameLevel, process the bonus points, if applicable for the badge. This will also * process grade change points for the given badge. * @param GameBadge $gameBadge * @param User $user */ public static function processBonusPointsOnLevelChange(GameLevel $gameLevel, User $user) { assert('$gameLevel->id > 0'); $gameLevelRulesClassName = $gameLevel->type . 'GameLevelRules'; $gamePoint = null; if ($gameLevelRulesClassName::hasBonusPointsOnLevelChange()) { $type = $gameLevelRulesClassName::getLevelBonusPointType(); $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, $user); $value = $gameLevelRulesClassName::getLevelBonusPointValue($gameLevel->value); } if ($gamePoint != null && $value > 0) { $gamePoint->addValue($value, false); $saved = $gamePoint->save(); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, $value); if (!$saved) { throw new NotSupportedException(); } } }
/** * Process any points that have been added to @see $pointTypesAndValuesByUserIdToAdd throughout the page * request. */ public function processDeferredPoints() { if (!$this->enabled) { return; } foreach (self::$pointTypesAndValuesByUserIdToAdd as $userId => $typeAndValues) { if ($typeAndValues != null) { foreach ($typeAndValues as $type => $value) { $gamePoint = GamePoint::resolveToGetByTypeAndPerson($type, User::getById($userId)); $gamePoint->addValue($value, false); $saved = $gamePoint->save(); GamePointTransaction::addTransactionResolvedForOptimization($gamePoint, $value); if (!$saved) { throw new NotSupportedException(); } } } } $this->resetDeferredPointTypesAndValuesByUserIdToAdd(); }