1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15
16 App::uses('Xml', 'Utility');
17
18 class ServerPlayersController extends AppController {
19
20 21 22 23 24
25 public $uses = array(
26 'ServerPlayer',
27 'PlayerStat',
28 'Player'
29 );
30
31 32 33 34 35
36 public $components = array(
37 'RequestHandler',
38 'Session',
39 'GeoIP',
40 'GeoIPCity'
41 );
42
43 44 45 46 47
48 public $helpers = array(
49 'Number'
50 );
51
52
53
54 55 56 57 58 59
60 public function index() {
61 $serverID = Configure::read('server_id');
62 $this->serverID = $serverID;
63
64
65 $_status = Configure::read('servers.' . $this->serverID . '.statusurl');
66 if ($_status == null || $_status == 'none') {
67 $result = $this->parseTable();
68 } elseif (Configure::read('servers.' . $this->serverID . '.statusurl') != null) {
69 $result = $this->parseXML();
70 } else {
71 throw new NotFoundException('ServerInfo type definition in Configuration not valid.');
72 }
73
74
75 $positions = array();
76 $playerNames = array();
77 foreach ($result as $teams => $players) {
78 foreach ($players as $k => $player) {
79 $positions[] = array(
80 'client' => $this->XlrFunctions->fixName($player['ServerPlayer']['Name']),
81 'latitude' => $this->GeoIPCity->getLatitude($player['ServerPlayer']['IP']),
82 'longitude' => $this->GeoIPCity->getLongitude($player['ServerPlayer']['IP'])
83 );
84 $playerNames[] = $this->XlrFunctions->fixName($player['ServerPlayer']['Name']);
85 }
86 }
87
88
89 foreach ($result as $teams => $players) {
90 foreach ($players as $k => $player) {
91
92 $result[$teams][$k]['ServerPlayer']['flag'] = $this->XlrFunctions->getFlag($player['ServerPlayer']['IP']);
93
94 $playerById = $this->PlayerStat->findByClientId($player['ServerPlayer']['DBID']);
95 if (!empty($playerById)) {
96 $result[$teams][$k]['ServerPlayer']['playerstats_id'] = $playerById['PlayerStat']['id'];
97 $result[$teams][$k]['ServerPlayer']['rank'] = $this->XlrFunctions->getRank($playerById['PlayerStat']['kills']);
98 $result[$teams][$k]['ServerPlayer']['skilleague'] = $this->XlrFunctions->getLeague($playerById['PlayerStat']['skill']);
99 $result[$teams][$k]['ServerPlayer']['skill'] = $playerById['PlayerStat']['skill'];
100 }
101 }
102 }
103
104 $serverInfo = $this->requestAction('server_info');
105
106 $serverLocation = $this->GeoIPCity->getPosition($serverInfo['Ip']);
107
108 if ($this->request->is('requested')) {
109 return $playerNames;
110 } else {
111
112 $this->set('serverPlayers', $result);
113
114 $this->set('playerPositions', $positions);
115
116 $this->set('serverLocation', $serverLocation);
117 }
118 return null;
119 }
120
121
122
123 124 125 126 127
128 public function parseTable() {
129 try {
130 if (isset($this->ServerPlayer) && !empty($this->ServerPlayer->table)) {
131 $_teams = $this->ServerPlayer->find('all', array(
132 'fields' => 'DISTINCT ServerPlayer.Team',
133 'order' => 'ServerPlayer.Team ASC'
134 ));
135
136
137 $teams = array();
138 foreach ($_teams as $team) {
139 $teams[(string)$team['ServerPlayer']['Team']] = $this->ServerPlayer->find('all', array(
140 'conditions' => array(
141 'ServerPlayer.Team' => $team['ServerPlayer']['Team']
142 ),
143 'order' => 'ServerPlayer.Score DESC'
144 ));
145 }
146
147 return $teams;
148 }
149 } catch (Exception $e) {
150 return array();
151 }
152 return null;
153 }
154
155
156
157 158 159 160 161
162 public function parseXML() {
163 if ((Configure::read('servers.' . $this->serverID . '.statusurl') != null)) {
164 $serverDetails = Configure::read('servers.' . $this->serverID . '.statusurl');
165 $this->statusURL = $serverDetails;
166 }
167
168
169 try {
170 $parsedXML = Xml::build($this->statusURL);
171
172
173 $this->xmlItem = Xml::toArray($parsedXML);
174
175
176
177 if (!isset($this->xmlItem['B3Status']['Clients'])) {
178 return array();
179 }
180
181
182 $serverClients = $this->xmlItem['B3Status']['Clients'];
183 if ($serverClients['@Total'] == 1) {
184 $_s['Client'][0] = $serverClients['Client'];
185 $serverClients = array();
186 $serverClients = $_s;
187 $serverClients['@Total'] = 1;
188 ksort($serverClients);
189 }
190
191
192
193 $teams = array();
194 $_res = array();
195 foreach ($serverClients as $list => $client) {
196 if (substr($list, 0, 1) != '@') {
197 foreach ($client as $k => $v) {
198 foreach ($v as $k1 => $v1) {
199 if (substr($k1, 0, 1) == '@') {
200 $_res[trim($k1, '@')] = $v1;
201 }
202 }
203 $teams[$v['@Team']][]['ServerPlayer'] = $_res;
204 }
205 }
206 }
207 ksort($teams);
208
209 } catch (Exception $e) {
210 $teams = array();
211 }
212
213 return $teams;
214 }
215
216 }
217