api.emotionhero.com/src/Models/GameRepository.php

47 lines
1.4 KiB
PHP
Raw Normal View History

2016-09-01 10:15:17 +00:00
<?php
namespace EmotionHero\Models;
use Doctrine\ORM\EntityRepository;
2016-09-01 15:33:00 +00:00
use EmotionHero\Tools\Position;
2016-09-01 10:15:17 +00:00
2016-09-01 10:26:33 +00:00
class GameRepository extends EntityRepository
2016-09-01 10:15:17 +00:00
{
2016-09-01 15:33:00 +00:00
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());
2016-09-01 10:15:17 +00:00
2016-09-01 15:33:00 +00:00
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();
}
2016-09-01 10:15:17 +00:00
}