From 311e966c89b9f2467722c591a754517f853c7b83 Mon Sep 17 00:00:00 2001 From: Lang Date: Tue, 1 Nov 2016 12:38:46 +0100 Subject: [PATCH] save the step data to db --- .../SeekerBundle/Topic/MineseekerTopic.php | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/src/Mine/SeekerBundle/Topic/MineseekerTopic.php b/src/Mine/SeekerBundle/Topic/MineseekerTopic.php index fb834f4..dcdac77 100644 --- a/src/Mine/SeekerBundle/Topic/MineseekerTopic.php +++ b/src/Mine/SeekerBundle/Topic/MineseekerTopic.php @@ -8,6 +8,7 @@ use Gos\Bundle\WebSocketBundle\Topic\TopicInterface; use Gos\Bundle\WebSocketBundle\Router\WampRequest; use Guzzle\Http\Message\Request; use Mine\SeekerBundle\Entity\Gamer; +use Mine\SeekerBundle\Entity\Step; use Ratchet\ConnectionInterface; use Ratchet\Wamp\Topic; use Symfony\Component\HttpFoundation\RequestStack; @@ -55,6 +56,7 @@ class MineseekerTopic implements TopicInterface if ($topic->count() > 2) { $topic->remove($connection); } else { + /** @var $users array Save users to database */ $users = $this->saveUserToDb($topic, $userName, $user, $topic->count()); $topic->broadcast([ @@ -99,6 +101,9 @@ class MineseekerTopic implements TopicInterface $user = $this->clientManipulator->getClient($connection); $userName = is_string($user) ? $user : $user->getUsername(); + /** Save every step by user to db */ + $this->saveStepToDb($topic, $event); + $topic->broadcast([ 'userTopicId' => $connection->resourceId, 'channel' => $topic->getId(), @@ -117,6 +122,36 @@ class MineseekerTopic implements TopicInterface return 'mineseeker.topic'; } + /** + * @param $topic + * @param $event + */ + private function saveStepToDb($topic, $event) + { + $gameAssoc = explode('/', $topic->getId())[2]; + + $playedGame = $this->em + ->getRepository('MineSeekerBundle:PlayedGame') + ->findOneByGameAssoc($gameAssoc); + + $step = new Step(); + + $step->setRow($event['coords'][0]); + $step->setCol($event['coords'][1]); + $step->setWBomb($event['bomb']); + $step->setPlayedGame($playedGame); + + $this->em->persist($step); + $this->em->flush(); + } + + /** + * @param $topic + * @param $userName + * @param $user + * @param $count + * @return array + */ private function saveUserToDb($topic, $userName, $user, $count) { $gameAssoc = explode('/', $topic->getId())[2];