<commit_before><?php

namespace Argon\GameBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use Argon\GameBundle\Entity\Character;
use Argon\GameBundle\Entity\CharacterAbility;
use Argon\GameBundle\Entity\CharacterSkill;

class CharacterController extends Controller
{
    public function indexAction(Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player   = $this->getUser();
        $entities = $this->getDoctrine()
                         ->getRepository('ArgonGameBundle:Character')
                         ->findByPlayer($player);

        return $this->render('ArgonGameBundle:Character:index.html.twig', array(
            'entities' => $entities,
        ));
    }

    public function gameAction(Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $form = $this->createForm('character_game', null, array(
            'action' => $this->generateUrl('character_game'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array(
            'label' => 'character_game.submit',
        ));

        if ($request->isMethod('POST')) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $data = $form->getData();
                $game = $data['game'];

                return $this->redirect($this->generateUrl('character_new', array(
                    'game' => $game,
                )));
            }
        }

        return $this->render('ArgonGameBundle:Character:game.html.twig', array(
            'form' => $form->createView(),
        ));
    }

    public function newAction(Request $request, $game)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        // {{{ TODO: Rewrite me with Parameter Converter
        $gameName    = $game;
        $gameFactory = $this->container->get('argon_game.provider.game_factory');
        $game        = $gameFactory->create($gameName);
        // }}}

        if (null === $game) {
            throw $this->createNotFoundException(sprintf('Game %s not found.', $gameName));
        }

        $character = new Character();
        $character->setPlayer($player);
        $character->setGame($game);

        foreach ($game->getAbilities() as $characterAbilityCode) {
            $characterAbility = new CharacterAbility();
            $characterAbility->setCode($characterAbilityCode);
            $characterAbility->setCharacter($character);

            $character->addAbility($characterAbility);
        }

