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