diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index 8a49780..32cae4d 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -134,6 +134,10 @@ class ScoreControllerProvider implements ControllerProviderInterface } $achievement_ids = [2]; // override for test purpose + + /* @var $userRepo Models\UserRepository */ + $userRepo = $this->_eh->getEm()->getRepository(Models\User::class); + $userRepo->updateTotalForUser($user); return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200); }); diff --git a/src/Models/User.php b/src/Models/User.php index 1ec7181..19d28c8 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -34,12 +34,35 @@ class User implements UserInterface */ private $games; + /** + * @ORM\Column(type="float") + * @JMS\Exclude + * @var float + */ + private $totalScore; + /** * @var \DateTime * @Gedmo\Timestampable(on="create") * @ORM\Column(type="datetime") */ protected $createdAt; + + /** + * @param float $score + * @return \EmotionHero\Models\User + */ + public function setTotalScore(float $score){ + $this->totalScore = $score; + return $this; + } + + /** + * @return float + */ + public function getTotalScore(){ + return $this->totalScore; + } /** * Sets createdAt. @@ -152,4 +175,15 @@ class User implements UserInterface $encoder = new JWTEncoder($c['secret_key'], $c['jwt_lifetime'], null); return $encoder->encode(['id' => $this->getUsername()]); } + + /** + * @JMS\VirtualProperty + * @return integer + */ + public function getRank() { + $em =Application::getInstance()->getEm(); + /* @var $userRepo UserRepository */ + $userRepo = $em->getRepository(static::class); + return $userRepo->getRankForUser($this); + } } diff --git a/src/Models/UserRepository.php b/src/Models/UserRepository.php index 2913093..e9bab80 100644 --- a/src/Models/UserRepository.php +++ b/src/Models/UserRepository.php @@ -56,13 +56,39 @@ class UserRepository extends EntityRepository implements UserProviderInterface public function supportsClass($class) { return $class == User::class; } + + public function updateTotalForUser(User $user) { + $query = $this->_em->createQuery( + "SELECT MAX(g.score) as score FROM ".Game::class." g INNER JOIN g.level l INNER JOIN g.user u WHERE g.user = :user GROUP BY g.level" + ) + ->setParameters([ + 'user' => $user, + ]); + $results = $query->getScalarResult(); + $score = 0; + foreach($results as $result) { + $score += $results['score']; + } + + $user->setTotalScore($score); + $this->_em->persist($user); + $this->_em->flush($user); + + return $score; + } - // $query = $this->_em->createQuery( - // 'SELECT ri FROM ...::class ri WHERE ri.result = :result AND ri.hitAt IS NULL ORDER BY ri.usedPosition ASC' - // ) - // ->setParameters([ - // 'result' => $result, - // ]) - // ->setMaxResults($limit); - // $resultItems = $query->getResult(); + /** + * @param \EmotionHero\Models\User $user + * @return integer + */ + public function getRankForUser(User $user) { + + $query = $this->_em->createQuery( + "SELECT COUNT(u.id) FROM ".User::class." u WHERE totalScore >= :totalScore" + ) + ->setParameters([ + 'totalScore' => $user->getTotalScore(), + ]); + return (int) $query->getSingleScalarResult(); + } } \ No newline at end of file diff --git a/www/faces.php b/www/faces.php index a7257f4..9d2e570 100644 --- a/www/faces.php +++ b/www/faces.php @@ -166,14 +166,6 @@ foreach(EmotionHero\Models\Emotions::$EMOTIONS as $emotion) { $circles = ""; -foreach($currentHit->getPoints()->getNormalisedPoints() as $i => $point) { - $circles .= ''; -} foreach($hitRepo->getBetterHit($currentHit)->getPoints()->getNormalisedPoints() as $i => $point) { $circles .= 'getBetterHit($currentHit)->getPoints()->getNormalisedPoints() r="1" />'; } +foreach($currentHit->getPoints()->getNormalisedPoints() as $i => $point) { + $circles .= ''; +} + + echo <<< EOSVG