        $form = $this->createForm('character', $character, array(
            'action' => $this->generateUrl('character_create', array('game' => $gameName)),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array(
            'label' => 'character.submit',
        ));

        if ($request->isMethod('POST')) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($character);
                $em->flush();

                $request->getSession()->getFlashBag()
                        ->add('success', 'character.created');

                return $this->redirect($this->generateUrl('character'));
            }
        }

        return $this->render('ArgonGameBundle:Character:new.html.twig', array(
            'game' => $game,
            'form' => $form->createView(),
        ));
    }

    public function viewAction(Character $character)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        if ($player !== $character->getPlayer()) {
            throw new AccessDeniedException();
        }

        $experiences = $this->getDoctrine()
                            ->getRepository('ArgonGameBundle:CharacterExperience')
                            ->findByCharacter($character);

        return $this->render('ArgonGameBundle:Character:view.html.twig', array(
            'character'   => $character,
            'experiences' => $experiences,
        ));
    }

    public function skillsAction(Character $character, Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        if ($player !== $character->getPlayer()) {
            throw new AccessDeniedException();
        }

        $characterSkills = array();
        $skills          = $this->getDoctrine()
                                ->getRepository('ArgonGameBundle:Skill')
                                ->findAll();

        foreach ($skills as $skill) {
            $found = $character->getSkills()->filter(function ($characterSkill) use ($skill) {
                return $characterSkill->getSkill() === $skill;
            });

            if (count($found) > 0) {
                $characterSkill = $found->first();
            } else {
                $characterSkill = new CharacterSkill();
                $characterSkill->setCharacter($character);
                $characterSkill->setSkill($skill);
            }

            $characterSkills[$skill->getSlug()] = $characterSkill;
        }

        $form = $this->createForm('character_skills', array('characterSkills' => $characterSkills), array(
            'action' => $this->generateUrl('character_skills_update', array('slug' => $character->getSlug())),
            'method' => 'POST',<commit_msg>[Game][Skills] Issue #9 : Order by Price<commit_after><?php

namespace Argon\GameBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Criteria;

use Argon\GameBundle\Entity\Character;
use Argon\GameBundle\Entity\CharacterAbility;
use Argon\GameBundle\Entity\CharacterSkill;

class CharacterController extends Controller
{
    public function indexAction(Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player   = $this->getUser();
        $entities = $this->getDoctrine()
                         ->getRepository('ArgonGameBundle:Character')
                         ->findByPlayer($player);

        return $this->render('ArgonGameBundle:Character:index.html.twig', array(
            'entities' => $entities,
        ));
    }

    public function gameAction(Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $form = $this->createForm('character_game', null, array(
            'action' => $this->generateUrl('character_game'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array(
            'label' => 'character_game.submit',
        ));

        if ($request->isMethod('POST')) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $data = $form->getData();
                $game = $data['game'];

                return $this->redirect($this->generateUrl('character_new', array(
                    'game' => $game,
                )));
            }
        }

        return $this->render('ArgonGameBundle:Character:game.html.twig', array(
            'form' => $form->createView(),
        ));
    }

    public function newAction(Request $request, $game)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        // {{{ TODO: Rewrite me with Parameter Converter
        $gameName    = $game;
        $gameFactory = $this->container->get('argon_game.provider.game_factory');
        $game        = $gameFactory->create($gameName);
        // }}}

        if (null === $game) {
            throw $this->createNotFoundException(sprintf('Game %s not found.', $gameName));
        }

        $character = new Character();
        $character->setPlayer($player);
        $character->setGame($game);

        foreach ($game->getAbilities() as $characterAbilityCode) {
            $characterAbility = new CharacterAbility();
            $characterAbility->setCode($characterAbilityCode);
            $characterAbility->setCharacter($character);

            $character->addAbility($characterAbility);
        }

        $form = $this->createForm('character', $character, array(
            'action' => $this->generateUrl('character_create', array('game' => $gameName)),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array(
            'label' => 'character.submit',
        ));

        if ($request->isMethod('POST')) {
            $form->handleRequest($request);

            if ($form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($character);
                $em->flush();

                $request->getSession()->getFlashBag()
                        ->add('success', 'character.created');

                return $this->redirect($this->generateUrl('character'));
            }
        }

        return $this->render('ArgonGameBundle:Character:new.html.twig', array(
            'game' => $game,
            'form' => $form->createView(),
        ));
    }

    public function viewAction(Character $character)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        if ($player !== $character->getPlayer()) {
            throw new AccessDeniedException();
        }

        $experiences = $this->getDoctrine()
                            ->getRepository('ArgonGameBundle:CharacterExperience')
                            ->findByCharacter($character);

        return $this->render('ArgonGameBundle:Character:view.html.twig', array(
            'character'   => $character,
            'experiences' => $experiences,
        ));
    }

    public function skillsAction(Character $character, Request $request)
    {
        if (false === $this->get('security.context')->isGranted('ROLE_USER')) {
            throw new AccessDeniedException();
        }

        $player = $this->getUser();

        if ($player !== $character->getPlayer()) {
            throw new AccessDeniedException();
        }

        $characterSkills = new ArrayCollection();
        $skills          = $this->getDoctrine()
                                ->getRepository('ArgonGameBundle:Skill')
                                ->findAll();

        foreach ($skills as $skill) {
            $found = $character->getSkills()->filter(function ($characterSkill) use ($skill) {
                return $characterSkill->getSkill() === $skill;
            });

            if (count($found) > 0) {
                $characterSkill = $found->first();
            } else {
                $characterSkill = new CharacterSkill();
                $characterSkill->setCharacter($character);
                $characterSkill->setSkill($skill);
            }

            $characterSkills[] = $characterSkill;
        }

        // {{{ Order By Price
        $criteria = new Criteria();
        $criteria->orderBy(array('price' => Criteria::ASC));

        $characterSkills = $characterSkills->matching($criteria);
        // }}}

        $form = $this->createForm('character_skills', array('characterSkills' => $characterSkills), array(
            'action' => $this->generateUrl('character_skills_update', array('slug' => $character->getSlug())),
            'method' => 'POST',
{'fraction_non_alphanumeric': 0.1544735142118863, 'fraction_numerical': 0.00024224806201550387, 'mean_word_length': 2.1016779363886804, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 193, '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': '6643484', 'n_tokens_mistral': 3319, 'n_tokens_neox': 3216, 'n_words': 535}