Add GET /interface/games/last
This commit is contained in:
parent
cc55dec827
commit
cc5c7ea11c
2 changed files with 89 additions and 0 deletions
|
@ -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) {
|
||||
|
|
|
@ -92,4 +92,14 @@ class Level
|
|||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Name of the level.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue