<commit_before><?php

namespace PlaygroundGame\Controller\Frontend;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use PlaygroundGame\Service\GameService;
use PlaygroundGame\Service\Prize as PrizeService;
use Zend\View\Model\JsonModel;
use Zend\Http\PhpEnvironment\Response;
use Zend\Stdlib\Parameters;

class GameController extends AbstractActionController
{
    /**
     * @var \PlaygroundGame\Service\GameService
     */
    protected $gameService;

    protected $prizeService;

    protected $options;

    protected $game;

    protected $user;

    protected $withGame = array(
        'home',
        'index',
        'terms',
        'conditions',
        'leaderboard',
        'register',
        'bounce',
        'prizes',
        'prize',
        'fangate',
        'share',
        'optin',
        'login',
        'play',
        'result',
        'preview',
        'list'
    );

    protected $withOnlineGame = array(
        'leaderboard',
        'register',
        'bounce',
        'play',
        'result'
    );

    protected $withAnyUser = array(
        'share',
        'result',
        'play'
    );

    public function setEventManager(\Zend\EventManager\EventManagerInterface $events)
    {
        parent::setEventManager($events);

        $controller = $this;
        $events->attach('dispatch', function (\Zend\Mvc\MvcEvent $e) use ($controller) {

            $identifier = $e->getRouteMatch()->getParam('id');
            $controller->game = $controller->getGameService()->checkGame($identifier, false);
            if (!$controller->game &&
                in_array($controller->params('action'), $controller->withGame)
            ) {
                return $controller->notFoundAction();
            }

            if ($controller->game &&
                $controller->game->isClosed() &&
                in_array($controller->params('action'), $controller->withOnlineGame)
            ) {
                return $controller->notFoundAction();
            }

            if ($controller->game) {
                // this is possible to create a specific game design in /design/frontend/default/custom.
                //It will precede all others templates.
                $templatePathResolver = $controller->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');
                $l = $templatePathResolver->getPaths();
                $templatePathResolver->addPath($l[0].'custom/'.$controller->game->getIdentifier());
            }

            $controller->user = $controller->zfcUserAuthentication()->getIdentity();
            if ($controller->game &&
                !$controller->user &&
                !$controller->game->getAnonymousAllowed() &&
                in_array($controller->params('action'), $controller->withAnyUser)
            ) {
                $redirect = urlencode(
                    $controller->url()->fromRoute(
                        'frontend/'.$controller->game->getClassType() . '/' . $controller->params('action'),
                        array('id' => $controller->game->getIdentifier()),
                        array('force_canonical' => true)
                    )
                );

                $urlRegister = $controller->url()->fromRoute(
                    'frontend/zfcuser/register',
                    array(),
                    array('force_canonical' => true)
                ) . '?redirect='.$redirect;

                // code permettant d'identifier un custom game
                // ligne $config = $controller->getGameService()->getServiceManager()->get('config');
                // ligne $customUrl = str_replace('frontend.', '', $e->getRouteMatch()->getParam('area', ''));
                // ligne if ($config['custom_games'][$controller->game->getIdentifier()] &&
                // ligne    $controller->getRequest()->getUri()->getHost() === $customUrl
                // ligne ) {
                return $controller->redirect()->toUrl($urlRegister);
            }

            return;
        }, 100); // execute before executing action logic
    }

