1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ps2alerts\Api\Loader\Statistics; |
4
|
|
|
|
5
|
|
|
use Ps2alerts\Api\Loader\Statistics\AbstractStatisticsLoader; |
6
|
|
|
use Ps2alerts\Api\QueryObjects\QueryObject; |
7
|
|
|
use Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository; |
8
|
|
|
|
9
|
|
|
class PlayerStatisticsLoader extends AbstractStatisticsLoader |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var \Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository |
13
|
|
|
*/ |
14
|
|
|
protected $repository; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Construct |
18
|
|
|
* |
19
|
|
|
* @param \Ps2alerts\Api\Repository\Statistics\PlayerTotalsRepository $repository |
20
|
|
|
*/ |
21
|
|
|
public function __construct(PlayerTotalsRepository $repository) |
22
|
|
|
{ |
23
|
|
|
$this->repository = $repository; |
24
|
|
|
$this->setCacheNamespace('Statistics'); |
25
|
|
|
$this->setType('Players'); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function readLeaderboard(array $post) |
29
|
|
|
{ |
30
|
|
|
$redisKey = "{$this->getCacheNamespace()}:{$this->getType()}:Leaderboards"; |
31
|
|
|
|
32
|
|
|
$queryObject = new QueryObject; |
33
|
|
|
|
34
|
|
|
// Build based on metric alone (order by playerKills for example) |
35
|
|
|
if (! empty($post['metric'] && empty($post['value'])) |
36
|
|
|
&& $this->validatePostVars('metric', $post['metric']) === true |
37
|
|
|
) { |
38
|
|
|
$direction = (! empty($post['direction']) ? $post['direction'] : 'desc'); |
39
|
|
|
|
40
|
|
|
$queryObject->setOrderBy($post['metric']); |
41
|
|
|
$queryObject->setOrderByDirection($direction); |
42
|
|
|
|
43
|
|
|
$redisKey .= ":{$post['metric']}-{$direction}"; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// Build based on metric and a value (such as order by playerKills by server) |
47
|
|
|
if (! empty($post['value']) |
48
|
|
|
&& ! empty($post['metric']) |
49
|
|
|
&& $this->validatePostVars('metric', $post['metric']) === true |
50
|
|
|
) { |
51
|
|
|
$op = (! empty($post['operator']) ? $post['operator'] : '='); |
52
|
|
|
|
53
|
|
|
$queryObject->addWhere([ |
54
|
|
|
'col' => $post['metric'], |
55
|
|
|
'op' => $op, |
56
|
|
|
'value' => $post['value'] |
57
|
|
|
]); |
58
|
|
|
|
59
|
|
|
$direction = (! empty($post['direction']) ? $post['direction'] : 'desc'); |
60
|
|
|
|
61
|
|
|
$queryObject->setOrderBy($post['metric']); |
62
|
|
|
$queryObject->setOrderByDirection($direction); |
63
|
|
|
|
64
|
|
|
$redisKey .= ":{$post['metric']}{$op}{$post['value']}-{$direction}"; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if (! empty($post['limit'])) { |
68
|
|
|
$post['limit'] = intval($post['limit']); |
69
|
|
|
|
70
|
|
|
// Hard encode a 50 limit |
71
|
|
|
if ($post['limit'] > 50) { |
72
|
|
|
$post['limit'] = 50; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
$queryObject->setLimit($post['limit']); |
76
|
|
|
} else { |
77
|
|
|
$post['limit'] = 50; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$redisKey .= ":limit-{$post['limit']}"; |
81
|
|
|
|
82
|
|
|
/*if ($this->checkRedis($redisKey)) { |
|
|
|
|
83
|
|
|
return $this->getFromRedis($redisKey); |
84
|
|
|
}*/ |
85
|
|
|
|
86
|
|
|
$this->setCacheExpireTime(3600); // 1 hour |
87
|
|
|
|
88
|
|
|
return $this->cacheAndReturn( |
89
|
|
|
$this->repository->read($queryObject), |
90
|
|
|
$redisKey |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function validatePostVars($field, $value) |
95
|
|
|
{ |
96
|
|
|
if ($field === 'metric' && ! empty($value)) { |
97
|
|
|
switch ($value) { |
98
|
|
|
case 'playerKills': |
99
|
|
|
case 'playerDeaths': |
100
|
|
|
case 'playerTeamKills': |
101
|
|
|
case 'playerSuicides': |
102
|
|
|
case 'playerFaction': |
103
|
|
|
case 'headshots': |
104
|
|
|
case 'playerServer': |
105
|
|
|
return true; |
106
|
|
|
break; |
|
|
|
|
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.