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 WeaponStatsController extends AppController {
17
18 /**
19 * @var array
20 */
21 public $uses = array('WeaponStat', 'PlayerWeapon');
22
23 /**
24 * Components
25 *
26 * @var array
27 */
28 public $components = array(
29 'RequestHandler',
30 'DataTable',
31 'Paginator',
32 );
33
34 /**
35 * Helpers
36 *
37 * @var array
38 */
39 public $helpers = array('Html', 'Form', 'Js');
40
41 //-------------------------------------------------------------------
42
43 /**
44 * Displays a list of weapons
45 */
46 public function index() {
47 $topKillWeapons = $this->getTopWeapons('kills', 5);
48 $this->set('topKillWeapons', $topKillWeapons);
49 $topTeamKillWeapons = $this->getTopWeapons('teamkills', 5);
50 $this->set('topTeamKillWeapons', $topTeamKillWeapons);
51 $topSuicideWeapons = $this->getTopWeapons('suicides', 5);
52 $this->set('topSuicideWeapons', $topSuicideWeapons);
53 }
54
55 //-------------------------------------------------------------------
56
57 /**
58 * Queries all weapon stats and pass data to view file app/View/WeaponStats/json/weapons_json.ctp
59 * to be processed by dataTables.
60 *
61 * Sample data returned:
62 * Array
63 * (
64 * [sEcho] => 1
65 * [iTotalRecords] => 93
66 * [iTotalDisplayRecords] => 93
67 * [aaData] => Array
68 * (
69 * [0] => Array
70 * (
71 * [0] => pos# //position
72 * [1] => M16A4 //weapon name
73 * [2] => 3314 //kills
74 * [3] => 90 //team kills
75 * [4] => 12 //suicides
76 * [5] => 1 //weapon id
77 * )
78 * [1] => Array
79 * (
80 * [0] => pos#
81 * [1] => Weapons/AK74M/AK74
82 * [2] => 1519
83 * [3] => 55
84 * [4] => 29
85 * [0] => 2
86 * )
87 * )
88 * )
89 * @return mixed
90 */
91 public function weaponStatsJson() {
92 $this->paginate = array(
93 'fields' => array (
94 'WeaponStat.name',
95 'WeaponStat.kills',
96 'WeaponStat.teamkills',
97 'WeaponStat.suicides',
98 'WeaponStat.id',
99 )
100 );
101
102 $data = $this->DataTable->getResponse('WeaponStat');
103
104 //Add a dummy value to the beginning of each weapon data array. We will modify it in view file as position numbers.
105 $dataLength = count($data['aaData']);
106 for ($i = 0; $i < $dataLength; $i++) {
107 array_unshift($data['aaData'][$i], 'pos#');
108 }
109
110 if ($this->request->is('requested')) {
111 return $data;
112 } else {
113 $this->set('weaponStats', $data);
114 }
115
116 return null;
117 }
118
119 //-------------------------------------------------------------------
120
121 /**
122 * Displays statistics of a specific weapon
123 *
124 * @param null $weaponID
125 * @return array
126 */
127 public function view($weaponID = null) {
128 $this->WeaponStat->id = $weaponID;
129 $weaponData = $this->WeaponStat->read();
130
131 $this->PlayerWeapon->Behaviors->attach('Containable');
132 $this->PlayerWeapon->contain(array('PlayerStat', 'PlayerStat.Player'));
133
134 $conditions = array(
135 'PlayerWeapon.weapon_id' => $weaponID,
136 'PlayerStat.hide' => 0,
137 );
138
139 $playerData = $this->PlayerWeapon->find('all', array(
140 'conditions' => $conditions,
141 'order' => 'PlayerWeapon.kills DESC',
142 'limit' => 10,
143 ));
144
145 if ($this->request->is('requested')) {
146 return array($weaponData);
147 } else {
148 $this->set('weaponData', $weaponData);
149 $this->set('playerData', $playerData);
150 }
151 return null;
152 }
153
154 //-------------------------------------------------------------------
155
156 /**
157 * Returns and array of top weapons for the selected action.
158 *
159 * @param string $action Accepted values are 'kills | teamkills | suicides'. Default is 'kills'
160 * @param int $weaponCount number of weapons to be returned. Default is 5
161 * @return array
162 */
163 public function getTopWeapons($action = 'kills', $weaponCount = 5) {
164 $topWeapons = $this->WeaponStat->find('all', array(
165 'limit' => $weaponCount,
166 'order' => 'WeaponStat.' . $action . ' desc'
167 )
168 );
169
170 $_topWeapons = array();
171 foreach ($topWeapons as $weapon) {
172 if ($weapon['WeaponStat'][$action] > 0) {
173 $_topWeapons[$weapon['WeaponStat']['name']] = $weapon['WeaponStat'][$action];
174 }
175 }
176
177 $totalActions = $this->WeaponStat->find('all', array(
178 'fields' => array(
179 'SUM(' . $action . ') as totals'
180 )
181 ));
182 $other = $totalActions[0][0]['totals'] - array_sum($_topWeapons);
183 if ($other > 0) {
184 $_topWeapons['Other'] = $other;
185 }
186
187 return $_topWeapons;
188 }
189
190 }