src/Controller/GameController.php line 55
<?php
namespace App\Controller;
use App\Entity\Category;
use App\Entity\Game;
use App\Entity\GameComment;
use App\Entity\User;
use App\Entity\UserGameConfig;
use App\Entity\UserTrophy;
use App\Entity\WeeklyHero;
use App\Entity\Word;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
#[Route('/{_locale}')]
class GameController extends CachedController
{
private $doctrine;
private $entityManager;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
$this->entityManager = $this->doctrine->getManager();
}
#[Route('/tous-les-jeux.html', name: 'front_all_games')]
public function list(): Response
{
/**
* TODO redirect vers display_game si non connecté
*/
$categories = $this->doctrine->getRepository(Category::class)->findAll();
if($categories)
{
return $this->render('games/list_categories.html.twig', [
'categories' => $categories,
]);
} else {
//throw error
}
}
#[Route('/theme-{slug}.html', name: 'front_theme_games')]
#[Route('/classe-{slug}.html', name: 'front_class_games')]
#[Route('/categorie-{slug}.html', name: 'front_category_games')]
public function categoryGames(Request $request, string $slug): Response
{
/**
* TODO redirect vers display_game si non connecté
*/
$user = $this->getUser();
$category = $this->doctrine->getRepository(Category::class)->findOneBySlug($slug);
if(!$category) {
}
$sortFree = $user != null && !$user->isSubscribed();
$locale = $request->getLocale();
$learningLocale = 'en';
$gameRepository = $this->doctrine->getRepository(Game::class);
$games = $gameRepository->getAllPublishedGames($locale, $learningLocale, $category);
$isTheme = $category->getType() == 'theme' ? 1 : 0;
usort($games, array( $this , "free_sort" ));
$nbGames = count($games);
$nbPerPage = 12;
$nbPages = ceil($nbGames / $nbPerPage);
return $this->render('games/list.html.twig', [
'user' => $user,
'games' => $games,
'category' => $category,
'nbPerPage' => $nbPerPage,
'nbPages' => $nbPages,
'isTheme' => $isTheme
]);
}
public function free_sort($a, $b){
if($a->getFree() && !$b->getFree()){
return -1;
}elseif(!$a->getFree() && $b->getFree()){
return 1;
}else{
return strcmp($a->getTitle(),$b->getTitle()) < 0 ? -1 : 1;
}
}
#[Route('/joue-avec-{slug}.html', name: 'front_hero_games')]
public function listHeroGames(string $slug): Response
{
$hero = $this->doctrine->getRepository(WeeklyHero::class)->findOneBySlug($slug);
if ($hero == null)
$this->redirectToRoute('front_all_games');
$wordsNb = $this->doctrine->getRepository(Word::class)->getRoundedCount(50);
$games = $this->doctrine->getRepository(Game::class)->getGamesByHero($hero->getId());
return $this->render('games/list_hero_games.html.twig', [
'games' => $games,
'hero' => $hero,
'wordsNb' => $wordsNb
]);
}
#[Route('/jeu-anglais-{slug}.html', name: 'play_game')]
public function game(string $slug): Response
{
/**
* TODO redirect vers display_game si non connecté
*/
$user = $this->getUser();
if (!$user)
{
return $this->redirectToRoute('display_game', ['slug' => $slug]);
}
$response = new Response();
$response->headers->set('Access-Control-Allow-Origin', '*');
$game = $this->doctrine->getRepository(Game::class)->findOneBy(['canonicalTitle' => $slug]);
if($game)
{
if(!$user->isSubscribed() && !$game->getFree())
{
return $this->redirectToRoute('display_game', ['slug' => $slug]);
} else {
return $this->render('games/play.html.twig', [
'game' => $game,
], $response);
}
} else {
//throw error
}
}
#[Route('/decouvre-jeu-anglais-{slug}.html', name: 'display_game')]
public function displayGame(string $slug): Response
{
$user = $this->getUser();
if ($user instanceof User && $user->isSubscribed())
{
return $this->redirectToRoute('play_game', ['slug' => $slug]);
}
/**
* TODO redirect vers play_game si connecté
*/
$response = new Response();
$response->headers->set('Access-Control-Allow-Origin', '*');
$game = $this->doctrine->getRepository(Game::class)->findOneBy(['canonicalTitle' => $slug]);
if($game)
{
return $this->render('games/display.html.twig', [
'game' => $game,
], $response);
} else {
//throw error
}
}
#[Route('/game/gameDescription{gameId}.xml', name: 'front_game_description')]
public function xmlDescription(int $gameId, Request $request): Response
{
$app = false;
return $this->gameDescription($gameId, $app, $request);
}
public function gameDescription($gameId, $app, Request $request)
{
//$gameUpdate = $this->doctrine->getRepository(Game::class)->getGameUpdate($gameId);
//if ($gameUpdate == null)
// throw new NotFoundHttpException('xmlDescriptionAction game not found');
//MANAGE A CACHE VERSION
$game = $this->doctrine->getRepository(Game::class)->findOneById($gameId);
$response = $this->getCachedResponse(array('contentType' => 'xml', 'lastModif' => $game->getUpdatedAt(), 'maxAge' => 0));
if (!$this->needUpdate($request, $response))
return $response;
if ($game == null)
throw new NotFoundHttpException('xmlDescriptionAction game not found');
//Prepare sound definitions
$soundDefinitions = $game->getSoundDefinitions();
$response->setLastModified($game->getLastModif());
//Prepare static files url
/*if ($request->isSecure())
$protocol = "https://";
else
$protocol = "http://";*/
$protocol = "https://";
$siteUrl = $protocol.$request->getHttpHost();
$staticFilesUrl = $this->getParameter('app.cdn_ssl_url');
$hasCDN = true;
if (!$staticFilesUrl || $this->getParameter('kernel.environment') == 'dev') {
$staticFilesUrl = $siteUrl;
$hasCDN = false;
}
$suggestions = array();
$words = array();
$releaseVersion = $this->getParameter('release_version');
return $this->render('games/xmlDescription.xml.twig', [
'game' => $game, 'soundDefinitions' => $soundDefinitions, 'words' => $words,
'hasCDN' => $hasCDN, 'siteUrl' => $siteUrl, 'staticFilesUrl' => $staticFilesUrl,
'suggestions' => $suggestions, 'releaseVersion' => $releaseVersion,
'appConfig' => $app
], $response);
}
#[Route('/game/userGameConfig{gameId}.xml', name: 'front_user_game_config')]
public function userGameConfig(int $gameId): Response
{
$user = $this->getUser();
$response = $this->getCachedResponse(array('private' => true, 'noCache' => true));
if (!($user instanceof User)) {
$response->setContent('ko');
return $response;
}
$userConfig = $this->doctrine->getRepository(UserGameConfig::class)->getUserGameConfig($user->getId(), $gameId);
$trophies = $this->doctrine->getRepository(UserTrophy::class)->getUserGameTrophies($user->getId(), $gameId);
$avatarId = $user->getAvatar() ? $user->getAvatar()->getId() : null;
$userSubscribed = $user->isSubscribed();
$response->headers->set('Content-Type', 'application/xml');
return $this->render('games/userConfig.xml.twig', [
'user' => $user,
'userConfig' => $userConfig,
'trophies' => $trophies,
'userSpeakos' => $user->getSpeakos(),
'avatarId' => $avatarId,
'challengeDatas' => null,
'userSubscribed' => $userSubscribed,
], $response);
}
#[Route('/game/comments-{gameId}.html', name: 'game_comments')]
public function gameComments($gameId, Request $request)
{
$game = $this->doctrine->getRepository(Game::class)->findOneById($gameId);
$response = $this->render('games/comments.html.twig', [
'game' => $game
]);
$response->setPublic();
$response->setMaxAge(600);
return $response;
}
#[Route('/gameComments{gameId}.{_format}', name: 'front_game_comments')]
public function showComments($gameId, $_format, Request $request) {
//Manage cached response
$commentsRepository = $this->doctrine->getRepository(GameComment::class);
$lastPost = $commentsRepository->getLastPostDate($gameId);
$response = $this->getCachedResponse(array('lastModif' => $lastPost, 'maxAge' => 0));
if (!$this->needUpdate($request, $response))
return $response;
$comments = $commentsRepository->getValidComments($gameId, 60);
if ($_format == 'html')
return $this->render('games/comments/showComments.html.twig', array('gameId' => $gameId, 'comments' => $comments), $response);
else if ($_format == 'json') {
return $this->render('games/comments/showComments.json.twig', array('gameId' => $gameId, 'validAnswers' => true, 'comments' => $comments), $response);
}
}
#[Route('/waitingComments{gameId}.{_format}', name: 'front_waiting_game_comments')]
public function showWaitingComments($gameId, $_format) {
$response = $this->getCachedResponse(array('private' => true, 'maxAge' => 0, 'sharedMaxAge' => 0));
$user = $this->getUser();
if ($user instanceof User) {
$waitingComments = $this->doctrine->getRepository(GameComment::class)->getWaitingComments($gameId, $user->getId());
$incorrectComments = $this->doctrine->getRepository(GameComment::class)->getIncorrectComments($gameId, $user->getId());
if ($_format == 'html')
return $this->render('games/comments/waitingComments.html.twig',
array('gameId' => $gameId, 'waitingComments' => $waitingComments, 'incorrectComments' => $incorrectComments), $response);
else if ($_format == 'json') {
//$response->headers->set('Content-Type', 'application/json');
$comments = array();
foreach ($waitingComments as $comment)
$comments[] = $comment;
foreach ($incorrectComments as $comment)
$comments[] = $comment;
return $this->render('games/comments/showComments.json.twig',
array('gameId' => $gameId, 'validAnswers' => false, 'comments' => $comments), $response);
}
}
$response->setContent('ko');
return $response;
}
#[Route('/postGameComment.json', name: 'front_post_game_comment')]
public function postComment(Request $request) {
$user = $this->getUser();
if ($user instanceof User) {
$content = $request->get('post', null);
$content = preg_replace('/\s{2,}/', ' ', $content);
$gameId = $request->get('gameId', null);
$postId = $request->get('postId', null);
$parentId = $request->get('parentId', null);
if ($content && $gameId) {
if ($postId) {
$comment = $this->doctrine->getRepository(GameComment::class)->findOneById($postId);
if ($comment) {
$comment->setComment($content);
$comment->setStatus(GameComment::STATUS_WAITING);
} else
return new Response('ko');
} else {
$game = $this->doctrine->getRepository(Game::class)->findOneById($gameId);
if ($game) {
$comment = new GameComment();
$comment->setComment($content);
$comment->setUser($user);
$comment->setGame($game);
} else
return new Response('{ "status": "ko" }');
}
$em = $this->doctrine->getManager();
if ($parentId) {
$parentComment = $this->doctrine->getRepository(GameComment::class)->findOneById($parentId);
$comment->setParent($parentComment);
}
$comment->setLastModif(new \DateTimeImmutable());
$em->persist($comment);
$em->flush();
$response = new Response();
$response->headers->set('Content-Type', 'application/json');
$response->setContent('{ "status": "ok", "commentId": '.$comment->getId().'}');
return $response;
}
}
return new Response('{ "status": "ko" }');
}
}