api.emotionhero.com/src/Api/ScoreControllerProvider.php

117 lines
4.1 KiB
PHP
Raw Normal View History

2016-09-01 12:15:17 +02: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 00:12:00 +02:00
/**
* Expects a full game + hits in the request
*/
2016-09-01 12:15:17 +02:00
$controllers->post('/me/games', function (Request $request, Application $app) {
2016-09-04 00:12:00 +02: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-04 00:25:38 +02:00
$game = new Models\Game();
2016-09-04 00:12:00 +02:00
$game->setUser($user);
$game->setLevel($level);
2016-09-04 00:25:38 +02:00
$game->setOriginalGameAt(new \DateTime($data['time']));
$map_hits = [];
2016-09-04 00:12:00 +02:00
foreach($data['hits'] as $data_hit) {
2016-09-04 00:25:38 +02:00
$hit = new Models\Hit();
$target = $this->_eh->getEm()->getRepository(Models\Target::class)->findOneBy(['position' => $data_hit['target_index'], 'level' => $level]);
2016-09-04 00:12:00 +02:00
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']);
}
2016-09-04 00:25:38 +02:00
$map_hits[$data_hit['id']] = $hit;
2016-09-04 00:12:00 +02:00
}
$this->_eh->getEm()->persist($game);
$this->_eh->getEm()->flush();
2016-09-04 00:25:38 +02:00
$hits_array = [];
foreach($map_hits as $hit_player_id => $hit) {
$hits_array[$hit_player_id] = $hit->getId();
}
2016-09-04 00:12:00 +02:00
2016-09-04 00:25:38 +02:00
return new JsonResponse(['success' => true, 'id' => $game->getId(), 'hits' => $hits_array], 200);
2016-09-01 12:15:17 +02:00
});
return $controllers;
}
}