Fibinger Ádám
2019-11-27 c5946f44341c29594803c5b2ca64dcaf335fbca3
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'    => [
cc3ac8 24             'number' => '#76',
812087 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         $this->state['stripe']['class'] = $class;
888906 54
812087 55         return $this;
56     }
888906 57
812087 58     private function setSimpleStripe()
59     {
60         unset($this->state['team']);
61     }
888906 62
812087 63     public function setCup(string $number, string $name)
64     {
65         $this->state['cup']['number'] = $number;
66         $this->state['cup']['name'] = $name;
67     }
888906 68
812087 69     /**
70      * @param string $color Csapat szín validáció
71      *
72      * Dob egy Exception-t ha nem orange / blue
73      */
74     private function testColor(string $color)
75     {
76         if (!in_array($color, $this->allowedColors))
77         {
78             throw new \InvalidArgumentException("Given colour " . $team_color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
79         }
80     }
888906 81
812087 82     public function setTeamName(string $team_color = self::TEAM_BLUE, $name)
83     {
84         $this->testColor($team_color);
85         $this->state['team'][$team_color]['name'] = $name;
86     }
888906 87
812087 88     public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator)
89     {
90         $this->testColor($team_color);
91         if (!in_array($operator, $this->allowedOperators))
92         {
93             //FIXME: védő és támadó operátorok külön
94             throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
95         }
cc3ac8 96
97         if (isset($this->state['team'][$team_color]['ban']) && count($this->state['team'][$team_color]['ban']) > 1)
98         {
99             throw new \InvalidArgumentException("Team " . $team_color . " already has 2 operators.");
100         }
101
102         $this->state['team'][$team_color]['ban'][] = $operator;
812087 103     }
888906 104
812087 105     public function __get($name)
106     {
107         if ($this->__isset($name))
108         {
109             return $this->state[$name];
110         }
888906 111
812087 112         return null;
113     }
888906 114
812087 115     public function __isset($name)
116     {
117         return isset($this->state[$name]);
118     }
888906 119
cc3ac8 120     public function loadFromJson(string $json)
812087 121     {
122         $state = json_decode($json);
cc3ac8 123         $this->loadFromArray($state);
124     }
125
126     public function loadFromArray(array $state)
127     {
888906 128
812087 129         unset($this->state);
130         $this->state = [];
888906 131
812087 132         if (!empty($state['stripe']["class"]))
133         {
134             $this->setClass($state['stripe']["class"]);
135         }
888906 136
c5946f 137         if (!empty($state['cup']['name']) || !empty($state['cup']['number']))
812087 138         {
c5946f 139             $cupNum = $state['cup']['number'] ?? '';
140             $cupName = $state['cup']['name'] ?? '';
141             $this->setCup($cupNum, $cupName);
812087 142         }
143
144         if (!empty($state['team']))
145         {
146             foreach ([self::TEAM_BLUE, self::TEAM_ORANGE] as $color)
147             {
148                 if (!empty($state['team'][$color]['name']))
149                 {
150                     $this->setTeamName($color, $state['team'][$color]['name']);
151                 }
152
153                 if (!empty($state['team'][$color]['ban']) && is_array($state['team'][$color]['ban']))
154                 {
155                     foreach ($state['team'][$color]['ban'] as $operator)
156                     {
157                         $this->addTeamBan($color, $operator);
158                     }
159                 }
160             }
161         }
162     }
163
164     public function getJson()
165     {
166         return json_encode($this->state);
167     }
168
cc3ac8 169     public function getOperators()
170     {
812087 171         return $this->allowedOperators;
172     }
cc3ac8 173
174     public function getState()
175     {
176         return $this->state;
177     }
1867e1 178
179     public function getType()
180     {
181         if (empty($this->state['stripe']['class']))
182         {
183             return null;
184         }
185
186         return $this->state['stripe']['class'];
187     }
888906 188 }