chg: dev: refactor the code - there was unnecessary codes and wrongly formatted or designed code that are related to Repositories #7
This commit is contained in:
@@ -15,7 +15,6 @@ use App\Entity\GridRow;
|
||||
use App\Entity\PlayedGame;
|
||||
use App\Entity\Step;
|
||||
use App\Interfaces\RpcManagerInterface;
|
||||
use App\Repository\PlayedGameRepository;
|
||||
use App\Repository\StepRepository;
|
||||
use DateTime;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
@@ -45,7 +44,6 @@ class RpcManager implements RpcManagerInterface
|
||||
public function __construct(
|
||||
private readonly EntityManagerInterface $em,
|
||||
private readonly LoggerInterface $logger,
|
||||
private readonly PlayedGameRepository $playedGameRepository,
|
||||
private readonly StepRepository $stepRepository,
|
||||
) {
|
||||
}
|
||||
@@ -54,7 +52,7 @@ class RpcManager implements RpcManagerInterface
|
||||
{
|
||||
$gameAssoc = is_array($params) ? $params[0] : $params;
|
||||
|
||||
$playedGame = $this->playedGameRepository->findOneByGameAssoc($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
if (null === $playedGame) {
|
||||
try {
|
||||
@@ -118,7 +116,7 @@ class RpcManager implements RpcManagerInterface
|
||||
|
||||
public function saveGrid(string $gameAssoc): bool
|
||||
{
|
||||
$existingGame = $this->playedGameRepository->findOneByGameAssoc($gameAssoc);
|
||||
$existingGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
if (null !== $existingGame) {
|
||||
return true;
|
||||
@@ -128,29 +126,25 @@ class RpcManager implements RpcManagerInterface
|
||||
$playedGame = new PlayedGame();
|
||||
$grid = new Grid();
|
||||
|
||||
try {
|
||||
foreach ($grid2d as $row) {
|
||||
$gridRow = new GridRow();
|
||||
$gridRow->gridCol = $row;
|
||||
$gridRow->grid = $grid;
|
||||
$this->em->persist($gridRow);
|
||||
}
|
||||
|
||||
$grid->playedGame = $playedGame;
|
||||
$this->em->persist($grid);
|
||||
|
||||
$playedGame->gameAssoc = $gameAssoc;
|
||||
$playedGame->uuid = Uuid::fromString($gameAssoc);
|
||||
$playedGame->grid = $grid;
|
||||
$playedGame->created = new DateTime();
|
||||
$playedGame->updated = new DateTime();
|
||||
$this->em->persist($playedGame);
|
||||
|
||||
$this->em->flush();
|
||||
} catch (Exception $e) {
|
||||
$this->logger->error($e->getMessage());
|
||||
foreach ($grid2d as $row) {
|
||||
$gridRow = new GridRow();
|
||||
$gridRow->gridCol = $row;
|
||||
$gridRow->grid = $grid;
|
||||
$this->em->persist($gridRow);
|
||||
}
|
||||
|
||||
$grid->playedGame = $playedGame;
|
||||
$this->em->persist($grid);
|
||||
|
||||
$playedGame->gameAssoc = $gameAssoc;
|
||||
$playedGame->uuid = Uuid::fromString($gameAssoc);
|
||||
$playedGame->grid = $grid;
|
||||
$playedGame->created = new DateTime();
|
||||
$playedGame->updated = new DateTime();
|
||||
$this->em->persist($playedGame);
|
||||
|
||||
$this->em->flush();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,6 @@ use App\Entity\PlayedGame;
|
||||
use App\Entity\Step;
|
||||
use App\Entity\User;
|
||||
use App\Interfaces\TopicManagerInterface;
|
||||
use App\Repository\PlayedGameRepository;
|
||||
use App\Repository\UserRepository;
|
||||
use DateTime;
|
||||
use DateTimeInterface;
|
||||
@@ -48,7 +47,6 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
private HubInterface $hub,
|
||||
private LoggerInterface $logger,
|
||||
private CacheManager $cacheManager,
|
||||
private PlayedGameRepository $playedGameRepository,
|
||||
private UserRepository $userRepository,
|
||||
private RequestStack $requestStack,
|
||||
private Security $security,
|
||||
@@ -57,7 +55,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
|
||||
public function subscribe(string $gameAssoc, string $userName): void
|
||||
{
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
if (null === $playedGame) {
|
||||
return;
|
||||
@@ -120,7 +118,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
{
|
||||
// If the game was still waiting for a second player, stamp it as abandoned
|
||||
// so it no longer appears in the waiting-games query, and remove from lobby.
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
if (null !== $playedGame) {
|
||||
$users = $this->getUserCollection($playedGame);
|
||||
if ($this->getPlayerCount($users) === 1) {
|
||||
@@ -148,7 +146,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
if (null !== $event['resign']) {
|
||||
$this->saveResignToDb($gameAssoc, $event['resign']);
|
||||
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
$users = $this->getUserCollection($playedGame);
|
||||
$count = $this->getPlayerCount($users);
|
||||
$topic = 'mineseeker/channel/' . $gameAssoc;
|
||||
@@ -177,7 +175,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
$player = $event['player']; // 'red' | 'blue'
|
||||
$isBomb = (bool)$event['bomb'];
|
||||
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
$grid = $this->loadGrid($gameAssoc);
|
||||
|
||||
/** Cells already revealed by previous steps (as "row,col" => true map) */
|
||||
@@ -267,7 +265,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
/** Load the grid rows from the database as a 2-D array. */
|
||||
private function loadGrid(string $gameAssoc): array
|
||||
{
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
$gridEntity = $playedGame?->grid;
|
||||
|
||||
if (null === $gridEntity) {
|
||||
@@ -569,11 +567,6 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
return $mines;
|
||||
}
|
||||
|
||||
private function getPlayedGame(string $gameAssoc): ?PlayedGame
|
||||
{
|
||||
return $this->playedGameRepository->findOneByGameAssoc($gameAssoc);
|
||||
}
|
||||
|
||||
private function getPlayerCount(array $users): int
|
||||
{
|
||||
$red = '' !== $users['red'] || '' !== $users['redAnon'] ? 1 : 0;
|
||||
@@ -584,7 +577,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
|
||||
private function saveResignToDb(string $gameAssoc, string $color): void
|
||||
{
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
$playedGame->resign = $color;
|
||||
$this->em->persist($playedGame);
|
||||
$this->em->flush();
|
||||
@@ -600,7 +593,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
array $bonusData = []
|
||||
): void {
|
||||
try {
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
$step = new Step();
|
||||
$step->row = $event['coords'][0];
|
||||
@@ -640,7 +633,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
|
||||
private function saveUserToDb(string $gameAssoc, string $userName, int $count): array
|
||||
{
|
||||
$playedGame = $this->getPlayedGame($gameAssoc);
|
||||
$playedGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($gameAssoc);
|
||||
|
||||
null !== $this->security->getUser()
|
||||
? $this->saveRegisteredUser($userName, $count, $playedGame)
|
||||
@@ -721,7 +714,7 @@ readonly class TopicManager implements TopicManagerInterface
|
||||
|
||||
public function publishChallenge(string $targetGameAssoc, string $challengerGameAssoc): void
|
||||
{
|
||||
$challengerGame = $this->getPlayedGame($challengerGameAssoc);
|
||||
$challengerGame = $this->em->getRepository(PlayedGame::class)->findOneByGameAssoc($challengerGameAssoc);
|
||||
$challengerName = 'Unknown';
|
||||
|
||||
if (null !== $challengerGame) {
|
||||
|
||||
Reference in New Issue
Block a user