protected function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getProjectApplication();
     $this->logger = $this->getLogger();
     $this->regionService = $app['region.skyforge.service'];
     $this->regionService->setRegion($input->getOption('region'));
     $this->em = $app['orm.ems'][$this->regionService->getDbConnectionNameByRegion()];
     $this->parseService = $app['parse.skyforge.service'];
     $this->parseService->setAuthData($this->regionService->getCredentials());
     $this->playerRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Player');
     $this->pantheonRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Pantheon');
     $this->communityRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Community');
     //        $lockFilePath = $app['config']['app']['path'].'/cache/curl/parse.lock';
     //        if (is_file($lockFilePath)) {
     //            throw new \RuntimeException('Another parse in progress');
     //        } else {
     //            file_put_contents($lockFilePath, getmypid());
     //        }
     if ($communityId = $input->getOption('id')) {
         $community = $this->communityRepository->find($communityId);
         $type = $this::TYPE_COMMUNITY;
         if (!$community) {
             $community = $this->pantheonRepository->find($communityId);
             $type = $this::TYPE_PANTHEON;
         }
         if (!$community) {
             $this->logger->addInfo(sprintf('Community with id %s not found in db', $communityId));
         } else {
             $this->updateCommunityMembers($community, $output, $type);
             $this->flush();
         }
     }
     if ($input->getOption('pantheons') || $input->getOption('communities')) {
         $lastId = $input->getOption('lastId');
         if ($input->getOption('pantheons')) {
             $sqlResponse = $this->em->createQuery("\n                    SELECT pt.id, count(pl.id) cnt\n                    FROM Erliz\\SkyforgeBundle\\Entity\\Pantheon pt\n                    LEFT JOIN pt.members pl\n                    group by pt.id\n                    order by cnt DESC")->getScalarResult();
             $type = $this::TYPE_PANTHEON;
             $repo = $this->pantheonRepository;
         } else {
             $sqlResponse = $this->em->createQuery("\n                    SELECT pt.id, count(pl.id) cnt\n                    FROM Erliz\\SkyforgeBundle\\Entity\\Community pt\n                    JOIN pt.members pl\n                    group by pt.id\n                    order by cnt DESC")->getScalarResult();
             $type = $this::TYPE_COMMUNITY;
             $repo = $this->communityRepository;
         }
         $communityIds = array_map('current', $sqlResponse);
         $communitiesCount = count($communityIds);
         /** @var CommunityInterface $community */
         foreach ($communityIds as $index => $communityId) {
             if ($communityId == $lastId) {
                 $lastId = false;
             }
             if ($lastId) {
                 continue;
             }
             $this->updateCommunityMembers($repo->find($communityId), $output, $type);
             $this->logger->addInfo(sprintf('Processed %s / %s', $index + 1, $communitiesCount));
             $this->flush();
         }
     }
     //        unlink($lockFilePath);
 }
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $app = $this->getProjectApplication();
     $this->logger = $this->getLogger();
     $this->regionService = $app['region.skyforge.service'];
     $this->regionService->setRegion($input->getOption('region'));
     $this->em = $app['orm.ems'][$this->regionService->getDbConnectionNameByRegion()];
     $this->parseService = $app['parse.skyforge.service'];
     $this->parseService->setAuthData($this->regionService->getCredentials());
     $this->pantheonRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Pantheon');
     $lockFilePath = $app['config']['app']['path'] . '/cache/curl/parse.lock';
     if (is_file($lockFilePath)) {
         throw new \RuntimeException('Another parse in progress');
     } else {
         file_put_contents($lockFilePath, getmypid());
     }
     $this->findCommunities($output);
     unlink($lockFilePath);
 }
