Fibinger Ádám
2020-07-17 1d106f9875a746657c0aebbe7ecacd51022dcaa2
Webesítés 0.000001 alfa
5 files added
1 files modified
249 ■■■■■ changed files
composer.json 3 ●●●● patch | view | raw | blame | history
index.php 46 ●●●●● patch | view | raw | blame | history
src/BattlefyTeamParser.php 98 ●●●●● patch | view | raw | blame | history
src/TwigFactory.php 63 ●●●●● patch | view | raw | blame | history
templates/html-base.twig 27 ●●●●● patch | view | raw | blame | history
templates/index.twig 12 ●●●●● patch | view | raw | blame | history
composer.json
@@ -12,6 +12,7 @@
  ],
  "require": {
    "php": ">=7.4.0",
    "vfiber/tablegenerator": "*"
    "vfiber/tablegenerator": "*",
    "twig/twig": "^3.0"
  }
}
index.php
New file
@@ -0,0 +1,46 @@
<?php
include 'vendor/autoload.php';
use TableGenerator\DataObject;
use Wargasz\FileHelper;
$twig = \Wargasz\TwigFactory::getEnvironment('templates/');
if (!empty($_POST['teams_json']))
{
    $parser = new \Wargasz\BattlefyTeamParser($_POST['teams_json']);
    $table = $parser->getTeamData();
    $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\HTMLDataTable(['id' => 'ccup']))->setDataObject($do);
    ob_start();
    $HTMLTable->renderTable();
    $tableData = ob_get_clean();
    file_put_contents('result.html', $tableData);
}
else
{
    $tableData = file_get_contents('result.html');
}
echo $twig->render('index.twig', ['table' => $tableData]);
src/BattlefyTeamParser.php
New file
@@ -0,0 +1,98 @@
<?php
namespace Wargasz;
class BattlefyTeamParser
{
    /**
     * @var mixed
     */
    protected $teamData;
    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!");
        }
    }
    public function getTeamData()
    {
        $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 = '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 TrackerHtmlParser($playerFileName);
                $playerData = $player->getPlayerData($playerName);
                $csapat['playerData'][$playerName] = $playerData;
                $team = [
                    'team'    => $csapat['name'],
                    'captain' => $csapat['captain'] == $playerName,
                ];
                $table[] = array_merge($team, $playerData);
            }
        }
        return $table;
    }
}
src/TwigFactory.php
New file
@@ -0,0 +1,63 @@
<?php
namespace Wargasz;
use Twig\Environment;
use Twig\Extension\CoreExtension;
use Twig\Extension\DebugExtension;
use Twig\Loader\FilesystemLoader;
use Twig\TwigFilter;
use Twig\TwigFunction;
class TwigFactory
{
    private const CACHE_DIR = 'cache/';
    /**
     * Visszatér egy beállított Twig_environment -el, amikbe már be van húzva a használt alap függvények
     *
     * @param array $fileSystemPaths
     * @param array $twigEnvironmentOptions
     *
     * @return Environment
     */
    public static function getEnvironment($fileSystemPaths = [], array $twigEnvironmentOptions = [])
    {
        $loader = null;
        if ($fileSystemPaths)
        {
            if (is_string($fileSystemPaths))
            {
                $fileSystemPaths = [$fileSystemPaths];
            }
            $loader = new FilesystemLoader($fileSystemPaths);
        }
        $twigEnvironmentOptions['cache'] = static::CACHE_DIR;
        $twigEnvironmentOptions['auto_reload'] = true;
        $twigEnvironmentOptions['debug'] = true;
        $twigEnvironment = new Environment($loader, $twigEnvironmentOptions);
        $twigEnvironment->addExtension(new DebugExtension());
        return self::addFunctions($twigEnvironment);
    }
    public static function addFunctions(Environment &$te)
    {
        return $te;
    }
    public static function clearFileCache()
    {
        $di = new \RecursiveDirectoryIterator(self::CACHE_DIR, \FilesystemIterator::SKIP_DOTS);
        $ri = new \RecursiveIteratorIterator($di, \RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($ri as $file)
        {
            $file->isDir() ? rmdir($file) : unlink($file);
        }
        return true;
    }
}
templates/html-base.twig
New file
@@ -0,0 +1,27 @@
<!doctype html>
<html lang="en">
<head>
    {% block head %}
        <!-- Required meta tags -->
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
        <!-- Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    {% endblock %}
</head>
<body>
{% block navigation %}
{% endblock %}
{% block body %}
    <h1>Hello, world!</h1>
{% endblock %}
{% block lazyload %}
    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
{% endblock %}
</body>
</html>
templates/index.twig
New file
@@ -0,0 +1,12 @@
{% extends 'html-base.twig' %}
{% block body %}
    <form method="post">
        <div class="form-group">
            <label for="exampleFormControlTextarea1">Teams JSON</label>
            <textarea name="teams_json" class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
        </div>
        <button type="submit" class="btn btn-primary mb-2">Teams JSON frissítése</button>
    </form>
    {{ table|raw }}
{% endblock %}