public function findWithDateStatByPantheon(Pantheon $pantheon)
 {
     $dql = '
         select
             p.*,
             pds.*
         from Erliz\\SkyforgeBundle\\Entity\\Player p
             left join (select player, MAX(max_prestige) max_prestige from Erliz\\SkyforgeBundle\\Entity\\PlayerDateStat group by player) m_pds
             left join Erliz\\SkyforgeBundle\\Entity\\PlayerDateStat pds on pds.max_prestige = m_pds.max_prestige and pds.player = p.id
         where p.pantheon = :pantheonId
         group by p.id
         order by pds.max_prestige DESC
     ';
     $params = array(':pantheonId' => $pantheon->getId());
     return new PlayerCollection($this->getEntityManager()->createQuery($dql)->setParameters($params)->useResultCache(true)->setResultCacheLifetime(1800)->getResult(Query::HYDRATE_ARRAY));
 }
 /**
  * @param Pantheon $community
  * @param OutputInterface    $output
  */
 private function updateCommunityDateStat(Pantheon $community, OutputInterface $output)
 {
     $this->logger->addInfo(sprintf('Update community "%s" with id %s', $community->getName(), $community->getId()));
     $today = new \DateTime('-4 hour');
     /** @var PantheonDateStat[] $communityDateStat */
     $communityDateStat = $community->getDateStat();
     if (count($communityDateStat) && $communityDateStat->first()->getDate()->format('Y:m:d') == $today->format('Y:m:d')) {
         $this->logger->addInfo(sprintf('Community "%s" with id %s already parsed today', $community->getName(), $community->getId()));
         return;
     }
     $aggregator = array('sumPrestige' => 0, 'maxPrestige' => 0, 'pveSumMobKills' => 0, 'pveSumBossKills' => 0, 'pveSumDeaths' => 0, 'pvpSumTime' => 0, 'pvpSumKills' => 0, 'pvpSumDeaths' => 0, 'pvpSumAssists' => 0);
     /** @var Player $member */
     foreach ($community->getMembers() as $member) {
         if (!count($member->getDateStat())) {
             continue;
         }
         /** @var PlayerDateStat $lastPlayerDateStat */
         $lastPlayerDateStat = $member->getDateStat()->first();
         $aggregator['sumPrestige'] += $lastPlayerDateStat->getMaxPrestige();
         $aggregator['maxPrestige'] = $aggregator['maxPrestige'] < $lastPlayerDateStat->getMaxPrestige() ? $lastPlayerDateStat->getMaxPrestige() : $aggregator['maxPrestige'];
         $aggregator['pvpSumTime'] += $lastPlayerDateStat->getPvpTime();
         //            $aggregator['pveSumTime'] += $lastPlayerDateStat->getPveTime();
         foreach ($member->getRoleStat() as $role) {
             $aggregator['pveSumMobKills'] += $role->getPveMobKills();
             $aggregator['pveSumBossKills'] += $role->getPveBossKills();
             $aggregator['pveSumDeaths'] += $role->getPveDeaths();
             $aggregator['pvpSumKills'] += $role->getPvpKills();
             $aggregator['pvpSumDeaths'] += $role->getPvpDeaths();
             $aggregator['pvpSumAssists'] += $role->getPvpAssists();
         }
     }
     $newPantheonDateStat = new PantheonDateStat();
     $newPantheonDateStat->setPantheon($community)->setDate($today)->setSumPrestige($aggregator['sumPrestige'])->setAvgPrestige(count($community->getMembers()) ? round($aggregator['sumPrestige'] / count($community->getMembers())) : 0)->setMembersCount(count($community->getMembers()))->setMaxPrestige($aggregator['maxPrestige'])->setPveSumMobKills($aggregator['pveSumMobKills'])->setPveSumBossKills($aggregator['pveSumBossKills'])->setPveSumDeaths($aggregator['pveSumDeaths'])->setPvpSumTime($aggregator['pvpSumTime'])->setPvpSumKills($aggregator['pvpSumKills'])->setPvpSumDeaths($aggregator['pvpSumDeaths'])->setPvpSumAssists($aggregator['pvpSumAssists']);
     $this->em->persist($newPantheonDateStat);
 }
 /**
  * Load data fixtures with the passed EntityManager
  *
  * @param ObjectManager $manager
  */
 function load(ObjectManager $manager)
 {
     return;
     $dataFile = __DIR__ . '/dump/Pantheon.yml';
     if (!file_exists($dataFile)) {
         throw new RuntimeException(sprintf('No file exist with fixture data on "%s" path', $dataFile));
     }
     $dataList = Yaml::parse($dataFile);
     foreach ($dataList as $item) {
         $pantheon = new Pantheon();
         $pantheon->setId($item['id'])->setName($item['name'])->setUpdatedAt(new \DateTime($item['updated_at']));
         if (!empty($item['img'])) {
             $pantheon->setImg($item['img']);
         }
         $manager->persist($pantheon);
     }
     $manager->flush();
 }
 /**
  * @param OutputInterface $output
  */
 private function findCommunities(OutputInterface $output)
 {
     $this->logger->addInfo('Finding new communities');
     $communities = array();
     try {
         for ($page = 1; $page <= 20; $page++) {
             $responseMessage = $this->parseService->getPage($this->makeCommunitiesMoreUrl(), true, $this->makeCommunitiesUrl(), array('t:zone' => 'bunchZone', 'bunchIndex' => $page));
             $response = json_decode($responseMessage);
             if (!$response) {
                 $this->logger->addInfo(sprintf('Empty page %s', $page));
                 break;
             }
             $pageCommunities = $this->parseService->getCommunities($response->content);
             $this->logger->addInfo(sprintf('Page %s parsed successful, get %s communities', $page, count($pageCommunities)));
             $communities = $communities + $pageCommunities;
             usleep(rand(500, 1500) * 1000);
         }
     } catch (RuntimeException $e) {
         $this->logger->addInfo('Exception: ' . $e->getMessage() . ' ' . $e->getCode());
     }
     foreach ($communities as $parsedCommunity) {
         $community = $this->pantheonRepository->find($parsedCommunity->id);
         if ($community) {
             continue;
         }
         $community = new Pantheon();
         $community->setId($parsedCommunity->id)->setImg($parsedCommunity->pic)->setName($parsedCommunity->name)->setIsActive(0)->setUpdatedAt(new DateTime());
         $this->em->persist($community);
     }
     $this->em->flush();
 }