47 lines
No EOL
1.4 KiB
PHP
47 lines
No EOL
1.4 KiB
PHP
<?php
|
|
namespace EmotionHero\Models;
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
use EmotionHero\Tools\Position;
|
|
|
|
class GameRepository extends EntityRepository
|
|
{
|
|
public function getPositionForGame(Game $game) {
|
|
$query = $this->_em->createQuery(
|
|
"SELECT COUNT(g.id) FROM ". Game::class ." g WHERE g.score < :score AND g.level = :level"
|
|
)
|
|
->setParameters([
|
|
'score' => $game->getScore(),
|
|
'level' => $game->getLevel(),
|
|
]);
|
|
$position = $query->getSingleScalarResult();
|
|
$total = $this->getGameCountForLevel($game->getLevel());
|
|
$highscore = $this->getHighscoreForLevel($game->getLevel());
|
|
|
|
return new Position($position, $total, $game->getScore(), $highscore);
|
|
}
|
|
|
|
public function getGameCountForLevel(Level $level) {
|
|
|
|
$query = $this->_em->createQuery(
|
|
"SELECT COUNT(g.id) FROM ".Game::class." g WHERE g.level = :level"
|
|
)
|
|
->setParameters([
|
|
'level' => $game->getLevel(),
|
|
]);
|
|
return $query->getSingleScalarResult();
|
|
}
|
|
|
|
/**
|
|
* @return float
|
|
*/
|
|
public function getHighscoreForLevel(Level $level) {
|
|
$query = $this->_em->createQuery(
|
|
"SELECT MAX(g.score) FROM ".Game::class." g WHERE g.level = :level"
|
|
)
|
|
->setParameters([
|
|
'level' => $game->getLevel(),
|
|
]);
|
|
return $query->getSingleScalarResult();
|
|
}
|
|
} |