public function indexAction(Request $request) { // set up the new game $history = new History(); $history->setPlayer1name('Computer 1'); $history->setPlayer2name('Computer 2'); // what are our allowed moves $action = $this->getDoctrine()->getRepository('AppBundle:Actions')->findAll(); foreach ($action as $v) { $actions[$v->id] = $v->action; } ksort($actions); // build our form $form = $this->createFormBuilder($history)->add('player1name', 'text', array('label' => 'Player 1 Name', 'label_attr' => array('class' => 'player1'), 'attr' => array('class' => 'player1', 'placeholder' => 'Enter Player 1 Name')))->add('player1action', 'choice', array('choices' => $actions, 'label' => 'Action', 'label_attr' => array('class' => 'player1'), 'attr' => array('class' => 'player1')))->add('button1', 'button', array('label' => 'Toggle Player', 'attr' => array('class' => 'toggle player1')))->add('player2name', 'text', array('label' => 'Player 2 Name', 'label_attr' => array('class' => 'player2'), 'attr' => array('class' => 'player2', 'placeholder' => 'Enter Player 2 Name')))->add('player2action', 'choice', array('choices' => $actions, 'label_attr' => array('class' => 'player2'), 'label' => 'Action', 'attr' => array('class' => 'player2')))->add('button2', 'button', array('label' => 'Toggle Player', 'attr' => array('class' => 'toggle player2')))->add('save', 'submit', array('label' => 'Play', 'attr' => array('class' => 'btn-success pull-right')))->getForm(); // if it submitted, we should do something with it $form->handleRequest($request); // check submitted and completed properly // form is submitted properly if ($form->isSubmitted() && $form->isValid()) { $theRequest = $request->request->get('form'); $count = count($actions) - 1; $p1 = $theRequest['player1action'] ? $theRequest['player1action'] : rand(1, $count); $p2 = $theRequest['player2action'] ? $theRequest['player2action'] : rand(1, $count); $theRequest['player1action'] = $p1; $theRequest['player2action'] = $p2; // check results of what each player submitted if they did not use the same move if ($p1 !== $p2) { // going to look for possible comparisons $compares = $this->getDoctrine()->getRepository('AppBundle:Compares'); // checking where both options were thrown $query = $compares->createQueryBuilder('a')->where("a.action1 in ({$p1}, {$p2}) AND a.action2 in ({$p1}, {$p2})")->getQuery(); // should only have 1 result, but lets stop when we do find that one $compare = $query->setMaxResults(1)->getOneOrNullResult(); // set that result for insert $history->setWinner($compare->action1); // or, they used the same move } else { $compare = array('id' => 0, 'action1' => $p1, 'action2' => $p2, 'result' => 'ties'); // set 0 for insert, indicating tie $history->setWinner(0); } // create our insert, and put it in the db $history->setPlayer1action($p1); $history->setPlayer2action($p2); // $history->setWinner($compare->action1); // this was already handled above, removed $history->setCreated_at(new \DateTime()); $em = $this->getDoctrine()->getManager(); $em->persist($history); $em->flush(); // all done, lets go to the results page so the players can see how they did return $this->render('default/results.html.twig', array('actions' => $actions, 'request' => $theRequest, 'compare' => $compare)); } // form is not submitted or not completed properly, so we send them to the form return $this->render('default/rpssl.html.twig', array('form' => $form->createView())); }
private function karmaAction($user, $action) { $history = new History(); if ($action === 'up') { $user->setKarma($user->getKarma() + 1); $history->setAction(true); } else { $user->setKarma($user->getKarma() - 1); $history->setAction(false); } $history->setUser($user); $history->setAuthor($this->getUser()); $this->getUser()->addHistoryByUser($history); $user->addHistory($history); $em = $this->getDoctrine()->getManager(); $em->persist($history); $em->persist($user); $em->flush(); }
/** * @param \Sonata\AdminBundle\Form\FormMapper $formMapper * * @return void */ protected function configureFormFields(FormMapper $formMapper) { $formMapper->add('title')->add('type', 'choice', ['choices' => History::getTypes()])->add('dateTime', 'datetime', ['label' => 'History_Date', 'widget' => 'single_text', 'format' => 'yyyy'])->add('text', 'textarea', ['attr' => ['class' => 'wysihtml5', 'style' => 'height:300px']])->add('mainPicture', 'sonata_type_model_list', ['required' => false, 'btn_list' => false], ['link_parameters' => ['context' => 'history', 'provider' => 'sonata.media.provider.image']])->add('galleryHasMedia', 'sonata_type_collection', ['required' => false, 'label' => 'Gallery'], ['edit' => 'inline', 'inline' => 'table', 'sortable' => 'position', 'targetEntity' => 'Application\\Sonata\\MediaBundle\\Entity\\GalleryHasMedia', 'admin_code' => 'sonata.media.admin.gallery_has_media', 'link_parameters' => ['context' => 'history', 'provider' => 'sonata.media.provider.image']]); }