From cc5c7ea11c73fe1d5c91b55dc8fed6bf43c1b1d7 Mon Sep 17 00:00:00 2001 From: Ruben Date: Thu, 3 Nov 2016 02:17:16 +0100 Subject: [PATCH] Add GET /interface/games/last --- src/Api/InterfaceControllerProvider.php | 79 +++++++++++++++++++++++++ src/Models/Level.php | 10 ++++ 2 files changed, 89 insertions(+) diff --git a/src/Api/InterfaceControllerProvider.php b/src/Api/InterfaceControllerProvider.php index a52d7e6..60b12e0 100644 --- a/src/Api/InterfaceControllerProvider.php +++ b/src/Api/InterfaceControllerProvider.php @@ -24,6 +24,85 @@ class InterfaceControllerProvider implements ControllerProviderInterface // creates a new controller based on the default route $controllers = $app['controllers_factory']; + // /interface/game/last + $controllers->get('/games/last', function (Application $app) { + + $gameRepo = $this->_eh->getEm()->getRepository(\EmotionHero\Models\Game::class); + /* @var $hitRepo EmotionHero\Models\HitRepository */ + $hitRepo = $this->_eh->getEm()->getRepository(\EmotionHero\Models\Hit::class); + + $game = $gameRepo->findOneBy([], ['id'=>'DESC']); + + // id + // score + // achievements, level, time, ranking, hits + $output = [ + 'id' => $game->getId(), + 'level' => $game->getLevel()->getName(), + 'score' => $game->getScore(), + 'achievements' => $game->getAchievements()->count(), + 'time' => $game->getCreatedAt()->format("Y-m-d H:i:s"), + 'hits' => [], + ]; + + // of one hit is on the same time as another, skip it + // as it is part of the same target group (which has been + // taken into account) + $times = []; + + foreach ($game->getHits() as $hit) { + $time = $hit->getTarget()->getTime(); + if(in_array($time, $times)) { + continue; + } + $times[] = $time; + + $targetSet = $hitRepo->getTargetSetForTarget($hit->getTarget()); + + $betterHit = $hitRepo->getBetterHit($hit); + + // score, emotions, expressions, points + $hitOutput = [ + 'id' => $hit->getId(), + 'score' => $hit->getScore(), + 'emotions' => [], + 'expressions' => [], + 'points' => [], + ]; + + foreach($targetSet as $target) { + $hitOutput['emotions'][$target->getEmotion()->getName()]['target_value'] = $target->getScore(); + $hitOutput['emotions'][$target->getEmotion()->getName()]['player_value'] = $hit->getEmotions()->getEmotionScore($target->getEmotion()); + } + + $expressions = array_keys($hit->getExpressions()::$EXPRESSIONS_2ND_PERSON); + sort($expressions); + foreach ($expressions as $expression) { + $score = $hit->getExpressions()->getExpressionScore($expression); + // if there is no better hit, better score == score + $betterScore = $betterHit ? $betterHit->getExpressions()->getExpressionScore($expression) : $score; + + $hitOutput['expressions'][$expression]['target_value'] = $betterScore; + $hitOutput['expressions'][$expression]['player_value'] = $score; + } + + foreach($hit->getPoints()->getNormalisedPoints() as $i => $point) { + $hitOutput['points']['player'][$i] = ['x'=> $point->getX(), 'y' => $point->getY()]; + } + + if(!empty($betterHit)) { + foreach($betterHit->getPoints()->getNormalisedPoints() as $i => $point) { + $hitOutput['points']['target'][$i] = ['x'=> $point->getX(), 'y' => $point->getY()]; + } + } + + $output['hits'][$time] = $hitOutput; + } + + return $app['serializer']->serialize($output, 'json'); + }); + + // /interface/images $controllers->get('/images', function (Application $app) { $getEmoBlocks = function($emotion, $feature, $steps = 6) { diff --git a/src/Models/Level.php b/src/Models/Level.php index 9c66d70..1f1c929 100644 --- a/src/Models/Level.php +++ b/src/Models/Level.php @@ -92,4 +92,14 @@ class Level { return $this->id; } + + /** + * Gets the Name of the level. + * + * @return string + */ + public function getName() + { + return $this->name; + } }