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