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