From 7e1ea4d5fb16450ce81f9b7f39784e7a84b73f9b Mon Sep 17 00:00:00 2001
From: Fibinger Ádám <adam.fibinger@wup.hu>
Date: Fri, 31 Jul 2020 17:20:21 +0200
Subject: [PATCH] Néhány apróbb update

---
 index.php                  |   26 +++++++++++--
 templates/index.twig       |   14 +++++++
 src/BattlefyTeamParser.php |   70 +++++++++++++++++++++++++++++++---
 src/FileHelper.php         |    2 
 4 files changed, 100 insertions(+), 12 deletions(-)

diff --git a/index.php b/index.php
index 57b1ba6..7a7195f 100644
--- a/index.php
+++ b/index.php
@@ -1,4 +1,6 @@
 <?php
+error_reporting(E_ALL);
+ini_set('display_errors', 1);
 include 'vendor/autoload.php';
 
 use TableGenerator\DataObject;
@@ -6,15 +8,24 @@
 
 $twig = \Wargasz\TwigFactory::getEnvironment('templates/');
 
+$messages = [];
+
 if (!empty($_POST['teams_json']))
 {
+	$forceDownload = isset($_POST['force_download']) && $_POST['force_download'] == 'on';
+
 	$parser = new \Wargasz\BattlefyTeamParser($_POST['teams_json']);
 
-	$table = $parser->getTeamData();
+	$table = $parser->getTeamData($forceDownload);
+	$messages = $parser->getMessages();
 
 	$cols = [
 		'team'          => 'Csapat Név',
-		'name'          => 'Név',
+		'name'          => ['Név',
+			function ($a, $originalRowData) {
+				return '<a href="https://r6.tracker.network/profile/pc/' . $a . '">' . $a . '</a>';
+			}
+		],
 		'captain'       => ['Kapitány',
 			function ($a, $originalRowData) {
 				return $a ? "Igen" : "Nem";
@@ -30,9 +41,13 @@
 		'alias_history' => 'Alias history'
 	];
 
+	file_put_contents(\Wargasz\BattlefyTeamParser::getWorkDir() . '/source-' . time() . '.json', $_POST['teams_json']);
+	file_put_contents(\Wargasz\BattlefyTeamParser::getWorkDir() . '/parsed.json', json_encode($parser->getParsedTeamData()));
+
+	//fixme: szétszedni, hogy ez ne a táblázatot írja meg egyből, hanem a nyers adatokat és azt olvassa vissza
 	$do = new DataObject($cols, $table);
 
-	$HTMLTable = (new \TableGenerator\Render\HTMLDataTable(['id' => 'ccup']))->setDataObject($do);
+	$HTMLTable = (new \TableGenerator\Render\HTMLTable(['id' => 'ccup']))->setDataObject($do);
 	ob_start();
 	$HTMLTable->renderTable();
 	$tableData = ob_get_clean();
@@ -43,4 +58,7 @@
 	$tableData = file_get_contents('result.html');
 }
 
-echo $twig->render('index.twig', ['table' => $tableData]);
\ No newline at end of file
+echo $twig->render('index.twig', [
+	'table'    => $tableData,
+	'messages' => $messages
+]);
\ No newline at end of file
diff --git a/src/BattlefyTeamParser.php b/src/BattlefyTeamParser.php
index 276fa93..a77b470 100644
--- a/src/BattlefyTeamParser.php
+++ b/src/BattlefyTeamParser.php
@@ -11,6 +11,18 @@
 	 */
 	protected $teamData;
 
+	/**
+	 * @var array
+	 */
+	protected $parsedTeamData = [];
+
+	/**
+	 * @var string[]
+	 */
+	protected $messages = [];
+
+	private $downloaded = 0;
+
 	public function __construct(string $jsonContent = '')
 	{
 		$this->teamData = json_decode($jsonContent, true);
@@ -21,7 +33,11 @@
 		}
 	}
 
-	public function getTeamData()
+	/**
+	 * @param bool $forceDownload Ha 'true', akkor mindenképp frissíti a lokális fájlt.
+	 * @return array
+	 */
+	public function getTeamData($forceDownload = false)
 	{
 		$csapatok = [];
 		$warning = [];
@@ -51,35 +67,46 @@
 		}
 
 		$table = [];
-
 		foreach ($csapatok as &$csapat)
 		{
-			$teamFolderName = 'workdir/' . FileHelper::normalizeString($csapat['name']);
+			$teamFolderName = self::getWorkDir() . '/' . 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))
+				if ($forceDownload || !file_exists($playerFileName))
 				{
 					$trackerUrl = 'https://r6.tracker.network/profile/pc/' . $playerName;
 
-					echo "Downloading: $trackerUrl => $playerFileName" . PHP_EOL;
+					$this->messages[] = "Downloading: $trackerUrl => $playerFileName";
 
 					if (!FileHelper::downloadFiles($trackerUrl, $playerFileName))
 					{
-						echo "Cannot get: $trackerUrl" . PHP_EOL;
+						$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;
 
@@ -92,7 +119,36 @@
 			}
 		}
 
+		$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;
+	}
+
 }
\ No newline at end of file
diff --git a/src/FileHelper.php b/src/FileHelper.php
index 51b62ce..3c18555 100644
--- a/src/FileHelper.php
+++ b/src/FileHelper.php
@@ -23,7 +23,7 @@
 
 	public static function normalizeString($source)
 	{
-		$pattern = "/[^[0-9][A-Z][a-z]]/";
+		$pattern = '/[^a-zA-Z0-9\ \.\-\_]/';
 		return preg_replace($pattern, "", $source);
 	}
 
diff --git a/templates/index.twig b/templates/index.twig
index b4da074..ad693ab 100644
--- a/templates/index.twig
+++ b/templates/index.twig
@@ -5,8 +5,22 @@
 			<label for="exampleFormControlTextarea1">Teams JSON</label>
 			<textarea name="teams_json" class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
 		</div>
+		<div class="form-check">
+			<input type="checkbox" class="form-check-input" id="exampleCheck1" name="force_download">
+			<label class="form-check-label" for="exampleCheck1">R6 Tracker fájlok újraletöltése (cache frissítés, ha van
+				változás menet közben)</label>
+		</div>
 		<button type="submit" class="btn btn-primary mb-2">Teams JSON frissítése</button>
 	</form>
 
+	{% if messages %}
+		<ul class="list-group">
+			<li class="list-group-item active">Rendszerüzenetek</li>
+			{% for message in messages %}
+				<li class="list-group-item">{{ message }}</li>
+			{% endfor %}
+		</ul>
+	{% endif %}
+
 	{{ table|raw }}
 {% endblock %}
\ No newline at end of file

--
Gitblit v1.8.0