Keep user totalScore and give rank on /meendpoint

This commit is contained in:
Ruben 2016-09-16 00:51:24 +01:00
parent 3103998514
commit f677d2d4a5
4 changed files with 82 additions and 16 deletions

View file

@ -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);
});

View file

@ -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);
}
}

View file

@ -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();
}
}

View file

@ -166,14 +166,6 @@ foreach(EmotionHero\Models\Emotions::$EMOTIONS as $emotion) {
$circles = "";
foreach($currentHit->getPoints()->getNormalisedPoints() as $i => $point) {
$circles .= '<circle
style="fill:#ff0000;"
id="point'.$i.'"
cx="'.$point->getX().'"
cy="'.$point->getY().'"
r="1" />';
}
foreach($hitRepo->getBetterHit($currentHit)->getPoints()->getNormalisedPoints() as $i => $point) {
$circles .= '<circle
@ -184,6 +176,16 @@ foreach($hitRepo->getBetterHit($currentHit)->getPoints()->getNormalisedPoints()
r="1" />';
}
foreach($currentHit->getPoints()->getNormalisedPoints() as $i => $point) {
$circles .= '<circle
style="fill:#ff0000;"
id="point'.$i.'"
cx="'.$point->getX().'"
cy="'.$point->getY().'"
r="1" />';
}
echo <<< EOSVG
<svg
xmlns:svg="http://www.w3.org/2000/svg"