    /**
     * Action called if matched action does not exist
     * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy
     * it has to be rendered in the controller. Hence the code below.
     *
     * This action is injected as a catchall action for each custom_games definition
     * This way, when a custom_game is created, the 404 is it's responsability and the
     * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml
     *
     *
     * @return \Zend\Stdlib\ResponseInterface
     */
    public function notFoundAction()
    {
        $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');

        // Figuring out the template path name based on the controller name
        $controller = explode('\\', get_class($this));
        $controllerPath = str_replace('Controller', '', end($controller));
        $controllerPath = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '-\\1', $controllerPath));

        $template = 'playground-game/'.$controllerPath . $this->getRequest()->getUri()->getPath();

        if (false === $templatePathResolver->resolve($template)) {

            $viewRender     = $this->getServiceLocator()->get('ViewRenderer');

            $this->getEvent()->getRouteMatch()->setParam('action', 'not-found');
            $this->response->setStatusCode(404);

            $res = 'error/404';

            $viewModel = $this->buildView($this->game);
            $viewModel->setTemplate($res);

            $this->layout()->setVariable("content", $viewRender->render($viewModel));<commit_msg>I isolate the custom templates so that they don't mix with the others<commit_after><?php

namespace PlaygroundGame\Controller\Frontend;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Session\Container;
use PlaygroundGame\Service\GameService;
use PlaygroundGame\Service\Prize as PrizeService;
use Zend\View\Model\JsonModel;
use Zend\Http\PhpEnvironment\Response;
use Zend\Stdlib\Parameters;

class GameController extends AbstractActionController
{
    /**
     * @var \PlaygroundGame\Service\GameService
     */
    protected $gameService;

    protected $prizeService;

    protected $options;

    protected $game;

    protected $user;

    protected $withGame = array(
        'home',
        'index',
        'terms',
        'conditions',
        'leaderboard',
        'register',
        'bounce',
        'prizes',
        'prize',
        'fangate',
        'share',
        'optin',
        'login',
        'play',
        'result',
        'preview',
        'list'
    );

    protected $withOnlineGame = array(
        'leaderboard',
        'register',
        'bounce',
        'play',
        'result'
    );

    protected $withAnyUser = array(
        'share',
        'result',
        'play'
    );

    public function setEventManager(\Zend\EventManager\EventManagerInterface $events)
    {
        parent::setEventManager($events);

        $controller = $this;
        $events->attach('dispatch', function (\Zend\Mvc\MvcEvent $e) use ($controller) {

            $identifier = $e->getRouteMatch()->getParam('id');
            $controller->game = $controller->getGameService()->checkGame($identifier, false);
            if (!$controller->game &&
                in_array($controller->params('action'), $controller->withGame)
            ) {
                return $controller->notFoundAction();
            }

            if ($controller->game &&
                $controller->game->isClosed() &&
                in_array($controller->params('action'), $controller->withOnlineGame)
            ) {
                return $controller->notFoundAction();
            }

            if ($controller->game) {
                // this is possible to create a specific game design in /design/frontend/default/custom.
                //It will precede all others templates.
                $templatePathResolver = $controller->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');
                $l = $templatePathResolver->getPaths();
                $templatePathResolver->addPath($l[0].'custom/'.$controller->game->getIdentifier());
            }

            $controller->user = $controller->zfcUserAuthentication()->getIdentity();
            if ($controller->game &&
                !$controller->user &&
                !$controller->game->getAnonymousAllowed() &&
                in_array($controller->params('action'), $controller->withAnyUser)
            ) {
                $redirect = urlencode(
                    $controller->url()->fromRoute(
                        'frontend/'.$controller->game->getClassType() . '/' . $controller->params('action'),
                        array('id' => $controller->game->getIdentifier()),
                        array('force_canonical' => true)
                    )
                );

                $urlRegister = $controller->url()->fromRoute(
                    'frontend/zfcuser/register',
                    array(),
                    array('force_canonical' => true)
                ) . '?redirect='.$redirect;

                // code permettant d'identifier un custom game
                // ligne $config = $controller->getGameService()->getServiceManager()->get('config');
                // ligne $customUrl = str_replace('frontend.', '', $e->getRouteMatch()->getParam('area', ''));
                // ligne if ($config['custom_games'][$controller->game->getIdentifier()] &&
                // ligne    $controller->getRequest()->getUri()->getHost() === $customUrl
                // ligne ) {
                return $controller->redirect()->toUrl($urlRegister);
            }

            return;
        }, 100); // execute before executing action logic
    }

    /**
     * Action called if matched action does not exist
     * For this view not to be catched by Zend\Mvc\View\RouteNotFoundStrategy
     * it has to be rendered in the controller. Hence the code below.
     *
     * This action is injected as a catchall action for each custom_games definition
     * This way, when a custom_game is created, the 404 is it's responsability and the
     * view can be defined in design/frontend/default/custom/$slug/playground_game/$gametype/404.phtml
     *
     *
     * @return \Zend\Stdlib\ResponseInterface
     */
    public function notFoundAction()
    {
        $templatePathResolver = $this->getServiceLocator()->get('Zend\View\Resolver\TemplatePathStack');

        // I create a template path in which I can find a custom template
        $controller = explode('\\', get_class($this));
        $controllerPath = str_replace('Controller', '', end($controller));
        $controllerPath = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '-\\1', $controllerPath));
        $template = 'playground-game/'.$controllerPath . '/custom' .$this->getRequest()->getUri()->getPath();

        if (false === $templatePathResolver->resolve($template)) {

            $viewRender     = $this->getServiceLocator()->get('ViewRenderer');

            $this->getEvent()->getRouteMatch()->setParam('action', 'not-found');
            $this->response->setStatusCode(404);

            $res = 'error/404';

            $viewModel = $this->buildView($this->game);
            $viewModel->setTemplate($res);

            $this->layout()->setVariable("content", $viewRender->render($viewModel));
{'fraction_non_alphanumeric': 0.14348576358930112, 'fraction_numerical': 0.002933563416738568, 'mean_word_length': 2.1352447930754668, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 153, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1788836', 'n_tokens_mistral': 3076, 'n_tokens_neox': 2944, 'n_words': 586}