Fibinger Ádám
2020-06-19 42082209d5dc6921afebea67196958e370b737c1
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',
420822 18         'valkyrie', 'vigil', 'warden', 'ying', 'zofia', 'wamai', 'kali', 'iana', 'oryx', 'no-ban', 'no-ban2',
19         'ace', 'melusi'
20         ];
812087 21     protected $state = [
22         'stripe' => [
23             'class' => 'team',
24         ],
25         'cup'    => [
cc3ac8 26             'number' => '#76',
812087 27             'name'   => '5on5 Open Cup',
ffa7c6 28             'bestof'   => '5',
812087 29         ],
30         'team'   => [
31             'orange' =>
32                 [
ffa7c6 33                     'name'  => 'Narancs Csapat',
34                     'ban'   => [
812087 35                         'mira',
36                         'jackal'
b15810 37                     ],
38                     'score' => [
ffa7c6 39                         true,
b15810 40                         true,
41                         true
812087 42                     ]
b15810 43
812087 44                 ],
45             'blue'   => [
ffa7c6 46                 'name'  => 'Kék csapat',
47                 'ban'   => [
812087 48                     'rook',
49                     'blitz'
b15810 50                 ],
51                 'score' => [
ffa7c6 52                     true,
b15810 53                     true,
54                     true
812087 55                 ]
56             ]
57         ]
58     ];
888906 59
812087 60     public function setClass(string $class): self
61     {
62         if (!in_array($class, $this->allowedStripeClasses))
63         {
64             throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
65         }
888906 66
812087 67         $this->state['stripe']['class'] = $class;
888906 68
812087 69         return $this;
70     }
888906 71
812087 72     private function setSimpleStripe()
73     {
74         unset($this->state['team']);
b15810 75     }
76
77     public function setScore(string $team, int $number, bool $score)
78     {
79         $this->state['team'][$team]['score'][$number] = $score;
812087 80     }
888906 81
ffa7c6 82     public function setCup(string $number, string $name, int $bestOf = 1)
812087 83     {
84         $this->state['cup']['number'] = $number;
85         $this->state['cup']['name'] = $name;
ffa7c6 86         $this->state['cup']['bestof'] = $bestOf;
812087 87     }
888906 88
812087 89     /**
90      * @param string $color Csapat szín validáció
91      *
92      * Dob egy Exception-t ha nem orange / blue
93      */
94     private function testColor(string $color)
95     {
96         if (!in_array($color, $this->allowedColors))
97         {
f6a0dc 98             throw new \InvalidArgumentException("Given colour " . $color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
812087 99         }
100     }
888906 101
ffa7c6 102     public function setTeamName(string $team_color = self::TEAM_BLUE, $name = '')
812087 103     {
104         $this->testColor($team_color);
105         $this->state['team'][$team_color]['name'] = $name;
106     }
888906 107
f6a0dc 108     public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator = '')
812087 109     {
110         $this->testColor($team_color);
111         if (!in_array($operator, $this->allowedOperators))
112         {
113             //FIXME: védő és támadó operátorok külön
114             throw new \InvalidArgumentException("Given operator not allowed:  " . $operator . " Allowed operators: " . implode(', ', $this->allowedOperators));
115         }
cc3ac8 116
117         if (isset($this->state['team'][$team_color]['ban']) && count($this->state['team'][$team_color]['ban']) > 1)
118         {
119             throw new \InvalidArgumentException("Team " . $team_color . " already has 2 operators.");
120         }
121
122         $this->state['team'][$team_color]['ban'][] = $operator;
812087 123     }
888906 124
812087 125     public function __get($name)
126     {
127         if ($this->__isset($name))
128         {
129             return $this->state[$name];
130         }
888906 131
812087 132         return null;
133     }
888906 134
812087 135     public function __isset($name)
136     {
137         return isset($this->state[$name]);
138     }
888906 139
cc3ac8 140     public function loadFromJson(string $json)
812087 141     {
142         $state = json_decode($json);
cc3ac8 143         $this->loadFromArray($state);
144     }
145
146     public function loadFromArray(array $state)
147     {
812087 148         unset($this->state);
149         $this->state = [];
888906 150
ffa7c6 151         if (!empty($state['team']['orange']['score'][0]))
152         {
153             $this->setScore('orange', 0, true);
b15810 154         }
155
ffa7c6 156         if (!empty($state['team']['orange']['score'][1]))
157         {
158             $this->setScore('orange', 1, true);
b15810 159         }
160
ffa7c6 161         if (!empty($state['team']['orange']['score'][2]))
162         {
163             $this->setScore('orange', 2, true);
b15810 164         }
165
ffa7c6 166         if (!empty($state['team']['blue']['score'][0]))
167         {
168             $this->setScore('blue', 0, true);
169         }
170
171         if (!empty($state['team']['blue']['score'][1]))
172         {
173             $this->setScore('blue', 1, true);
174         }
175
176         if (!empty($state['team']['blue']['score'][2]))
177         {
178             $this->setScore('blue', 2, true);
b15810 179         }
180
812087 181         if (!empty($state['stripe']["class"]))
182         {
183             $this->setClass($state['stripe']["class"]);
184         }
888906 185
ffa7c6 186         if (!empty($state['cup']['name']) || !empty($state['cup']['number']) || !empty($state['cup']['bestof']))
812087 187         {
c5946f 188             $cupNum = $state['cup']['number'] ?? '';
189             $cupName = $state['cup']['name'] ?? '';
ffa7c6 190             $bestOf = (int) $state['cup']['bestof'] ?? 1;
191             $this->setCup($cupNum, $cupName, $bestOf);
812087 192         }
193
194         if (!empty($state['team']))
195         {
196             foreach ([self::TEAM_BLUE, self::TEAM_ORANGE] as $color)
197             {
198                 if (!empty($state['team'][$color]['name']))
199                 {
200                     $this->setTeamName($color, $state['team'][$color]['name']);
201                 }
202
203                 if (!empty($state['team'][$color]['ban']) && is_array($state['team'][$color]['ban']))
204                 {
205                     foreach ($state['team'][$color]['ban'] as $operator)
206                     {
207                         $this->addTeamBan($color, $operator);
208                     }
209                 }
210             }
211         }
212     }
213
214     public function getJson()
215     {
216         return json_encode($this->state);
217     }
218
cc3ac8 219     public function getOperators()
220     {
812087 221         return $this->allowedOperators;
222     }
cc3ac8 223
224     public function getState()
225     {
226         return $this->state;
227     }
1867e1 228
229     public function getType()
230     {
231         if (empty($this->state['stripe']['class']))
232         {
233             return null;
234         }
235
236         return $this->state['stripe']['class'];
237     }
888906 238 }