Check hit hasImage

This commit is contained in:
Ruben 2016-09-14 16:48:50 +01:00
parent 3bc73d412f
commit 59f141ea9b
4 changed files with 41 additions and 11 deletions

View file

@ -152,7 +152,7 @@ class ScoreControllerProvider implements ControllerProviderInterface
return new JsonResponse(['success' => false, 'message' => "no files given"], 400); 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) { foreach($valid_parts as $part) {
$path = $app['file.path'] . "/hits/$part"; $path = $app['file.path'] . "/hits/$part";
if(!is_dir($path)) { if(!is_dir($path)) {
@ -178,16 +178,25 @@ class ScoreControllerProvider implements ControllerProviderInterface
return new JsonResponse(['success' => false, 'message' => "unknown face part '$part'"], 400); 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 */ /* @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()) { if(empty($hit) || $hit->getGame()->getUser()->getId() != $user->getId()) {
return new JsonResponse(['success' => false, 'message' => "Hit not found."], 400); 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); return new JsonResponse(['success' => true, 'message' => "images saved."], 200);
}); });

View file

@ -44,6 +44,12 @@ class Hit
*/ */
private $score; 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) */ /** @ORM\Embedded(class="Attributes", columnPrefix=false) */
private $attributes; private $attributes;
@ -57,9 +63,6 @@ class Hit
/** @ORM\Embedded(class = "Points", columnPrefix=false) */ /** @ORM\Embedded(class = "Points", columnPrefix=false) */
private $points; private $points;
public function __construct() public function __construct()
{ {
$this->hits = new ArrayCollection(); $this->hits = new ArrayCollection();
@ -111,7 +114,7 @@ class Hit
/** /**
* Gets the value of emotions. * Gets the value of emotions.
* *
* @return mixed * @return Emotions
*/ */
public function getEmotions() public function getEmotions()
{ {
@ -173,6 +176,22 @@ class Hit
return $this; 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']; public static $FEATURES = ['brows','nose','mouth_left','mouth_right'];
/** /**
@ -205,6 +224,7 @@ class Hit
} }
return $string; return $string;
} }
} }

View file

@ -33,15 +33,16 @@ class HitRepository extends EntityRepository
* @return Hit * @return Hit
* @throws \Exception * @throws \Exception
*/ */
public function getClosestHit(string $emotionField, float $score) { public function getClosestHitWithImage(string $emotionField, float $score) {
if(!in_array($emotionField, Emotions::$EMOTIONS)) if(!in_array($emotionField, Emotions::$EMOTIONS))
throw new \Exception("Invalid emotion!"); throw new \Exception("Invalid emotion!");
$query = $this->_em->createQuery( $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([ ->setParameters([
'score' => $score, 'score' => $score,
'has' => true,
]) ])
->setMaxResults(1); ->setMaxResults(1);
return $query->getSingleResult(); return $query->getSingleResult();

View file

@ -27,7 +27,7 @@ font-weight:bold;
foreach(range(0, 100, 10) as $i) { foreach(range(0, 100, 10) as $i) {
/* @var $hitRepo EmotionHero\Models\HitRepository */ /* @var $hitRepo EmotionHero\Models\HitRepository */
$hitRepo = $em->getRepository(EmotionHero\Models\Hit::class); $hitRepo = $em->getRepository(EmotionHero\Models\Hit::class);
$hit = $hitRepo->getClosestHit("anger", $i); $hit = $hitRepo->getClosestHitWithImage("anger", $i);
$img = $hit->getFeatureImgAsString("brows"); $img = $hit->getFeatureImgAsString("brows");
$score = $hit->getEmotions()->getEmotionScore("anger"); $score = $hit->getEmotions()->getEmotionScore("anger");