Пример #3
0
 /**
  * @param string $playerId
  */
 public function updatePlayer($playerId, $withFlush = false)
 {
     $app = $this->getApp();
     /** @var EntityManager $em */
     $em = $this->getEntityManager();
     /** @var ParseService $parseService */
     $parseService = $app['parse.skyforge.service'];
     $parseService->setAuthData($this->regionService->getCredentials());
     /** @var Player $player */
     $player = $this->playerRepo->find($playerId);
     try {
         $avatarDataTimeStart = microtime(true);
         $avatarData = $parseService->getDataFromProfilePage($parseService->getPage($this->makeProfileUrlByPlayerId($playerId)));
         $this->logger->addInfo(sprintf('Avatar data take %s sec to proceed', microtime(true) - $avatarDataTimeStart));
         $statDataTimeStart = microtime(true);
         $statData = json_decode($parseService->getPage($this->makeStatUrlByPlayerId($playerId), true));
         $this->logger->addInfo(sprintf('Stat data take %s sec to proceed', microtime(true) - $statDataTimeStart));
     } catch (RuntimeException $e) {
         if ($e->getCode() == 403) {
             throw new RuntimeException(sprintf('Stat data are closed for player "%s"', $playerId), 403);
         } else {
             throw $e;
         }
     }
     if (!$player) {
         $player = new Player();
         $player->setId($playerId);
         $em->persist($player);
     }
     $currentPantheon = null;
     if ($avatarData && $avatarData['pantheon_id']) {
         $currentPantheon = $this->pantheonRepo->find($avatarData['pantheon_id']);
     }
     $currentRole = $this->roleRepo->findOneBy(array('name' => $avatarData['role_name']));
     if (empty($currentRole)) {
         $currentRole = $this->roleRepo->find(1);
     }
     $today = new \DateTime('-4 hour');
     $shownDates = $statData->avatarStats->daysToShow;
     $dailyStatsLoopTimeStart = microtime(true);
     foreach ($statData->avatarStats->dailyStats as $key => $dayStat) {
         // -4 hour server update on 4 hour at night
         $date = new \DateTime($key - $shownDates + 1 . ' day -4 hour');
         $totalTime = null;
         if ($key + 1 == $shownDates) {
             $totalTime = $statData->avatarStats->secondsPlayed;
         }
         if ($oldDateStat = $this->dateStatRepo->findOneBy(array('player' => $player->getId(), 'date' => $date))) {
             if ($today->format('Y-m-d') == $oldDateStat->getDate()->format('Y-m-d')) {
                 $oldDateStat->setCurrentPrestige($avatarData['prestige']['current'])->setMaxPrestige($avatarData['prestige']['max'])->setRole($currentRole)->setPantheon($currentPantheon)->setTotalTime($totalTime);
             }
             $oldDateStat->setPveMobKills($dayStat->pveMobKills)->setPveBossKills($dayStat->pveBossKills)->setPveDeaths($dayStat->pveDeaths)->setPvpKills($dayStat->pvpKills)->setPvpDeaths($dayStat->pvpDeaths)->setPvpAssists($dayStat->pvpAssists);
             continue;
         }
         $player->setName($avatarData['name'])->setNick($avatarData['nick'])->setImg($avatarData['img'])->setCurrentRole($currentRole);
         $dateStat = new PlayerDateStat();
         $dateStat->setPlayer($player)->setDate($date)->setRole($currentRole)->setPantheon($currentPantheon)->setTotalTime($totalTime)->setCurrentPrestige($avatarData['prestige']['current'])->setMaxPrestige($avatarData['prestige']['max'])->setPveMobKills($dayStat->pveMobKills)->setPveBossKills($dayStat->pveBossKills)->setPveDeaths($dayStat->pveDeaths)->setPvpKills($dayStat->pvpKills)->setPvpDeaths($dayStat->pvpDeaths)->setPvpAssists($dayStat->pvpAssists);
         if (isset($statData->adventureStats) && isset($statData->adventureStats->byAdventureStats)) {
             $dateStat->setPvpTime($this->getTimeSpentByAdventureType($statData->adventureStats->byAdventureStats, $this::PVP_TYPE));
         }
         $em->persist($dateStat);
     }
     $this->logger->addInfo(sprintf('Daily stat take %s sec to proceed', microtime(true) - $dailyStatsLoopTimeStart));
     $classStatsLoopTimeStart = microtime(true);
     foreach ($statData->avatarStats->classStats as $roleData) {
         $role = $this->roleRepo->findOneBy(array('resourceId' => $roleData->characterClass->resourceId));
         if (!$role) {
             throw new RuntimeException('New role detected ' . json_encode($roleData->characterClass));
         }
         $roleStat = $this->roleStatRepo->findOneBy(array('player' => $player->getId(), 'role' => $role->getId()));
         if (!$roleStat) {
             $roleStat = new PlayerRoleStat();
             $roleStat->setPlayer($player)->setRole($role);
             $em->persist($roleStat);
         }
         $roleDataStat = $roleData->stats;
         $roleStat->setSecondsActivePlayed($roleData->secondsActivePlayed)->setSecondsPlayed($roleData->secondsPlayed)->setPveMobKills($roleDataStat->pveMobKills)->setPveBossKills($roleDataStat->pveBossKills)->setPveDeaths($roleDataStat->pveDeaths)->setPvpKills($roleDataStat->pvpKills)->setPvpDeaths($roleDataStat->pvpDeaths)->setPvpAssists($roleDataStat->pvpAssists);
     }
     $this->logger->addInfo(sprintf('Class stat take %s sec to proceed', microtime(true) - $classStatsLoopTimeStart));
     if ($withFlush) {
         $flushTimeStart = microtime(true);
         $em->flush();
         $this->logger->addInfo(sprintf('Flush db take %s sec to proceed', microtime(true) - $flushTimeStart));
     }
 }