Private
Public Access
1
0

renamed the acme to mineseeker && handle when the user connection has been lost

This commit is contained in:
2016-11-01 11:54:38 +01:00
parent a4f9b603a2
commit 6bfb08b21c
6 changed files with 26 additions and 20 deletions

View File

@@ -0,0 +1,154 @@
<?php
namespace Mine\SeekerBundle\Topic;
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 MineseekerTopic implements TopicInterface
{
/** @var ClientManipulatorInterface */
protected $clientManipulator;
/** @var EntityManager */
protected $em;
/** @var RequestStack */
protected $requestStack;
/**
* MineseekerTopic constructor.
*
* @param $clientManipulator ClientManipulatorInterface
* @param EntityManager $entityManager
* @param RequestStack $requestStack
*/
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $entityManager, RequestStack $requestStack)
{
$this->clientManipulator = $clientManipulator;
$this->em = $entityManager;
$this->requestStack = $requestStack;
}
/**
* This will receive any Subscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
/** this will broadcast the message to ALL subscribers of this topic. */
$user = $this->clientManipulator->getClient($connection);
$userName = is_string($user) ? $user : $user->getUsername();
/** if more user wants to connect than 2 to one channel */
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(),
'users' => $users
]);
}
}
/**
* This will receive any UnSubscription requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @return void
*/
public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request)
{
/** this will broadcast the message to ALL subscribers of this topic. */
$topic->broadcast(['msg' => $connection->resourceId . " has left " . $topic->getId()]);
}
/**
* This will receive any Publish requests for this topic.
*
* @param ConnectionInterface $connection
* @param Topic $topic
* @param WampRequest $request
* @param $event
* @param array $exclude
* @param array $eligible
* @return mixed|void
* @internal param Topic $Topic
* @internal param array $eligibles
*/
public function onPublish(ConnectionInterface $connection, Topic $topic, WampRequest $request, $event, array $exclude, array $eligible)
{
$user = $this->clientManipulator->getClient($connection);
$userName = is_string($user) ? $user : $user->getUsername();
$topic->broadcast([
'userTopicId' => $connection->resourceId,
'channel' => $topic->getId(),
'user' => $userName,
'userCnt' => $topic->count(),
'data' => $event
]);
}
/**
* Like RPC is will use to prefix the channel
* @return string
*/
public function getName()
{
return 'mineseeker.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() : ''
);
}
}