2026-04-09 20:21:01 +02:00
|
|
|
<?php declare(strict_types=1);
|
2019-10-27 18:51:28 +01:00
|
|
|
/**
|
|
|
|
|
* 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;
|
2026-04-09 20:21:01 +02:00
|
|
|
use App\Interfaces\RpcManagerInterface;
|
2019-10-27 18:51:28 +01:00
|
|
|
use DateTime;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Exception;
|
2026-04-09 22:00:53 +02:00
|
|
|
use JsonException;
|
2019-10-27 18:51:28 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2026-04-09 22:00:53 +02:00
|
|
|
use RuntimeException;
|
2019-10-27 18:51:28 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class RpcManager
|
|
|
|
|
*
|
2026-04-09 20:21:01 +02:00
|
|
|
* @package App\Util
|
|
|
|
|
* @author Lang <https://www.splendidbear.org>
|
|
|
|
|
* @category Class
|
|
|
|
|
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
|
|
|
|
|
* @link www.splendidbear.org
|
|
|
|
|
* @since 2026. 04. 09.
|
2019-10-27 18:51:28 +01:00
|
|
|
*/
|
2026-04-09 22:00:53 +02:00
|
|
|
class RpcManager implements RpcManagerInterface
|
2019-10-27 18:51:28 +01:00
|
|
|
{
|
2026-04-09 20:21:01 +02:00
|
|
|
public function __construct(
|
2026-04-09 22:00:53 +02:00
|
|
|
private readonly EntityManagerInterface $entityManager,
|
|
|
|
|
private readonly LoggerInterface $logger,
|
2026-04-09 20:21:01 +02:00
|
|
|
) {
|
2019-10-27 18:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getConnectInformation($params): string
|
|
|
|
|
{
|
2026-04-09 12:10:37 +02:00
|
|
|
$gameAssoc = is_array($params) ? $params[0] : $params;
|
|
|
|
|
$grid = $this->getGrid($gameAssoc);
|
|
|
|
|
$users = null !== $grid ? $this->getUsers($gameAssoc) : null;
|
2019-10-27 18:51:28 +01:00
|
|
|
|
2026-04-09 22:00:53 +02:00
|
|
|
try {
|
|
|
|
|
return base64_encode(json_encode([
|
|
|
|
|
'grid' => $grid,
|
|
|
|
|
'users' => $users,
|
|
|
|
|
], JSON_THROW_ON_ERROR, 512));
|
|
|
|
|
} catch (JsonException $e) {
|
|
|
|
|
throw new RuntimeException($e->getMessage());
|
|
|
|
|
}
|
2019-10-27 18:51:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function saveGrid($data): bool
|
|
|
|
|
{
|
2026-04-09 22:00:53 +02:00
|
|
|
$existingGame = $this->entityManager
|
|
|
|
|
->getRepository(PlayedGame::class)
|
|
|
|
|
->findOneByGameAssoc($data[1]);
|
|
|
|
|
|
|
|
|
|
if (null !== $existingGame) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-27 18:51:28 +01:00
|
|
|
$playedGame = new PlayedGame();
|
|
|
|
|
$grid = new Grid();
|
2026-04-09 22:00:53 +02:00
|
|
|
try {
|
|
|
|
|
$rows = json_decode(base64_decode($data[0]), true, 512, JSON_THROW_ON_ERROR);
|
|
|
|
|
} catch (JsonException $e) {
|
|
|
|
|
throw new RuntimeException($e->getMessage());
|
|
|
|
|
}
|
2019-10-27 18:51:28 +01:00
|
|
|
|
|
|
|
|
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 (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
|
|
|
|
|
{
|
2026-04-09 20:21:01 +02:00
|
|
|
return [
|
|
|
|
|
'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() : '',
|
|
|
|
|
];
|
2019-10-27 18:51:28 +01:00
|
|
|
}
|
|
|
|
|
}
|