From 59f141ea9bc1dc575809ef860c0bae9ac5d210a4 Mon Sep 17 00:00:00 2001 From: Ruben Date: Wed, 14 Sep 2016 16:48:50 +0100 Subject: [PATCH] Check hit hasImage --- src/Api/ScoreControllerProvider.php | 17 +++++++++++++---- src/Models/Hit.php | 28 ++++++++++++++++++++++++---- src/Models/HitRepository.php | 5 +++-- www/faces.php | 2 +- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index 52b657d..2069f06 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -152,7 +152,7 @@ class ScoreControllerProvider implements ControllerProviderInterface return new JsonResponse(['success' => false, 'message' => "no files given"], 400); } - $valid_parts = ['brows','mouth_left','mouth_right','nose']; + $valid_parts = Models\Hit::$FEATURES; foreach($valid_parts as $part) { $path = $app['file.path'] . "/hits/$part"; if(!is_dir($path)) { @@ -178,16 +178,25 @@ class ScoreControllerProvider implements ControllerProviderInterface return new JsonResponse(['success' => false, 'message' => "unknown face part '$part'"], 400); } + /* @var $em \Doctrine\ORM\EntityManager */ + $em = $app['entity.manager']; + /* @var $hitRepo Models\HitRepository */ + $hitRepo = $em->getRepository(Models\Hit::class); + /* @var $hit Models\Hit */ - $hit = $app['entity.manager']->getRepository(Models\Hit::class)->find($hit_id); + $hit = $hitRepo->find($hit_id); if(empty($hit) || $hit->getGame()->getUser()->getId() != $user->getId()) { return new JsonResponse(['success' => false, 'message' => "Hit not found."], 400); } + $targetFilename = $hit->getFeatureFilename($part); - $file->move($app['file.path'] . "/hits/{$part}/", "{$hit->getId()}.jpg" ); - + $file->move($targetFilename); + $hit->setHasImage(true); + $em->persist($hit); } + $em->flush() + return new JsonResponse(['success' => true, 'message' => "images saved."], 200); }); diff --git a/src/Models/Hit.php b/src/Models/Hit.php index c8499e2..cdf9410 100644 --- a/src/Models/Hit.php +++ b/src/Models/Hit.php @@ -44,6 +44,12 @@ class Hit */ private $score; + /** + * @var boolean Is set to true when an image is saved to disk + * @ORM\Column(type="boolean") + */ + private $hasImage = false; + /** @ORM\Embedded(class="Attributes", columnPrefix=false) */ private $attributes; @@ -57,9 +63,6 @@ class Hit /** @ORM\Embedded(class = "Points", columnPrefix=false) */ private $points; - - - public function __construct() { $this->hits = new ArrayCollection(); @@ -111,7 +114,7 @@ class Hit /** * Gets the value of emotions. * - * @return mixed + * @return Emotions */ public function getEmotions() { @@ -173,6 +176,22 @@ class Hit return $this; } + /** + * @return boolean + */ + public function hasImage() { + return (bool) $this->hasImage; + } + + /** + * @param bool $hasImage + * @return self + */ + public function setHasImage(bool $hasImage) { + $this->hasImage = $hasImage; + return $this; + } + public static $FEATURES = ['brows','nose','mouth_left','mouth_right']; /** @@ -205,6 +224,7 @@ class Hit } return $string; } + } diff --git a/src/Models/HitRepository.php b/src/Models/HitRepository.php index 62c28c9..5196fc0 100644 --- a/src/Models/HitRepository.php +++ b/src/Models/HitRepository.php @@ -33,15 +33,16 @@ class HitRepository extends EntityRepository * @return Hit * @throws \Exception */ - public function getClosestHit(string $emotionField, float $score) { + public function getClosestHitWithImage(string $emotionField, float $score) { if(!in_array($emotionField, Emotions::$EMOTIONS)) throw new \Exception("Invalid emotion!"); $query = $this->_em->createQuery( - "SELECT h, ABS(:score - h.emotions.$emotionField) as HIDDEN distance FROM ".Hit::class." h ORDER BY distance ASC " + "SELECT h, ABS(:score - h.emotions.$emotionField) as HIDDEN distance FROM ".Hit::class." h WHERE h.hasImage = :has ORDER BY distance ASC " ) ->setParameters([ 'score' => $score, + 'has' => true, ]) ->setMaxResults(1); return $query->getSingleResult(); diff --git a/www/faces.php b/www/faces.php index 592143c..023959f 100644 --- a/www/faces.php +++ b/www/faces.php @@ -27,7 +27,7 @@ font-weight:bold; foreach(range(0, 100, 10) as $i) { /* @var $hitRepo EmotionHero\Models\HitRepository */ $hitRepo = $em->getRepository(EmotionHero\Models\Hit::class); - $hit = $hitRepo->getClosestHit("anger", $i); + $hit = $hitRepo->getClosestHitWithImage("anger", $i); $img = $hit->getFeatureImgAsString("brows"); $score = $hit->getEmotions()->getEmotionScore("anger");