Fibinger Ádám
2019-10-10 8120873e7ddac228c72f7333d6ca7246319ca2da
commit | author | age
888906 1 <?php
2
3 namespace EOG\Models;
4
5 class Stripe
6 {
812087 7     const TEAM_BLUE = 'blue';
8     const TEAM_ORANGE = 'orange';
888906 9
812087 10     private $allowedColors = [self::TEAM_BLUE, self::TEAM_ORANGE];
11     private $allowedStripeClasses = ['team', 'simple', 'team-ban'];
12     private $allowedOperators = [
13         'alibi', 'amaru', 'ash', 'bandit', 'blackbeard', 'blitz', 'buck', 'capitao', 'castle',
14         'caveira', 'clash', 'doc', 'dokkaebi', 'echo', 'ela', 'finka', 'frost', 'fuze',
15         'glaz', 'goyo', 'gridlock', 'hibana', 'iq', 'jackal', 'jager', 'kaid', 'kapkan',
16         'lesion', 'lion', 'maestro', 'maverick', 'mira', 'montagne', 'mozzie', 'mute', 'nokk',
17         'nomad', 'pulse', 'rook', 'sledge', 'smoke', 'tachanka', 'thatcher', 'thermite', 'twitch',
18         'valkyrie', 'vigil', 'warden', 'ying', 'zofia'];
19     protected $state = [
20         'stripe' => [
21             'class' => 'team',
22         ],
23         'cup'    => [
24             'number' => '#75',
25             'name'   => '5on5 Open Cup',
26         ],
27         'team'   => [
28             'orange' =>
29                 [
30                     'name' => 'Narancs Csapat',
31                     'ban'  => [
32                         'mira',
33                         'jackal'
34                     ]
35                 ],
36             'blue'   => [
37                 'name' => 'Kék csapat',
38                 'ban'  => [
39                     'rook',
40                     'blitz'
41                 ]
42             ]
43         ]
44     ];
888906 45
812087 46     public function setClass(string $class): self
47     {
48         if (!in_array($class, $this->allowedStripeClasses))
49         {
50             throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
51         }
888906 52
812087 53         if ($class === 'simple')
54         {
55             $this->setSimpleStripe();
56         }
888906 57
812087 58         $this->state['stripe']['class'] = $class;
888906 59
812087 60         return $this;
61     }
888906 62
812087 63     private function setSimpleStripe()
64     {
65         unset($this->state['team']);
66     }
888906 67
812087 68     public function setCup(string $number, string $name)
69     {
70         $this->state['cup']['number'] = $number;
71         $this->state['cup']['name'] = $name;
72     }
888906 73
812087 74     /**
75      * @param string $color Csapat szín validáció
76      *
77      * Dob egy Exception-t ha nem orange / blue
78      */
79     private function testColor(string $color)
80     {
81         if (!in_array($color, $this->allowedColors))
82         {
83             throw new \InvalidArgumentException("Given colour " . $team_color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
84         }
85     }
888906 86
812087 87     public function setTeamName(string $team_color = self::TEAM_BLUE, $name)
88     {
89         $this->testColor($team_color);
90         $this->state['team'][$team_color]['name'] = $name;
91     }
888906 92
812087 93     public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator)
94     {
95         $this->testColor($team_color);
96         if (!in_array($operator, $this->allowedOperators))
97         {
98             //FIXME: védő és támadó operátorok külön
99             throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
100         }
101     }
888906 102
812087 103     public function __get($name)
104     {
105         if ($this->__isset($name))
106         {
107             return $this->state[$name];
108         }
888906 109
812087 110         return null;
111     }
888906 112
812087 113     public function __isset($name)
114     {
115         return isset($this->state[$name]);
116     }
888906 117
812087 118     public function loadJson(string $json)
119     {
120         $state = json_decode($json);
888906 121
812087 122         unset($this->state);
123         $this->state = [];
888906 124
812087 125         if (!empty($state['stripe']["class"]))
126         {
127             $this->setClass($state['stripe']["class"]);
128         }
888906 129
812087 130         if (!empty($state['cup']['name']) && !empty($state['cup']['number']))
131         {
132             $this->setCup($state['cup']['number'], $state['cup']['name']);
133         }
134
135         if (!empty($state['team']))
136         {
137             foreach ([self::TEAM_BLUE, self::TEAM_ORANGE] as $color)
138             {
139                 if (!empty($state['team'][$color]['name']))
140                 {
141                     $this->setTeamName($color, $state['team'][$color]['name']);
142                 }
143
144                 if (!empty($state['team'][$color]['ban']) && is_array($state['team'][$color]['ban']))
145                 {
146                     foreach ($state['team'][$color]['ban'] as $operator)
147                     {
148                         $this->addTeamBan($color, $operator);
149                     }
150                 }
151             }
152         }
153     }
154
155     public function getJson()
156     {
157         return json_encode($this->state);
158     }
159
160     public function getOperators() {
161         return $this->allowedOperators;
162     }
888906 163 }