diff --git a/src/Rpc/MineseekerRpc.php b/src/Rpc/MineseekerRpc.php index ad51d9d..1261d2f 100644 --- a/src/Rpc/MineseekerRpc.php +++ b/src/Rpc/MineseekerRpc.php @@ -1,34 +1,39 @@ + */ class MineseekerRpc implements RpcInterface { - /** @var EntityManager $em */ - protected $em; - - protected $grid; - - private $logger; + /** @var RpcManager $manager */ + private $manager; /** * MineseekerRpc constructor. - * @param EntityManager $entityManager + * + * @param RpcManager $manager */ - public function __construct(EntityManager $entityManager, LoggerInterface $logger) + public function __construct(RpcManager $manager) { - $this->em = $entityManager; - $this->logger = $logger; + $this->manager = $manager; } /** @@ -36,145 +41,36 @@ class MineseekerRpc implements RpcInterface * * @return string */ - public function getName() + public function getName(): string { return 'mineseeker.rpc'; } /** + * It handles the game starting processes + * * @param ConnectionInterface $connection - * @param WampRequest $request - * @param array $params + * @param WampRequest $request + * @param array $params + * * @return boolean */ - public function startGame(ConnectionInterface $connection, WampRequest $request, array $params) + public function startGame(ConnectionInterface $connection, WampRequest $request, array $params): bool { - return $this->saveGrid($params); + return $this->manager->saveGrid($params); } /** + * It handles when somebody trying to connect to the party + * * @param ConnectionInterface $connection - * @param WampRequest $request - * @param array $params + * @param WampRequest $request + * @param array $params + * * @return string Json string for frontend w/ numbering consideration. (=> a number is not string) */ - public function connectGame(ConnectionInterface $connection, WampRequest $request, array $params) + public function connectGame(ConnectionInterface $connection, WampRequest $request, array $params): string { - $grid = $this->getGrid($params); - $users = null !== $grid ? $this->getUsers($params) : null; - - return base64_encode(json_encode( - array( - 'grid' => $grid, - 'users' => $users - ) - )); - } - - /** - * @param $gameAssoc - * @return array - */ - private function getGrid($gameAssoc) - { - $this->reConnect(); - $getsee = array(); - - $this->em->clear(); - - $grid = $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc); - - if (null !== $grid) { - foreach ($grid->getGrid()->getGridRow()->toArray() as $row) { - $getsee[] = $row->getGridCol(); - } - - return $getsee; - } - - return null; - } - - /** - * @param $gameAssoc - * @return array - */ - private function getUsers($gameAssoc) - { - $this->reConnect(); - return $this->getUserCollection( - $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc) - ); - } - - /** - * Get user collection from PlayedGame entity - * - * @param $playedGame - * @return array - */ - private function getUserCollection($playedGame) - { - return array( - 'red' => null !== $playedGame->getRed() ? $playedGame->getRed()->getUsername() : '', - 'blue' => null !== $playedGame->getBlue() ? $playedGame->getBlue()->getUsername() : '', - 'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '', - 'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : '' - ); - } - - /** - * @param $data - * @return boolean - */ - private function saveGrid($data) - { -// $this->reConnect(); - $playedGame = new PlayedGame(); - $grid = new Grid(); - - foreach (json_decode(base64_decode($data[0])) as $row) { - $gridRow = new GridRow(); - - $gridRow->setGridCol($row); - - /** Save Row */ - $gridRow->setGrid($grid); - $this->em->persist($gridRow); - } - - /** Save Grid */ - $grid->setPlayedGame($playedGame); - $this->em->persist($grid); - - /** Save PlayedGame */ - $playedGame->setGameAssoc($data[1]); - $playedGame->setGrid($grid); - $playedGame->setCreated(new \DateTime()); - $playedGame->setUpdated(new \DateTime()); - $this->em->persist($playedGame); - - $this->em->flush(); - - return true; - } - - /** Handle prod MySQL timeout */ - private function reConnect() - { -// try { - $connection = $this->em->getConnection(); - - if (false === $connection->ping()) { - $connection->close(); - $connection->connect(); - } -// } catch(PDOException $ex) { -// $this->logger->error($ex->getMessage()); -// } + return $this->manager->getConnectInformation($params); } } diff --git a/src/Topic/MineseekerTopic.php b/src/Topic/MineseekerTopic.php index ca901f7..5464fc3 100644 --- a/src/Topic/MineseekerTopic.php +++ b/src/Topic/MineseekerTopic.php @@ -1,132 +1,40 @@ + */ class MineseekerTopic implements TopicInterface { - /** @var ClientManipulatorInterface $clientManipulator */ - protected $clientManipulator; - - /** @var EntityManager $em */ - protected $em; - - /** @var RequestStack $requestStack */ - protected $requestStack; - - /** @var LoggerInterface $logger */ - protected $logger; + /** @var TopicManager $manager */ + private $manager; /** * MineseekerTopic constructor. * - * @param $clientManipulator ClientManipulatorInterface - * @param EntityManager $entityManager - * @param RequestStack $requestStack - * @param LoggerInterface $logger + * @param TopicManager $manager */ - public function __construct( - ClientManipulatorInterface $clientManipulator, - EntityManager $entityManager, - RequestStack $requestStack, - LoggerInterface $logger - ) + public function __construct(TopicManager $manager) { - $this->clientManipulator = $clientManipulator; - $this->em = $entityManager; - $this->requestStack = $requestStack; - $this->logger = $logger; - } - - /** - * 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->controlUsers($topic, $userName, $user); - - $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(); - - /** Save every step by user to db */ - if (null === $event['resign']) { - $this->saveStepToDb($topic, $event); - } else { - $this->saveResignToDb($topic, $event['resign']); - } - - $topic->broadcast([ - 'userTopicId' => $connection->resourceId, - 'channel' => $topic->getId(), - 'user' => $userName, - 'userCnt' => $topic->count(), - 'data' => $event - ]); + $this->manager = $manager; } /** @@ -134,188 +42,62 @@ class MineseekerTopic implements TopicInterface * * @return string */ - public function getName() + public function getName(): string { return 'mineseeker.topic'; } /** - * Save Resign event to database + * This will receive any Subscription requests for this topic. * - * @param $topic - * @param $color + * @param ConnectionInterface $connection + * @param Topic $topic + * @param WampRequest $request + * + * @return void */ - private function saveResignToDb($topic, $color) + public function onSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request): void { - $this->reConnect(); - $gameAssoc = explode('/', $topic->getId())[2]; - - $playedGame = $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc); - - $playedGame->setResign($color); - $this->em->persist($playedGame); - $this->em->flush(); + $this->manager->subscribe($topic, $connection); } /** - * Save steps and point information to database + * This will receive any UnSubscription requests for this topic. * - * @param $topic - * @param $event + * @param ConnectionInterface $connection + * @param Topic $topic + * @param WampRequest $request + * + * @return void */ - private function saveStepToDb($topic, $event) + public function onUnSubscribe(ConnectionInterface $connection, Topic $topic, WampRequest $request): void { - $this->reConnect(); - $gameAssoc = explode('/', $topic->getId())[2]; - - /** @var PlayedGame $playedGame */ - $playedGame = $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc); - - $step = new Step(); - - $step->setRow($event['coords'][0]); - $step->setCol($event['coords'][1]); - $step->setWBomb($event['bomb']); - $step->setPlayedGame($playedGame); - $step->setCreated(new \DateTime()); - $this->em->persist($step); - - $playedGame->setBluePoints($event['bluePoints']); - $playedGame->setRedPoints($event['redPoints']); - $playedGame->setBlueExplodedBomb($event['blueExplodedBomb'] ? true : null); - $playedGame->setRedExplodedBomb($event['redExplodedBomb'] ? true : null); - $playedGame->setUpdated(new \DateTime()); - $this->em->persist($playedGame); - - $this->em->flush(); + $this->manager->unSubscribe($topic, $connection); } /** - * Control all users in a channel + * This will receive any Publish requests for this topic. * - * @param $topic - * @param $userName - * @param $user - * @return array - */ - private function controlUsers($topic, $userName, $user) - { - $this->reConnect(); - $gameAssoc = explode('/', $topic->getId())[2]; - - $playedGame = $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc); - - /** @var $users {array} */ - $users = $this->getUserCollection($playedGame); - - $red = "" !== $users['red'] || "" !== $users['redAnon'] ? 1 : 0; - $blue = "" !== $users['blue'] || "" !== $users['blueAnon'] ? 1 : 0; - $one = $topic->count() === 1; - $two = $topic->count() === 2; - - /** This checks it is a reconnection */ - if (($one && ($red + $blue === 0)) || ($two && ($red + $blue === 1))) { - /** @var $users {array} w/ save users to database */ - $users = $this->saveUserToDb($topic, $userName, $user, $topic->count()); - } - - return $users; - } - - /** - * Save user data to database + * @param ConnectionInterface $connection + * @param Topic $topic + * @param WampRequest $request + * @param $event + * @param array $exclude + * @param array $eligible * - * @param $topic - * @param $userName - * @param $user - * @param $count - * @return array + * @return mixed|void + * @internal param Topic $Topic + * @internal param array $eligibles */ - private function saveUserToDb($topic, $userName, $user, $count) + public function onPublish( + ConnectionInterface $connection, + Topic $topic, + WampRequest $request, + $event, + array $exclude, + array $eligible + ) { - $this->reConnect(); - $gameAssoc = explode('/', $topic->getId())[2]; - - $playedGame = $this->em - ->getRepository(PlayedGame::class) - ->findOneByGameAssoc($gameAssoc); - - /** the user is not anonym */ - if (!is_string($user)) { - $FOSUser = $this->em - ->getRepository(User::class) - ->findOneByUsername($userName); - - if ($count == 1) { - /** @var $random {integer} Active player: red: 0, blue: 1 */ - $random = rand(0, 1); - !$random ? $playedGame->setRed($FOSUser) : $playedGame->setBlue($FOSUser); - } else { - null === $playedGame->getRed() && null === $playedGame->getRedAnon() - ? $playedGame->setRed($FOSUser) - : $playedGame->setBlue($FOSUser); - } - } else { - // $request = $this->requestStack->getCurrentRequest(); // TODO nem megy... - - $anon = new Gamer(); - $anon->setUsername($userName); - $anon->setConnTimestamp(new \DateTime()); - $this->em->persist($anon); - - if ($count == 1) { - /** @var $random {integer} Active player: red: 0, blue: 1 */ - $random = rand(0, 1); - !$random ? $playedGame->setRedAnon($anon) : $playedGame->setBlueAnon($anon); - } else { - null === $playedGame->getRed() && null === $playedGame->getRedAnon() - ? $playedGame->setRedAnon($anon) - : $playedGame->setBlueAnon($anon); - } - } - - $this->em->persist($playedGame); - $this->em->flush(); - - return $this->getUserCollection($playedGame); - } - - /** - * Get user collection from PlayedGame entity - * - * @param $playedGame - * @return array - */ - private function getUserCollection($playedGame) - { - return array( - 'red' => null !== $playedGame->getRed() ? $playedGame->getRed()->getUsername() : '', - 'blue' => null !== $playedGame->getBlue() ? $playedGame->getBlue()->getUsername() : '', - 'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '', - 'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : '' - ); - } - - /** - * Handle prod MySQL timeout - */ - private function reConnect() - { -// try { - $connection = $this->em->getConnection(); - - if (false === $connection->ping()) { - $connection->close(); - $connection->connect(); - } -// } catch(PDOException $ex) { -// $this->logger->error($ex->getMessage()); -// } + $this->manager->publish($topic, $connection, $event); } } diff --git a/src/Util/Interfaces/RpcManagerInterface.php b/src/Util/Interfaces/RpcManagerInterface.php new file mode 100644 index 0000000..7ecea4b --- /dev/null +++ b/src/Util/Interfaces/RpcManagerInterface.php @@ -0,0 +1,35 @@ + + */ +class RpcManager extends WebsocketManager implements RpcManagerInterface +{ + /** @var EntityManager $em */ + protected $entityManager; + + /** @var LoggerInterface $logger */ + private $logger; + + /** + * MineseekerRpc constructor. + * + * @param EntityManagerInterface $entityManager + * @param LoggerInterface $logger + */ + public function __construct(EntityManagerInterface $entityManager, LoggerInterface $logger) + { + $this->entityManager = $this->reConnect($entityManager); + $this->logger = $logger; + + parent::__construct($logger); + } + + /** + * {@inheritDoc} + */ + public function getConnectInformation($params): string + { + $grid = $this->getGrid($params); + $users = null !== $grid ? $this->getUsers($params) : null; + + return base64_encode(json_encode(array( + 'grid' => $grid, + 'users' => $users + ), JSON_THROW_ON_ERROR, 512)); + } + + /** + * {@inheritDoc} + */ + public function saveGrid($data): bool + { + $playedGame = new PlayedGame(); + $grid = new Grid(); + $rows = json_decode(base64_decode($data[0]), true, 512, JSON_THROW_ON_ERROR); + + try { + foreach ($rows as $row) { + $gridRow = new GridRow(); + + $gridRow->setGridCol($row); + + /** Save Row */ + $gridRow->setGrid($grid); + $this->entityManager->persist($gridRow); + + } + + /** Save Grid */ + $grid->setPlayedGame($playedGame); + $this->entityManager->persist($grid); + + /** Save PlayedGame */ + $playedGame->setGameAssoc($data[1]); + $playedGame->setGrid($grid); + $playedGame->setCreated(new DateTime()); + $playedGame->setUpdated(new DateTime()); + $this->entityManager->persist($playedGame); + + $this->entityManager->flush(); + } catch (ORMException $e) { + $this->logger->error($e->getMessage()); + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + } + + return true; + } + + /** + * It gets the current Grid by PlayedGame/gameAssoc + * + * @param $gameAssoc + * + * @return array + */ + private function getGrid($gameAssoc): ?array + { + $gridCols = array(); + + try { + $this->entityManager->clear(); + + /** @var PlayedGame $playedGame */ + $playedGame = $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc); + + if (null === $playedGame) { + return null; + } + + if (null === $rows = $playedGame->getGrid()) { + return null; + } + + $rows = $rows->getGridRow(); + + /** @var GridRow $row */ + foreach ($rows as $row) { + $gridCols[] = $row->getGridCol(); + } + + return $gridCols; + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + } + + return null; + } + + /** + * Get the Users by PlayedGame + * + * @param $gameAssoc + * + * @return array + */ + private function getUsers($gameAssoc): array + { + return $this->getUserCollection( + $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc) + ); + } + + /** + * Get user collection from PlayedGame entity + * + * @param PlayedGame $playedGame + * + * @return array + */ + private function getUserCollection(PlayedGame $playedGame): array + { + return array( + 'red' => null !== $playedGame->getRed() ? $playedGame->getRed()->getUsername() : '', + 'blue' => null !== $playedGame->getBlue() ? $playedGame->getBlue()->getUsername() : '', + 'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '', + 'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : '' + ); + } +} diff --git a/src/Util/TopicManager.php b/src/Util/TopicManager.php new file mode 100644 index 0000000..4de56a6 --- /dev/null +++ b/src/Util/TopicManager.php @@ -0,0 +1,328 @@ + + */ +class TopicManager extends WebsocketManager implements TopicManagerInterface +{ + /** @var ClientManipulatorInterface $clientManipulator */ + protected $clientManipulator; + + /** @var EntityManagerInterface $entityManager */ + protected $entityManager; + + /** @var RequestStack $requestStack */ + protected $requestStack; + + /** @var LoggerInterface $logger */ + protected $logger; + + /** + * TopicManager constructor. + * + * @param ClientManipulatorInterface $clientManipulator + * @param EntityManagerInterface $entityManager + * @param RequestStack $requestStack + * @param LoggerInterface $logger + */ + public function __construct( + ClientManipulatorInterface $clientManipulator, + EntityManagerInterface $entityManager, + RequestStack $requestStack, + LoggerInterface $logger + ) + { + $this->clientManipulator = $clientManipulator; + $this->entityManager = $this->reConnect($entityManager); + $this->requestStack = $requestStack; + $this->logger = $logger; + + parent::__construct($logger); + } + + /** + * {@inheritDoc} + */ + public function subscribe(Topic $topic, ConnectionInterface $connection): void + { + /** 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->controlUsers($topic, $userName, $user); + + $topic->broadcast([ + 'userTopicId' => $connection->resourceId, + 'channel' => $topic->getId(), + 'user' => $userName, + 'userCnt' => $topic->count(), + 'users' => $users + ]); + } + } + + /** + * {@inheritDoc} + */ + public function unSubscribe(Topic $topic, ConnectionInterface $connection): void + { + /** This will broadcasts the message to ALL subscribers of this topic. */ + $topic->broadcast(array('msg' => $connection->resourceId . ' has left ' . $topic->getId())); + } + + /** + * {@inheritDoc} + */ + public function publish(Topic $topic, ConnectionInterface $connection, $event): void + { + $user = $this->clientManipulator->getClient($connection); + $userName = is_string($user) ? $user : $user->getUsername(); + + /** Save every step by user to db */ + null === $event['resign'] + ? $this->saveStepToDb($topic, $event) + : $this->saveResignToDb($topic, $event['resign']); + + $topic->broadcast([ + 'userTopicId' => $connection->resourceId, + 'channel' => $topic->getId(), + 'user' => $userName, + 'userCnt' => $topic->count(), + 'data' => $event + ]); + } + + /** + * Save Resign event to database + * + * @param $topic + * @param $color + */ + private function saveResignToDb(Topic $topic, $color): void + { + $gameAssoc = explode('/', $topic->getId())[2]; + + /** @var PlayedGame $playedGame */ + $playedGame = $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc); + + $playedGame->setResign($color); + + $this->entityManager->persist($playedGame); + $this->entityManager->flush(); + } + + /** + * Save steps and point information to database + * + * @param $topic + * @param $event + */ + private function saveStepToDb(Topic $topic, $event): void + { + try { + $gameAssoc = explode('/', $topic->getId())[2]; + + /** @var PlayedGame $playedGame */ + $playedGame = $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc); + + $step = new Step(); + + $step->setRow($event['coords'][0]); + $step->setCol($event['coords'][1]); + $step->setWBomb($event['bomb']); + $step->setPlayedGame($playedGame); + $step->setCreated(new DateTime()); + + $this->entityManager->persist($step); + + $playedGame->setBluePoints($event['bluePoints']); + $playedGame->setRedPoints($event['redPoints']); + $playedGame->setBlueExplodedBomb($event['blueExplodedBomb'] ? true : null); + $playedGame->setRedExplodedBomb($event['redExplodedBomb'] ? true : null); + $playedGame->setUpdated(new DateTime()); + + $this->entityManager->persist($playedGame); + + $this->entityManager->flush(); + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + } + } + + /** + * Control all users in a channel + * + * @param Topic $topic + * @param string $userName + * @param $user + * + * @return array + */ + private function controlUsers(Topic $topic, string $userName, $user): array + { + $gameAssoc = explode('/', $topic->getId())[2]; + + /** @var PlayedGame $playedGame */ + $playedGame = $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc); + + /** @var $users {array} */ + $users = $this->getUserCollection($playedGame); + + $red = '' !== $users['red'] || '' !== $users['redAnon'] ? 1 : 0; + $blue = '' !== $users['blue'] || '' !== $users['blueAnon'] ? 1 : 0; + $one = $topic->count() === 1; + $two = $topic->count() === 2; + + /** This checks it is a reconnection */ + if (($one && ($red + $blue === 0)) || ($two && ($red + $blue === 1))) { + /** @var $users {array} w/ save users to database */ + $users = $this->saveUserToDb($topic, $userName, $user, $topic->count()); + } + + return $users; + } + + /** + * Save user data to database + * + * @param $topic + * @param $userName + * @param $user + * @param $count + * + * @return array + */ + private function saveUserToDb(Topic $topic, string $userName, $user, $count) + { + $gameAssoc = explode('/', $topic->getId())[2]; + + /** @var PlayedGame $playedGame */ + $playedGame = $this->entityManager + ->getRepository(PlayedGame::class) + ->findOneByGameAssoc($gameAssoc); + + /** when the user is not anonym */ + !is_string($user) + ? $this->saveRegisteredUser($userName, $count, $playedGame) + : $this->saveAnonUser($userName, $count, $playedGame); + + $this->entityManager->persist($playedGame); + $this->entityManager->flush(); + + return $this->getUserCollection($playedGame); + } + + /** + * Saves the registered user to the database + * + * @param string $userName + * @param int $count + * @param PlayedGame $playedGame + */ + private function saveRegisteredUser(string $userName, int $count, PlayedGame $playedGame) + { + /** @var User $FOSUser */ + $user = $this->entityManager + ->getRepository(User::class) + ->findOneByUsername($userName); + + try { + if ($count === 1) { + /** @var $random {integer} Active player: red: 0, blue: 1 */ + $random = random_int(0, 1); + !$random ? $playedGame->setRed($user) : $playedGame->setBlue($user); + } else { + null === $playedGame->getRed() && null === $playedGame->getRedAnon() + ? $playedGame->setRed($user) + : $playedGame->setBlue($user); + } + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + } + } + + /** + * Save anonymous Gamer to database + * + * @param string $userName + * @param int $count + * @param PlayedGame $playedGame + */ + private function saveAnonUser(string $userName, int $count, PlayedGame $playedGame) + { + // $request = $this->requestStack->getCurrentRequest(); // TODO nem megy... + + try { + $anon = new Gamer(); + $anon->setUsername($userName); + $anon->setConnTimestamp(new DateTime()); + $this->entityManager->persist($anon); + + if ($count === 1) { + /** @var $random {integer} Active player: red: 0, blue: 1 */ + $random = random_int(0, 1); + !$random ? $playedGame->setRedAnon($anon) : $playedGame->setBlueAnon($anon); + } else { + null === $playedGame->getRed() && null === $playedGame->getRedAnon() + ? $playedGame->setRedAnon($anon) + : $playedGame->setBlueAnon($anon); + } + } catch (Exception $e) { + $this->logger->error($e->getMessage()); + } + } + + /** + * Get user collection from PlayedGame entity + * + * @param $playedGame + * + * @return array + */ + private function getUserCollection(PlayedGame $playedGame): array + { + return array( + 'red' => null !== $playedGame->getRed() ? $playedGame->getRed()->getUsername() : '', + 'blue' => null !== $playedGame->getBlue() ? $playedGame->getBlue()->getUsername() : '', + 'redAnon' => null !== $playedGame->getRedAnon() ? $playedGame->getRedAnon()->getUserName() : '', + 'blueAnon' => null !== $playedGame->getBlueAnon() ? $playedGame->getBlueAnon()->getUserName() : '' + ); + } +} diff --git a/src/Util/WebsocketManager.php b/src/Util/WebsocketManager.php new file mode 100644 index 0000000..59dc7b1 --- /dev/null +++ b/src/Util/WebsocketManager.php @@ -0,0 +1,57 @@ + + */ +class WebsocketManager implements WebsocketManagerInterface +{ + /** @var LoggerInterface $logger */ + private $logger; + + /** + * WebsocketManager constructor. + * + * @param LoggerInterface $logger + */ + public function __construct(LoggerInterface $logger) + { + $this->logger = $logger; + } + + /** + * {@inheritDoc} + */ + public function reConnect(EntityManagerInterface $entityManager): ?EntityManagerInterface + { + try { + $connection = $entityManager->getConnection(); + + if (false === $connection->ping()) { + $connection->close(); + $connection->connect(); + } + } catch (PDOException $e) { + $this->logger->error($e->getMessage()); + } + + return $entityManager; + } +}