refactor: use persisted games in HomepageController
This commit is contained in:
@@ -4,77 +4,54 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Gateway\TMDBGateway;
|
use App\Entity\Game;
|
||||||
use App\Repository\ActorRepository;
|
use App\Entity\User;
|
||||||
use App\Repository\MovieRepository;
|
use App\Repository\GameRepository;
|
||||||
|
use App\Service\GameGridGenerator;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
use Symfony\Component\Serializer\SerializerInterface;
|
|
||||||
|
|
||||||
class HomepageController extends AbstractController
|
class HomepageController extends AbstractController
|
||||||
{
|
{
|
||||||
public function __construct(
|
|
||||||
private readonly ActorRepository $actorRepository
|
|
||||||
) {}
|
|
||||||
|
|
||||||
#[Route('/', name: 'app_homepage')]
|
#[Route('/', name: 'app_homepage')]
|
||||||
public function index(SerializerInterface $serializer): Response
|
public function index(
|
||||||
{
|
Request $request,
|
||||||
// Final actor to be guessed
|
GameRepository $gameRepository,
|
||||||
$mainActor = $this->actorRepository->findOneRandom(4);
|
GameGridGenerator $gridGenerator,
|
||||||
|
): Response {
|
||||||
|
/** @var User|null $user */
|
||||||
|
$user = $this->getUser();
|
||||||
|
|
||||||
// Actors for the grid
|
$game = null;
|
||||||
$actors = [];
|
|
||||||
$leftSize = 0;
|
if ($user) {
|
||||||
$rightSize = 0;
|
$game = $gameRepository->findActiveForUser($user);
|
||||||
foreach (str_split(strtolower($mainActor->getName())) as $char) {
|
} else {
|
||||||
if (!preg_match('/[a-z]/', $char)) {
|
$gameId = $request->getSession()->get('current_game_id');
|
||||||
continue;
|
if ($gameId) {
|
||||||
|
$game = $gameRepository->find($gameId);
|
||||||
|
if (!$game || $game->getStatus() !== Game::STATUS_IN_PROGRESS) {
|
||||||
|
$request->getSession()->remove('current_game_id');
|
||||||
|
$game = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tryFindActor = 0;
|
if (!$game) {
|
||||||
do {
|
return $this->render('homepage/index.html.twig', [
|
||||||
$actor = $this->actorRepository->findOneRandom(4, $char);
|
'game' => null,
|
||||||
++$tryFindActor;
|
]);
|
||||||
} while (
|
|
||||||
$actor === $mainActor
|
|
||||||
|| in_array($actor, array_map(fn ($actorMap) => $actorMap['actor'], $actors))
|
|
||||||
|| $tryFindActor < 5
|
|
||||||
);
|
|
||||||
|
|
||||||
$actorData = [
|
|
||||||
'actor' => $actor,
|
|
||||||
'pos' => strpos($actor->getName(), $char),
|
|
||||||
];
|
|
||||||
|
|
||||||
if ($leftSize < $actorData['pos']) {
|
|
||||||
$leftSize = $actorData['pos'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rightSizeActor = strlen($actor->getName()) - $actorData['pos'] - 1;
|
$gridData = $gridGenerator->computeGridData($game);
|
||||||
if ($rightSize < $rightSizeActor) {
|
|
||||||
$rightSize = $rightSizeActor;
|
|
||||||
}
|
|
||||||
|
|
||||||
$actors[] = $actorData;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Predict grid size
|
|
||||||
$width = $rightSize + $leftSize + 1;
|
|
||||||
$middle = $leftSize;
|
|
||||||
|
|
||||||
// Build JSON-serializable grid for React
|
|
||||||
$grid = array_map(fn (array $actorData) => [
|
|
||||||
'actorName' => $actorData['actor']->getName(),
|
|
||||||
'actorId' => $actorData['actor']->getId(),
|
|
||||||
'pos' => $actorData['pos'],
|
|
||||||
], $actors);
|
|
||||||
|
|
||||||
return $this->render('homepage/index.html.twig', [
|
return $this->render('homepage/index.html.twig', [
|
||||||
'grid' => $grid,
|
'game' => $game,
|
||||||
'width' => $width,
|
'grid' => $gridData['grid'],
|
||||||
'middle' => $middle,
|
'width' => $gridData['width'],
|
||||||
|
'middle' => $gridData['middle'],
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user