Skip to content

Commit 4279561

Browse files
RushawayA1mDev
andcommitted
Refactor player rank calculation: move to PlayerRepository with PDO and OptionService
Co-Authored-By: A1m` <33463136+A1mDev@users.noreply.github.com>
1 parent c0c793c commit 4279561

6 files changed

Lines changed: 93 additions & 93 deletions

File tree

web/bootstrap.php‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Utils\Logger;
99
use Repository\OptionsRepository;
1010
use Repository\GameRepository;
11+
use Repository\PlayerRepository;
1112
use Service\OptionService;
1213

1314
$container = new class
@@ -68,4 +69,12 @@
6869
return new GameRepository($c->get('pdo'), $c->get('logger'));
6970
});
7071

72+
$container->set(PlayerRepository::class, function($c) {
73+
return new PlayerRepository(
74+
$c->get('pdo'),
75+
$c->get('logger'),
76+
$c->get(OptionService::class)
77+
);
78+
});
79+
7180
return $container;

web/classes/Repository/PlayerRepository.php‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
namespace Repository;
33

44
use PDO;
5+
use PDOException;
56
use Utils\Logger;
7+
use Service\OptionService;
68

79
class PlayerRepository
810
{
911
private PDO $pdo;
1012
private Logger $logger;
13+
private OptionService $optionService;
1114

12-
public function __construct(PDO $pdo, Logger $logger)
15+
public function __construct(PDO $pdo, Logger $logger, OptionService $optionService)
1316
{
1417
$this->pdo = $pdo;
1518
$this->logger = $logger;
19+
$this->optionService = $optionService;
1620
}
1721

1822
public function getPlayerSuggestions(string $game, string $search, int $limit = 30) : array
@@ -49,4 +53,63 @@
4953
return [];
5054
}
5155
}
52-
}
56+
57+
public function getPlayerRank(string $game, string $rankingType, int $playerPoints, int $playerKills, int $playerDeaths) : ?int
58+
{
59+
$allowedRankingType = $this->optionService->getRankingTypeChoices();
60+
if (empty($allowedRankingType)) {
61+
$allowedRankingType = ['kills', 'skill']; // default params
62+
}
63+
64+
if (!in_array($rankingType, $allowedRankingType, true)) {
65+
return null;
66+
}
67+
68+
$tempDeaths = $playerDeaths;
69+
if ($tempDeaths == 0) {
70+
$tempDeaths = 1;
71+
}
72+
73+
$kpd = $playerKills / $tempDeaths;
74+
75+
$sql = "
76+
SELECT
77+
COUNT(*) + 1
78+
FROM
79+
hlstats_Players
80+
WHERE
81+
game = :game
82+
AND
83+
hideranking = 0
84+
AND
85+
kills >= 1
86+
AND (
87+
{$rankingType} > :points
88+
OR (
89+
{$rankingType} = :points
90+
AND (kills / IF(deaths = 0, 1, deaths) > :kpd)
91+
)
92+
)
93+
";
94+
95+
try {
96+
$stmt = $this->pdo->prepare($sql);
97+
98+
$stmt->execute([
99+
'game' => $game,
100+
'points' => $playerPoints,
101+
'kpd' => $kpd,
102+
]);
103+
104+
$count = $stmt->fetchColumn();
105+
if ($count === false) {
106+
return null;
107+
}
108+
109+
return (int)$count;
110+
} catch (PDOException $e) {
111+
$this->logger->error("Failed to get player rank: " . $e->getMessage());
112+
return null;
113+
}
114+
}
115+
}

web/includes/functions.php

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -557,62 +557,6 @@ function getJSText($js)
557557
return "\t<script type=\"text/javascript\" src=\"".INCLUDE_PATH."/js/$js.js\"></script> \n";
558558
}
559559

560-
// :game = $playerdata['game'];
561-
// $rankingType = $g_options['rankingtype'];
562-
// $playerDeaths = $playerdata['deaths'];
563-
// $playerValue = $playerdata[$rankingType]
564-
// $playerKills = $playerdata['kills']
565-
function get_player_rank(DB_mysql $db, string $game, string $rankingType, int $playerPoints, int $playerKills, int $playerDeaths) : ?int
566-
{
567-
$allowedRankingType = ['kills', 'skill'];
568-
if (!in_array($rankingType, $allowedRankingType, true)) {
569-
return null;
570-
}
571-
572-
$gameEscape = $db->escape($game);
573-
$rankingTypeEscape = $db->escape($rankingType);
574-
$playerPoinsEscape = $db->escape($playerPoints);
575-
576-
$tempDeaths = $playerDeaths;
577-
if ($tempDeaths == 0) {
578-
$tempDeaths = 1;
579-
}
580-
581-
$kpdEscape = $db->escape($playerKills / $tempDeaths);
582-
583-
$sql = "
584-
SELECT
585-
COUNT(*)
586-
FROM
587-
hlstats_Players
588-
WHERE
589-
game = '{$gameEscape}'
590-
AND
591-
hideranking = 0
592-
AND
593-
kills >= 1
594-
AND (
595-
{$rankingTypeEscape} > {$playerPoinsEscape}
596-
OR (
597-
{$rankingTypeEscape} = {$playerPoinsEscape}
598-
AND (kills / IF(deaths = 0, 1, deaths) > {$kpdEscape})
599-
)
600-
)
601-
";
602-
603-
$dbResult = $db->query($sql);
604-
if ($dbResult === false) {
605-
return null;
606-
}
607-
608-
$rank = 0;
609-
list($rank) = $db->fetch_row($dbResult);
610-
$rank++;
611-
612-
$db->free_result($dbResult);
613-
return $rank;
614-
}
615-
616560
/**
617561
* Convert colors Usage: color::hex2rgb("FFFFFF")
618562
*

web/pages/ingame/statsme.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@
3636
For support and installation notes visit http://www.hlxcommunity.com
3737
*/
3838

39+
// Player Details
3940
if (!defined('IN_HLSTATS')) {
4041
die('Do not access this file directly.');
4142
}
4243

43-
// Player Details
44-
44+
$container = require ROOT_PATH . '/bootstrap.php';
45+
$playerRepo = $container->get(\Repository\PlayerRepository::class);
46+
4547
$player = valid_request(intval($_GET['player']), true);
4648
$uniqueid = valid_request(strval($_GET['uniqueid']), false);
4749
$game = valid_request(strval($_GET['game']), false);
@@ -177,14 +179,7 @@
177179
$plKills = $playerdata['kills'];
178180
$playerDeaths = $playerdata['deaths'];
179181

180-
$rank = get_player_rank(
181-
$db,
182-
$plGame,
183-
$rankType,
184-
$plValue,
185-
$plKills,
186-
$playerDeaths
187-
);
182+
$rank = $playerRepo->getPlayerRank($plGame, $rankType, $plValue, $plKills, $playerDeaths);
188183

189184
if (is_null($rank)) {
190185
$rank = 'Unknown';

web/pages/playerinfo_general.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
if (!defined('IN_HLSTATS')) {
4040
die('Do not access this file directly.');
4141
}
42+
43+
$container = require ROOT_PATH . '/bootstrap.php';
44+
$playerRepo = $container->get(\Repository\PlayerRepository::class);
4245
?>
4346

4447
<?php printSectionTitle('Player Information'); ?>
@@ -424,14 +427,7 @@
424427
$plKills = $playerdata['kills'];
425428
$playerDeaths = $playerdata['deaths'];
426429

427-
$rank = get_player_rank(
428-
$db,
429-
$plGame,
430-
$rankType,
431-
$plValue,
432-
$plKills,
433-
$playerDeaths
434-
);
430+
$rank = $playerRepo->getPlayerRank($plGame, $rankType, $plValue, $plKills, $playerDeaths);
435431

436432
if (is_null($rank)) {
437433
$rank = 'Unknown';

web/sig.php

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@
7171
require (INCLUDE_PATH . '/class_db.php');
7272
require (INCLUDE_PATH . '/functions.php');
7373

74+
$container = require ROOT_PATH . '/bootstrap.php';
75+
$playerRepo = $container->get(\Repository\PlayerRepository::class);
76+
$optionService = $container->get(\Service\OptionService::class);
77+
7478
$db_classname = 'DB_' . DB_TYPE;
7579
if (class_exists($db_classname))
7680
{
@@ -81,9 +85,6 @@
8185
error('Database class does not exist. Please check your config.php file for DB_TYPE');
8286
}
8387

84-
$container = require ROOT_PATH . '/bootstrap.php';
85-
$optionService = $container->get(\Service\OptionService::class);
86-
8788
$g_options = $optionService->getAllOptions();
8889
if (empty($g_options)) {
8990
error('Warning: Could not find any options in the database. Check HLStats configuration.');
@@ -276,14 +277,7 @@ function f_num($number) {
276277
$plKills = $playerdata['kills'];
277278
$playerDeaths = $playerdata['deaths'];
278279

279-
$rank = get_player_rank(
280-
$db,
281-
$plGame,
282-
$rankType,
283-
$plValue,
284-
$plKills,
285-
$playerDeaths
286-
);
280+
$rank = $playerRepo->getPlayerRank($plGame, $rankType, $plValue, $plKills, $playerDeaths);
287281

288282
if (is_null($rank)) {
289283
$rank = 'Unknown';
@@ -427,11 +421,10 @@ function f_num($number) {
427421
$timestamp = $playerdata['connection_time'];
428422
$days = floor($timestamp / 86400);
429423
$hours = $days * 24;
430-
$hours += floor($timestamp / 3600) % 24;
431-
if ($hours < 10) {
432-
$hours = '0' . (int)$hours;
433-
}
434-
$min = floor($timestamp / 60) % 60;
424+
$hours += floor($timestamp / 3600 % 24);
425+
if ($hours < 10)
426+
$hours = '0'.$hours;
427+
$min = floor($timestamp / 60 % 60);
435428
if ($min < 10)
436429
$min = '0'.$min;
437430
$sec = floor($timestamp % 60);
@@ -494,4 +487,4 @@ function f_num($number) {
494487
imagedestroy($watermark);
495488

496489
}
497-
?>
490+
?>

0 commit comments

Comments
 (0)