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

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