2016-09-01 10:15:17 +00:00
|
|
|
<?php
|
|
|
|
namespace EmotionHero\Api;
|
|
|
|
|
|
|
|
use EmotionHero\Application as EH;
|
|
|
|
use Silex\Application;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
|
|
|
use Silex\Api\ControllerProviderInterface;
|
|
|
|
use EmotionHero\Models;
|
|
|
|
|
|
|
|
class ScoreControllerProvider implements ControllerProviderInterface
|
|
|
|
{
|
|
|
|
/** @var EH */
|
|
|
|
protected $_eh;
|
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->_eh = EH::getInstance();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function connect(Application $app)
|
|
|
|
{
|
|
|
|
// creates a new controller based on the default route
|
|
|
|
$controllers = $app['controllers_factory'];
|
|
|
|
|
|
|
|
$controllers->get('/', function (Application $app) {
|
|
|
|
return "OK";
|
|
|
|
});
|
|
|
|
|
|
|
|
$controllers->get('/levels', function (Application $app) {
|
|
|
|
$levels = $this->_eh->getEm()->getRepository(Models\Level::class)->findAll();
|
|
|
|
return $app['serializer']->serialize($levels, 'json');
|
|
|
|
});
|
|
|
|
|
|
|
|
$controllers->get('/emotions', function (Application $app) {
|
|
|
|
$levels = $this->_eh->getEm()->getRepository(Models\Emotion::class)->findAll();
|
|
|
|
return $app['serializer']->serialize($levels, 'json');
|
|
|
|
});
|
|
|
|
|
|
|
|
$controllers->get('/me', function (Application $app) {
|
|
|
|
$token = $app['security.token_storage']->getToken();
|
|
|
|
$user = $token->getUser();
|
|
|
|
return $app['serializer']->serialize($user, 'json');
|
|
|
|
});
|
|
|
|
|
2016-09-04 10:54:48 +00:00
|
|
|
$controllers->get('/games/{gameId}', function(Request $request, Application $app) {
|
|
|
|
|
|
|
|
$game = $request->attributes->get('game');
|
|
|
|
|
2016-09-04 11:36:41 +00:00
|
|
|
if(empty($game)) {
|
2016-09-04 12:01:30 +00:00
|
|
|
return new CustomJsonResponse(['message' => 'Game not found'], function($data) use ($app){return $app['serializer']->serialize($data, 'json');}, 404);
|
2016-09-04 11:36:41 +00:00
|
|
|
}
|
2016-09-04 12:01:30 +00:00
|
|
|
|
2016-09-04 12:03:48 +00:00
|
|
|
return new CustomJsonResponse($game, $app['serializer.json'], 200);
|
2016-09-04 10:54:48 +00:00
|
|
|
})
|
|
|
|
->bind('game')
|
|
|
|
->convert('game', function($game, Request $request) use ($app){ return $app['entity.manager']->getRepository(Models\Game::class)->find($request->attributes->get('gameId'));});
|
|
|
|
|
2016-09-03 22:12:00 +00:00
|
|
|
/**
|
|
|
|
* Expects a full game + hits in the request
|
|
|
|
*/
|
2016-09-04 10:54:48 +00:00
|
|
|
$controllers->post('/games', function (Request $request, Application $app) {
|
2016-09-04 15:53:50 +00:00
|
|
|
|
2016-09-03 22:12:00 +00:00
|
|
|
$data = json_decode($request->getContent(), true);
|
|
|
|
$token = $app['security.token_storage']->getToken();
|
|
|
|
$user = $token->getUser();
|
|
|
|
|
|
|
|
if($data == null || !isSet($data['lvl_id'])) {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "Invalid JSON body"], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$level = $this->_eh->getEm()->getRepository(Models\Level::class)->find($data['lvl_id']);
|
|
|
|
if(empty($level)) {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "Invalid level"], 400);
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:25:38 +00:00
|
|
|
$game = new Models\Game();
|
2016-09-03 22:12:00 +00:00
|
|
|
$game->setUser($user);
|
|
|
|
$game->setLevel($level);
|
2016-09-03 22:25:38 +00:00
|
|
|
$game->setOriginalGameAt(new \DateTime($data['time']));
|
|
|
|
|
|
|
|
$map_hits = [];
|
2016-09-03 22:12:00 +00:00
|
|
|
|
|
|
|
foreach($data['hits'] as $data_hit) {
|
2016-09-03 22:25:38 +00:00
|
|
|
$hit = new Models\Hit();
|
|
|
|
$target = $this->_eh->getEm()->getRepository(Models\Target::class)->findOneBy(['position' => $data_hit['target_index'], 'level' => $level]);
|
2016-09-03 22:12:00 +00:00
|
|
|
if(empty($target)){
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "Invalid target for hit"], 400);
|
|
|
|
}
|
|
|
|
|
2016-09-03 22:55:36 +00:00
|
|
|
$hit->setTarget($target);
|
|
|
|
|
|
|
|
$hit->setScore($data_hit['score']);
|
2016-09-03 22:12:00 +00:00
|
|
|
|
|
|
|
// attributes
|
2016-09-04 12:51:10 +00:00
|
|
|
$hit->getAttributes()->setGlasses($data_hit['glasses']);
|
2016-09-12 16:27:06 +00:00
|
|
|
foreach($hit->getAttributes()->ATTRIBUTES as $attribute) {
|
2016-09-04 12:32:49 +00:00
|
|
|
$hit->getAttributes()->setAttribute($attribute, $data_hit[$attribute]);
|
2016-09-03 22:12:00 +00:00
|
|
|
}
|
|
|
|
// emotions
|
2016-09-12 16:27:06 +00:00
|
|
|
foreach($hit->getEmotions()->EMOTIONS as $emotion) {
|
2016-09-04 12:32:49 +00:00
|
|
|
$hit->getEmotions()->setEmotion($emotion, $data_hit['emotions'][$emotion]);
|
2016-09-03 22:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//expressions
|
2016-09-12 16:27:06 +00:00
|
|
|
foreach($hit->getExpressions()->EXPRESSIONS as $expression) {
|
2016-09-04 12:32:49 +00:00
|
|
|
$hit->getExpressions()->setExpression($expression, $data_hit['expressions'][$expression]);
|
2016-09-03 22:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//points
|
|
|
|
foreach(range(0, 33) as $i) {
|
2016-09-04 14:48:23 +00:00
|
|
|
$hit->getPoints()->setPoint($i, $data_hit['points']["$i"]['x'], $data_hit['points']["$i"]['y']);
|
2016-09-03 22:12:00 +00:00
|
|
|
}
|
2016-09-03 22:25:38 +00:00
|
|
|
|
2016-09-04 10:54:48 +00:00
|
|
|
|
2016-09-03 23:00:08 +00:00
|
|
|
// set game appends hit to game
|
|
|
|
$hit->setGame($game);
|
|
|
|
|
2016-09-03 22:25:38 +00:00
|
|
|
$map_hits[$data_hit['id']] = $hit;
|
2016-09-03 22:12:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->_eh->getEm()->persist($game);
|
|
|
|
$this->_eh->getEm()->flush();
|
|
|
|
|
2016-09-03 22:25:38 +00:00
|
|
|
$hits_array = [];
|
|
|
|
foreach($map_hits as $hit_player_id => $hit) {
|
|
|
|
$hits_array[$hit_player_id] = $hit->getId();
|
|
|
|
}
|
2016-09-03 22:12:00 +00:00
|
|
|
|
2016-09-10 00:30:28 +00:00
|
|
|
$achievement_ids = [];
|
|
|
|
foreach($game->getAchievements() as $achievement) {
|
|
|
|
$achievement_ids[] = $achievement->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$achievement_ids = [2]; // override for test purpose
|
|
|
|
|
|
|
|
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array, 'achievements' => $achievement_ids], 200);
|
2016-09-01 10:15:17 +00:00
|
|
|
});
|
2016-09-12 16:27:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Expects a full game + hits in the request
|
|
|
|
* add images as post multipart data.
|
|
|
|
* filename: hit_id-imagetype.jpg (ie. 12341-brows.jpg)
|
|
|
|
*/
|
|
|
|
$controllers->post('/images', function (Request $request, Application $app) {
|
|
|
|
$token = $app['security.token_storage']->getToken();
|
|
|
|
/* @var $user Models\User */
|
|
|
|
$user = $token->getUser();
|
|
|
|
if($request->files->count() < 1) {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "no files given"], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$valid_parts = ['brows','mouth_left','mouth_right','nose'];
|
|
|
|
foreach($valid_parts as $part) {
|
|
|
|
$path = $app['file.path'] . "/hits/$part";
|
|
|
|
if(!is_dir($path)) {
|
|
|
|
mkdir($path, 0777, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($request->files->all() as $file) {
|
|
|
|
/* @var $file \Symfony\Component\HttpFoundation\File\UploadedFile */
|
|
|
|
$filename = $file->getClientOriginalName();
|
|
|
|
$ext = $file->getClientOriginalExtension();
|
|
|
|
if($ext != 'jpg' || $file->getMimeType() != 'image/jpeg') {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "Image not image/jpeg"], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// name without .jpg
|
|
|
|
$id_string = substr($filename, 0, -4);
|
|
|
|
|
|
|
|
list($hit_id, $part) = \explode('-', $id_string);
|
|
|
|
|
|
|
|
if(!in_array($part, $valid_parts)) {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "unknown face part."], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @var $hit Models\Hit */
|
|
|
|
$hit = $app['entity.manager']->getRepository(Models\Hit::class)->find($hit_id);
|
|
|
|
if(empty($hit) || $hit->getGame()->getUser()->getId() != $user->getId()) {
|
|
|
|
return new JsonResponse(['success' => false, 'message' => "Hit not found."], 400);
|
|
|
|
}
|
|
|
|
|
|
|
|
$file->move($app['file.path'] . "/hits/{$part}/", "{$hit->getId()}.jpg" );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return new JsonResponse(['success' => true, 'message' => "images saved."], 200);
|
|
|
|
});
|
2016-09-01 10:15:17 +00:00
|
|
|
|
|
|
|
return $controllers;
|
|
|
|
}
|
2016-09-04 15:53:50 +00:00
|
|
|
}
|