Generate grid
This commit is contained in:
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Repository\ActorRepository;
|
||||
use App\Repository\MovieRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -14,24 +15,60 @@ use Symfony\Component\Serializer\SerializerInterface;
|
||||
class HomepageController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MovieRepository $movieRepository,
|
||||
private readonly TMDBGateway $TMDBGateway,
|
||||
private readonly ActorRepository $actorRepository
|
||||
) {}
|
||||
|
||||
#[Route('/')]
|
||||
public function index(SerializerInterface $serializer): Response
|
||||
{
|
||||
$movie = $this->movieRepository->findOneBy([]);
|
||||
$creditsContext = $this->TMDBGateway->getMovieCredits($movie->getTmdbId());
|
||||
$cast = $creditsContext->cast;
|
||||
$actors = [];
|
||||
foreach ($cast as $actor) {
|
||||
if (2 <= $actor->popularity) {
|
||||
$actors[] = $actor;
|
||||
}
|
||||
}
|
||||
dd($actors);
|
||||
// Final actor to be guessed
|
||||
$mainActor = $this->actorRepository->findOneRandom(4);
|
||||
|
||||
return $this->render('homepage/index.html.twig');
|
||||
// Actors for the grid
|
||||
$actors = [];
|
||||
$leftSize = 0;
|
||||
$rightSize = 0;
|
||||
foreach (str_split(strtolower($mainActor->getName())) as $char) {
|
||||
if (!preg_match('/[a-z]/', $char)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$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'];
|
||||
}
|
||||
|
||||
$rightSizeActor = strlen($actor->getName()) - $actorData['pos'] - 1;
|
||||
if ($rightSize < $rightSizeActor) {
|
||||
$rightSize = $rightSizeActor;
|
||||
}
|
||||
|
||||
$actors[] = $actorData;
|
||||
}
|
||||
|
||||
// Predict grid size
|
||||
$width = $rightSize + $leftSize + 1;
|
||||
$middle = $leftSize;
|
||||
|
||||
return $this->render('homepage/index.html.twig', [
|
||||
'mainActor' => $mainActor,
|
||||
'actors' => $actors,
|
||||
'width' => $width,
|
||||
'middle' => $middle,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user