From b40e9386fb4253a5866b8e4cdcda0c2023783856 Mon Sep 17 00:00:00 2001
From: Fibinger Ádám <adam.fibinger@wup.hu>
Date: Thu, 17 Oct 2019 15:50:49 +0200
Subject: [PATCH] Csapatok előtöltése az API adatok alapján

---
 /dev/null                        |   94 -------------
 common/base.php                  |   14 ++
 EOG/Utils/TwigFactory.php        |    2 
 form.php                         |   84 ++++-------
 templates/admin/stripe-form.twig |  118 ++++++++++++++++
 save-teams.php                   |   20 ++
 EOG/Models/TeamList.php          |   70 ++++++++++
 7 files changed, 257 insertions(+), 145 deletions(-)

diff --git a/EOG/Models/TeamList.php b/EOG/Models/TeamList.php
new file mode 100644
index 0000000..7897eba
--- /dev/null
+++ b/EOG/Models/TeamList.php
@@ -0,0 +1,70 @@
+<?php
+
+
+namespace EOG\Models;
+
+
+class TeamList
+{
+    /**
+     * @var string[] Csapatok listája (előtöltve)
+     */
+    protected $teams = [];
+
+    private $requiredRawFields = ['name', 'seed', 'status'];
+
+    public function fromJson(string $jsonString)
+    {
+        $rawData = json_decode($jsonString, true);
+
+        if ($rawData === null) return false;
+
+        if (!is_array($rawData)) {
+            return false;
+        }
+
+        $this->teams = [];
+
+        foreach ($rawData as $key => $team) {
+            $rawKeys = array_keys($team);
+            if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) {
+                continue;
+            }
+
+            if ($team['status'] == 'checkedIn') {
+                $this->teams[] = $team;
+            }
+        }
+
+        return (bool)count($this->teams);
+    }
+
+    public function getTeamNames()
+    {
+        return array_column($this->teams, 'name');
+    }
+
+    /**
+     * @return string[]
+     */
+    public function getTeams(): array
+    {
+        return $this->teams;
+    }
+
+    /**
+     * @param string[] $teams
+     * @return TeamList
+     */
+    public function setTeams(array $teams): TeamList
+    {
+        $this->teams = $teams;
+
+        return $this;
+    }
+
+    public function getJson()
+    {
+        return json_encode($this->teams);
+    }
+}
\ No newline at end of file
diff --git a/EOG/Utils/TwigFactory.php b/EOG/Utils/TwigFactory.php
index 36f67f1..329d908 100644
--- a/EOG/Utils/TwigFactory.php
+++ b/EOG/Utils/TwigFactory.php
@@ -42,7 +42,7 @@
 			$loader = new FilesystemLoader($fileSystemPaths);
 		}
 
-		$twigEnvironmentOptions['cache'] = static::CACHE_DIR;
+		//$twigEnvironmentOptions['cache'] = static::CACHE_DIR;
 		$twigEnvironmentOptions['auto_reload'] = true;
 
 		if (IS_DEV)
diff --git a/common/base.php b/common/base.php
new file mode 100644
index 0000000..1038268
--- /dev/null
+++ b/common/base.php
@@ -0,0 +1,14 @@
+<?php
+
+ini_set('display_errors', E_ALL);
+
+include "vendor/autoload.php";
+
+define('IS_DEV', true);
+
+define('SITE_ROOT', dirname(__DIR__));
+define('OVERLAY_DIR', dirname(SITE_ROOT) . '/overlays/');
+
+$last_json = OVERLAY_DIR . 'last.json';
+
+$twig = \EOG\Utils\TwigFactory::getEnvironment(SITE_ROOT);
\ No newline at end of file
diff --git a/form.php b/form.php
index 3d6c68c..5340e72 100644
--- a/form.php
+++ b/form.php
@@ -1,65 +1,49 @@
 <?php
-ini_set('display_errors', E_ALL);
-
-include "vendor/autoload.php";
-
-define('IS_DEV', (!isset($_SERVER['HTTP_HOST'])));
-define('SITE_ROOT', __DIR__);
-
-if (IS_DEV)
-{
-	define('OVERLAY_DIR', dirname(__DIR__) . '/esl-overlay/');
-}
-else
-{
-	define('OVERLAY_DIR', dirname(__DIR__) . '/overlays/');
-}
-
-$last_json = OVERLAY_DIR . 'last.json';
-
-$twig = \EOG\Utils\TwigFactory::getEnvironment(SITE_ROOT);
+include_once "common/base.php";
 
 $s = new \EOG\Models\Stripe();
 
