From cc3ac843ada92f513339d893c8da534e3c5b9ae7 Mon Sep 17 00:00:00 2001
From: Fibinger Ádám <adam.fibinger@wup.hu>
Date: Tue, 15 Oct 2019 16:47:12 +0200
Subject: [PATCH] Meccs történet alapok

---
 EOG/Models/MatchHistory.php |  201 +++++++++++++++++++++++++++++++++
 form.php                    |   67 ++++++++--
 EOG/Models/Stripe.php       |   29 +++-
 templates/admin/form.twig   |   59 +++++----
 4 files changed, 306 insertions(+), 50 deletions(-)

diff --git a/EOG/Models/MatchHistory.php b/EOG/Models/MatchHistory.php
new file mode 100644
index 0000000..720edcf
--- /dev/null
+++ b/EOG/Models/MatchHistory.php
@@ -0,0 +1,201 @@
+<?php
+
+
+namespace EOG\Models;
+
+
+class MatchHistory implements \Iterator, \Countable
+{
+    const ROLE_ATTACK = 'att';
+    const ROLE_DEFEND = 'def';
+
+    protected $sites = [
+        'Bank' => [
+            'CEO Office / Executive Lounge',
+            'Open Area / Staff Room',
+            'Tellers\' Office / Archives',
+            'Basement CCTV Room / Basement Lockers',
+        ],
+
+        'Border' => [
+            'Supply Room / Customs Inspection',
+            'Workshop / Ventilation Room',
+            'Bathroom / Tellers',
+            'Armory Lockers / Archives',
+        ],
+
+        'Club House' => [
+            'Bedroom / Gym',
+            'Cash Room / CCTV Room',
+            'Bar / Stock Room',
+            'Basement Church / Basement Arsenal Room',
+        ],
+
+        'Coastline' => [
+            'Billiards Room / Hookah Lounge',
+            'Theater / Penthouse',
+            'Kitchen / Service Entrance',
+            'Blue Bar / Sunrise Bar',
+        ],
+
+        'Consulate' => [
+            'Meeting Room / Consulate Office',
+            'Press Room / Lobby',
+            'Basement Archives / Tellers',
+            'Basement Cafeteria / Basement Garage',
+        ],
+
+        'Kafe Dostoyevsky' => [
+            '3rd Floor Cocktail Lounge',
+            'Fireplace Hall / Mining Room',
+            'Reading Room / Fireplace Hall',
+            'Kitchen Service / Kitchen Cooking'
+        ],
+
+        'Villa' => [
+            'Aviator Room / Games Room',
+            'Trophy Room / Statuary Room',
+            'Living Room / Library',
+            'Dining Room / Kitchen'
+        ]
+    ];
+
+    private $position = 0;
+
+    protected $currentMap = 'Villa';
+    protected $history = [
+        [
+            'site' => 'Aviator Room / Games Room',
+            'win' => Stripe::TEAM_ORANGE,
+            'win_role' => self::ROLE_DEFEND
+        ],
+        [
+            'site' => 'Trophy Room / Statuary Room',
+            'win' => Stripe::TEAM_BLUE,
+            'win_role' => self::ROLE_ATTACK
+        ],
+        [
+            'site' => 'Dining Room / Kitchen',
+            'win' => Stripe::TEAM_ORANGE,
+            'win_role' => self::ROLE_ATTACK
+        ]
+    ];
+
+    /**
+     * @return array
+     */
+    public function getSites(): array
+    {
+        return $this->sites;
+    }
+
+    /**
+     * @return string
+     */
+    public function getCurrentMap(): string
+    {
+        return $this->currentMap;
+    }
+
+    /**
+     * @return array
+     */
+    public function getHistory(): array
+    {
+        return $this->history;
+    }
+
+    private function validMap($mapName)
+    {
+        $mapList = array_keys($this->sites);
+        return in_array($mapName, $mapList);
+    }
+
+    private function validSite($siteName)
+    {
+        return in_array($siteName, $this->sites[$this->currentMap]);
+    }
+
+    public function setCurrentMap(string $mapName)
+    {
+
+        if (!$this->validMap($mapName)) {
+            throw new \InvalidArgumentException("No such map: " . $mapName);
+        }
+
+        $this->currentMap = $mapName;
+    }
+
+    public function addHistoryItem(string $site, string $winner = Stripe::TEAM_BLUE, $win_role = MatchHistory::ROLE_DEFEND)
+    {
+        if (!$this->validSite($site)) {
+            throw new \InvalidArgumentException("No such site: " . $site . " on map: " . $this->currentMap);
+        }
+        if (!in_array($winner, [Stripe::TEAM_BLUE, Stripe::TEAM_ORANGE])) {
+            throw new \InvalidArgumentException("No such team: " . $winner);
+        }
+        if (!in_array($win_role, [MatchHistory::ROLE_DEFEND, MatchHistory::ROLE_ATTACK])) {
+            throw new \InvalidArgumentException("No such role: " . $win_role);
+        }
+
+        $this->history[] = [
+            'site' => $site,
+            'win' => $winner,
+            'win_role' => $win_role
+        ];
+    }
+
+    public function getState(): array
+    {
+        return
+            [
+                'history' => $this->history,
+                'map' => $this->currentMap
+            ];
+    }
+
+    public function loadState(array $state): self
+    {
+        if (!empty($state['map'])) {
+            $this->setCurrentMap($state['map']);
+        }
+
+        if (!empty($state['history'])) {
+            foreach ($state['history'] as $item) {
+                $this->addHistoryItem($item['site'], $item['win'], $item['win_role']);
+            }
+        }
+        return $this;
+    }
+
+    public function current()
+    {
+        return $this->history[$this->position];
+    }
+
+    public function next()
+    {
+        $this->position++;
+    }
+
+    public function key()
+    {
+        return $this->position;
+    }
+
+    public function valid()
+    {
+        return isset($this->history[$this->position]);
+    }
+
+    public function rewind()
+    {
+        $this->position = 0;
+    }
+
+    public function count()
+    {
+        return count($this->history);
+    }
+
+}
\ No newline at end of file
diff --git a/EOG/Models/Stripe.php b/EOG/Models/Stripe.php
index 0f91067..1531e3f 100644
--- a/EOG/Models/Stripe.php
+++ b/EOG/Models/Stripe.php
@@ -21,7 +21,7 @@
 			'class' => 'team',
 		],
 		'cup'    => [
-			'number' => '#75',
+			'number' => '#76',
 			'name'   => '5on5 Open Cup',
 		],
 		'team'   => [
@@ -48,11 +48,6 @@
 		if (!in_array($class, $this->allowedStripeClasses))
 		{
 			throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
-		}
-
-		if ($class === 'simple')
-		{
-			$this->setSimpleStripe();
 		}
 
 		$this->state['stripe']['class'] = $class;
@@ -98,6 +93,13 @@
 			//FIXME: védő és támadó operátorok külön
 			throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
 		}
