Private
Public Access
1
0

add player names to UI

This commit is contained in:
2016-11-01 10:52:39 +01:00
parent a566ba8cf8
commit a4f9b603a2
7 changed files with 79 additions and 26 deletions

View File

@@ -6,8 +6,11 @@ use Doctrine\ORM\EntityManager;
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
use Guzzle\Http\Message\Request;
use Mine\SeekerBundle\Entity\Gamer;
use Ratchet\ConnectionInterface;
use Ratchet\Wamp\Topic;
use Symfony\Component\HttpFoundation\RequestStack;
class AcmeTopic implements TopicInterface
{
@@ -15,18 +18,23 @@ class AcmeTopic implements TopicInterface
protected $clientManipulator;
/** @var EntityManager */
protected $entityManager;
protected $em;
/** @var RequestStack */
protected $requestStack;
/**
* AcmeTopic constructor.
*
* @param $clientManipulator ClientManipulatorInterface
* @param EntityManager $entityManager
* @param RequestStack $requestStack
*/
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $entityManager)
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $entityManager, RequestStack $requestStack)
{
$this->clientManipulator = $clientManipulator;
$this->entityManager = $entityManager;
$this->em = $entityManager;
$this->requestStack = $requestStack;
}
/**
@@ -47,11 +55,14 @@ class AcmeTopic implements TopicInterface
if ($topic->count() > 2) {
$topic->remove($connection);
} else {
$users = $this->saveUserToDb($topic, $userName, $user, $topic->count());
$topic->broadcast([
'userTopicId' => $connection->resourceId,
'channel' => $topic->getId(),
'user' => $userName,
'userCnt' => $topic->count()
'userCnt' => $topic->count(),
'users' => $users
]);
}
}
@@ -105,4 +116,39 @@ class AcmeTopic implements TopicInterface
{
return 'acme.topic';
}
}
private function saveUserToDb($topic, $userName, $user, $count)
{
$gameAssoc = explode('/', $topic->getId())[2];
$playedGame = $this->em
->getRepository('MineSeekerBundle:PlayedGame')
->findOneByGameAssoc($gameAssoc);
if (!is_string($user)) {
$FOSUser = $this->em
->getRepository('JotunheimrUserBundle:User')
->findOneByUsername($userName);
$count == 1 ? $playedGame->setRed($FOSUser) : $playedGame->setBlue($FOSUser);
} else {
// $request = $this->requestStack->getCurrentRequest(); // TODO nem megy...
$anon = new Gamer();
$anon->setUserName($userName);
$this->em->persist($anon);
$count == 1 ? $playedGame->setRedAnon($anon) : $playedGame->setBlueAnon($anon);
}
$this->em->persist($playedGame);
$this->em->flush();
return array(
'red' => $playedGame->getRed() !== null ? $playedGame->getRed()->getUsername() : '',
'blue' => $playedGame->getBlue() !== null ? $playedGame->getBlue()->getUsername() : '',
'redAnon' => $playedGame->getRedAnon() !== null ? $playedGame->getRedAnon()->getUserName() : '',
'blueAnon' => $playedGame->getBlueAnon() !== null ? $playedGame->getBlueAnon()->getUserName() : ''
);
}
}