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
|
@ -135,6 +135,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,6 +34,13 @@ 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")
|
||||||
|
@ -41,6 +48,22 @@ class User implements UserInterface
|
||||||
*/
|
*/
|
||||||
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,12 +57,38 @@ class UserRepository extends EntityRepository implements UserProviderInterface
|
||||||
return $class == User::class;
|
return $class == User::class;
|
||||||
}
|
}
|
||||||
|
|
||||||
// $query = $this->_em->createQuery(
|
public function updateTotalForUser(User $user) {
|
||||||
// 'SELECT ri FROM ...::class ri WHERE ri.result = :result AND ri.hitAt IS NULL ORDER BY ri.usedPosition ASC'
|
$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([
|
)
|
||||||
// 'result' => $result,
|
->setParameters([
|
||||||
// ])
|
'user' => $user,
|
||||||
// ->setMaxResults($limit);
|
]);
|
||||||
// $resultItems = $query->getResult();
|
$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;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @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();
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -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