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 PlayerWeaponsController 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 'PlayerWeapon',
43 );
44
45 //-------------------------------------------------------------------
46
47 /**
48 * Displays player's weapons 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 $topKillWeapons = $this->getPlayerTopWeapons($playerID, 'kills', 5);
57 $this->set('topKillWeapons', $topKillWeapons);
58 $topDeathWeapons = $this->getPlayerTopWeapons($playerID, 'deaths', 5);
59 $this->set('topDeathWeapons', $topDeathWeapons);
60 $topTeamKillWeapons = $this->getPlayerTopWeapons($playerID, 'teamkills', 5);
61 $this->set('topTeamKillWeapons', $topTeamKillWeapons);
62 $topTeamDeathWeapons = $this->getPlayerTopWeapons($playerID, 'teamdeaths', 5);
63 $this->set('topTeamDeathWeapons', $topTeamDeathWeapons);
64 $topSuicideWeapons = $this->getPlayerTopWeapons($playerID, 'suicides', 5);
65 $this->set('topSuicideWeapons', $topSuicideWeapons);
66 }
67
68 //-------------------------------------------------------------------
69
70 /**
71 * Queries player weapons and pass data to view file app/View/PlayerWeapons/json/player_weapons_json.ctp
72 * to be processed by dataTables.
73 *
74 * Sample data returned:
75 * Array
76 * (
77 * [sEcho] => 1
78 * [iTotalRecords] => 13125
79 * [iTotalDisplayRecords] => 83
80 * [aaData] => Array
81 * (
82 * [0] => Array
83 * (
84 * [0] => pos# //position number
85 * [1] => SV98 //weapon name
86 * [2] => 13 //kills
87 * [3] => 19 //deaths
88 * [4] => 0 //teamkills
89 * [5] => 0 //teamdeaths
90 * [6] => 0 //suicides
91 * [7] => 34 //weapon id
92 * )
93 * )
94 * )
95 *
96 * @param null $playerID player id
97 * @return mixed
98 */
99 public function playerWeaponsJson($playerID = null) {
100 $conditions = array(
101 'PlayerWeapon.player_id' => $playerID
102 );
103
104 $this->paginate = array(
105 'fields' => array (
106 'WeaponStat.name',
107 'PlayerWeapon.kills',
108 'PlayerWeapon.deaths',
109 'PlayerWeapon.teamkills',
110 'PlayerWeapon.teamdeaths',
111 'PlayerWeapon.suicides',
112 'WeaponStat.id',
113 ),
114 'conditions' => $conditions,
115 );
116
117 $data = $this->DataTable->getResponse('PlayerWeapon');
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('playerWeapons', $data);
130 }
131 return null;
132 }
133
134 //-------------------------------------------------------------------
135
136 /**
137 * Returns and array of top player weapons for the selected action.
138 *
139 * @param null $playerID
140 * @param string $action Accepted values are 'kills | deaths | teamkills | teamdeaths | suicides'. Default is 'kills'
141 * @param int $weaponCount number of weapons to be returned. Default is 5
142 * @return array
143 */
144 public function getPlayerTopWeapons($playerID = null, $action = 'kills', $weaponCount = 5) {
145 $playerData = $this->PlayerStat->find('first', array(
146 'conditions' => array (
147 'PlayerStat.id' => $playerID
148 )
149 )
150 );
151 $playerTotalAction = $playerData['PlayerStat'][$action];
152
153 $topWeapons = $this->PlayerWeapon->find('all', array(
154 'conditions' => array(
155 'PlayerWeapon.player_id' => $playerID,
156 ),
157 'limit' => $weaponCount,
158 'order' => 'PlayerWeapon.' . $action . ' desc'
159 )
160 );
161
162 $playerTopWeapons = array();
163 foreach ($topWeapons as $weapon) {
164 if ($weapon['PlayerWeapon'][$action] > 0) {
165 $playerTopWeapons[$weapon['WeaponStat']['name']] = $weapon['PlayerWeapon'][$action];
166 }
167 }
168
169 $other = $playerTotalAction - array_sum($playerTopWeapons);
170 if ($other > 0) {
171 $playerTopWeapons['Other'] = $playerTotalAction - array_sum($playerTopWeapons);
172 }
173
174 return $playerTopWeapons;
175 }
176
177 }