/**
  * @param CommunityInterface $community
  * @param Output             $output
  */
 private function updateCommunityMembers(CommunityInterface $community, Output $output)
 {
     $this->logger->addInfo(sprintf('Checking community "%s" with %s', $community->getName(), $community->getId()));
     $today = new \DateTime('-4 hour');
     $loopTimeStart = microtime(true);
     $failsCount = 0;
     $members = $community->getMembers();
     if ($output->isVerbose()) {
         $this->logger->addInfo(sprintf('Found %s members', count($members)));
     }
     foreach ($members as $player) {
         $memberTimeStart = microtime(true);
         $this->logger->addInfo(sprintf('Checking user "%s" with id "%s"', $player->getNick(), $player->getId()));
         $alreadyParseCheckTime = microtime(true);
         if (count($player->getDateStat()) && $player->getDateStat()->first()->getDate()->format('Y:m:d') == $today->format('Y:m:d')) {
             $this->logger->addInfo(sprintf('Player "%s" with id %s already parsed today', $player->getNick(), $player->getId()));
             if ($output->isVerbose()) {
                 $this->logger->addInfo(sprintf('Already parse check take %s sec to process', microtime(true) - $alreadyParseCheckTime));
             }
             continue;
         }
         if ($output->isVerbose()) {
             $this->logger->addInfo(sprintf('Already parse check take %s sec to process', microtime(true) - $alreadyParseCheckTime));
         }
         $playerUpdateTimeStart = microtime(true);
         try {
             $this->statService->updatePlayer($player->getId());
         } catch (RuntimeException $e) {
             if ($e->getCode() == 403) {
                 $failsCount++;
                 if ($failsCount >= 10) {
                     throw $e;
                 }
                 $this->logger->addWarning(sprintf('Fail to update player "%s" with id %s', $player->getNick(), $player->getId()));
             } else {
                 throw $e;
             }
         }
         if ($output->isVerbose()) {
             $this->logger->addInfo(sprintf('Player parse take %s sec to process', microtime(true) - $playerUpdateTimeStart));
         }
         $sleepMicroTime = rand(500, 1500) * 1000;
         if ($output->isVerbose()) {
             $this->logger->addInfo(sprintf('Member take %s sec to process', microtime(true) - $memberTimeStart));
             $this->logger->addInfo(sprintf('Sleep for %s sec', $sleepMicroTime / 1000 / 1000));
         }
         usleep($sleepMicroTime);
     }
     if ($output->isVerbose()) {
         $this->logger->addInfo(sprintf('Loop take %s sec to process', microtime(true) - $loopTimeStart));
     }
 }
Ejemplo n.º 2
0
 /**
  * @param CommunityInterface $community
  *
  * @return array
  */
 public function findByCommunity(CommunityInterface $community)
 {
     $qb = $this->getEntityManager()->createQueryBuilder();
     $qb->select('p')->from('Erliz\\SkyforgeBundle\\Entity\\Player', 'p')->leftJoin('Erliz\\SkyforgeBundle\\Entity\\Community', 'c')->where($qb->expr()->eq('c.id', ':community_id'))->setParameter('community_id', $community->getId());
     return $qb->getQuery()->getResult();
 }
 /**
  * @param CommunityInterface $community
  * @param OutputInterface    $output
  * @param string             $type
  */
 private function updateCommunityMembers(CommunityInterface $community, OutputInterface $output, $type)
 {
     $this->logger->addInfo(sprintf('Checking %s "%s" with %s', $type, $community->getName(), $community->getId()));
     $members = array();
     try {
         for ($page = 1; $page <= 20; $page++) {
             if ($this->regionService->getRegion() == RegionService::RU_REGION) {
                 $responseMessage = $this->parseService->getPage($this->makeCommunityMembersMoreUrl($community->getId()), true, $this->makeCommunityMembersUrl($community->getId()), array('t:zone' => 'bunchZone', 'bunchIndex' => $page));
                 $response = json_decode($responseMessage);
             } else {
                 $response = $this->parseService->getPage($this->makeCommunityMembersUrl($community->getId()) . '?page=' . $page, false, $this->makeCommunityMembersUrl($community->getId()));
             }
             if (!$response) {
                 $this->logger->addInfo(sprintf('Empty page %s', $page));
                 break;
             }
             if ($this->regionService->getRegion() == RegionService::RU_REGION) {
                 $pageMembers = $this->parseService->getMembersFromCommunityPage($response->content);
             } else {
                 $pageMembers = $this->parseService->getMembersFromCommunityPage($response);
             }
             $this->logger->addInfo(sprintf('Page %s parsed successful, get %s members', $page, count($pageMembers)));
             $members = $members + $pageMembers;
             usleep(rand(500, 1500) * 1000);
         }
     } catch (RuntimeException $e) {
         $this->logger->addInfo('Exception: ' . $e->getMessage() . ' ' . $e->getCode());
     }
     if ($type == $this::TYPE_PANTHEON) {
         $dbMembers = $this->playerRepository->findBy(array('pantheon' => $community->getId()));
     } elseif ($type == $this::TYPE_COMMUNITY) {
         $dbMembers = $this->playerRepository->findByCommunity($community);
     } else {
         throw new \InvalidArgumentException(sprintf('Unknown community type "%s" to find members', $type));
     }
     /** @var Player $member */
     foreach ($dbMembers as $member) {
         if ($type == $this::TYPE_PANTHEON) {
             $member->removePantheon();
         } elseif ($type == $this::TYPE_COMMUNITY) {
             $member->getCommunities()->removeElement($community);
         } else {
             throw new \InvalidArgumentException(sprintf('Unknown community type "%s" to remove community', $type));
         }
     }
     foreach ($members as $parsedMember) {
         $player = $this->playerRepository->find($parsedMember->id);
         if (!$player) {
             $player = new Player();
             $player->setId($parsedMember->id);
             $this->em->persist($player);
         }
         if ($parsedMember->name) {
             $player->setName($parsedMember->name);
         }
         if ($parsedMember->nick) {
             $player->setNick($parsedMember->nick);
         } else {
             $player->setNick('');
         }
         $community->addMember($player);
         if ($type == $this::TYPE_PANTHEON) {
             $player->setPantheon($community);
         } elseif ($type == $this::TYPE_COMMUNITY) {
             $player->getCommunities()->add($community);
         } else {
             throw new \InvalidArgumentException(sprintf('Unknown community type "%s" to add for member', $type));
         }
     }
     $community->setUpdatedAt(new DateTime());
     usleep(rand(500, 1500) * 1000);
 }