/** * Evolution of beans and utility function in the game * * @param ObjectManager $em * @param int $user_id * @param Partida $partida * @return array */ public function partidaEvolution($em, $user_id, $partida) { $userPartidaLogic = new UserPartidaLogic(); $ofertasPartida = array(); //ofertas aceptadas en una partida (tanto de las enviadas como recividas) $ofertas = $em->getRepository('BaseBundle:Ofertas')->findAllUserGameDeals($user_id, $partida->getId()); foreach ($ofertas as $oferta) { /** @var Ofertas $oferta */ //calcular alubias en funcion de si oferta enviada o recibida if ($oferta->getIdCreador()->getId() == $user_id) { $tmp = $userPartidaLogic->beansStatus($oferta, 1); } else { $tmp = $userPartidaLogic->beansStatus($oferta, 2); } $tmp['modificado'] = $oferta->getModificado(); array_push($ofertasPartida, $tmp); } /** @var UserPartida $userpartida */ $userpartida = $em->getRepository('BaseBundle:UserPartida')->findByIDS($user_id, $partida->getId()); $evolucion = array(); $a_rojas_tmp = $userpartida->getAluRojaInicial(); $a_blancas_tmp = $userpartida->getAluBlancaInicial(); foreach ($ofertasPartida as $oferta) { $oferta['aluRoja'] += $a_rojas_tmp; $oferta['aluBlanca'] += $a_blancas_tmp; $oferta['fUtilidad'] = $this->calculateFUtilidad($oferta['aluRoja'], $oferta['aluBlanca'], $partida); $a_blancas_tmp = $oferta['aluBlanca']; $a_rojas_tmp = $oferta['aluRoja']; array_push($evolucion, $oferta); } return $evolucion; }
/** * Distributes randomly the beans to the players, and starts the game. * * @param Request $request * @param int $id_partida * @return RedirectResponse|AccessDeniedException */ public function distributeBeansAction(Request $request, $id_partida) { $locale = $request->get('_locale'); $request->setLocale($locale); $request->getSession()->set('_locale', $locale); //Security control. Check user roles. $response = $this->checkSecurity($request); if ($response instanceof RedirectResponse) { return $response; } $admin_id = $this->getUser()->getId(); $em = $this->getDoctrine()->getManager(); try { /** @var Partida $partida */ $partida = $em->getRepository('BaseBundle:Partida')->isMyAdminGame($id_partida, $admin_id); /** @var QueryBuilder $qb */ $qb = $em->createQueryBuilder(); //localizamos el objeto $qb->select('u')->from('BaseBundle:UserPartida', 'u')->where($qb->expr()->eq('u.idPartida', '?1'))->setParameter(1, $id_partida); $query = $qb->getQuery(); $jugadores = $query->getResult(); $logic = new UserPartidaLogic(); $logic->distributeBeans($partida, $jugadores, $em); //actualizar partida a empezado $partida->setEmpezado(true); $em->flush(); return new RedirectResponse($this->get('router')->generate('game_statistics', array('id_partida' => $id_partida))); } catch (\Exception $e) { return new AccessDeniedException('You shall not pass!'); } }
/** * Accept or reject a deal * * @param Request $request * @return RedirectResponse */ public function acceptRejectAction(Request $request) { /** @var ObjectManager $em */ $em = $this->getDoctrine()->getManager(); $acceptReject = $this->createForm(new AcceptRejectDealType()); $acceptReject->handleRequest($request); if ($request->isMethod('POST')) { $data = $acceptReject->getData(); /* idC -> idCreador idD -> idDestinatario idP -> idPartida idO -> idOferta */ try { $em->getRepository('BaseBundle:Ofertas')->checkDeal($data['idC'], $data['idP'], $data['idO'], $data['idD']); if ($acceptReject->get('accept')->isClicked()) { /** @var Ofertas $oferta */ $oferta = $em->getRepository('BaseBundle:Ofertas')->findOneById($data['idO']); $userPartidaLogic = new UserPartidaLogic(); $aluBlanca = $oferta->getAluBlancaIn() - $oferta->getAluBlancaOut(); $aluRoja = $oferta->getAlurojaIn() - $oferta->getAluRojaOut(); $resultado1 = $userPartidaLogic->updateBeans($data['idC'], $data['idP'], $aluRoja, $aluBlanca, $em); $aluBlanca = $oferta->getAluBlancaOut() - $oferta->getAluBlancaIn(); $aluRoja = $oferta->getAluRojaOut() - $oferta->getAlurojaIn(); $resultado2 = $userPartidaLogic->updateBeans($data['idD'], $data['idP'], $aluRoja, $aluBlanca, $em); $resultado3 = $em->getRepository('BaseBundle:Ofertas')->updateStatus(OfertaLogic::ACEPTADA, $data['idO']); $em->getRepository('BaseBundle:Log')->action2log($data['idD'], Loglogic::ACEPTAROFERTA, $data['idC']); if ($resultado1 && $resultado2 && $resultado3) { $this->get('session')->getFlashBag()->add('correct', ''); } else { $this->get('session')->getFlashBag()->add('error', ''); } } elseif ($acceptReject->get('reject')->isClicked()) { $resultado = $em->getRepository('BaseBundle:Ofertas')->updateStatus(OfertaLogic::RECHAZADA, $data['idO']); $em->getRepository('BaseBundle:Log')->action2log($data['idD'], Loglogic::RECHAZAROFERTA, $data['idC']); if ($resultado) { $this->get('session')->getFlashBag()->add('reject', ''); } } } catch (\Exception $e) { $this->get('session')->getFlashBag()->add('error', ''); } } //recoger variables de sesión $session = $this->container->get('session'); $id_partida = $session->get('id_partida'); $session->remove('id_partida'); return new RedirectResponse($this->get('router')->generate('partida_home', array('id_partida' => $id_partida))); }