Attempt to insert games

This commit is contained in:
Ruben 2016-09-03 23:12:00 +01:00
parent 0f801b98bb
commit b189ed7456
3 changed files with 1220 additions and 19 deletions

View file

@ -43,20 +43,66 @@ class ScoreControllerProvider implements ControllerProviderInterface
return $app['serializer']->serialize($user, 'json'); return $app['serializer']->serialize($user, 'json');
}); });
/**
* Expects a full game + hits in the request
*/
$controllers->post('/me/games', function (Request $request, Application $app) { $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"; if($data == null || !isSet($data['lvl_id'])) {
$a .= print_r($request->attributes->all(), true); return new JsonResponse(['success' => false, 'message' => "Invalid JSON body"], 400);
$a .= print_r($request->query->all(), true); }
$a .= print_r($request->request->all(), true);
$a .= "\n\n";
$a .= $request->getContent();
file_put_contents(__DIR__.'/../../cache/me_games', $a);
// $vars = json_decode($request->getContent(), true); $level = $this->_eh->getEm()->getRepository(Models\Level::class)->find($data['lvl_id']);
// var_dump($vars); if(empty($level)) {
return $app['serializer']->serialize($levels, 'json'); 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; return $controllers;

View file

@ -38,7 +38,7 @@ class Game
private $level; private $level;
/** /**
* @ORM\OneToMany(targetEntity="Hit", mappedBy="game", fetch="EXTRA_LAZY") * @ORM\OneToMany(targetEntity="Hit", mappedBy="game", fetch="EXTRA_LAZY", cascade={"persist"})
*/ */
private $hits; private $hits;
@ -48,6 +48,13 @@ class Game
*/ */
private $score; private $score;
/**
* @var \DateTime $originalGameAt
*
* @ORM\Column(type="datetime")
*/
private $originalGameAt;
/** /**
* @var \DateTime $created * @var \DateTime $created
* *
@ -135,4 +142,46 @@ class Game
public function getPosition() { public function getPosition() {
return Application::getInstance()->getEm()->getRepository(static::class)->getPositionForGame($this); 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;
}
} }

File diff suppressed because it is too large Load diff