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'));
     /** @var EntityManager $em */
     $this->em = $app['orm.ems'][$this->regionService->getDbConnectionNameByRegion()];
     /** @var StatService $statService */
     $this->statService = $app['stat.skyforge.service'];
     $pantheonRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Pantheon');
     $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 ($playerId = $input->getOption('avatar')) {
         $this->statService->updatePlayer($playerId, true);
     }
     if ($communityId = $input->getOption('id')) {
         $community = $communityRepository->find($communityId);
         if (!$community) {
             $community = $pantheonRepository->find($communityId);
         }
         if (!$community) {
             $this->logger->addInfo(sprintf('Community with id %s not found in db', $communityId));
         } else {
             $this->updateCommunityMembers($community, $output);
             $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                    JOIN pt.members pl\n                    group by pt.id\n                    order by cnt DESC")->getScalarResult();
             $repo = $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();
             $repo = $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);
             $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->pantheonRepository = $this->em->getRepository('Erliz\\SkyforgeBundle\\Entity\\Pantheon');
     if ($input->getOption('id')) {
         $this->updateCommunityDateStat($this->pantheonRepository->find($input->getOption('id')), $output);
     } else {
         /** @var Pantheon $community */
         foreach ($this->pantheonRepository->findAll() as $community) {
             $this->updateCommunityDateStat($community, $output);
         }
     }
     $this->em->flush();
 }
Exemplo n.º 3
0
 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'));
     $authData = $this->regionService->getAuthOptions();
     $client = Client::getInstance();
     //        $client->getEngine()->debug(true);
     $client->getEngine()->addOption('--config=' . $app['config']['app']['path'] . '/phantomjs.json');
     $client->getEngine()->setPath($app['config']['app']['path'] . '/../bin/phantomjs');
     //        $request = $client->getMessageFactory()->createCaptureRequest($authData['url'], $authData['method']);
     //        $request->setRequestData($authData['data']);
     $request = $client->getMessageFactory()->createCaptureRequest('http://erliz.ru/', 'GET');
     $response = $client->getMessageFactory()->createResponse();
     $request->setDelay(5);
     $request->setViewportSize(1920, 1080);
     $request->setOutputFile($app['config']['app']['path'] . '/log/screen_test_' . time() . '.jpg');
     $client->send($request, $response);
     if ($response->getStatus() === 200) {
         print_r($response->getContent());
     }
     //        print_r($client->getLog());
 }
Exemplo n.º 4
0
 private function authorize()
 {
     if ($this->regionService->getRegion() != RegionService::RU_REGION) {
         throw new RuntimeException('Need authorize', 403);
     }
     $this->createClient(true, true);
     $authOptions = $this->regionService->getAuthOptions();
     /** @var ResponseInterface $response */
     if ($authOptions['method'] == 'GET') {
         $response = $this->client->get($authOptions['url']);
     } elseif ($authOptions['method'] == 'POST') {
         $response = $this->client->post($authOptions['url'], array('body' => $authOptions['data']));
     }
     $this->client->get($this->regionService->getProjectUrl() . 'skyforgenews');
     $this->exportCookie();
     //        foreach ($this->history->getRequests() as $request) {
     //            echo $request->getUrl() ."\n";
     //        }
     echo "authorize!\n";
     $this->tryAuthorize = true;
 }
 private function makeCommunitiesMoreUrl()
 {
     return $this->regionService->getProjectUrl() . 'communities.morebutton:loadbunch';
 }
Exemplo n.º 6
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));
     }
 }
 /**
  * @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);
 }