2016-10-18 15:58:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Mine\SeekerBundle\Topic;
|
|
|
|
|
|
2016-10-25 11:19:50 +02:00
|
|
|
use Doctrine\ORM\EntityManager;
|
2016-10-19 13:27:55 +02:00
|
|
|
use Gos\Bundle\WebSocketBundle\Client\ClientManipulatorInterface;
|
2016-10-18 15:58:53 +02:00
|
|
|
use Gos\Bundle\WebSocketBundle\Topic\TopicInterface;
|
|
|
|
|
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
|
2016-11-01 10:52:39 +01:00
|
|
|
use Guzzle\Http\Message\Request;
|
|
|
|
|
use Mine\SeekerBundle\Entity\Gamer;
|
2016-11-01 12:38:46 +01:00
|
|
|
use Mine\SeekerBundle\Entity\Step;
|
2016-10-18 15:58:53 +02:00
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
use Ratchet\Wamp\Topic;
|
2016-11-01 10:52:39 +01:00
|
|
|
use Symfony\Component\HttpFoundation\RequestStack;
|
2016-10-18 15:58:53 +02:00
|
|
|
|
2016-11-01 11:54:38 +01:00
|
|
|
class MineseekerTopic implements TopicInterface
|
2016-10-18 15:58:53 +02:00
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** @var ClientManipulatorInterface */
|
|
|
|
|
protected $clientManipulator;
|
|
|
|
|
|
2016-10-25 11:19:50 +02:00
|
|
|
/** @var EntityManager */
|
2016-11-01 10:52:39 +01:00
|
|
|
protected $em;
|
|
|
|
|
|
|
|
|
|
/** @var RequestStack */
|
|
|
|
|
protected $requestStack;
|
2016-10-25 11:19:50 +02:00
|
|
|
|
2016-10-19 13:27:55 +02:00
|
|
|
/**
|
2016-11-01 11:54:38 +01:00
|
|
|
* MineseekerTopic constructor.
|
2016-10-19 13:27:55 +02:00
|
|
|
*
|
|
|
|
|
* @param $clientManipulator ClientManipulatorInterface
|
2016-10-25 11:19:50 +02:00
|
|
|
* @param EntityManager $entityManager
|
2016-11-01 10:52:39 +01:00
|
|
|
* @param RequestStack $requestStack
|
2016-10-19 13:27:55 +02:00
|
|
|
*/
|
2016-11-01 10:52:39 +01:00
|
|
|
public function __construct(ClientManipulatorInterface $clientManipulator, EntityManager $entityManager, RequestStack $requestStack)
|
2016-10-19 13:27:55 +02:00
|
|
|
{
|
|
|
|
|
$this->clientManipulator = $clientManipulator;
|
2016-11-01 10:52:39 +01:00
|
|
|
$this->em = $entityManager;
|
|
|
|
|
$this->requestStack = $requestStack;
|
2016-10-19 13:27:55 +02:00
|
|
|
}
|
2016-10-18 15:58:53 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** this will broadcast the message to ALL subscribers of this topic. */
|
|
|
|
|
$user = $this->clientManipulator->getClient($connection);
|
2016-10-25 11:19:50 +02:00
|
|
|
$userName = is_string($user) ? $user : $user->getUsername();
|
|
|
|
|
|
2016-10-31 18:07:37 +01:00
|
|
|
/** if more user wants to connect than 2 to one channel */
|
|
|
|
|
if ($topic->count() > 2) {
|
|
|
|
|
$topic->remove($connection);
|
|
|
|
|
} else {
|
2016-11-01 12:38:46 +01:00
|
|
|
/** @var $users array Save users to database */
|
2016-11-01 10:52:39 +01:00
|
|
|
$users = $this->saveUserToDb($topic, $userName, $user, $topic->count());
|
|
|
|
|
|
2016-10-31 18:07:37 +01:00
|
|
|
$topic->broadcast([
|
|
|
|
|
'userTopicId' => $connection->resourceId,
|
|
|
|
|
'channel' => $topic->getId(),
|
|
|
|
|
'user' => $userName,
|
2016-11-01 10:52:39 +01:00
|
|
|
'userCnt' => $topic->count(),
|
|
|
|
|
'users' => $users
|
2016-10-31 18:07:37 +01:00
|
|
|
]);
|
|
|
|
|
}
|
2016-10-18 15:58:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
|
|
|
|
{
|
2016-10-19 13:27:55 +02:00
|
|
|
/** this will broadcast the message to ALL subscribers of this topic. */
|
2016-10-18 15:58:53 +02:00
|
|
|
$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)
|
|
|
|
|
{
|
2016-10-25 11:19:50 +02:00
|
|
|
$user = $this->clientManipulator->getClient($connection);
|
|
|
|
|
$userName = is_string($user) ? $user : $user->getUsername();
|
|
|
|
|
|
2016-11-01 12:38:46 +01:00
|
|
|
/** Save every step by user to db */
|
|
|
|
|
$this->saveStepToDb($topic, $event);
|
|
|
|
|
|
2016-10-18 15:58:53 +02:00
|
|
|
$topic->broadcast([
|
2016-10-25 11:19:50 +02:00
|
|
|
'userTopicId' => $connection->resourceId,
|
|
|
|
|
'channel' => $topic->getId(),
|
|
|
|
|
'user' => $userName,
|
|
|
|
|
'userCnt' => $topic->count(),
|
|
|
|
|
'data' => $event
|
2016-10-18 15:58:53 +02:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Like RPC is will use to prefix the channel
|
2016-11-01 12:58:09 +01:00
|
|
|
*
|
2016-10-18 15:58:53 +02:00
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getName()
|
|
|
|
|
{
|
2016-11-01 11:54:38 +01:00
|
|
|
return 'mineseeker.topic';
|
2016-10-18 15:58:53 +02:00
|
|
|
}
|
2016-11-01 10:52:39 +01:00
|
|
|
|
2016-11-01 12:38:46 +01:00
|
|
|
/**
|
2016-11-01 12:58:09 +01:00
|
|
|
* Save steps and point information to database
|
|
|
|
|
*
|
2016-11-01 12:38:46 +01:00
|
|
|
* @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);
|
2016-11-01 12:58:09 +01:00
|
|
|
|
|
|
|
|
$playedGame->setBluePoints($event['bluePoints']);
|
|
|
|
|
$playedGame->setRedPoints($event['redPoints']);
|
|
|
|
|
$this->em->persist($playedGame);
|
|
|
|
|
|
2016-11-01 12:38:46 +01:00
|
|
|
$this->em->flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2016-11-01 12:58:09 +01:00
|
|
|
* Save user data to database
|
|
|
|
|
*
|
2016-11-01 12:38:46 +01:00
|
|
|
* @param $topic
|
|
|
|
|
* @param $userName
|
|
|
|
|
* @param $user
|
|
|
|
|
* @param $count
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2016-11-01 10:52:39 +01:00
|
|
|
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() : ''
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|