-if (!empty($_POST['stripe']))
-{
-	$s->loadFromArray($_POST['stripe']);
+if (!empty($_POST['stripe'])) {
+    $s->loadFromArray($_POST['stripe']);
 
-	if (!empty($s->getState()))
-	{
-		file_put_contents($last_json, $s->getJson());
-	}
+    if (!empty($s->getState())) {
+        file_put_contents($last_json, $s->getJson());
+    }
 
-	$s->setClass('simple');
-	$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
-	file_put_contents(OVERLAY_DIR . 'simple.html', $html_content);
+    $s->setClass('simple');
+    $html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+    file_put_contents(OVERLAY_DIR . 'simple.html', $html_content);
 
-	$s->setClass('team');
-	$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
-	file_put_contents(OVERLAY_DIR . 'team.html', $html_content);
+    $s->setClass('team');
+    $html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+    file_put_contents(OVERLAY_DIR . 'team.html', $html_content);
 
-	$s->setClass('team-ban');
-	$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
-	file_put_contents(OVERLAY_DIR . 'team-ban.html', $html_content);
+    $s->setClass('team-ban');
+    $html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+    file_put_contents(OVERLAY_DIR . 'team-ban.html', $html_content);
 
+} else {
+    if (file_exists($last_json)) {
+        $raw_json = file_get_contents($last_json);
+        $stateArray = json_decode($raw_json, true);
+        if (is_array($stateArray)) {
+            $s->loadFromArray($stateArray);
+        }
+    }
 }
-else
-{
-	if (file_exists($last_json))
-	{
-		$raw_json = file_get_contents($last_json);
-		$stateArray = json_decode($raw_json, true);
-		if (is_array($stateArray))
-		{
-			$s->loadFromArray($stateArray);
-		}
-	}
+
+$teams = new \EOG\Models\TeamList();
+
+if (file_exists(OVERLAY_DIR . 'teams.json')) {
+    var_dump($teams->fromJson(file_get_contents(OVERLAY_DIR . 'teams.json')));
 }
 
 $var = [
-	'stripe'    => $s,
-	'operators' => $s->getOperators(),
-	'post'      => !empty($_POST['stripe'])
+    'stripe' => $s,
+    'teamNames' => $teams->getTeamNames(),
+    'operators' => $s->getOperators(),
+    'post' => !empty($_POST['stripe'])
 ];
-echo $twig->render('templates/admin/form.twig', $var);
+
+echo $twig->render('templates/admin/stripe-form.twig', $var);
 
diff --git a/save-teams.php b/save-teams.php
new file mode 100644
index 0000000..0d2fde4
--- /dev/null
+++ b/save-teams.php
@@ -0,0 +1,20 @@
+<?php
+
+include_once "common/base.php";
+
+if (empty($_POST['json'])) {
+    header('Location: /generator/form.php', true, 302);
+}
+$teams = new \EOG\Models\TeamList();
+
+$success = false;
+
+if ($teams->fromJson($_POST['json'])) {
+    $success = file_put_contents(OVERLAY_DIR . 'teams.json', $teams->getJson());
+}
+
+if ($success) {
+    header('Location: /generator/form.php', true, 302);
+} else {
+    echo "Szar van a palacsintában";
+}
diff --git a/templates/admin/form.twig b/templates/admin/form.twig
deleted file mode 100644
index f1b65a8..0000000
--- a/templates/admin/form.twig
+++ /dev/null
@@ -1,94 +0,0 @@
-{% extends "templates/admin/html-skeleton.twig" %}
-{% block body %}
-	<form action="/generator/form.php" method="post">
-		<input type="hidden" name="stripe[class]" value="team-ban"/>
-
-		<div class="form-group">
-			<div class="form-row">
-				<div class="col-md-8 mb-3">
-					<label for="validationServer01">Kupa neve</label>
-					<input type="text" name="stripe[cup][name]" class="form-control" id="validationServer01"
-						   placeholder="Pld.: 5on5 Bomb Cup" value="{{ stripe.cup.name }}" required>
-				</div>
-				<div class="col-md-2 mb-3">
-					<label for="validationServer02">Kupa sorszám</label>
-					<input type="text" name="stripe[cup][number]" class="form-control" id="validationServer02" placeholder="Pld.: #654"
-						   value="{{ stripe.cup.number }}" required>
-					<div class="valid-feedback">
-						Looks good!
-					</div>
-				</div>
-			</div>
-		</div>
-
-		<div class="form-group">
-			<div class="form-row">
-				<div class="form-group bg-primary">
-					<div class="col-md-12 mb-4">
-						<label for="team_blue_name" class="bg-primary">Kék csapat neve</label>
-						<input type="text" name="stripe[team][blue][name]" class="form-control" id="team_blue_name"
-							   placeholder="Pld.: Impress!ve" value="{{ stripe.team.blue.name }}" required>
-					</div>
-					<div class="col-md-12 mb-4">
-						<label for="op-blue" class="bg-primary">Banolt operátorok</label>
-						<select name="stripe[team][blue][ban][]" class="operators" id="op-blue" style="width: 100%"
-								multiple>
-							{% for operator in operators %}
-								<option value="{{ operator }}" {{ operator in stripe.team.blue.ban ? "selected"}}>{{ operator|capitalize }}</option>
-							{% endfor %}
-						</select>
-					</div>
-				</div>
-				<div class="form-group">
-						<button class="btn btn-info" title="Csapat színcsere" onclick="swapTeams(); return false;"><=></button>
-				</div>
-				<div class="form-group bg-warning">
-					<div class="col-md-12 mb-4">
-						<label for="team_orange_name">Narancs csapat neve</label>
-						<input type="text" name="stripe[team][orange][name]" class="form-control" id="team_orange_name"
-							   placeholder="Pld.: Opress!ve" value="{{ stripe.team.orange.name }}" required>
-					</div>
-					<div class="col-md-12 mb-4">
-						<label for="op-orange" class="bg-warning">Banolt operátorok</label>
-						<select name="stripe[team][orange][ban][]" class="operators" id="op-orange" style="width: 100%"
-								multiple>
-							{% for operator in operators %}
-								<option value="{{ operator }}" {{ operator in stripe.team.orange.ban ? "selected"}}>{{ operator|capitalize }}</option>
-							{% endfor %}
-						</select>
-					</div>
-				</div>
-			</div>
-		</div>
-		<div class="form-group">
-			<button class="btn btn-primary mx-auto" type="submit">Overlay felülírása</button>
-		</div>
-	</form>
-
-	<form action="/generator/form.php" method="post">
-
-	</form>
-{% endblock %}
-{% block lazyload %}
-	{{ parent() }}
-	<script type="text/javascript">
-        $('.operators').chosen(
-            {
-                'placeholder_text_multiple': 'Banolt operátorok',
-                'max_selected_options': 2
-            }
-        );
-
-        function swapTeams() {
-        	var blue = document.getElementById('team_blue_name');
-        	var orange = document.getElementById('team_orange_name');
-
-        	var blue_name = blue.value;
-        	blue.value = orange.value;
-        	orange.value = blue_name;
-
-        	return false;
-		}
-
-	</script>
-{% endblock %}
\ No newline at end of file
diff --git a/templates/admin/stripe-form.twig b/templates/admin/stripe-form.twig
new file mode 100644
index 0000000..e315559
--- /dev/null
+++ b/templates/admin/stripe-form.twig
@@ -0,0 +1,118 @@
+{% extends "templates/admin/html-skeleton.twig" %}
+{% block body %}
+    <form action="/generator/form.php" method="post">
+        <input type="hidden" name="stripe[class]" value="team-ban"/>
+
+        <div class="form-group">
+            <div class="form-row">
+                <div class="col-md-8 mb-3">
+                    <label for="validationServer01">Kupa neve</label>
+                    <input type="text" name="stripe[cup][name]" class="form-control" id="validationServer01"
+                           placeholder="Pld.: 5on5 Bomb Cup" value="{{ stripe.cup.name }}" required>
+                </div>
+                <div class="col-md-2 mb-3">
+                    <label for="validationServer02">Kupa sorszám</label>
+                    <input type="text" name="stripe[cup][number]" class="form-control" id="validationServer02"
+                           placeholder="Pld.: #654"
+                           value="{{ stripe.cup.number }}" required>
+                </div>
+            </div>
+        </div>
+        <div class="form-group">
+            <div class="form-row">
+                <label for="teamname">Csapat gyorsfeltöltés:</label>
+                <select id="teamname">
+                    {% for teamName in teamNames %}
+                        <option value="{{ teamName }}">{{ teamName }}</option>
+                    {% endfor %}
+                </select>
+                <button class="btn btn-primary"
+                        title="Legyen az itt kiválasztott a kék csapat"
+                        onclick="$('#team_blue_name').val($('#teamname').val()); return false;">Legyen kék</button>
+
+                <button class="btn btn-warning"
+                        title="Csapat színcsere"
+                        onclick="$('#team_orange_name').val($('#teamname').val()); return false;">Legyen narancs</button>
+            </div>
+        </div>
+        <div class="form-group">
+            <div class="form-row">
+                <div class="form-group bg-primary">
+                    <div class="col-md-12 mb-4">
+                        <label for="team_blue_name" class="bg-primary">Kék csapat neve</label>
+                        <input type="text" name="stripe[team][blue][name]" class="form-control" id="team_blue_name"
+                               placeholder="Pld.: Impress!ve" value="{{ stripe.team.blue.name }}" required>
+                    </div>
+                    <div class="col-md-12 mb-4">
+                        <label for="op-blue" class="bg-primary">Banolt operátorok</label>
+                        <select name="stripe[team][blue][ban][]" class="operators" id="op-blue" style="width: 100%"
+                                multiple>
+                            {% for operator in operators %}
+                                <option value="{{ operator }}" {{ operator in stripe.team.blue.ban ? "selected" }}>{{ operator|capitalize }}</option>
+                            {% endfor %}
+                        </select>
+                    </div>
+                </div>
+                <div class="form-group">
+                    <button class="btn btn-info" title="Csapat színcsere" onclick="swapTeams(); return false;"><=>
+                    </button>
+                </div>
+                <div class="form-group bg-warning">
+                    <div class="col-md-12 mb-4">
+                        <label for="team_orange_name">Narancs csapat neve</label>
+                        <input type="text" name="stripe[team][orange][name]" class="form-control" id="team_orange_name"
+                               placeholder="Pld.: Opress!ve" value="{{ stripe.team.orange.name }}" required>
+                    </div>
+                    <div class="col-md-12 mb-4">
+                        <label for="op-orange" class="bg-warning">Banolt operátorok</label>
+                        <select name="stripe[team][orange][ban][]" class="operators" id="op-orange" style="width: 100%"
+                                multiple>
+                            {% for operator in operators %}
+                                <option value="{{ operator }}" {{ operator in stripe.team.orange.ban ? "selected" }}>{{ operator|capitalize }}</option>
+                            {% endfor %}
+                        </select>
+                    </div>
+                </div>
+            </div>
+        </div>
+        <div class="form-group">
+            <button class="btn btn-primary mx-auto" type="submit">Overlay felülírása</button>
+        </div>
+    </form>
+
+    <form action="/generator/save-teams.php" method="post">
+        <div class="form-group">
+            <div class="col-md-4 mb-4">
+                <label for="json">Csapatok API Input adatai:</label>
+                Pld: https://api.eslgaming.com/play/v1/leagues/198846/contestants válasz eredménye
+                <textarea class="form-control" name="json" id="json"></textarea>
+                <button class="btn btn-secondary" type="submit">Csapatok frissítése</button>
+            </div>
+        </div>
+    </form>
+{% endblock %}
+{% block lazyload %}
+    {{ parent() }}
+    <script type="text/javascript">
+        $('.operators').chosen(
+            {
+                'placeholder_text_multiple': 'Banolt operátorok',
+                'max_selected_options': 2
+            }
+        );
+
+        $('#teamname').chosen();
+
+        function swapTeams() {
+            var blue = document.getElementById('team_blue_name');
+            var orange = document.getElementById('team_orange_name');
+
+            var blue_name = blue.value;
+            blue.value = orange.value;
+            orange.value = blue_name;
+
+            return false;
+        }
+
+    </script>
+{% endblock %}
\ No newline at end of file

--
Gitblit v1.8.0