BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

userrank.class.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. #declare(strict_types=1);
  3. class UserRank
  4. {
  5. const PREFIX = 'percentiles_'; // Prefix for memcache keys, to make life easier
  6. // Returns a 101 row array (101 percentiles - 0 - 100), with the minimum value for that percentile as the value for each row
  7. // BTW - ingenious
  8. private static function build_table($MemKey, $Query)
  9. {
  10. $QueryID = G::$DB->get_query_id();
  11. G::$DB->query("
  12. DROP TEMPORARY TABLE IF EXISTS temp_stats");
  13. G::$DB->query("
  14. CREATE TEMPORARY TABLE temp_stats (
  15. ID int(10) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  16. Val bigint(20) NOT NULL
  17. );");
  18. G::$DB->query("
  19. INSERT INTO temp_stats (Val) ".
  20. $Query);
  21. G::$DB->query("
  22. SELECT COUNT(ID)
  23. FROM temp_stats");
  24. list($UserCount) = G::$DB->next_record();
  25. G::$DB->query("
  26. SELECT MIN(Val)
  27. FROM temp_stats
  28. GROUP BY CEIL(ID / (".(int)$UserCount." / 100));");
  29. $Table = G::$DB->to_array();
  30. G::$DB->set_query_id($QueryID);
  31. // Give a little variation to the cache length, so all the tables don't expire at the same time
  32. G::$Cache->cache_value($MemKey, $Table, 3600 * 24 * rand(800, 1000) * 0.001);
  33. return $Table;
  34. }
  35. private static function table_query($TableName)
  36. {
  37. switch ($TableName) {
  38. case 'uploaded':
  39. $Query = "
  40. SELECT Uploaded
  41. FROM users_main
  42. WHERE Enabled = '1'
  43. AND Uploaded > 0
  44. ORDER BY Uploaded;";
  45. break;
  46. case 'downloaded':
  47. $Query = "
  48. SELECT Downloaded
  49. FROM users_main
  50. WHERE Enabled = '1'
  51. AND Downloaded > 0
  52. ORDER BY Downloaded;";
  53. break;
  54. case 'uploads':
  55. $Query = "
  56. SELECT COUNT(t.ID) AS Uploads
  57. FROM users_main AS um
  58. JOIN torrents AS t ON t.UserID = um.ID
  59. WHERE um.Enabled = '1'
  60. GROUP BY um.ID
  61. ORDER BY Uploads;";
  62. break;
  63. case 'requests':
  64. $Query = "
  65. SELECT COUNT(r.ID) AS Requests
  66. FROM users_main AS um
  67. JOIN requests AS r ON r.FillerID = um.ID
  68. WHERE um.Enabled = '1'
  69. GROUP BY um.ID
  70. ORDER BY Requests;";
  71. break;
  72. case 'posts':
  73. $Query = "
  74. SELECT COUNT(p.ID) AS Posts
  75. FROM users_main AS um
  76. JOIN forums_posts AS p ON p.AuthorID = um.ID
  77. WHERE um.Enabled = '1'
  78. GROUP BY um.ID
  79. ORDER BY Posts;";
  80. break;
  81. case 'bounty':
  82. $Query = "
  83. SELECT SUM(rv.Bounty) AS Bounty
  84. FROM users_main AS um
  85. JOIN requests_votes AS rv ON rv.UserID = um.ID
  86. WHERE um.Enabled = '1' " .
  87. "GROUP BY um.ID
  88. ORDER BY Bounty;";
  89. break;
  90. case 'artists':
  91. $Query = "
  92. SELECT COUNT(ta.ArtistID) AS Artists
  93. FROM torrents_artists AS ta
  94. JOIN torrents_group AS tg ON tg.ID = ta.GroupID
  95. JOIN torrents AS t ON t.GroupID = tg.ID
  96. WHERE t.UserID != ta.UserID
  97. GROUP BY tg.ID
  98. ORDER BY Artists ASC";
  99. break;
  100. }
  101. return $Query;
  102. }
  103. public static function get_rank($TableName, $Value)
  104. {
  105. if ($Value == 0) {
  106. return 0;
  107. }
  108. $Table = G::$Cache->get_value(self::PREFIX.$TableName);
  109. if (!$Table) {
  110. //Cache lock!
  111. $Lock = G::$Cache->get_value(self::PREFIX.$TableName.'_lock');
  112. if ($Lock) {
  113. return false;
  114. } else {
  115. G::$Cache->cache_value(self::PREFIX.$TableName.'_lock', '1', 300);
  116. $Table = self::build_table(self::PREFIX.$TableName, self::table_query($TableName));
  117. G::$Cache->delete_value(self::PREFIX.$TableName.'_lock');
  118. }
  119. }
  120. $LastPercentile = 0;
  121. foreach ($Table as $Row) {
  122. list($CurValue) = $Row;
  123. if ($CurValue >= $Value) {
  124. return $LastPercentile;
  125. }
  126. $LastPercentile++;
  127. }
  128. return 100; // 100th percentile
  129. }
  130. public static function overall_score($Uploaded, $Downloaded, $Uploads, $Requests, $Posts, $Bounty, $Artists, $Ratio)
  131. {
  132. // We can do this all in 1 line, but it's easier to read this way
  133. if ($Ratio > 1) {
  134. $Ratio = 1;
  135. }
  136. $TotalScore = 0;
  137. if (in_array(false, func_get_args(), true)) {
  138. return false;
  139. }
  140. $TotalScore += $Uploaded * 15;
  141. $TotalScore += $Downloaded * 8;
  142. $TotalScore += $Uploads * 25;
  143. $TotalScore += $Requests * 2;
  144. $TotalScore += $Posts;
  145. $TotalScore += $Bounty;
  146. $TotalScore += $Artists;
  147. $TotalScore /= (15 + 8 + 25 + 2 + 1 + 1 + 1);
  148. $TotalScore *= $Ratio;
  149. return $TotalScore;
  150. }
  151. }