Fibinger Ádám
2019-10-17 a4186ab0762833a4c4dd741e6dd112185efc2272
commit | author | age
cc3ac8 1 <?php
2
3
4 namespace EOG\Models;
5
6
7 class MatchHistory implements \Iterator, \Countable
8 {
9     const ROLE_ATTACK = 'att';
10     const ROLE_DEFEND = 'def';
11
a4186a 12     protected $maps = [
cc3ac8 13         'Bank' => [
14             'CEO Office / Executive Lounge',
15             'Open Area / Staff Room',
16             'Tellers\' Office / Archives',
17             'Basement CCTV Room / Basement Lockers',
18         ],
19
20         'Border' => [
21             'Supply Room / Customs Inspection',
22             'Workshop / Ventilation Room',
23             'Bathroom / Tellers',
24             'Armory Lockers / Archives',
25         ],
26
27         'Club House' => [
28             'Bedroom / Gym',
29             'Cash Room / CCTV Room',
30             'Bar / Stock Room',
31             'Basement Church / Basement Arsenal Room',
32         ],
33
34         'Coastline' => [
35             'Billiards Room / Hookah Lounge',
36             'Theater / Penthouse',
37             'Kitchen / Service Entrance',
38             'Blue Bar / Sunrise Bar',
39         ],
40
41         'Consulate' => [
42             'Meeting Room / Consulate Office',
43             'Press Room / Lobby',
44             'Basement Archives / Tellers',
45             'Basement Cafeteria / Basement Garage',
46         ],
47
48         'Kafe Dostoyevsky' => [
49             '3rd Floor Cocktail Lounge',
50             'Fireplace Hall / Mining Room',
51             'Reading Room / Fireplace Hall',
52             'Kitchen Service / Kitchen Cooking'
53         ],
54
55         'Villa' => [
56             'Aviator Room / Games Room',
57             'Trophy Room / Statuary Room',
58             'Living Room / Library',
59             'Dining Room / Kitchen'
60         ]
61     ];
62
63     private $position = 0;
64
65     protected $currentMap = 'Villa';
66     protected $history = [
67         [
68             'site' => 'Aviator Room / Games Room',
69             'win' => Stripe::TEAM_ORANGE,
70             'win_role' => self::ROLE_DEFEND
71         ],
72         [
73             'site' => 'Trophy Room / Statuary Room',
74             'win' => Stripe::TEAM_BLUE,
75             'win_role' => self::ROLE_ATTACK
76         ],
77         [
78             'site' => 'Dining Room / Kitchen',
79             'win' => Stripe::TEAM_ORANGE,
80             'win_role' => self::ROLE_ATTACK
81         ]
82     ];
83
84     /**
85      * @return array
86      */
a4186a 87     public function getMaps(): array
cc3ac8 88     {
a4186a 89         return $this->maps;
90     }
91
92     public function getMapNames()
93     {
94         return array_keys($this->maps);
cc3ac8 95     }
96
97     /**
98      * @return string
99      */
100     public function getCurrentMap(): string
101     {
102         return $this->currentMap;
a4186a 103     }
104
105     public function getCurrentSites(): array
106     {
107         return $this->maps[$this->currentMap];
cc3ac8 108     }
109
110     /**
111      * @return array
112      */
113     public function getHistory(): array
114     {
115         return $this->history;
116     }
117
118     private function validMap($mapName)
119     {
a4186a 120         $mapList = array_keys($this->maps);
cc3ac8 121         return in_array($mapName, $mapList);
122     }
123
124     private function validSite($siteName)
125     {
a4186a 126         return in_array($siteName, $this->maps[$this->currentMap]);
cc3ac8 127     }
128
129     public function setCurrentMap(string $mapName)
130     {
131
132         if (!$this->validMap($mapName)) {
133             throw new \InvalidArgumentException("No such map: " . $mapName);
134         }
135
136         $this->currentMap = $mapName;
137     }
138
139     public function addHistoryItem(string $site, string $winner = Stripe::TEAM_BLUE, $win_role = MatchHistory::ROLE_DEFEND)
140     {
141         if (!$this->validSite($site)) {
142             throw new \InvalidArgumentException("No such site: " . $site . " on map: " . $this->currentMap);
143         }
144         if (!in_array($winner, [Stripe::TEAM_BLUE, Stripe::TEAM_ORANGE])) {
145             throw new \InvalidArgumentException("No such team: " . $winner);
146         }
147         if (!in_array($win_role, [MatchHistory::ROLE_DEFEND, MatchHistory::ROLE_ATTACK])) {
148             throw new \InvalidArgumentException("No such role: " . $win_role);
149         }
150
151         $this->history[] = [
152             'site' => $site,
153             'win' => $winner,
154             'win_role' => $win_role
155         ];
156     }
157
158     public function getState(): array
159     {
160         return
161             [
162                 'history' => $this->history,
163                 'map' => $this->currentMap
164             ];
165     }
166
167     public function loadState(array $state): self
168     {
169         if (!empty($state['map'])) {
170             $this->setCurrentMap($state['map']);
171         }
172
173         if (!empty($state['history'])) {
174             foreach ($state['history'] as $item) {
175                 $this->addHistoryItem($item['site'], $item['win'], $item['win_role']);
176             }
177         }
178         return $this;
179     }
180
181     public function current()
182     {
183         return $this->history[$this->position];
184     }
185
186     public function next()
187     {
188         $this->position++;
189     }
190
191     public function key()
192     {
193         return $this->position;
194     }
195
196     public function valid()
197     {
198         return isset($this->history[$this->position]);
199     }
200
201     public function rewind()
202     {
203         $this->position = 0;
204     }
205
206     public function count()
207     {
208         return count($this->history);
209     }
210
211 }