From cb9ca2b735b9fedc479799e88ddb731251b35b08 Mon Sep 17 00:00:00 2001 From: Fibinger Ádám <adam.fibinger@wup.hu> Date: Sun, 10 Nov 2019 13:27:28 +0100 Subject: [PATCH] ESL Api alapján JSON-ból felhúzni a csapatokat gyors kiegészítésre --- templates/admin/history-round.twig | 67 ++++++++++++- templates/admin/stripe-form.twig | 4 templates/admin/history.twig | 50 +++++++++- save-teams.php | 31 ++++- templates/overlay-base.twig | 2 EOG/Models/TeamList.php | 103 +++++++++++--------- 6 files changed, 186 insertions(+), 71 deletions(-) diff --git a/EOG/Models/TeamList.php b/EOG/Models/TeamList.php index 7897eba..f61a8de 100644 --- a/EOG/Models/TeamList.php +++ b/EOG/Models/TeamList.php @@ -6,65 +6,72 @@ class TeamList { - /** - * @var string[] Csapatok listája (előtöltve) - */ - protected $teams = []; + /** + * @var string[] Csapatok listája (előtöltve) + */ + protected $teams = []; - private $requiredRawFields = ['name', 'seed', 'status']; + private $requiredRawFields = ['name', 'seed', 'status']; - public function fromJson(string $jsonString) - { - $rawData = json_decode($jsonString, true); + public function fromJson(string $jsonString) : int + { + $rawData = json_decode($jsonString, true); - if ($rawData === null) return false; + if ($rawData === null) + { + throw new \InvalidArgumentException("Could not decode JSON"); + } - if (!is_array($rawData)) { - return false; - } + if (!is_array($rawData)) + { + throw new \InvalidArgumentException("Data is not array"); + } - $this->teams = []; + $this->teams = []; - foreach ($rawData as $key => $team) { - $rawKeys = array_keys($team); - if (count(array_intersect($rawKeys, $this->requiredRawFields)) != 3) { - continue; - } + 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; - } - } + if ($team['status'] == 'checkedIn') + { + $this->teams[] = $team; + } + } - return (bool)count($this->teams); - } + return (bool)count($this->teams); + } - public function getTeamNames() - { - return array_column($this->teams, 'name'); - } + public function getTeamNames() + { + return array_column($this->teams, 'name'); + } - /** - * @return string[] - */ - public function getTeams(): array - { - return $this->teams; - } + /** + * @return string[] + */ + public function getTeams(): array + { + return $this->teams; + } - /** - * @param string[] $teams - * @return TeamList - */ - public function setTeams(array $teams): TeamList - { - $this->teams = $teams; + /** + * @param string[] $teams + * @return TeamList + */ + public function setTeams(array $teams): TeamList + { + $this->teams = $teams; - return $this; - } + return $this; + } - public function getJson() - { - return json_encode($this->teams); - } + public function getJson() + { + return json_encode($this->teams); + } } \ No newline at end of file diff --git a/save-teams.php b/save-teams.php index 0d2fde4..d9906c1 100644 --- a/save-teams.php +++ b/save-teams.php @@ -2,19 +2,34 @@ include_once "common/base.php"; -if (empty($_POST['json'])) { - header('Location: /generator/form.php', true, 302); +if (empty($_POST['json'])) +{ + header('Location: /generator/form.php', true, 302); } $teams = new \EOG\Models\TeamList(); +if (!$teams->fromJson($_POST['json'])) +{ + echo "Nincs benne becsekkolt csapat."; + return; + +} + $success = false; -if ($teams->fromJson($_POST['json'])) { - $success = file_put_contents(OVERLAY_DIR . 'teams.json', $teams->getJson()); +$success = file_put_contents(TEAMS_JSON, $teams->getJson()); + +if (!$success) +{ + var_dump($teams->getJson()); + var_dump(is_writable(TEAMS_JSON)); } -if ($success) { - header('Location: /generator/form.php', true, 302); -} else { - echo "Szar van a palacsintában"; +if ($success) +{ + header('Location: /generator/form.php', true, 302); } +else +{ + echo "Szar van a palacsintában: " . TEAMS_JSON; +} \ No newline at end of file diff --git a/templates/admin/history-round.twig b/templates/admin/history-round.twig index 168cf7c..f45bff9 100644 --- a/templates/admin/history-round.twig +++ b/templates/admin/history-round.twig @@ -1,10 +1,63 @@ {% set classes = [ 'btn btn-outline-secondary', 'btn btn-outline-success', 'btn btn-outline-danger', 'btn btn-outline-warning', 'btn btn-outline-info', 'btn btn-outline-light', 'btn btn-outline-dark', 'btn btn-outline-link', 'btn btn-outline-primary'] %} {% set activeClasses = [ 'btn btn-secondary', 'btn btn-success', 'btn btn-danger', 'btn btn-warning', 'btn btn-info', 'btn btn-light', 'btn btn-dark', 'btn btn-link', 'btn btn-primary'] %} -<div class="form-row"> - {% for site in history.getCurrentSites %} - <button class="{{ cycle(classes, loop.index0)}}" id="r_{{ loop.parent.loop.index0 }}_b_{{ loop.index0 }}" - data-outline="{{ cycle(classes, loop.index0)}}" data-selected="{{ cycle(activeClasses, loop.index0)}}" onclick="return false;">{{ site }}</button> - {% endfor %} -</div> -{{ dump(round) }} \ No newline at end of file +<div class="round"> + <h4>Round #{{ loop.index }}</h4> + <div class="form-row" id="s_{{ loop.index0 }}"> + <div class="form-row"> + {% for site in history.getCurrentSites %} + <button + class="site {{ site == round.site ? cycle(activeClasses, loop.index0) : cycle(classes, loop.index0) }}" + id="s_{{ loop.parent.loop.index0 }}_b_{{ loop.index0 }}" + data-outline="{{ cycle(classes, loop.index0) }}" + data-selected="{{ cycle(activeClasses, loop.index0) }}" + onclick="return siteSelected(this);" + data-form="s_{{ loop.parent.loop.index0 }}" + data-site="{{ site }}" + data-input="i_{{ loop.parent.loop.index0 }}_site">{{ site }}</button> + {% endfor %} + </div> + </div> + + <div class="form-row" id="w_{{ loop.index0 }}"> + <button class="win {{ round.win == "blue" ? "btn btn-primary" : "btn btn-outline-primary" }}" + id="w_{{ loop.index0 }}_b_{{ loop.index0 }}" + data-outline="btn btn-outline-primary" + data-selected="btn btn-primary" + onclick="return winSelected(this);" + data-form="w_{{ loop.index0 }}" + data-team="blue" + data-input="i_{{ loop.index0 }}_win">{{ stripe.team.blue.name }} <br/>győzelem</button> + <button class="win {{ round.win == "orange" ? "btn btn-warning" : "btn btn-outline-warning" }}" + id="w_{{ loop.index0 }}_b_{{ loop.index0 }}" + data-outline="btn btn-outline-warning" + data-selected="btn btn-warning" + onclick="return winSelected(this);" + data-form="w_{{ loop.index0 }}" + data-team="blue" + data-input="i_{{ loop.index0 }}_win"> {{ stripe.team.orange.name }}<br/> győzelem </button> + </div> + + <div class="form-row" id="r_{{ loop.index0 }}"> + <button class="role {{ round.win == "blue" ? "btn btn-primary" : "btn btn-outline-primary" }}" + id="w_{{ loop.index0 }}_b_{{ loop.index0 }}" + data-outline="btn btn-outline-primary" + data-selected="btn btn-primary" + onclick="return winSelected(this);" + data-form="w_{{ loop.index0 }}" + data-team="blue" + data-input="i_{{ loop.index0 }}_win">{{ stripe.team.blue.name }} <br/>győzelem</button> + <button class="win {{ round.win == "orange" ? "btn btn-warning" : "btn btn-outline-warning" }}" + id="w_{{ loop.index0 }}_b_{{ loop.index0 }}" + data-outline="btn btn-outline-warning" + data-selected="btn btn-warning" + onclick="return winSelected(this);" + data-form="w_{{ loop.index0 }}" + data-team="blue" + data-input="i_{{ loop.index0 }}_win"> {{ stripe.team.orange.name }}<br/> győzelem </button> + </div> + <input type="hidden" id="i_{{ loop.index0 }}_site" name="round[{{ loop.index0 }}][site]" value="{{ round.site }}"> + <input type="hidden" id="i_{{ loop.index0 }}_win" name="round[{{ loop.index0 }}][win]" value="{{ round.win }}"> + <input type="hidden" id="i_{{ loop.index0 }}_win_role" name="round[{{ loop.index0 }}][win_role]" + value="{{ round.win_role }}"> +</div> \ No newline at end of file diff --git a/templates/admin/history.twig b/templates/admin/history.twig index 387fabf..6af5d76 100644 --- a/templates/admin/history.twig +++ b/templates/admin/history.twig @@ -1,6 +1,10 @@ {% extends "admin/html-skeleton.twig" %} {% block body %} - <form action="/generator/match-history.php" method="post"> + <form action="/generator/match-history.php" method="post" onsubmit="return false;"> + <input type="submit" value="Mentés"> + <label for="currentMap"> + Map: + </label> <select name="currentMap" id="currentMap" onchange="updateSites()"> {% for map in history.getMapNames %} <option value="{{ map }}" {{ history.getCurrentMap == map ? "selected" }}>{{ map }}</option> @@ -20,9 +24,45 @@ {{ parent() }} <script type="text/javascript"> var maps = {{ history.getMaps|json_encode|raw }} - function updateSites() { - var currentMap = document.getElementById('currentMap').value; - console.log(maps[currentMap]); - } + function updateSites() { + var currentMap = document.getElementById('currentMap').value; + console.log(maps[currentMap]); + } + + + function siteSelected(button) + { + var but = $(button); + var button_data = but.data(); + resetSiteOutlines(button_data.form); + $('#' + button_data.input).val(button_data.site); + but.removeClass().addClass("site " + but.data('selected')); + return false; + } + + function resetSiteOutlines(form_id) { + $('#'+form_id + ' button.site').each(function (idx, button) { + const but = $(button); + but.removeClass().addClass("site " + but.data('outline')); + console.error(but); + }); + } + + function winSelected(button) { + var but = $(button); + var button_data = but.data(); + resetWinOutlines(button_data.form); + $('#' + button_data.input).val(button_data.site); + but.removeClass().addClass("win " + but.data('selected')); + return false; + } + + function resetWinOutlines(form_id) { + $('#'+form_id + ' button.win').each(function (idx, button) { + const but = $(button); + but.removeClass().addClass("win " + but.data('outline')); + }); + } + </script> {% endblock %} \ No newline at end of file diff --git a/templates/admin/stripe-form.twig b/templates/admin/stripe-form.twig index 11898f5..6d759fd 100644 --- a/templates/admin/stripe-form.twig +++ b/templates/admin/stripe-form.twig @@ -80,6 +80,8 @@ </div> </form> + <a href="/generator/match-history.php" class="btn btn-secondary">Meccstörténet</a> + <form action="/generator/save-teams.php" method="post"> <div class="form-group"> <div class="col-md-4 mb-4"> @@ -90,8 +92,6 @@ </div> </div> </form> - - <a href="/generator/match-history.php" class="btn btn-secondary">Meccstörténet</a> {% endblock %} {% block lazyload %} {{ parent() }} diff --git a/templates/overlay-base.twig b/templates/overlay-base.twig index 72d02a5..91eeb45 100644 --- a/templates/overlay-base.twig +++ b/templates/overlay-base.twig @@ -58,7 +58,7 @@ {% block info_right %} <span class="info right"> - <img class="esl-logo" src="/assets/ESL_Logo.png"/> + {# <img class="esl-logo" src="/assets/ESL_Logo.png"/> #} </span> {% endblock %} </div> -- Gitblit v1.8.0