+
+		if (isset($this->state['team'][$team_color]['ban']) && count($this->state['team'][$team_color]['ban']) > 1)
+		{
+			throw new \InvalidArgumentException("Team " . $team_color . " already has 2 operators.");
+		}
+
+		$this->state['team'][$team_color]['ban'][] = $operator;
 	}
 
 	public function __get($name)
@@ -115,9 +117,14 @@
 		return isset($this->state[$name]);
 	}
 
-	public function loadJson(string $json)
+	public function loadFromJson(string $json)
 	{
 		$state = json_decode($json);
+		$this->loadFromArray($state);
+	}
+
+	public function loadFromArray(array $state)
+	{
 
 		unset($this->state);
 		$this->state = [];
@@ -157,7 +164,13 @@
 		return json_encode($this->state);
 	}
 
-	public function getOperators() {
+	public function getOperators()
+	{
 		return $this->allowedOperators;
 	}
+
+	public function getState()
+	{
+		return $this->state;
+	}
 }
\ No newline at end of file
diff --git a/form.php b/form.php
index a56ebfe..e928767 100644
--- a/form.php
+++ b/form.php
@@ -15,25 +15,64 @@
 	define('OVERLAY_DIR', dirname(__DIR__) . '/overlays/');
 }
 
+$last_json = OVERLAY_DIR.'last.json';
+
 $twig = \EOG\Utils\TwigFactory::getEnvironment(SITE_ROOT);
 
