Gazelle/classes/user_rank.class.php

165 lines
3.9 KiB
PHP
Raw Normal View History

2011-03-28 14:21:28 +00:00
<?
define('PREFIX', 'percentiles_'); // Prefix for memcache keys, to make life easier
2013-06-19 08:01:09 +00:00
class UserRank {
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
// Returns a 101 row array (101 percentiles - 0 - 100), with the minimum value for that percentile as the value for each row
// BTW - ingenious
2013-06-19 08:01:09 +00:00
private static function build_table($MemKey, $Query) {
2013-06-06 08:01:03 +00:00
global $Cache, $DB;
2013-02-22 08:00:24 +00:00
2013-06-06 08:01:03 +00:00
$DB->query("
DROP TEMPORARY TABLE IF EXISTS temp_stats");
2013-02-22 08:00:24 +00:00
2013-05-27 08:00:58 +00:00
$DB->query("
CREATE TEMPORARY TABLE temp_stats (
ID int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
Val bigint(20) NOT NULL
);");
2013-02-22 08:00:24 +00:00
2013-06-06 08:01:03 +00:00
$DB->query("
INSERT INTO temp_stats (Val) ".
$Query);
2013-02-22 08:00:24 +00:00
2013-06-06 08:01:03 +00:00
$DB->query("
SELECT COUNT(ID)
FROM temp_stats");
2011-03-28 14:21:28 +00:00
list($UserCount) = $DB->next_record();
2013-02-22 08:00:24 +00:00
2013-05-27 08:00:58 +00:00
$DB->query("
SELECT MIN(Val)
FROM temp_stats
2013-07-10 00:08:53 +00:00
GROUP BY CEIL(ID / (".(int)$UserCount." / 100));");
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
$Table = $DB->to_array();
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
// Give a little variation to the cache length, so all the tables don't expire at the same time
2013-05-01 08:00:16 +00:00
$Cache->cache_value($MemKey, $Table, 3600 * 24 * rand(800, 1000) * 0.001);
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
return $Table;
}
2013-02-22 08:00:24 +00:00
2013-06-19 08:01:09 +00:00
private static function table_query($TableName) {
2013-05-01 08:00:16 +00:00
switch ($TableName) {
2011-03-28 14:21:28 +00:00
case 'uploaded':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT Uploaded
FROM users_main
2013-07-10 00:08:53 +00:00
WHERE Enabled = '1'
2013-05-27 08:00:58 +00:00
AND Uploaded > 0
ORDER BY Uploaded;";
2011-03-28 14:21:28 +00:00
break;
case 'downloaded':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT Downloaded
FROM users_main
2013-07-10 00:08:53 +00:00
WHERE Enabled = '1'
2013-05-27 08:00:58 +00:00
AND Downloaded > 0
ORDER BY Downloaded;";
2011-03-28 14:21:28 +00:00
break;
case 'uploads':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT COUNT(t.ID) AS Uploads
FROM users_main AS um
2013-07-10 00:08:53 +00:00
JOIN torrents AS t ON t.UserID = um.ID
WHERE um.Enabled = '1'
2013-05-27 08:00:58 +00:00
GROUP BY um.ID
ORDER BY Uploads;";
2011-03-28 14:21:28 +00:00
break;
case 'requests':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT COUNT(r.ID) AS Requests
FROM users_main AS um
2013-07-10 00:08:53 +00:00
JOIN requests AS r ON r.FillerID = um.ID
WHERE um.Enabled = '1'
2013-05-27 08:00:58 +00:00
GROUP BY um.ID
ORDER BY Requests;";
2011-03-28 14:21:28 +00:00
break;
case 'posts':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT COUNT(p.ID) AS Posts
FROM users_main AS um
2013-07-10 00:08:53 +00:00
JOIN forums_posts AS p ON p.AuthorID = um.ID
WHERE um.Enabled = '1'
2013-05-27 08:00:58 +00:00
GROUP BY um.ID
ORDER BY Posts;";
2011-03-28 14:21:28 +00:00
break;
case 'bounty':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT SUM(rv.Bounty) AS Bounty
FROM users_main AS um
2013-07-10 00:08:53 +00:00
JOIN requests_votes AS rv ON rv.UserID = um.ID
WHERE um.Enabled = '1' " .
"GROUP BY um.ID
2013-05-27 08:00:58 +00:00
ORDER BY Bounty;";
2011-03-28 14:21:28 +00:00
break;
case 'artists':
2013-05-27 08:00:58 +00:00
$Query = "
SELECT COUNT(ta.ArtistID) AS Artists
FROM torrents_artists AS ta
2013-07-10 00:08:53 +00:00
JOIN torrents_group AS tg ON tg.ID = ta.GroupID
2013-05-27 08:00:58 +00:00
JOIN torrents AS t ON t.GroupID = tg.ID
WHERE t.UserID != ta.UserID
GROUP BY tg.ID
ORDER BY Artists ASC";
2011-03-28 14:21:28 +00:00
break;
}
return $Query;
}
2013-02-22 08:00:24 +00:00
2013-06-19 08:01:09 +00:00
public static function get_rank($TableName, $Value) {
2013-04-20 08:01:01 +00:00
if ($Value == 0) {
return 0;
}
2011-03-28 14:21:28 +00:00
global $Cache, $DB;
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
$Table = $Cache->get_value(PREFIX.$TableName);
2013-04-20 08:01:01 +00:00
if (!$Table) {
2011-03-28 14:21:28 +00:00
//Cache lock!
2013-07-10 00:08:53 +00:00
$Lock = $Cache->get_value(PREFIX.$TableName.'_lock');
2013-04-20 08:01:01 +00:00
if ($Lock) {
2012-03-03 08:00:28 +00:00
return false;
2011-03-28 14:21:28 +00:00
} else {
2013-07-10 00:08:53 +00:00
$Cache->cache_value(PREFIX.$TableName.'_lock', '1', 300);
2013-06-19 08:01:09 +00:00
$Table = self::build_table(PREFIX.$TableName, self::table_query($TableName));
2013-07-10 00:08:53 +00:00
$Cache->delete_value(PREFIX.$TableName.'_lock');
2011-03-28 14:21:28 +00:00
}
}
$LastPercentile = 0;
foreach ($Table as $Row) {
list($CurValue) = $Row;
2013-04-20 08:01:01 +00:00
if ($CurValue >= $Value) {
2011-03-28 14:21:28 +00:00
return $LastPercentile;
}
$LastPercentile++;
}
return 100; // 100th percentile
}
2013-02-22 08:00:24 +00:00
2013-06-19 08:01:09 +00:00
public static function overall_score($Uploaded, $Downloaded, $Uploads, $Requests, $Posts, $Bounty, $Artists, $Ratio) {
2011-03-28 14:21:28 +00:00
// We can do this all in 1 line, but it's easier to read this way
2013-04-20 08:01:01 +00:00
if ($Ratio > 1) {
$Ratio = 1;
}
2011-03-28 14:21:28 +00:00
$TotalScore = 0;
2013-04-20 08:01:01 +00:00
if (in_array(false, func_get_args(), true)) {
2012-03-03 08:00:28 +00:00
return false;
}
2013-05-01 08:00:16 +00:00
$TotalScore += $Uploaded * 15;
$TotalScore += $Downloaded * 8;
$TotalScore += $Uploads * 25;
$TotalScore += $Requests * 2;
2011-03-28 14:21:28 +00:00
$TotalScore += $Posts;
$TotalScore += $Bounty;
$TotalScore += $Artists;
2013-05-01 08:00:16 +00:00
$TotalScore /= (15 + 8 + 25 + 2 + 1 + 1 + 1);
2011-03-28 14:21:28 +00:00
$TotalScore *= $Ratio;
return $TotalScore;
}
2013-02-22 08:00:24 +00:00
2011-03-28 14:21:28 +00:00
}
?>