diff --git a/.gitignore b/.gitignore index 57fa17b..bbe19c7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ vendor composer.lock cache/db.sqlite -cache/proxies/__CG__* +cache/proxy/__CG__* config/* !config/_default.php .phpintel diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index 1234279..4b22fc9 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -60,7 +60,6 @@ class ScoreControllerProvider implements ControllerProviderInterface * Expects a full game + hits in the request */ $controllers->post('/games', function (Request $request, Application $app) { -error_reporting(E_ALL);ini_set('display_errors',1); $data = json_decode($request->getContent(), true); $token = $app['security.token_storage']->getToken(); diff --git a/src/Models/Game.php b/src/Models/Game.php index 311267f..a70b37c 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -134,15 +134,7 @@ class Game return $this->createdAt; } - /** - * @todo Em should not be fetched here. Quick fix for now... - * - * @JMS\VirtualProperty - * @return Position - */ - public function getPosition() { - return Application::getInstance()->getEm()->getRepository(static::class)->getPositionForGame($this); - } + /** * Sets the value of user. @@ -185,4 +177,46 @@ class Game return $this; } + + + + + + + /** + * @todo Em should not be fetched here. Quick fix for now... + * + * @JMS\VirtualProperty + * @return Position + */ + public function getPosition() { + return Application::getInstance()->getEm()->getRepository(static::class)->getPositionForGame($this); + } + + + /** + * @todo Em should not be fetched here. Quick fix for now... + * + * @JMS\VirtualProperty + * @return Position + */ + public function getHint() { + $lowest_scoring_hit = null; + foreach($this->hits as $hit) { + if(!$lowest_scoring_hit || $lowest_scoring_hit->getScore() > $hit->getScore()) { + $lowest_scoring_hit = $hit; + } + } + + if(!$lowest_scoring_hit) + return ""; + + $target = $lowest_scoring_hit->getTarget(); + $emotion = $target->getEmotion(); + + $text = "When you had to feel " + . $target->getScore() ."% " . $emotion->getName() + ." you showed " . $hit->getEmotions()->getEmotionScore($emotion) ."%. To show you empathy, ..."; + return $text; + } } diff --git a/src/Models/Hit.php b/src/Models/Hit.php index eef8210..ee1a422 100644 --- a/src/Models/Hit.php +++ b/src/Models/Hit.php @@ -11,6 +11,7 @@ use Doctrine\Common\Collections\ArrayCollection; * * @ORM\Table(name="hits") * @ORM\Entity + * @ORM\Entity(repositoryClass="EmotionHero\Models\HitRepository") */ class Hit { @@ -388,6 +389,26 @@ class Emotions{ return $this->surprise; } + /** + * Get the score of given emotion + * @param string|Emotion $name name of the emotion + * @return float The score + */ + public function getEmotionScore($emotion) { + if(!is_string($emotion)) { + if(is_a($emotion, Emotion::class)) { + $emotion = $emotion->getName(); + } else { + throw new RuntimeException("Invalid emotion", 1); + } + } + $emotions = static::$EMOTIONS; + if(in_array($emotion, $emotions)) { + return $this->$emotion; + } + return 0; + } + public function setEmotion(string $emotion, float $value) { $emotions = static::$EMOTIONS; diff --git a/src/Models/HitRepository.php b/src/Models/HitRepository.php new file mode 100644 index 0000000..bc0fda5 --- /dev/null +++ b/src/Models/HitRepository.php @@ -0,0 +1,47 @@ +_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->getCountForLevel($game->getLevel()); + $highscore = $this->getHighscoreForLevel($game->getLevel()); + + return new Position($position, $total, $game->getScore(), $highscore); + } + + public function getCountForLevel(Level $level) { + + $query = $this->_em->createQuery( + "SELECT COUNT(g.id) FROM ".Game::class." g WHERE g.level = :level" + ) + ->setParameters([ + 'level' => $level, + ]); + 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' => $level, + ]); + return $query->getSingleScalarResult(); + } +} \ No newline at end of file diff --git a/src/Models/Target.php b/src/Models/Target.php index 57e5bdd..4942dc6 100644 --- a/src/Models/Target.php +++ b/src/Models/Target.php @@ -77,4 +77,74 @@ class Target public function addHit(Hit $hit){ $this->hits->add($hit); } + + /** + * Gets the value of id. + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Gets the Incremental position in level. + * + * @return int + */ + public function getPosition() + { + return $this->position; + } + + /** + * Gets the value of level. + * + * @return mixed + */ + public function getLevel() + { + return $this->level; + } + + /** + * Gets the value of time. + * + * @return float + */ + public function getTime() + { + return $this->time; + } + + /** + * Gets the value of emotion. + * + * @return mixed + */ + public function getEmotion() + { + return $this->emotion; + } + + /** + * Gets the Required score. + * + * @return int + */ + public function getScore() + { + return $this->score; + } + + /** + * Gets the value of hits. + * + * @return mixed + */ + public function getHits() + { + return $this->hits; + } }