1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15
16 App::uses('Xml', 'Utility');
17
18 19 20
21 class ServerInfoController extends AppController {
22
23 24 25 26 27
28 public $uses = array(
29 'Dashboard.Server',
30 'ServerInfo',
31 'PlayerStat',
32 'WeaponStat',
33 'MapStat'
34 );
35
36 37 38 39 40
41 public $helpers = array(
42 'Number'
43 );
44
45 46 47 48 49
50 public $components = array(
51 'RequestHandler',
52 'Session',
53 'GeoIP'
54 );
55
56 57 58 59 60
61 public $xmlItem = array();
62
63 64 65 66 67
68 public $serverID = null;
69
70
71
72 73 74 75 76 77 78 79
80 public function getServerInfo($serverID) {
81
82
83
84
85 $statusUrl = Configure::read('servers.' . $serverID . '.statusurl');
86
87 if ($statusUrl == null || $statusUrl == 'none') {
88 $data = $this->parseTable();
89 } elseif ($statusUrl != null) {
90 $data = $this->parseXML();
91 } else {
92 throw new NotFoundException('ServerInfo type definition in Configuration not valid.');
93 }
94
95
96 $this->Server->read(null, $serverID);
97 $this->Server->set('servername_a', $data['sv_hostname']);
98 $this->Server->save();
99
100 return $data;
101 }
102
103
104
105 106 107 108 109 110 111
112 public function index($id = null) {
113 if ($id == null) {
114 $serverID = Configure::read('server_id');
115 } else {
116 $serverID = $id;
117 }
118
119
120 $this->serverID = $serverID;
121
122 $result = $this->getServerInfo($serverID);
123
124
125 $result['total_players'] = $this->PlayerStat->find('count');
126 $totalKills = $this->PlayerStat->find('first', array('fields' => array('SUM(kills) as total_kills')));
127 $result['total_kills'] = $totalKills[0]['total_kills'];
128 $result['server_country_code'] = strtolower($this->GeoIP->country_code($result['Ip']));
129 $result['server_location'] = $this->GeoIP->country_name($result['Ip']);
130
131 $this->WeaponStat->unbindModel(array('hasMany' => array('PlayerWeapon')));
132 $favWeapon = $this->WeaponStat->find('first', array('order' => 'kills DESC'));
133 $result['favorite_weapon'] = array_key_exists('WeaponStat', $favWeapon) ? $favWeapon['WeaponStat']['name'] : 'n.a.';
134 $result['favorite_weapon_id'] = array_key_exists('WeaponStat', $favWeapon) ? $favWeapon['WeaponStat']['id'] : '';
135 $this->MapStat->unbindModel(array('hasMany' => array('PlayerMap')));
136 $curMap = $this->MapStat->findByName($result['Map']);
137 $result['current_map_id'] = array_key_exists('MapStat', $curMap) ? $curMap['MapStat']['id'] : '';
138 $favMap = $this->MapStat->find('first', array('order' => 'rounds DESC'));
139 $result['favorite_map'] = array_key_exists('MapStat', $favMap) ? $favMap['MapStat']['name'] : 'n.a.';
140 $result['favorite_map_id'] = array_key_exists('MapStat', $favMap) ? $favMap['MapStat']['id'] : '';
141
142
143
144 if ($this->request->is('requested')) {
145 return $result;
146 } else {
147 $this->set('serverInfo', $result);
148 }
149 return null;
150 }
151
152
153
154 155 156 157 158
159 public function prepareArray() {
160 $result = array(
161 'sv_hostname' => '',
162 'serverDescription' => Configure::read('servers.' . $this->serverID . '.slogan'),
163 'Ip' => '0.0.0.0',
164 'Port' => '0000',
165 'Map' => 'Not Available',
166 'OnlinePlayers' => '-',
167 'sv_maxclients' => '-',
168 'Rounds' => '-',
169 'totalplayers' => 'Not Available',
170 'totalkills' => 'Not Available',
171 );
172 return $result;
173 }
174
175
176
177 178 179 180 181
182 public function parseTable() {
183
184 $result = $this->prepareArray();
185 try {
186 $data = $this->ServerInfo->find('all');
187 foreach ($data as $row => $serverInfo) {
188 foreach ($serverInfo as $key => $value) {
189 foreach ($value as $k => $v) {
190 if ($k == 'name') {
191 $name = $v;
192 }
193 if ($k == 'value') {
194 $value = $v;
195 }
196 }
197 $result[$name] = $value;
198 }
199 }
200 } catch (Exception $e) {
201 $result['serverDescription'] = 'Current status of this server is not available.';
202 }
203 return $result;
204 }
205
206
207
208 209 210 211 212
213 public function parseXML() {
214 $result = $this->prepareArray();
215
216 if ((Configure::read('servers.' . $this->serverID . '.statusurl') != null)) {
217 $serverDetails = Configure::read('servers.' . $this->serverID . '.statusurl');
218 $this->statusURL = $serverDetails;
219 }
220
221
222 try {
223 $parsedXML = Xml::build($this->statusURL);
224
225
226 $this->xmlItem = Xml::toArray($parsedXML);
227
228
229 if (!isset($this->xmlItem['B3Status']['Game'])) {
230 return $result;
231 }
232
233
234 $serverInfo = $this->xmlItem['B3Status']['Game'];
235
236 foreach ($serverInfo as $key => $value) {
237 if (substr($key, 0, 1) == '@') {
238 $name = substr($key, 1);
239 $result[$name] = $value;
240 }
241 }
242
243
244 $serverData = $this->xmlItem['B3Status']['Game']['Data'];
245 foreach ($serverData as $key => $value) {
246 foreach ($value as $k => $v) {
247 if ($k == '@Name') {
248 $name = $v;
249 }
250 if ($k == '@Value') {
251 $value = $v;
252 }
253 }
254 $result[$name] = $value;
255 }
256 } catch (Exception $e) {
257 $result['serverDescription'] = 'Current status of this server is not available.';
258 }
259
260 return $result;
261 }
262
263 }