/**
  * Load data fixtures with the passed EntityManager
  *
  * @param ObjectManager $manager
  */
 function load(ObjectManager $manager)
 {
     return;
     $dataFile = __DIR__ . '/dump/PlayerRoleStat.yml';
     if (!file_exists($dataFile)) {
         throw new RuntimeException(sprintf('No file exist with fixture data on "%s" path', $dataFile));
     }
     $dataList = Yaml::parse($dataFile);
     $playerRepo = $manager->getRepository('Erliz\\SkyforgeBundle\\Entity\\Player');
     $roleRepo = $manager->getRepository('Erliz\\SkyforgeBundle\\Entity\\Role');
     foreach ($dataList as $item) {
         $playerRoleStat = new PlayerRoleStat();
         $playerRoleStat->setPlayer($playerRepo->find($item['player']))->setRole($roleRepo->findOneBy(array('name' => $item['role'])))->setSecondsPlayed($item['seconds_played'])->setSecondsActivePlayed($item['seconds_active_played'])->setPveBossKills($item['pve_boss_kills'])->setPveMobKills($item['pve_mob_kills'])->setPveDeaths($item['pve_deaths'])->setPvpKills($item['pvp_kills'])->setPvpDeaths($item['pvp_deaths'])->setPvpAssists($item['pvp_assists'])->setModifiedAt(new \DateTime($item['modified_at']));
         $manager->persist($playerRoleStat);
         $manager->flush();
     }
 }
Example #2
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));
     }
 }