diff --git a/src/Api/ScoreControllerProvider.php b/src/Api/ScoreControllerProvider.php index 1655954..e08916a 100644 --- a/src/Api/ScoreControllerProvider.php +++ b/src/Api/ScoreControllerProvider.php @@ -43,20 +43,66 @@ class ScoreControllerProvider implements ControllerProviderInterface return $app['serializer']->serialize($user, 'json'); }); + /** + * Expects a full game + hits in the request + */ $controllers->post('/me/games', function (Request $request, Application $app) { - var_dump($request->getContent()); + $data = json_decode($request->getContent(), true); + $token = $app['security.token_storage']->getToken(); + $user = $token->getUser(); - $a = date("Y-m-d H:i:s")."\n"; - $a .= print_r($request->attributes->all(), true); - $a .= print_r($request->query->all(), true); - $a .= print_r($request->request->all(), true); - $a .= "\n\n"; - $a .= $request->getContent(); + if($data == null || !isSet($data['lvl_id'])) { + return new JsonResponse(['success' => false, 'message' => "Invalid JSON body"], 400); + } - file_put_contents(__DIR__.'/../../cache/me_games', $a); - // $vars = json_decode($request->getContent(), true); - // var_dump($vars); - return $app['serializer']->serialize($levels, 'json'); + + $level = $this->_eh->getEm()->getRepository(Models\Level::class)->find($data['lvl_id']); + if(empty($level)) { + return new JsonResponse(['success' => false, 'message' => "Invalid level"], 400); + } + + $game = new Game(); + $game->setUser($user); + $game->setLevel($level); + $game->setOriginalGameAt(new DateTime($data['time'])); + + foreach($data['hits'] as $data_hit) { + $hit = new Hit; + $target = $this->_eh->getEm()->getRepository(Models\Target::class)->findOneBy(['position' => $hit['target_index'], 'level' => $level]); + if(empty($target)){ + return new JsonResponse(['success' => false, 'message' => "Invalid target for hit"], 400); + } + + // set game appends hit to game + $hit->setGame($game); + + // attributes + $hit->setGlasses($data_hit['glasses']); + foreach(Models\Hit::$ATTRIBUTES as $attribute) { + $hit->setAttribute($data_hit[$attribute]); + } + // emotions + foreach(Models\Hit::$EMOTIONS as $emotion) { + $hit->setEmotion($data_hit['emotions'][$emotion]); + } + + //expressions + foreach(Models\Hit::$EXPRESSIONS as $expression) { + $hit->setExpression($data_hit['expressions'][$expression]); + } + + //points + foreach(range(0, 33) as $i) { + $hit->setPoint($i, $data_hit['points']["$i"]['x'], $data_hit['points']["$i"]['y']); + } + } + + $this->_eh->getEm()->persist($game); + $this->_eh->getEm()->flush(); + + + // return $app['serializer']->serialize($levels, 'json'); + return new JsonResponse(['success' => true, 'remoteId' => $game->getId()], 200); }); return $controllers; diff --git a/src/Models/Game.php b/src/Models/Game.php index 8cfbfd8..183b674 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -38,7 +38,7 @@ class Game private $level; /** - * @ORM\OneToMany(targetEntity="Hit", mappedBy="game", fetch="EXTRA_LAZY") + * @ORM\OneToMany(targetEntity="Hit", mappedBy="game", fetch="EXTRA_LAZY", cascade={"persist"}) */ private $hits; @@ -48,6 +48,13 @@ class Game */ private $score; + /** + * @var \DateTime $originalGameAt + * + * @ORM\Column(type="datetime") + */ + private $originalGameAt; + /** * @var \DateTime $created * @@ -135,4 +142,46 @@ class Game public function getPosition() { return Application::getInstance()->getEm()->getRepository(static::class)->getPositionForGame($this); } + + /** + * Sets the value of user. + * + * @param mixed $user the user + * + * @return self + */ + public function setUser(User $user) + { + $this->user = $user; + + return $this; + } + + /** + * Sets the value of level. + * + * @param mixed $level the level + * + * @return self + */ + public function setLevel(Level $level) + { + $this->level = $level; + + return $this; + } + + /** + * Sets the value of originalGameAt. + * + * @param \DateTime $date Set the time the original game was played on the device. + * + * @return self + */ + public function setOriginalGameAt(\DateTime $date) + { + $this->originalGameAt = $date; + + return $this; + } } diff --git a/src/Models/Hit.php b/src/Models/Hit.php index b97662f..f47d695 100644 --- a/src/Models/Hit.php +++ b/src/Models/Hit.php @@ -14,6 +14,10 @@ use Doctrine\Common\Collections\ArrayCollection; */ class Hit { + public static $EMOTIONS = ['anger','contempt','disgust','fear','joy','sadness','surprise']; + public static $ATTRIBUTES = ['gender','ethnicity','age']; + public static $EXPRESSIONS = ['roll','pitch','yaw','inter_ocular_distance','mouth_open','lip_press','brow_raise','nose_wrinkler','lip_depressor','brow_furrow','attention','smile','inner_brow_raiser','chin_raiser','smirk','lip_suck','upper_lip_raiser','lip_pucker','eye_closure','engagement','valence']; + /** * @var integer * @@ -31,7 +35,7 @@ class Hit private $target; /** - * @ORM\ManyToOne(targetEntity="Game", inversedBy="hits") + * @ORM\ManyToOne(targetEntity="Game", inversedBy="hits",, cascade={"persist"}) * @ORM\JoinColumn(name="game_id", referencedColumnName="id", nullable=false) */ private $game; @@ -42,12 +46,6 @@ class Hit */ private $score; - /** - * @ORM\ManyToOne(targetEntity="Emotion", inversedBy="targets") - * @ORM\JoinColumn(name="emotion_id", referencedColumnName="id", nullable=false) - */ - private $emotion; - /** * @var string * @ORM\Column(name="gender",columnDefinition="VARCHAR(1)") @@ -612,4 +610,1112 @@ class Hit { return $this->score; } + + /** + * Gets the value of id. + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Gets the value of target. + * + * @return mixed + */ + public function getTarget() + { + return $this->target; + } + + /** + * Gets the value of game. + * + * @return mixed + */ + public function getGame() + { + return $this->game; + } + + /** + * Gets the value of emotion. + * + * @return mixed + */ + public function getEmotion() + { + return $this->emotion; + } + + /** + * Gets the value of gender. + * + * @return string + */ + public function getGender() + { + return $this->gender; + } + + /** + * Gets the value of ethnicity. + * + * @return string + */ + public function getEthnicity() + { + return $this->ethnicity; + } + + /** + * Gets the value of age. + * + * @return string + */ + public function getAge() + { + return $this->age; + } + + /** + * Gets the value of glasses. + * + * @return boolean + */ + public function getGlasses() + { + return $this->glasses; + } + + /** + * Gets the value of roll. + * + * @return float Head roll angle + */ + public function getRoll() + { + return $this->roll; + } + + /** + * Gets the value of pitch. + * + * @return float Head pitch angle + */ + public function getPitch() + { + return $this->pitch; + } + + /** + * Gets the value of yaw. + * + * @return float Head yaw angle + */ + public function getYaw() + { + return $this->yaw; + } + + /** + * Gets the value of inter_ocular_distance. + * + * @return float Distance between two outer eye corners (mm?) + */ + public function getInterOcularDistance() + { + return $this->inter_ocular_distance; + } + + /** + * Gets the value of mouth_open. + * + * @return float Expression parameter + */ + public function getMouthOpen() + { + return $this->mouth_open; + } + + /** + * Gets the value of lip_press. + * + * @return float Expression parameter + */ + public function getLipPress() + { + return $this->lip_press; + } + + /** + * Gets the value of brow_raise. + * + * @return float Expression parameter + */ + public function getBrowRaise() + { + return $this->brow_raise; + } + + /** + * Gets the value of nose_wrinkler. + * + * @return float Expression parameter + */ + public function getNoseWrinkler() + { + return $this->nose_wrinkler; + } + + /** + * Gets the value of lip_depressor. + * + * @return float Expression parameter + */ + public function getLipDepressor() + { + return $this->lip_depressor; + } + + /** + * Gets the value of brow_furrow. + * + * @return float Expression parameter + */ + public function getBrowFurrow() + { + return $this->brow_furrow; + } + + /** + * Gets the value of attention. + * + * @return float Expression parameter + */ + public function getAttention() + { + return $this->attention; + } + + /** + * Gets the value of smile. + * + * @return float Expression parameter + */ + public function getSmile() + { + return $this->smile; + } + + /** + * Gets the value of inner_brow_raiser. + * + * @return float Expression parameter + */ + public function getInnerBrowRaiser() + { + return $this->inner_brow_raiser; + } + + /** + * Gets the value of chin_raiser. + * + * @return float Expression parameter + */ + public function getChinRaiser() + { + return $this->chin_raiser; + } + + /** + * Gets the value of smirk. + * + * @return float Expression parameter + */ + public function getSmirk() + { + return $this->smirk; + } + + /** + * Gets the value of lip_suck. + * + * @return float Expression parameter + */ + public function getLipSuck() + { + return $this->lip_suck; + } + + /** + * Gets the value of upper_lip_raiser. + * + * @return float Expression parameter + */ + public function getUpperLipRaiser() + { + return $this->upper_lip_raiser; + } + + /** + * Gets the value of lip_pucker. + * + * @return float Expression parameter + */ + public function getLipPucker() + { + return $this->lip_pucker; + } + + /** + * Gets the value of eye_closure. + * + * @return float Expression parameter + */ + public function getEyeClosure() + { + return $this->eye_closure; + } + + /** + * Gets the value of engagement. + * + * @return float 'Expression' parameter + */ + public function getEngagement() + { + return $this->engagement; + } + + /** + * Gets the value of valence. + * + * @return float 'Expression' parameter + */ + public function getValence() + { + return $this->valence; + } + + /** + * Gets the value of anger. + * + * @return float Emotion parameter + */ + public function getAnger() + { + return $this->anger; + } + + /** + * Gets the value of contempt. + * + * @return float Emotion parameter + */ + public function getContempt() + { + return $this->contempt; + } + + /** + * Gets the value of disgust. + * + * @return float Emotion parameter + */ + public function getDisgust() + { + return $this->disgust; + } + + /** + * Gets the value of fear. + * + * @return float Emotion parameter + */ + public function getFear() + { + return $this->fear; + } + + /** + * Gets the value of joy. + * + * @return float Emotion parameter + */ + public function getJoy() + { + return $this->joy; + } + + /** + * Gets the value of sadness. + * + * @return float Emotion parameter + */ + public function getSadness() + { + return $this->sadness; + } + + /** + * Gets the value of surprise. + * + * @return float Emotion parameter + */ + public function getSurprise() + { + return $this->surprise; + } + + /** + * Gets the value of point_0x. + * + * @return float Facial landmark + */ + public function getPoint0x() + { + return $this->point_0x; + } + + /** + * Gets the value of point_0y. + * + * @return float Facial landmark + */ + public function getPoint0y() + { + return $this->point_0y; + } + + /** + * Gets the value of point_1x. + * + * @return float Facial landmark + */ + public function getPoint1x() + { + return $this->point_1x; + } + + /** + * Gets the value of point_1y. + * + * @return float Facial landmark + */ + public function getPoint1y() + { + return $this->point_1y; + } + + /** + * Gets the value of point_2x. + * + * @return float Facial landmark + */ + public function getPoint2x() + { + return $this->point_2x; + } + + /** + * Gets the value of point_2y. + * + * @return float Facial landmark + */ + public function getPoint2y() + { + return $this->point_2y; + } + + /** + * Gets the value of point_3x. + * + * @return float Facial landmark + */ + public function getPoint3x() + { + return $this->point_3x; + } + + /** + * Gets the value of point_3y. + * + * @return float Facial landmark + */ + public function getPoint3y() + { + return $this->point_3y; + } + + /** + * Gets the value of point_4x. + * + * @return float Facial landmark + */ + public function getPoint4x() + { + return $this->point_4x; + } + + /** + * Gets the value of point_4y. + * + * @return float Facial landmark + */ + public function getPoint4y() + { + return $this->point_4y; + } + + /** + * Gets the value of point_5x. + * + * @return float Facial landmark + */ + public function getPoint5x() + { + return $this->point_5x; + } + + /** + * Gets the value of point_5y. + * + * @return float Facial landmark + */ + public function getPoint5y() + { + return $this->point_5y; + } + + /** + * Gets the value of point_6x. + * + * @return float Facial landmark + */ + public function getPoint6x() + { + return $this->point_6x; + } + + /** + * Gets the value of point_6y. + * + * @return float Facial landmark + */ + public function getPoint6y() + { + return $this->point_6y; + } + + /** + * Gets the value of point_7x. + * + * @return float Facial landmark + */ + public function getPoint7x() + { + return $this->point_7x; + } + + /** + * Gets the value of point_7y. + * + * @return float Facial landmark + */ + public function getPoint7y() + { + return $this->point_7y; + } + + /** + * Gets the value of point_8x. + * + * @return float Facial landmark + */ + public function getPoint8x() + { + return $this->point_8x; + } + + /** + * Gets the value of point_8y. + * + * @return float Facial landmark + */ + public function getPoint8y() + { + return $this->point_8y; + } + + /** + * Gets the value of point_9x. + * + * @return float Facial landmark + */ + public function getPoint9x() + { + return $this->point_9x; + } + + /** + * Gets the value of point_9y. + * + * @return float Facial landmark + */ + public function getPoint9y() + { + return $this->point_9y; + } + + /** + * Gets the value of point_10x. + * + * @return float Facial landmark + */ + public function getPoint10x() + { + return $this->point_10x; + } + + /** + * Gets the value of point_10y. + * + * @return float Facial landmark + */ + public function getPoint10y() + { + return $this->point_10y; + } + + /** + * Gets the value of point_11x. + * + * @return float Facial landmark + */ + public function getPoint11x() + { + return $this->point_11x; + } + + /** + * Gets the value of point_11y. + * + * @return float Facial landmark + */ + public function getPoint11y() + { + return $this->point_11y; + } + + /** + * Gets the value of point_12x. + * + * @return float Facial landmark + */ + public function getPoint12x() + { + return $this->point_12x; + } + + /** + * Gets the value of point_12y. + * + * @return float Facial landmark + */ + public function getPoint12y() + { + return $this->point_12y; + } + + /** + * Gets the value of point_13x. + * + * @return float Facial landmark + */ + public function getPoint13x() + { + return $this->point_13x; + } + + /** + * Gets the value of point_13y. + * + * @return float Facial landmark + */ + public function getPoint13y() + { + return $this->point_13y; + } + + /** + * Gets the value of point_14x. + * + * @return float Facial landmark + */ + public function getPoint14x() + { + return $this->point_14x; + } + + /** + * Gets the value of point_14y. + * + * @return float Facial landmark + */ + public function getPoint14y() + { + return $this->point_14y; + } + + /** + * Gets the value of point_15x. + * + * @return float Facial landmark + */ + public function getPoint15x() + { + return $this->point_15x; + } + + /** + * Gets the value of point_15y. + * + * @return float Facial landmark + */ + public function getPoint15y() + { + return $this->point_15y; + } + + /** + * Gets the value of point_16x. + * + * @return float Facial landmark + */ + public function getPoint16x() + { + return $this->point_16x; + } + + /** + * Gets the value of point_16y. + * + * @return float Facial landmark + */ + public function getPoint16y() + { + return $this->point_16y; + } + + /** + * Gets the value of point_17x. + * + * @return float Facial landmark + */ + public function getPoint17x() + { + return $this->point_17x; + } + + /** + * Gets the value of point_17y. + * + * @return float Facial landmark + */ + public function getPoint17y() + { + return $this->point_17y; + } + + /** + * Gets the value of point_18x. + * + * @return float Facial landmark + */ + public function getPoint18x() + { + return $this->point_18x; + } + + /** + * Gets the value of point_18y. + * + * @return float Facial landmark + */ + public function getPoint18y() + { + return $this->point_18y; + } + + /** + * Gets the value of point_19x. + * + * @return float Facial landmark + */ + public function getPoint19x() + { + return $this->point_19x; + } + + /** + * Gets the value of point_19y. + * + * @return float Facial landmark + */ + public function getPoint19y() + { + return $this->point_19y; + } + + /** + * Gets the value of point_20x. + * + * @return float Facial landmark + */ + public function getPoint20x() + { + return $this->point_20x; + } + + /** + * Gets the value of point_20y. + * + * @return float Facial landmark + */ + public function getPoint20y() + { + return $this->point_20y; + } + + /** + * Gets the value of point_21x. + * + * @return float Facial landmark + */ + public function getPoint21x() + { + return $this->point_21x; + } + + /** + * Gets the value of point_21y. + * + * @return float Facial landmark + */ + public function getPoint21y() + { + return $this->point_21y; + } + + /** + * Gets the value of point_22x. + * + * @return float Facial landmark + */ + public function getPoint22x() + { + return $this->point_22x; + } + + /** + * Gets the value of point_22y. + * + * @return float Facial landmark + */ + public function getPoint22y() + { + return $this->point_22y; + } + + /** + * Gets the value of point_23x. + * + * @return float Facial landmark + */ + public function getPoint23x() + { + return $this->point_23x; + } + + /** + * Gets the value of point_23y. + * + * @return float Facial landmark + */ + public function getPoint23y() + { + return $this->point_23y; + } + + /** + * Gets the value of point_24x. + * + * @return float Facial landmark + */ + public function getPoint24x() + { + return $this->point_24x; + } + + /** + * Gets the value of point_24y. + * + * @return float Facial landmark + */ + public function getPoint24y() + { + return $this->point_24y; + } + + /** + * Gets the value of point_25x. + * + * @return float Facial landmark + */ + public function getPoint25x() + { + return $this->point_25x; + } + + /** + * Gets the value of point_25y. + * + * @return float Facial landmark + */ + public function getPoint25y() + { + return $this->point_25y; + } + + /** + * Gets the value of point_26x. + * + * @return float Facial landmark + */ + public function getPoint26x() + { + return $this->point_26x; + } + + /** + * Gets the value of point_26y. + * + * @return float Facial landmark + */ + public function getPoint26y() + { + return $this->point_26y; + } + + /** + * Gets the value of point_27x. + * + * @return float Facial landmark + */ + public function getPoint27x() + { + return $this->point_27x; + } + + /** + * Gets the value of point_27y. + * + * @return float Facial landmark + */ + public function getPoint27y() + { + return $this->point_27y; + } + + /** + * Gets the value of point_28x. + * + * @return float Facial landmark + */ + public function getPoint28x() + { + return $this->point_28x; + } + + /** + * Gets the value of point_28y. + * + * @return float Facial landmark + */ + public function getPoint28y() + { + return $this->point_28y; + } + + /** + * Gets the value of point_29x. + * + * @return float Facial landmark + */ + public function getPoint29x() + { + return $this->point_29x; + } + + /** + * Gets the value of point_29y. + * + * @return float Facial landmark + */ + public function getPoint29y() + { + return $this->point_29y; + } + + /** + * Gets the value of point_30x. + * + * @return float Facial landmark + */ + public function getPoint30x() + { + return $this->point_30x; + } + + /** + * Gets the value of point_30y. + * + * @return float Facial landmark + */ + public function getPoint30y() + { + return $this->point_30y; + } + + /** + * Gets the value of point_31x. + * + * @return float Facial landmark + */ + public function getPoint31x() + { + return $this->point_31x; + } + + /** + * Gets the value of point_31y. + * + * @return float Facial landmark + */ + public function getPoint31y() + { + return $this->point_31y; + } + + /** + * Gets the value of point_32x. + * + * @return float Facial landmark + */ + public function getPoint32x() + { + return $this->point_32x; + } + + /** + * Gets the value of point_32y. + * + * @return float Facial landmark + */ + public function getPoint32y() + { + return $this->point_32y; + } + + /** + * Gets the value of point_33x. + * + * @return float Facial landmark + */ + public function getPoint33x() + { + return $this->point_33x; + } + + /** + * Gets the value of point_33y. + * + * @return float Facial landmark + */ + public function getPoint33y() + { + return $this->point_33y; + } + + public function setGame(Game $game) { + $this->game = $game; + $game->addHit($this); + return $this; + } + + /** + * Sets the value of score. + * + * @param float The score this hit resulted in $score the score + * + * @return self + */ + public function setScore($score) + { + $this->score = $score; + + return $this; + } + + public function setAttribute(string $attribute, string $value) { + $attributes = static::$ATTRIBUTES; + if(in_array($attribute, $attributes)) { + $this->$attribute = $value; + } + return $this; + } + + public function setEmotion(string $emotion, float $value) { + $emotions = static::$EMOTIONS; + if(in_array($emotion, $emotions)) { + $this->$emotion = $value > 100 ? 100 : $value; + } + return $this; + } + + + public function setExpression(string $expression, float $value) { + $expressions = static::$EXPRESSIONS; + if(in_array($expression, $expressions)) { + $this->$expression = $value > 100 ? 100 : $value; + } + return $this; + } + + public function setPoint(int $nr, float $x, float $y) { + if($nr <= 33 && $nr >= 0) { + $this->{"point_".$nr."x"} = $x; + $this->{"point_".$nr."y"} = $y; + } + return $this; + } + + + /** + * Sets the value of glasses. + * + * @param boolean $glasses the glasses + * + * @return self + */ + public function setGlasses(bool $glasses) + { + $this->glasses = $glasses; + + return $this; + } }