Private
Public Access
1
0

chg: dev: refactor Rpc and Topic classes #3

This commit is contained in:
2019-10-27 18:51:28 +01:00
parent 8355cc90ed
commit 2daab7140e
8 changed files with 767 additions and 414 deletions

View File

@@ -1,34 +1,39 @@
<?php
/**
* This file is part of the SplendidBear Websites' projects.
*
* Copyright (c) 2019 @ www.splendidbear.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Rpc;
use App\Entity\Grid;
use App\Entity\GridRow;
use App\Entity\PlayedGame;
use Doctrine\DBAL\Driver\PDOException;
use Doctrine\ORM\EntityManager;
use Psr\Log\LoggerInterface;
use App\Util\RpcManager;
use Ratchet\ConnectionInterface;
use Gos\Bundle\WebSocketBundle\RPC\RpcInterface;
use Gos\Bundle\WebSocketBundle\Router\WampRequest;
/**
* Class MineseekerRpc
*
* @package App\Rpc
* @author system7 <https://www.splendidbear.org>
*/
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);
}
}