src/Controller/GameController.php line 55

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Category;
  4. use App\Entity\Game;
  5. use App\Entity\GameComment;
  6. use App\Entity\User;
  7. use App\Entity\UserGameConfig;
  8. use App\Entity\UserTrophy;
  9. use App\Entity\WeeklyHero;
  10. use App\Entity\Word;
  11. use Doctrine\Persistence\ManagerRegistry;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. #[Route('/{_locale}')]
  17. class GameController extends CachedController
  18. {
  19.     private $doctrine;
  20.     private $entityManager;
  21.     
  22.     public function __construct(ManagerRegistry $doctrine)
  23.     {
  24.         $this->doctrine $doctrine;
  25.         $this->entityManager $this->doctrine->getManager();
  26.     }
  27.     #[Route('/tous-les-jeux.html'name'front_all_games')]
  28.     public function list(): Response
  29.     {
  30.         /**
  31.          * TODO redirect vers display_game si non connecté
  32.          */
  33.         $categories $this->doctrine->getRepository(Category::class)->findAll();
  34.         if($categories)
  35.         {
  36.             return $this->render('games/list_categories.html.twig', [
  37.                 'categories' => $categories,
  38.             ]);
  39.         } else {
  40.             //throw error
  41.         }
  42.         
  43.     }
  44.     
  45.     #[Route('/theme-{slug}.html'name'front_theme_games')]
  46.     #[Route('/classe-{slug}.html'name'front_class_games')]
  47.     #[Route('/categorie-{slug}.html'name'front_category_games')]
  48.     public function categoryGames(Request $requeststring $slug): Response
  49.     {
  50.         /**
  51.          * TODO redirect vers display_game si non connecté
  52.          */
  53.         $user $this->getUser();
  54.         $category $this->doctrine->getRepository(Category::class)->findOneBySlug($slug);
  55.         if(!$category) {
  56.         }
  57.         $sortFree $user != null && !$user->isSubscribed();
  58.         $locale $request->getLocale();
  59.         $learningLocale 'en';
  60.         
  61.         $gameRepository $this->doctrine->getRepository(Game::class);
  62.         $games $gameRepository->getAllPublishedGames($locale$learningLocale$category);
  63.         
  64.         $isTheme $category->getType() == 'theme' 0;
  65.         usort($games, array( $this "free_sort" ));
  66.         $nbGames count($games);
  67.         $nbPerPage 12;
  68.         $nbPages ceil($nbGames $nbPerPage);
  69.         return $this->render('games/list.html.twig', [
  70.             'user' => $user,
  71.             'games' => $games,
  72.             'category' => $category,
  73.             'nbPerPage' => $nbPerPage,
  74.             'nbPages' => $nbPages,
  75.             'isTheme' => $isTheme
  76.         ]);
  77.     }
  78.     
  79.     public function free_sort($a$b){
  80.         
  81.         if($a->getFree() && !$b->getFree()){
  82.             return -1;
  83.         }elseif(!$a->getFree() && $b->getFree()){
  84.             return 1;
  85.         }else{
  86.             return strcmp($a->getTitle(),$b->getTitle()) < ?  -1;
  87.         }
  88.     }
  89.     
  90.     #[Route('/joue-avec-{slug}.html'name'front_hero_games')]
  91.     public function listHeroGames(string $slug): Response
  92.     {
  93.         $hero $this->doctrine->getRepository(WeeklyHero::class)->findOneBySlug($slug);
  94.         if ($hero == null)
  95.             $this->redirectToRoute('front_all_games');
  96.         $wordsNb $this->doctrine->getRepository(Word::class)->getRoundedCount(50);
  97.         $games $this->doctrine->getRepository(Game::class)->getGamesByHero($hero->getId());
  98.         return $this->render('games/list_hero_games.html.twig', [
  99.             'games' => $games
  100.             'hero' => $hero
  101.             'wordsNb' => $wordsNb
  102.         ]);
  103.     }
  104.     #[Route('/jeu-anglais-{slug}.html'name'play_game')]
  105.     public function game(string $slug): Response
  106.     {
  107.         /**
  108.          * TODO redirect vers display_game si non connecté
  109.          */
  110.         $user $this->getUser();
  111.         if (!$user
  112.         {
  113.             return $this->redirectToRoute('display_game', ['slug' => $slug]);
  114.         }
  115.         $response = new Response();
  116.         $response->headers->set('Access-Control-Allow-Origin''*');
  117.         $game $this->doctrine->getRepository(Game::class)->findOneBy(['canonicalTitle' => $slug]);
  118.         if($game)
  119.         {
  120.             if(!$user->isSubscribed() && !$game->getFree())
  121.             {
  122.                 return $this->redirectToRoute('display_game', ['slug' => $slug]);
  123.             } else {
  124.                 return $this->render('games/play.html.twig', [
  125.                     'game' => $game,
  126.                 ], $response);
  127.             }
  128.             
  129.         } else {
  130.             //throw error
  131.         }
  132.         
  133.     }
  134.     #[Route('/decouvre-jeu-anglais-{slug}.html'name'display_game')]
  135.     public function displayGame(string $slug): Response
  136.     {
  137.         $user $this->getUser();
  138.         if ($user instanceof User && $user->isSubscribed()) 
  139.         {
  140.             return $this->redirectToRoute('play_game', ['slug' => $slug]);
  141.         }
  142.         /**
  143.          * TODO redirect vers play_game si connecté
  144.          */
  145.         
  146.          $response = new Response();
  147.          $response->headers->set('Access-Control-Allow-Origin''*');
  148.         $game $this->doctrine->getRepository(Game::class)->findOneBy(['canonicalTitle' => $slug]);
  149.         if($game)
  150.         {
  151.             return $this->render('games/display.html.twig', [
  152.                 'game' => $game,
  153.             ], $response);
  154.         } else {
  155.             //throw error
  156.         }
  157.     }
  158.     #[Route('/game/gameDescription{gameId}.xml'name'front_game_description')]
  159.     public function xmlDescription(int $gameIdRequest $request): Response
  160.     {
  161.         $app false;
  162.         return $this->gameDescription($gameId$app$request);
  163.     }
  164.     public function gameDescription($gameId$appRequest $request)
  165.     {
  166.         //$gameUpdate = $this->doctrine->getRepository(Game::class)->getGameUpdate($gameId);
  167.         //if ($gameUpdate == null)
  168.         //    throw new NotFoundHttpException('xmlDescriptionAction game not found');
  169.         //MANAGE A CACHE VERSION
  170.         
  171.         $game $this->doctrine->getRepository(Game::class)->findOneById($gameId);
  172.         $response $this->getCachedResponse(array('contentType' => 'xml''lastModif' => $game->getUpdatedAt(), 'maxAge' => 0));
  173.         if (!$this->needUpdate($request$response))
  174.             return $response;
  175.         if ($game == null)
  176.             throw new NotFoundHttpException('xmlDescriptionAction game not found');
  177.         //Prepare sound definitions
  178.         $soundDefinitions $game->getSoundDefinitions();
  179.         $response->setLastModified($game->getLastModif());
  180.         //Prepare static files url
  181.         /*if ($request->isSecure())
  182.             $protocol = "https://";
  183.         else
  184.             $protocol = "http://";*/
  185.         $protocol "https://";
  186.         $siteUrl $protocol.$request->getHttpHost();
  187.         $staticFilesUrl $this->getParameter('app.cdn_ssl_url');
  188.         $hasCDN true;
  189.         if (!$staticFilesUrl || $this->getParameter('kernel.environment') == 'dev') {
  190.             $staticFilesUrl $siteUrl;
  191.             $hasCDN false;
  192.         }
  193.         $suggestions = array();
  194.         $words = array();
  195.         $releaseVersion $this->getParameter('release_version');
  196.         return $this->render('games/xmlDescription.xml.twig', [
  197.                     'game' => $game'soundDefinitions' => $soundDefinitions'words' => $words,
  198.                     'hasCDN' => $hasCDN'siteUrl' => $siteUrl'staticFilesUrl' => $staticFilesUrl,
  199.                     'suggestions' => $suggestions'releaseVersion' => $releaseVersion,
  200.                     'appConfig' => $app
  201.                 ], $response);
  202.     }
  203.     #[Route('/game/userGameConfig{gameId}.xml'name'front_user_game_config')]
  204.     public function userGameConfig(int $gameId): Response
  205.     {
  206.         $user $this->getUser();
  207.         
  208.         $response $this->getCachedResponse(array('private' => true'noCache' => true));
  209.         if (!($user instanceof User)) {
  210.             $response->setContent('ko');
  211.             return $response;
  212.         }
  213.         $userConfig $this->doctrine->getRepository(UserGameConfig::class)->getUserGameConfig($user->getId(), $gameId);
  214.         $trophies $this->doctrine->getRepository(UserTrophy::class)->getUserGameTrophies($user->getId(), $gameId);
  215.         $avatarId $user->getAvatar() ? $user->getAvatar()->getId() : null;
  216.         $userSubscribed $user->isSubscribed();
  217.         $response->headers->set('Content-Type''application/xml');
  218.         return $this->render('games/userConfig.xml.twig', [
  219.             'user' => $user,
  220.             'userConfig' => $userConfig,
  221.             'trophies' => $trophies,
  222.             'userSpeakos' => $user->getSpeakos(), 
  223.             'avatarId' => $avatarId,
  224.             'challengeDatas' => null,
  225.             'userSubscribed' => $userSubscribed,
  226.         ], $response);
  227.     }
  228.     
  229.     
  230.     #[Route('/game/comments-{gameId}.html'name'game_comments')]
  231.     public function gameComments($gameIdRequest $request)
  232.     {
  233.         
  234.         $game $this->doctrine->getRepository(Game::class)->findOneById($gameId);
  235.         $response $this->render('games/comments.html.twig', [
  236.             'game' => $game
  237.         ]);
  238.         $response->setPublic();
  239.         $response->setMaxAge(600);
  240.         return $response;
  241.     }
  242.     
  243.     #[Route('/gameComments{gameId}.{_format}'name'front_game_comments')]
  244.     public function showComments($gameId$_formatRequest $request) {
  245.         //Manage cached response
  246.         $commentsRepository $this->doctrine->getRepository(GameComment::class);
  247.         $lastPost $commentsRepository->getLastPostDate($gameId);
  248.         $response $this->getCachedResponse(array('lastModif' => $lastPost'maxAge' => 0));
  249.         if (!$this->needUpdate($request$response))
  250.             return $response;
  251.         $comments $commentsRepository->getValidComments($gameId60);
  252.         if ($_format == 'html')
  253.             return $this->render('games/comments/showComments.html.twig', array('gameId' => $gameId'comments' => $comments), $response);
  254.         else if ($_format == 'json') {
  255.             return $this->render('games/comments/showComments.json.twig', array('gameId' => $gameId'validAnswers' => true'comments' => $comments), $response);
  256.         }
  257.     }
  258.     
  259.     #[Route('/waitingComments{gameId}.{_format}'name'front_waiting_game_comments')]
  260.     public function showWaitingComments($gameId$_format) {
  261.         $response $this->getCachedResponse(array('private' => true'maxAge' => 0'sharedMaxAge' => 0));
  262.         $user $this->getUser();
  263.         if ($user instanceof User) {
  264.             $waitingComments $this->doctrine->getRepository(GameComment::class)->getWaitingComments($gameId$user->getId());
  265.             $incorrectComments $this->doctrine->getRepository(GameComment::class)->getIncorrectComments($gameId$user->getId());
  266.             if ($_format == 'html')
  267.                 return $this->render('games/comments/waitingComments.html.twig',
  268.                         array('gameId' => $gameId'waitingComments' => $waitingComments'incorrectComments' => $incorrectComments), $response);
  269.             else if ($_format == 'json') {
  270.                 //$response->headers->set('Content-Type', 'application/json');
  271.                 $comments = array();
  272.                 foreach ($waitingComments as $comment)
  273.                     $comments[] = $comment;
  274.                 foreach ($incorrectComments as $comment)
  275.                     $comments[] = $comment;
  276.                 return $this->render('games/comments/showComments.json.twig',
  277.                         array('gameId' => $gameId'validAnswers' => false'comments' => $comments), $response);
  278.             }
  279.         }
  280.         $response->setContent('ko');
  281.         return $response;
  282.     }
  283.     
  284.     #[Route('/postGameComment.json'name'front_post_game_comment')]
  285.     public function postComment(Request $request) {
  286.         $user $this->getUser();
  287.         if ($user instanceof User) {
  288.             $content $request->get('post'null);
  289.             $content preg_replace('/\s{2,}/'' '$content);
  290.         
  291.             $gameId $request->get('gameId'null);
  292.             $postId $request->get('postId'null);
  293.             $parentId $request->get('parentId'null);
  294.             if ($content && $gameId) {
  295.                 if ($postId) {
  296.                     $comment $this->doctrine->getRepository(GameComment::class)->findOneById($postId);
  297.                     if ($comment) {
  298.                         $comment->setComment($content);
  299.                         $comment->setStatus(GameComment::STATUS_WAITING);
  300.                     } else
  301.                         return new Response('ko');
  302.                 } else {
  303.                     $game $this->doctrine->getRepository(Game::class)->findOneById($gameId);
  304.                     if ($game) {
  305.                         $comment = new GameComment();
  306.                         $comment->setComment($content);
  307.                         $comment->setUser($user);
  308.                         $comment->setGame($game);
  309.                     } else
  310.                         return new Response('{ "status": "ko" }');
  311.                 }
  312.                 $em $this->doctrine->getManager();
  313.                 if ($parentId) {
  314.                     $parentComment $this->doctrine->getRepository(GameComment::class)->findOneById($parentId);
  315.                     $comment->setParent($parentComment);
  316.                 }
  317.                 $comment->setLastModif(new \DateTimeImmutable());
  318.                 $em->persist($comment);
  319.                 $em->flush();
  320.                 $response = new Response();
  321.                 $response->headers->set('Content-Type''application/json');
  322.                 $response->setContent('{ "status": "ok", "commentId": '.$comment->getId().'}');
  323.                 return $response;
  324.             }
  325.         }
  326.         return new Response('{ "status": "ko" }');
  327.     }
  328.     
  329. }