Private
Public Access
1
0
Files
MineSeeker/src/Util/RpcManager.php

184 lines
4.8 KiB
PHP
Raw Normal View History

<?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\Util;
use App\Entity\Grid;
use App\Entity\GridRow;
use App\Entity\PlayedGame;
use App\Util\Interfaces\RpcManagerInterface;
use DateTime;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\ORMException;
use Exception;
use Psr\Log\LoggerInterface;
/**
* Class RpcManager
*
* @package App\Utils
* @author system7 <https://www.splendidbear.org>
*/
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
{
$gameAssoc = is_array($params) ? $params[0] : $params;
$grid = $this->getGrid($gameAssoc);
$users = null !== $grid ? $this->getUsers($gameAssoc) : 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() : ''
);
}
}