Oppaitime'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.

ratio_reqs.php 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?
  2. //------------------- Ratio requirements -----------------------//
  3. // Clear old seed time history
  4. $DB->query("
  5. DELETE FROM users_torrent_history
  6. WHERE Date < DATE('".sqltime()."' - INTERVAL 7 DAY) + 0");
  7. // Store total seeded time for each user in a temp table
  8. $DB->query("TRUNCATE TABLE users_torrent_history_temp");
  9. $DB->query("
  10. INSERT INTO users_torrent_history_temp
  11. (UserID, SumTime)
  12. SELECT UserID, SUM(Time)
  13. FROM users_torrent_history
  14. GROUP BY UserID");
  15. // Insert new row with <NumTorrents> = 0 with <Time> being number of seconds short of 72 hours.
  16. // This is where we penalize torrents seeded for less than 72 hours
  17. $DB->query("
  18. INSERT INTO users_torrent_history
  19. (UserID, NumTorrents, Date, Time)
  20. SELECT UserID, 0, UTC_DATE() + 0, 259200 - SumTime
  21. FROM users_torrent_history_temp
  22. WHERE SumTime < 259200");
  23. // Set <Weight> to the time seeding <NumTorrents> torrents
  24. $DB->query("
  25. UPDATE users_torrent_history
  26. SET Weight = NumTorrents * Time");
  27. // Calculate average time spent seeding each of the currently active torrents.
  28. // This rounds the results to the nearest integer because SeedingAvg is an int column.
  29. $DB->query("TRUNCATE TABLE users_torrent_history_temp");
  30. $DB->query("
  31. INSERT INTO users_torrent_history_temp
  32. (UserID, SeedingAvg)
  33. SELECT UserID, SUM(Weight) / SUM(Time)
  34. FROM users_torrent_history
  35. GROUP BY UserID");
  36. // Remove dummy entry for torrents seeded less than 72 hours
  37. $DB->query("
  38. DELETE FROM users_torrent_history
  39. WHERE NumTorrents = '0'");
  40. // Get each user's amount of snatches of existing torrents
  41. $DB->query("TRUNCATE TABLE users_torrent_history_snatch");
  42. $DB->query("
  43. INSERT INTO users_torrent_history_snatch (UserID, NumSnatches)
  44. SELECT xs.uid, COUNT(DISTINCT xs.fid)
  45. FROM xbt_snatched AS xs
  46. JOIN torrents AS t ON t.ID = xs.fid
  47. GROUP BY xs.uid");
  48. // Get the fraction of snatched torrents seeded for at least 72 hours this week
  49. // Essentially take the total number of hours seeded this week and divide that by 72 hours * <NumSnatches>
  50. $DB->query("
  51. UPDATE users_main AS um
  52. JOIN users_torrent_history_temp AS t ON t.UserID = um.ID
  53. JOIN users_torrent_history_snatch AS s ON s.UserID = um.ID
  54. SET um.RequiredRatioWork = (1 - (t.SeedingAvg / s.NumSnatches))
  55. WHERE s.NumSnatches > 0");
  56. // TODO: change from PHP_INT_MAX to INF when we get prepared statements working (because apparently that works)
  57. $DownloadBarrier = PHP_INT_MAX;
  58. foreach (RATIO_REQUIREMENTS as $Requirement) {
  59. list($Download, $Ratio, $MinRatio) = $Requirement;
  60. $DB->query("
  61. UPDATE users_main
  62. SET RequiredRatio = RequiredRatioWork * $Ratio
  63. WHERE Downloaded >= $Download
  64. AND Downloaded < $DownloadBarrier");
  65. $DB->query("
  66. UPDATE users_main
  67. SET RequiredRatio = $MinRatio
  68. WHERE Downloaded >= $Download
  69. AND Downloaded < $DownloadBarrier
  70. AND RequiredRatio < $MinRatio");
  71. $DownloadBarrier = $Download;
  72. }
  73. ?>