From ef54434d88f2caf5b1e35b30a9c7380af624d190 Mon Sep 17 00:00:00 2001
From: Fibinger Ádám <adam.fibinger@wup.hu>
Date: Fri, 16 Oct 2020 15:44:13 +0200
Subject: [PATCH] JSON letöltés tournament ID alapján

---
 index.php            |   34 +++++++++++++++++++++++++++++-----
 templates/index.twig |    4 ++++
 2 files changed, 33 insertions(+), 5 deletions(-)

diff --git a/index.php b/index.php
index 0bcf0bc..a9eb688 100644
--- a/index.php
+++ b/index.php
@@ -2,10 +2,10 @@
 error_reporting(E_ALL);
 ini_set('display_errors', 1);
 date_default_timezone_set('Europe/Budapest');
+session_start();
 include 'vendor/autoload.php';
 
 use TableGenerator\DataObject;
-use Wargasz\FileHelper;
 
 $twig = \Wargasz\TwigFactory::getEnvironment('templates/');
 
@@ -34,6 +34,29 @@
 	'alias_history' => 'Alias history'
 ];
 
+if (!empty($_POST['tournament_id']) && strlen($_POST['tournament_id']) == 24)
+{
+	// 5f71b30a3ba5b62baf0659a8
+	// https://api.battlefy.com/tournaments/5f71b30a3ba5b62baf0659a8/teams?page=1&limit=64
+
+	if (preg_match('/[a-f0-9]{24}/sm', $_POST['tournament_id']))
+	{
+		$string = file_get_contents('https://api.battlefy.com/tournaments/' . $_POST['tournament_id'] . '/teams?page=1&limit=64');
+
+		$messages[] = 'Downloading tournament ID: ' . $_POST['tournament_id'] . ' => ' . strlen($string);
+
+		if ($string && json_decode($string) !== false)
+		{
+			$_POST['teams_json'] = $string;
+			$_SESSION['tournament_id'] = $_POST['tournament_id'];
+		}
+		else
+		{
+			$messages[] = 'Sikertelen JSON letöltés...';
+		}
+	}
+}
+
 if (!empty($_POST['teams_json']))
 {
 	$forceDownload = isset($_POST['force_download']) && $_POST['force_download'] == 'on';
@@ -41,7 +64,7 @@
 	$parser = new \Wargasz\BattlefyTeamParser($_POST['teams_json']);
 
 	$table = $parser->getTeamData($forceDownload);
-	$messages = $parser->getMessages();
+	$messages = array_merge($messages, $parser->getMessages());
 
 	file_put_contents(\Wargasz\BattlefyTeamParser::getWorkDir() . '/table.json', json_encode($table));
 	file_put_contents(\Wargasz\BattlefyTeamParser::getWorkDir() . '/source-' . time() . '.json', $_POST['teams_json']);
@@ -79,7 +102,8 @@
 }
 
 echo $twig->render('index.twig', [
-	'last_access' => $last_access,
-	'table'       => $tableData,
-	'messages'    => $messages
+	'tournament_id' => $_SESSION['tournament_id'] ?? '',
+	'last_access'   => $last_access,
+	'table'         => $tableData,
+	'messages'      => $messages
 ]);
\ No newline at end of file
diff --git a/templates/index.twig b/templates/index.twig
index 11ea8e2..43afc10 100644
--- a/templates/index.twig
+++ b/templates/index.twig
@@ -8,6 +8,10 @@
 			<label for="exampleFormControlTextarea1">Teams JSON</label>
 			<textarea name="teams_json" class="form-control" id="exampleFormControlTextarea1" rows="3"></textarea>
 		</div>
+		<div class="form-group">
+			<label for="tournament_id">Tournament ID</label>
+			<input name="tournament_id" id="tournament_id" class="form-control" maxlength="24" minlength="24" pattern="[a-f0-9]{24}" value="{{ tournament_id }}"/>
+		</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

--
Gitblit v1.8.0