|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
// @todo Go over this ENTIRE file again as there's been major refactors since and it's likely broken! |
|
4
|
|
|
|
|
5
|
|
|
namespace Ps2alerts\Api\Controller\Endpoint\Leaderboards; |
|
6
|
|
|
|
|
7
|
|
|
use Ps2alerts\Api\Exception\CensusEmptyException; |
|
8
|
|
|
use Ps2alerts\Api\Exception\CensusErrorException; |
|
9
|
|
|
use Ps2alerts\Api\Repository\Metrics\PlayerTotalRepository; |
|
10
|
|
|
use Ps2alerts\Api\Transformer\Leaderboards\PlayerLeaderboardTransformer; |
|
11
|
|
|
use Ps2alerts\Api\Controller\Endpoint\Data\DataEndpointController; |
|
12
|
|
|
|
|
13
|
|
|
class LeaderboardPlayerEndpointController extends AbstractLeaderboardEndpointController |
|
14
|
|
|
{ |
|
15
|
|
|
protected $dataEndpoint; |
|
16
|
|
|
protected $repository; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Construct |
|
20
|
|
|
* |
|
21
|
|
|
* @param DataEndpointController |
|
22
|
|
|
* @param PlayerTotalRepository |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct( |
|
25
|
|
|
DataEndpointController $dataEndpoint, |
|
26
|
|
|
PlayerTotalRepository $repository |
|
27
|
|
|
) { |
|
28
|
|
|
$this->repository = $repository; |
|
29
|
|
|
$this->dataEndpoint = $dataEndpoint; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Get Player Leaderboard |
|
34
|
|
|
* |
|
35
|
|
|
* @return \League\Fractal\Manager |
|
36
|
|
|
*/ |
|
37
|
|
|
public function players() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$valid = $this->validateRequestVars(); |
|
40
|
|
|
|
|
41
|
|
|
// If validation didn't pass, chuck 'em out |
|
42
|
|
|
if ($valid !== true) { |
|
43
|
|
|
return $this->respondWithError($valid->getMessage(), self::CODE_WRONG_ARGS); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$server = $_GET['server']; |
|
47
|
|
|
$limit = $_GET['limit']; |
|
48
|
|
|
$offset = $_GET['offset']; |
|
49
|
|
|
|
|
50
|
|
|
// Translate field into table specific columns |
|
51
|
|
|
if (isset($_GET['field'])) { |
|
52
|
|
|
$field = $this->getField($_GET['field']); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (! isset($field)) { |
|
56
|
|
|
return $this->respondWithError('Field wasn\'t provided and is required.', self::CODE_WRONG_ARGS); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Perform Query |
|
60
|
|
|
$query = $this->repository->newQuery(); |
|
61
|
|
|
$query->cols(['*']); |
|
62
|
|
|
$query->orderBy(["{$field} desc"]); |
|
63
|
|
|
|
|
64
|
|
|
if (isset($server)) { |
|
65
|
|
|
$query->where('playerServer = ?', $server); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (isset($limit)) { |
|
69
|
|
|
$query->limit($limit); |
|
70
|
|
|
} else { |
|
71
|
|
|
$query->limit(10); // Set default limit |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (isset($offset)) { |
|
75
|
|
|
$query->offset($offset); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
$players = $this->repository->fireStatementAndReturn($query); |
|
79
|
|
|
|
|
80
|
|
|
$count = count($players); |
|
81
|
|
|
|
|
82
|
|
|
// Gets outfit details |
|
83
|
|
|
for ($i = 0; $i < $count; $i++) { |
|
84
|
|
|
if (! empty($players[$i]['playerOutfit'])) { |
|
85
|
|
|
// Gets outfit details |
|
86
|
|
|
try { |
|
87
|
|
|
$outfit = $this->dataEndpoint->getOutfit($players[$i]['playerOutfit']); |
|
88
|
|
|
} catch (CensusErrorException $e) { |
|
89
|
|
|
$outfit = null; |
|
90
|
|
|
} catch (CensusEmptyException $e) { |
|
91
|
|
|
$outfit = null; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
$players[$i]['playerOutfit'] = $outfit; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
return $this->respond( |
|
99
|
|
|
'collection', |
|
100
|
|
|
$players, |
|
101
|
|
|
new PlayerLeaderboardTransformer |
|
102
|
|
|
); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Gets the appropiate field for the table and handles some table naming oddities |
|
107
|
|
|
* @param string $input Field to look at |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
|
View Code Duplication |
public function getField($input) { |
|
|
|
|
|
|
111
|
|
|
$field = null; |
|
112
|
|
|
|
|
113
|
|
|
switch ($input) { |
|
114
|
|
|
case 'kills': |
|
115
|
|
|
$field = 'playerKills'; |
|
116
|
|
|
break; |
|
117
|
|
|
case 'deaths': |
|
118
|
|
|
$field = 'playerDeaths'; |
|
119
|
|
|
break; |
|
120
|
|
|
case 'teamkills': |
|
121
|
|
|
$field = 'playerTeamKills'; |
|
122
|
|
|
break; |
|
123
|
|
|
case 'suicides': |
|
124
|
|
|
$field = 'playerSuicides'; |
|
125
|
|
|
break; |
|
126
|
|
|
case 'headshots': |
|
127
|
|
|
$field = 'headshots'; |
|
128
|
|
|
break; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
return $field; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: