Fibinger Ádám
2020-07-15 a9e4941e46a1246e1d43e15df548c28a2f0f4f2b
Játékos adatok kinyerése + Táblázatba renderelése
2 files added
1 files deleted
2 files modified
305 ■■■■ changed files
composer.json 8 ●●●●● patch | view | raw | blame | history
generate-table.php 108 ●●●●● patch | view | raw | blame | history
src/FileHelper.php 20 ●●●●● patch | view | raw | blame | history
src/TrackerHtmlParser.php 105 ●●●●● patch | view | raw | blame | history
test.php 64 ●●●●● patch | view | raw | blame | history
composer.json
@@ -4,6 +4,14 @@
      "Wargasz\\": "src/"
    }
  },
  "repositories": [
    {
      "type": "git",
      "url": "https://github.com/VFiber/TableGenerator.git"
    }
  ],
  "require": {
    "php": ">=7.4.0",
    "vfiber/tablegenerator": "*"
  }
}
generate-table.php
New file
@@ -0,0 +1,108 @@
<?php
include 'vendor/autoload.php';
use TableGenerator\DataObject;
use Wargasz\FileHelper;
$nyers_json = file_get_contents('teams_20200712.json');
$json = json_decode($nyers_json, true);
if (!is_array($json))
{
    echo "Nem jó a fájl!";
    return false;
}
$csapatok = [];
$warning = [];
foreach ($json 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 = 'workdir/' . FileHelper::normalizeString($csapat['name']);
    if (!file_exists($teamFolderName))
    {
        mkdir($teamFolderName, 0777, true);
    }
    // https://r6.tracker.network/profile/pc/Fiber.I
    foreach ($csapat['players'] as $playerName)
    {
        $playerFileName = $teamFolderName . '/' . FileHelper::normalizeString($playerName) . '.html';
        if (!file_exists($playerFileName))
        {
            $trackerUrl = 'https://r6.tracker.network/profile/pc/' . $playerName;
            echo "Downloading: $trackerUrl => $playerFileName" . PHP_EOL;
            if (!FileHelper::downloadFiles($trackerUrl, $playerFileName))
            {
                echo "Cannot get: $trackerUrl" . PHP_EOL;
            }
        }
        $player = new \Wargasz\TrackerHtmlParser($playerFileName);
        $playerData = $player->getPlayerData($playerName);
        $csapat['playerData'][$playerName] = $playerData;
        $team = [
            'team'    => $csapat['name'],
            'captain' => $csapat['captain'] == $playerName,
        ];
        $table[] = array_merge($team, $playerData);
    }
}
$cols = [
    'team'          => 'Csapat Név',
    'name'          => 'Név',
    'captain'       => ['Kapitány',
        function ($a, $originalRowData) {
            return $a ? "Igen" : "Nem";
        }
    ],
    'valid'         => [
        'Valid',
        function ($a, $originalRowData) {
            return $a ? $originalRowData['level'] >= 100 ? "Igen" : "Nem-LVL" : "Nem-NoUser";
        }
    ],
    'level'         => 'LvL',
    'alias_history' => 'Alias history'
];
$do = new DataObject($cols, $table);
$HTMLTable = (new \TableGenerator\Render\HTMLTable(['id' => 'ccup']))->setDataObject($do);
ob_start();
$HTMLTable->renderTable();
file_put_contents('result.html', ob_get_clean());
src/FileHelper.php
@@ -6,7 +6,7 @@
{
    private static $ch;
    public static function getFile($url, $targetFileName = '')
    public static function downloadFiles($url, $targetFileName = '')
    {
        $ch = self::createCURLObject($url);
@@ -34,7 +34,7 @@
            CURLOPT_URL            => trim($url),
            CURLOPT_SSL_VERIFYPEER => 0,
            CURLOPT_SSL_VERIFYHOST => 0,
            CURLOPT_FOLLOWLOCATION => 1
            CURLOPT_FOLLOWLOCATION => 0
        ];
        if (self::$ch == null)
@@ -46,4 +46,20 @@
        return self::$ch;
    }
    public static function getFiles($directory): array
    {
        $Directory = new \RecursiveDirectoryIterator($directory);
        $Iterator = new \RecursiveIteratorIterator($Directory);
        $Regex = new \RegexIterator($Iterator, '/^.+\.html$/i', \RecursiveRegexIterator::GET_MATCH);
        $files = [];
        foreach ($Regex as $file)
        {
            $files[] = $file;
        }
        return $files;
    }
}
src/TrackerHtmlParser.php
New file
@@ -0,0 +1,105 @@
<?php
namespace Wargasz;
class TrackerHtmlParser
{
    protected $fileContent;
    /**
     * TrackerHtmlParser constructor.
     * @param $fileName
     */
    public function __construct($fileName)
    {
        if (!file_exists($fileName))
        {
            throw new \InvalidArgumentException("Nincs ilyen fájl: " . $fileName);
        }
        $this->fileContent = file_get_contents($fileName);
    }
    public function isPlayerExists()
    {
        return !stristr($this->fileContent, '404 Not Found. We are unable to find your profile.');
    }
    public function getLevel(): int
    {
        if (preg_match('%Level<\/div>.*?<div class=\"trn-defstat__value\">.*?([0-9]+)%sm', $this->fileContent, $matches))
        {
            return $matches[1];
        }
        return 0;
    }
    /**
     * @return array
     */
    public function getAliasHistory(): array
    {
        $matches = [];
        if (!preg_match('%Alias History</h2>.*(<table.*?</table>)%sm', $this->fileContent, $matches))
        {
            return [];
        }
        $dom = new \DOMDocument();
        $dom->loadXML($matches[1]);
        $rows = $dom->getElementsByTagName("tr");
        $history = [];
        foreach ($rows as $row)
        {
            $cells = $row->getElementsByTagName('td');
            if ($cells->length < 2) continue;
            $history[] = [
                'name' => trim($cells->item(0)->nodeValue),
                'date' => trim($cells->item(1)->nodeValue),
            ];
        }
        return $history;
    }
    public function getPlayerData($originalName = ''): array
    {
        if (!$this->isPlayerExists())
        {
            return [
                'name'          => $originalName,
                'valid'         => false,
                'level'         => 0,
                'alias_history' => '',
            ];
        }
        $aliasHistory = $this->getAliasHistory();
        $h = [];
        if (!empty($aliasHistory))
        {
            foreach ($aliasHistory as $item)
            {
                $h[] = $item['name'] . ' => ' . $item['date'];
            }
        }
        $aliasHistory = implode(" / ", $h);
        return [
            'name'          => $originalName,
            'valid'         => true,
            'level'         => $this->getLevel(),
            'alias_history' => $aliasHistory,
        ];
    }
}
test.php
File was deleted