Fibinger Ádám
2019-10-15 cc3ac843ada92f513339d893c8da534e3c5b9ae7
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
12     protected $sites = [
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      */
87     public function getSites(): array
88     {
89         return $this->sites;
90     }
91
92     /**
93      * @return string
94      */
95     public function getCurrentMap(): string
96     {
97         return $this->currentMap;
98     }
99
100     /**
101      * @return array
102      */
103     public function getHistory(): array
104     {
105         return $this->history;
106     }
107
108     private function validMap($mapName)
109     {
110         $mapList = array_keys($this->sites);
111         return in_array($mapName, $mapList);
112     }
113
114     private function validSite($siteName)
115     {
116         return in_array($siteName, $this->sites[$this->currentMap]);
117     }
118
119     public function setCurrentMap(string $mapName)
120     {
121
122         if (!$this->validMap($mapName)) {
123             throw new \InvalidArgumentException("No such map: " . $mapName);
124         }
125
126         $this->currentMap = $mapName;
127     }
128
129     public function addHistoryItem(string $site, string $winner = Stripe::TEAM_BLUE, $win_role = MatchHistory::ROLE_DEFEND)
130     {
131         if (!$this->validSite($site)) {
132             throw new \InvalidArgumentException("No such site: " . $site . " on map: " . $this->currentMap);
133         }
134         if (!in_array($winner, [Stripe::TEAM_BLUE, Stripe::TEAM_ORANGE])) {
135             throw new \InvalidArgumentException("No such team: " . $winner);
136         }
137         if (!in_array($win_role, [MatchHistory::ROLE_DEFEND, MatchHistory::ROLE_ATTACK])) {
138             throw new \InvalidArgumentException("No such role: " . $win_role);
139         }
140
141         $this->history[] = [
142             'site' => $site,
143             'win' => $winner,
144             'win_role' => $win_role
145         ];
146     }
147
148     public function getState(): array
149     {
150         return
151             [
152                 'history' => $this->history,
153                 'map' => $this->currentMap
154             ];
155     }
156
157     public function loadState(array $state): self
158     {
159         if (!empty($state['map'])) {
160             $this->setCurrentMap($state['map']);
161         }
162
163         if (!empty($state['history'])) {
164             foreach ($state['history'] as $item) {
165                 $this->addHistoryItem($item['site'], $item['win'], $item['win_role']);
166             }
167         }
168         return $this;
169     }
170
171     public function current()
172     {
173         return $this->history[$this->position];
174     }
175
176     public function next()
177     {
178         $this->position++;
179     }
180
181     public function key()
182     {
183         return $this->position;
184     }
185
186     public function valid()
187     {
188         return isset($this->history[$this->position]);
189     }
190
191     public function rewind()
192     {
193         $this->position = 0;
194     }
195
196     public function count()
197     {
198         return count($this->history);
199     }
200
201 }