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 6.1KB

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