Enigma
This commit is contained in:
parent
419a438acd
commit
f69c9c8c3d
|
@ -9,6 +9,9 @@ use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||||
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
|
||||||
|
use DateTime;
|
||||||
|
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
|
||||||
|
use Symfony\Component\Validator\Constraints\NotBlank;
|
||||||
|
|
||||||
class DefaultController extends Controller
|
class DefaultController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -18,19 +21,101 @@ class DefaultController extends Controller
|
||||||
*/
|
*/
|
||||||
public function indexAction($enigma = null)
|
public function indexAction($enigma = null)
|
||||||
{
|
{
|
||||||
if (!$this->get('session')->get('user')) {
|
if ($this->getUser() === null) {
|
||||||
$this->get('session')->set(
|
$this->get('session')->set(
|
||||||
'redirect_to',
|
'redirect_to',
|
||||||
$this->generateUrl('enigma', ['enigma' => null !== $enigma ? $enigma : null])
|
$this->generateUrl('enigma', ['enigma' => $enigma !== null ? $enigma : null])
|
||||||
);
|
);
|
||||||
|
|
||||||
return $this->redirectToRoute('enigma_login');
|
return $this->redirectToRoute('enigma_login');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($enigma !== null) {
|
||||||
|
return $this->redirectToRoute('enigma_show', ['enigma' => $enigma]);
|
||||||
|
}
|
||||||
|
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Route("/enigma/{enigma}/resoudre", name="enigma_show", requirements={"enigma": "\w{32}"})
|
||||||
|
* @Template()
|
||||||
|
*/
|
||||||
|
public function showEnigmaAction($enigma, Request $request)
|
||||||
|
{
|
||||||
|
if ($this->getUser() === null) {
|
||||||
|
$this->get('session')->set(
|
||||||
|
'redirect_to',
|
||||||
|
$this->generateUrl('enigma_show', ['enigma' => $enigma])
|
||||||
|
);
|
||||||
|
|
||||||
|
return $this->redirectToRoute('enigma_login');
|
||||||
|
}
|
||||||
|
|
||||||
|
$enigma = $this
|
||||||
|
->getDoctrine()
|
||||||
|
->getRepository('MmiEnigmaBundle:Enigma')
|
||||||
|
->findOneBy([
|
||||||
|
'hash' => $enigma,
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($enigma === null) {
|
||||||
|
return $this->redirect('enigma');
|
||||||
|
}
|
||||||
|
|
||||||
|
$timer = $this
|
||||||
|
->getRepository('MmiEnigmaBundle:Timer')
|
||||||
|
->findOneBy([
|
||||||
|
'team_id' => $this->getUser()->getId(),
|
||||||
|
'enigma_id' => $enigma->getId(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
if ($timer !== null) {
|
||||||
|
if (time() - $timer->getDate()->getTimestamp() < 60 * 30) {
|
||||||
|
return $this->redirectToRoute('enigma_too_late');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
$timer = new Timer();
|
||||||
|
$timer
|
||||||
|
->setTeam($this->getUser())
|
||||||
|
->setEnigma($enigma)
|
||||||
|
->setDate(new DateTime('now'))
|
||||||
|
->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$answer = $this
|
||||||
|
->getDoctrine()
|
||||||
|
->getRepository('MmiEnigmaBundle:Awnser')
|
||||||
|
->findOneBy([
|
||||||
|
'team_id' => $this->getUser()->getId(),
|
||||||
|
'enigma_id' => $enigma->getId(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
|
||||||
|
$form = $this->createFormBuilder()
|
||||||
|
->add('content', TextareaType::class, [
|
||||||
|
'required' => true,
|
||||||
|
'constraints' => [
|
||||||
|
new NotBlank(),
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->add('Soumettre', SubmitType::class)
|
||||||
|
->getForm();
|
||||||
|
|
||||||
|
if ($request->isMethod('post')) {
|
||||||
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
if ($form->isValid()) {
|
||||||
|
$form->getData()->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'form' => $form->createView(),
|
||||||
|
'enigma' => $enigma,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @Route("/enigma/login", name="enigma_login")
|
* @Route("/enigma/login", name="enigma_login")
|
||||||
* @Template()
|
* @Template()
|
||||||
|
@ -40,7 +125,7 @@ class DefaultController extends Controller
|
||||||
$redirectTo = $this->get('session')->get('redirect_to');
|
$redirectTo = $this->get('session')->get('redirect_to');
|
||||||
$redirect = $redirectTo ? $this->redirect($redirectTo) : $this->redirectToRoute('enigma');
|
$redirect = $redirectTo ? $this->redirect($redirectTo) : $this->redirectToRoute('enigma');
|
||||||
|
|
||||||
if ($this->get('session')->get('user')) {
|
if ($this->getUser() !== null) {
|
||||||
return $redirect;
|
return $redirect;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -54,7 +139,8 @@ class DefaultController extends Controller
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
if ($form->isValid()) {
|
if ($form->isValid()) {
|
||||||
$user = $this->getDoctrine()
|
$user = $this
|
||||||
|
->getDoctrine()
|
||||||
->getRepository('MmiEnigmaBundle:Team')
|
->getRepository('MmiEnigmaBundle:Team')
|
||||||
->findOneBy([
|
->findOneBy([
|
||||||
'username' => $form->getData()['username'],
|
'username' => $form->getData()['username'],
|
||||||
|
@ -76,4 +162,9 @@ class DefaultController extends Controller
|
||||||
'invalid' => !empty($invalid),
|
'invalid' => !empty($invalid),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getUser()
|
||||||
|
{
|
||||||
|
return $this->get('session')->get('user');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
test
|
Loading…
Reference in a new issue