1 <?php
2 /**
3 * XLRstats : Real Time Player Stats (http://www.xlrstats.com)
4 * (CC) BY-NC-SA 2005-2013, Mark Weirath, Özgür Uysal
5 *
6 * Licensed under the Creative Commons BY-NC-SA 3.0 License
7 * Redistributions of files must retain the above copyright notice.
8 *
9 * @link http://www.xlrstats.com
10 * @license Creative Commons BY-NC-SA 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/)
11 * @package app.Controller
12 * @since XLRstats v3.0
13 * @version 0.1
14 */
15
16 class PlayerMapsController extends AppController {
17
18 /**
19 * Helpers
20 *
21 * @var array
22 */
23 public $helpers = array('Js' => array('Jquery'));
24
25 /**
26 * Components
27 *
28 * @var array
29 */
30 public $components = array(
31 'RequestHandler',
32 'DataTable',
33 'Paginator',
34 );
35
36 /**
37 * Models
38 * @var array
39 */
40 public $uses = array(
41 'PlayerStat',
42 'PlayerMap'
43 );
44
45 //-------------------------------------------------------------------
46
47 /**
48 * Displays player's maps via dataTables.
49 *
50 * @param int $playerID is passed to the dataTable script's "sAjaxSource".
51 */
52 public function view($playerID = null) {
53 $this->set('playerID', $playerID);
54 $this->layout = 'ajax';
55
56 $topKillMaps = $this->getPlayerTopMaps($playerID, 'kills', 5);
57 $this->set('topKillMaps', $topKillMaps);
58 $topDeathMaps = $this->getPlayerTopMaps($playerID, 'deaths', 5);
59 $this->set('topDeathMaps', $topDeathMaps);
60 $topTeamKillMaps = $this->getPlayerTopMaps($playerID, 'teamkills', 5);
61 $this->set('topTeamKillMaps', $topTeamKillMaps);
62 $topTeamDeathMaps = $this->getPlayerTopMaps($playerID, 'teamdeaths', 5);
63 $this->set('topTeamDeathMaps', $topTeamDeathMaps);
64 $topSuicideMaps = $this->getPlayerTopMaps($playerID, 'suicides', 5);
65 $this->set('topSuicideMaps', $topSuicideMaps);
66 }
67
68 //-------------------------------------------------------------------
69
70 /**
71 * Queries player maps and pass data to view file json/player_maps_json.ctp
72 * to be processed by dataTables.
73 *
74 * Sample data returned:
75 * Array
76 * (
77 * [sEcho] => 1
78 * [iTotalRecords] => 3777
79 * [iTotalDisplayRecords] => 17
80 * [aaData] => Array
81 * (
82 * [0] => Array
83 * (
84 * [0] => pos# //position number
85 * [1] => MP_001 //map name
86 * [2] => 314 //kills
87 * [3] => 165 //deaths
88 * [4] => 4 //teamkills
89 * [5] => 1 //teamdeaths
90 * [6] => 3 //suicides
91 * [7] => 19 //map id
92 * )
93 * )
94 * )
95 *
96 * @param null $playerID player id
97 * @return mixed
98 */
99 public function playerMapsJson($playerID = null) {
100 $conditions = array(
101 'PlayerMap.player_id' => $playerID
102 );
103
104 $this->paginate = array(
105 'fields' => array (
106 'MapStat.name',
107 'PlayerMap.kills',
108 'PlayerMap.deaths',
109 'PlayerMap.teamkills',
110 'PlayerMap.teamdeaths',
111 'PlayerMap.suicides',
112 'PlayerMap.map_id',
113 ),
114 'conditions' => $conditions,
115 );
116
117 $data = $this->DataTable->getResponse('PlayerMap');
118
119 //Add a dummy value to the beginning of each player weapon data array. We will modify it in view file as position numbers.
120 $dataLength = count($data['aaData']);
121 for ($i = 0; $i < $dataLength; $i++) {
122 array_unshift($data['aaData'][$i], 'pos#');
123 }
124 //pr($data);
125
126 if ($this->request->is('requested')) {
127 return $data;
128 } else {
129 $this->set('playerMaps', $data);
130 }
131
132 return null;
133 }
134
135 //-------------------------------------------------------------------
136
137 /**
138 * Returns and array of top player maps for the selected action.
139 *
140 * @param null $playerID
141 * @param string $action Accepted values are 'kills | deaths | teamkills | teamdeaths | suicides'. Default is 'kills'
142 * @param int $mapCount number of weapons to be returned. Default is 5
143 * @return array
144 */
145 public function getPlayerTopMaps($playerID = null, $action = 'kills', $mapCount = 5) {
146 $playerData = $this->PlayerStat->find('first', array(
147 'conditions' => array (
148 'PlayerStat.id' => $playerID
149 )
150 )
151 );
152 $playerTotalAction = $playerData['PlayerStat'][$action];
153
154 $topMaps = $this->PlayerMap->find('all', array(
155 'conditions' => array(
156 'PlayerMap.player_id' => $playerID,
157 ),
158 'limit' => $mapCount,
159 'order' => 'PlayerMap.' . $action . ' desc'
160 )
161 );
162
163 $playerTopMaps = array();
164 foreach ($topMaps as $map) {
165 if ($map['PlayerMap'][$action] > 0) {
166 $playerTopMaps[$map['MapStat']['name']] = $map['PlayerMap'][$action];
167 }
168 }
169
170 $other = $playerTotalAction - array_sum($playerTopMaps);
171 if ($other > 0) {
172 $playerTopMaps['Other'] = $playerTotalAction - array_sum($playerTopMaps);
173 }
174
175 return $playerTopMaps;
176 }
177
178 }
179