Fibinger Ádám
2020-07-31 7e1ea4d5fb16450ce81f9b7f39784e7a84b73f9b
commit | author | age
1d106f 1 <?php
2
3
4 namespace Wargasz;
5
6
7 class BattlefyTeamParser
8 {
9     /**
10      * @var mixed
11      */
12     protected $teamData;
13
7e1ea4 14     /**
15      * @var array
16      */
17     protected $parsedTeamData = [];
18
19     /**
20      * @var string[]
21      */
22     protected $messages = [];
23
24     private $downloaded = 0;
25
1d106f 26     public function __construct(string $jsonContent = '')
27     {
28         $this->teamData = json_decode($jsonContent, true);
29
30         if (!is_array($this->teamData))
31         {
32             throw new \InvalidArgumentException("Nem dekódolható a JSON megfelelően!");
33         }
34     }
35
7e1ea4 36     /**
37      * @param bool $forceDownload Ha 'true', akkor mindenképp frissíti a lokális fájlt.
38      * @return array
39      */
40     public function getTeamData($forceDownload = false)
1d106f 41     {
42         $csapatok = [];
43         $warning = [];
44
45         foreach ($this->teamData as $team)
46         {
47             $jatekosok = [];
48
49             foreach ($team['players'] as $playerName)
50             {
51                 $jatekosok[] = $playerName['inGameName'];
52             };
53
54             $csapatok[] = [
55                 'name'    => $team['name'],
56                 'captain' => $team['captain']['inGameName'],
57                 'players' => $jatekosok
58             ];
59
60             if (count($jatekosok) > 8)
61             {
62                 $warning[] = [
63                     'team' => $team['name'],
64                     'msg'  => 'Túl sok játékos a csapatban!',
65                 ];
66             }
67         }
68
69         $table = [];
70         foreach ($csapatok as &$csapat)
71         {
7e1ea4 72             $teamFolderName = self::getWorkDir() . '/' . FileHelper::normalizeString($csapat['name']);
73
1d106f 74             if (!file_exists($teamFolderName))
75             {
76                 mkdir($teamFolderName, 0777, true);
77             }
78
79             foreach ($csapat['players'] as $playerName)
80             {
81                 $playerFileName = $teamFolderName . '/' . FileHelper::normalizeString($playerName) . '.html';
82
7e1ea4 83                 if ($forceDownload || !file_exists($playerFileName))
1d106f 84                 {
85                     $trackerUrl = 'https://r6.tracker.network/profile/pc/' . $playerName;
86
7e1ea4 87                     $this->messages[] = "Downloading: $trackerUrl => $playerFileName";
1d106f 88
89                     if (!FileHelper::downloadFiles($trackerUrl, $playerFileName))
90                     {
7e1ea4 91                         $this->messages[] = "Cannot get: $trackerUrl";
92                     }
93
94                     $this->downloaded++;
95
96                     if (rand($this->downloaded, 10) > 5)
97                     {
98                         sleep(2);
99                         $this->downloaded = 0;
1d106f 100                     }
101                 }
102
103                 $player = new TrackerHtmlParser($playerFileName);
104
105                 $playerData = $player->getPlayerData($playerName);
7e1ea4 106
107                 if ($playerData['valid'] == false) {
108                     unlink($playerFileName);
109                 }
1d106f 110
111                 $csapat['playerData'][$playerName] = $playerData;
112
113                 $team = [
114                     'team'    => $csapat['name'],
115                     'captain' => $csapat['captain'] == $playerName,
116                 ];
117
118                 $table[] = array_merge($team, $playerData);
119             }
120         }
121
7e1ea4 122         $this->parsedTeamData = $csapat;
123
1d106f 124         return $table;
125     }
126
7e1ea4 127     /**
128      * @return array
129      */
130     public function getParsedTeamData(): array
131     {
132         return $this->parsedTeamData;
133     }
134
135     /**
136      * @return string[]
137      */
138     public function getMessages(): array
139     {
140         return $this->messages;
141     }
142
143     public static function getWorkDir(\DateTime $date = null)
144     {
145         if ($date === null)
146         {
147             $date = new \DateTime();
148         }
149
150         $yearWeek = $date->format("Y-W");
151         return 'workdir/' . $yearWeek;
152     }
153
1d106f 154 }