-if (!empty($_POST))
+$s = new \EOG\Models\Stripe();
+
+if (!empty($_POST['stripe']))
 {
-	$s = new \EOG\Models\Stripe();
+	$s->loadFromArray($_POST['stripe']);
 
-	$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+    if (!empty($s->stripe['class'])) {
+        file_put_contents($last_json, $s->getJson());
+    }
 
-	file_put_contents(OVERLAY_DIR . 'generated.html', $html_content);
+	switch ($s->stripe['class'])
+	{
+		case 'simple':
+			$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+			file_put_contents(OVERLAY_DIR . 'simple.html', $html_content);
+			break;
+		case 'team':
+			$s->setClass('simple');
+			$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+			file_put_contents(OVERLAY_DIR . 'simple.html', $html_content);
 
-	echo OVERLAY_DIR . 'generated.html';
+			$s->setClass('team');
+			$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+			file_put_contents(OVERLAY_DIR . 'team.html', $html_content);
+
+			break;
+		case 'team-ban':
+			$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-ban');
+			$html_content = $twig->render('templates/overlay-base.twig', ['stripe' => $s]);
+			file_put_contents(OVERLAY_DIR . 'team-ban.html', $html_content);
+			break;
+	}
+} 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
-{
-	$s = new \EOG\Models\Stripe();
-	$var = [
-		'stripe' => $s,
-		'operators' => $s->getOperators()
-	];
-	echo $twig->render('templates/admin/form.twig', $var);
-}
+
+$var = [
+	'stripe'    => $s,
+	'operators' => $s->getOperators(),
+	'post'      => !empty($_POST['stripe'])
+];
+echo $twig->render('templates/admin/form.twig', $var);
 
diff --git a/templates/admin/form.twig b/templates/admin/form.twig
index 04f9b8f..83d20b1 100644
--- a/templates/admin/form.twig
+++ b/templates/admin/form.twig
@@ -1,24 +1,7 @@
 {% extends "templates/admin/html-skeleton.twig" %}
 {% block body %}
-	<form>
-		<div class="form-group">
-			<h6>Overlay típus:</h6>
-			<div class="form-check">
-				<label class="form-check-label">
-					<input type="radio" name="stripe[class]" value="simple"> Csak kupa név és logók
-				</label>
-			</div>
-			<div class="form-check">
-				<label class="form-check-label">
-					<input type="radio" name="stripe[class]" value="team">Kupanév, logók, csapatnevek
-				</label>
-			</div>
-			<div class="form-check">
-				<label class="form-check-label">
-					<input type="radio" name="stripe[class]" value="team-ban">Kupanév, logók, csapatnevek, banok
-				</label>
-			</div>
-		</div>
+	<form action="/form.php" method="post">
+		<input type="hidden" name="stripe[class]" value="team-ban"/>
 
 		<div class="form-group">
 			<div class="form-row">
@@ -40,13 +23,13 @@
 
 		<div class="form-group">
 			<div class="form-row">
-				<div class="form-group">
-					<div class="col-md-12 mb-3">
+				<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-3">
+					<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>
@@ -57,14 +40,17 @@
 					</div>
 				</div>
 				<div class="form-group">
-					<div class="col-md-12 mb-3">
-						<label for="team_orange_name" class="bg-warning">Narancs csapat neve</label>
+						<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-3">
-						<label for="op-blue" class="bg-warning">Banolt operátorok</label>
-						<select name="stripe[team][blue][ban]" class="operators" id="op-blue" style="width: 100%"
+					<div class="col-md-12 mb-4">
+						<label for="op-orange" class="bg-warning">Banolt operátorok</label>
+						<select name="stripe[team][blue][ban]" class="operators" id="op-orange" style="width: 100%"
 								multiple>
 							{% for operator in operators %}
 								<option value="{{ operator }}">{{ operator|capitalize }}</option>
@@ -74,8 +60,13 @@
 				</div>
 			</div>
 		</div>
+		<div class="form-group">
+			<button class="btn btn-primary mx-auto" type="submit">Overlay felülírása</button>
+		</div>
+	</form>
 
-		<button class="btn btn-primary" type="submit">Overlay felülírása</button>
+	<form action="/form.php" method="post">
+
 	</form>
 {% endblock %}
 {% block lazyload %}
@@ -87,5 +78,17 @@
                 '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

--
Gitblit v1.8.0