예제 #1
0
 /**
  * Save a vote
  *
  * @param Combination $combination
  * @param Picture     $votedForPicture
  * @param Competitor  $competitor
  * @param string      $clientIp
  * @param string      $sessionId
  *
  * @return Vote
  */
 public function saveVote(Combination $combination, Picture $votedForPicture, Competitor $competitor, $clientIp, $sessionId)
 {
     $vote = new Vote();
     $vote->setCombination($combination);
     $vote->setVotedForPicture($votedForPicture);
     if ($combination->getLeftPicture() === $votedForPicture) {
         $vote->setVotedAgainstPicture($combination->getRightPicture());
     } else {
         $vote->setVotedAgainstPicture($combination->getLeftPicture());
     }
     $vote->setCompetitor($competitor);
     $vote->setClientIp($clientIp);
     $vote->setSessionId($sessionId);
     $this->entityManger->persist($vote);
     $this->entityManger->flush();
     return $vote;
 }
예제 #2
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $container = $this->getContainer();
     /**
      * @var CombinationManager
      */
     $combinationManager = $container->get('hot_or_not.combination_manager');
     /**
      * @var CompetitorManager
      */
     $competitorManager = $container->get('hot_or_not.competitor_manager');
     /**
      * @var VoteManager
      */
     $voteManager = $container->get('hot_or_not.vote_manager');
     $combinations = $combinationManager->getCombinations();
     $competitors = $competitorManager->getCompetitors();
     $amount = $input->getArgument('amount');
     if (!$amount) {
         $amount = 100;
     }
     $em = $container->get('doctrine')->getManager();
     $existingVotes = [];
     if ($amount > count($combinations)) {
         $output->writeln(sprintf('Tried to add %d votes, but the system only has %d combinations', $amount, count($combinations)));
     } else {
         for ($i = 0; $i < $amount; $i++) {
             /**
              * @var Combination
              */
             $combination = $combinations[array_rand($combinations)];
             /**
              * @var Competitor
              */
             $competitor = $competitors[array_rand($competitors)];
             $voteUniqueId = $combination->getId() . '-' . $competitor->getId();
             if (in_array($voteUniqueId, $existingVotes)) {
                 $output->writeln(sprintf('Wont add duplicate vote for combination %d and competitor %d', $combination->getId(), $competitor->getId()));
                 $i--;
             } else {
                 $existingVotes[] = $voteUniqueId;
                 $votedForPicture = $combination->getLeftPicture();
                 $votedAgianstPicture = $combination->getRightPicture();
                 if (1 === rand(0, 1)) {
                     $votedAgianstPicture = $combination->getLeftPicture();
                     $votedForPicture = $combination->getRightPicture();
                 }
                 $vote = new Vote();
                 $vote->setCompetitor($competitor);
                 $vote->setCombination($combination);
                 $vote->setClientIp('localhost');
                 $vote->setSessionId('local');
                 $vote->setVotedForPicture($votedForPicture);
                 $vote->setVotedAgainstPicture($votedAgianstPicture);
                 $em->persist($vote);
             }
         }
         $em->flush();
         $output->writeln(sprintf('Added %d random votes', $amount));
     }
 }