Fibinger Ádám
2020-04-06 b6eaf573fe0ebaf462ce46fc04ea24b4c0774480
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
 
namespace EOG\Models;
 
class Stripe
{
    const TEAM_BLUE = 'blue';
    const TEAM_ORANGE = 'orange';
 
    private $allowedColors = [self::TEAM_BLUE, self::TEAM_ORANGE];
    private $allowedStripeClasses = ['team', 'simple', 'team-ban'];
    private $allowedOperators = [
        'alibi', 'amaru', 'ash', 'bandit', 'blackbeard', 'blitz', 'buck', 'capitao', 'castle',
        'caveira', 'clash', 'doc', 'dokkaebi', 'echo', 'ela', 'finka', 'frost', 'fuze',
        'glaz', 'goyo', 'gridlock', 'hibana', 'iq', 'jackal', 'jager', 'kaid', 'kapkan',
        'lesion', 'lion', 'maestro', 'maverick', 'mira', 'montagne', 'mozzie', 'mute', 'nokk',
        'nomad', 'pulse', 'rook', 'sledge', 'smoke', 'tachanka', 'thatcher', 'thermite', 'twitch',
        'valkyrie', 'vigil', 'warden', 'ying', 'zofia', 'wamai', 'kali', 'iana', 'oryx'];
    protected $state = [
        'stripe' => [
            'class' => 'team',
        ],
        'cup'    => [
            'number' => '#76',
            'name'   => '5on5 Open Cup',
        ],
        'team'   => [
            'orange' =>
                [
                    'name' => 'Narancs Csapat',
                    'ban'  => [
                        'mira',
                        'jackal'
                    ],
                    'score' => [
                        true,
                        true
                    ]
 
                ],
            'blue'   => [
                'name' => 'Kék csapat',
                'ban'  => [
                    'rook',
                    'blitz'
                ],
                'score' => [
                    true,
                    true
                ]
            ]
        ]
    ];
 
    public function setClass(string $class): self
    {
        if (!in_array($class, $this->allowedStripeClasses))
        {
            throw new \InvalidArgumentException("Given class " . $class . " not allowed. Allowed classes: " . implode(', ', $this->allowedStripeClasses));
        }
 
        $this->state['stripe']['class'] = $class;
 
        return $this;
    }
 
    private function setSimpleStripe()
    {
        unset($this->state['team']);
    }
 
    public function setScore(string $team, int $number, bool $score)
    {
        $this->state['team'][$team]['score'][$number] = $score;
    }
 
    public function setCup(string $number, string $name)
    {
        $this->state['cup']['number'] = $number;
        $this->state['cup']['name'] = $name;
    }
 
    /**
     * @param string $color Csapat szín validáció
     *
     * Dob egy Exception-t ha nem orange / blue
     */
    private function testColor(string $color)
    {
        if (!in_array($color, $this->allowedColors))
        {
            throw new \InvalidArgumentException("Given colour " . $color . " not allowed. Allowed colours: " . implode(', ', $this->allowedColors));
        }
    }
 
    public function setTeamName(string $team_color = self::TEAM_BLUE, $name= '')
    {
        $this->testColor($team_color);
        $this->state['team'][$team_color]['name'] = $name;
    }
 
    public function addTeamBan(string $team_color = self::TEAM_BLUE, $operator = '')
    {
        $this->testColor($team_color);
        if (!in_array($operator, $this->allowedOperators))
        {
            //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)
    {
        if ($this->__isset($name))
        {
            return $this->state[$name];
        }
 
        return null;
    }
 
    public function __isset($name)
    {
        return isset($this->state[$name]);
    }
 
    public function loadFromJson(string $json)
    {
        $state = json_decode($json);
        $this->loadFromArray($state);
    }
 
    public function loadFromArray(array $state)
    {
        unset($this->state);
        $this->state = [];
 
        if (!empty($state['team']['orange']['score'][0])) {
            $this->setScore('orange',0,true);
        }
 
        if (!empty($state['team']['orange']['score'][1])) {
            $this->setScore('orange',1,true);
        }
 
        if (!empty($state['team']['blue']['score'][0])) {
            $this->setScore('blue',0,true);
        }
 
        if (!empty($state['team']['blue']['score'][1])) {
            $this->setScore('blue',1,true);
        }
 
        if (!empty($state['stripe']["class"]))
        {
            $this->setClass($state['stripe']["class"]);
        }
 
        if (!empty($state['cup']['name']) || !empty($state['cup']['number']))
        {
            $cupNum = $state['cup']['number'] ?? '';
            $cupName = $state['cup']['name'] ?? '';
            $this->setCup($cupNum, $cupName);
        }
 
        if (!empty($state['team']))
        {
            foreach ([self::TEAM_BLUE, self::TEAM_ORANGE] as $color)
            {
                if (!empty($state['team'][$color]['name']))
                {
                    $this->setTeamName($color, $state['team'][$color]['name']);
                }
 
                if (!empty($state['team'][$color]['ban']) && is_array($state['team'][$color]['ban']))
                {
                    foreach ($state['team'][$color]['ban'] as $operator)
                    {
                        $this->addTeamBan($color, $operator);
                    }
                }
            }
        }
    }
 
    public function getJson()
    {
        return json_encode($this->state);
    }
 
    public function getOperators()
    {
        return $this->allowedOperators;
    }
 
    public function getState()
    {
        return $this->state;
    }
 
    public function getType()
    {
        if (empty($this->state['stripe']['class']))
        {
            return null;
        }
 
        return $this->state['stripe']['class'];
    }
}