Keep user totalScore and give rank on /meendpoint
This commit is contained in:
parent
3103998514
commit
f677d2d4a5
4 changed files with 82 additions and 16 deletions
|
@ -134,6 +134,10 @@ class ScoreControllerProvider implements ControllerProviderInterface
|
||||||
}
|
}
|
||||||
|
|
||||||
$achievement_ids = [2]; // override for test purpose
|
$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);
|
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200);
|
||||||
});
|
});
|
||||||
|
|
|
@ -34,12 +34,35 @@ class User implements UserInterface
|
||||||
*/
|
*/
|
||||||
private $games;
|
private $games;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ORM\Column(type="float")
|
||||||
|
* @JMS\Exclude
|
||||||
|
* @var float
|
||||||
|
*/
|
||||||
|
private $totalScore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var \DateTime
|
* @var \DateTime
|
||||||
* @Gedmo\Timestampable(on="create")
|
* @Gedmo\Timestampable(on="create")
|
||||||
* @ORM\Column(type="datetime")
|
* @ORM\Column(type="datetime")
|
||||||
*/
|
*/
|
||||||
protected $createdAt;
|
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.
|
* Sets createdAt.
|
||||||
|
@ -152,4 +175,15 @@ class User implements UserInterface
|
||||||
$encoder = new JWTEncoder($c['secret_key'], $c['jwt_lifetime'], null);
|
$encoder = new JWTEncoder($c['secret_key'], $c['jwt_lifetime'], null);
|
||||||
return $encoder->encode(['id' => $this->getUsername()]);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,13 +56,39 @@ class UserRepository extends EntityRepository implements UserProviderInterface
|
||||||
public function supportsClass($class) {
|
public function supportsClass($class) {
|
||||||
return $class == User::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'
|
* @param \EmotionHero\Models\User $user
|
||||||
// )
|
* @return integer
|
||||||
// ->setParameters([
|
*/
|
||||||
// 'result' => $result,
|
public function getRankForUser(User $user) {
|
||||||
// ])
|
|
||||||
// ->setMaxResults($limit);
|
$query = $this->_em->createQuery(
|
||||||
// $resultItems = $query->getResult();
|
"SELECT COUNT(u.id) FROM ".User::class." u WHERE totalScore >= :totalScore"
|
||||||
|
)
|
||||||
|
->setParameters([
|
||||||
|
'totalScore' => $user->getTotalScore(),
|
||||||
|
]);
|
||||||
|
return (int) $query->getSingleScalarResult();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -166,14 +166,6 @@ foreach(EmotionHero\Models\Emotions::$EMOTIONS as $emotion) {
|
||||||
|
|
||||||
|
|
||||||
$circles = "";
|
$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) {
|
foreach($hitRepo->getBetterHit($currentHit)->getPoints()->getNormalisedPoints() as $i => $point) {
|
||||||
$circles .= '<circle
|
$circles .= '<circle
|
||||||
|
@ -184,6 +176,16 @@ foreach($hitRepo->getBetterHit($currentHit)->getPoints()->getNormalisedPoints()
|
||||||
r="1" />';
|
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
|
echo <<< EOSVG
|
||||||
<svg
|
<svg
|
||||||
xmlns:svg="http://www.w3.org/2000/svg"
|
xmlns:svg="http://www.w3.org/2000/svg"
|
||||||
|
|
Loading…
Reference in a new issue