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.

ratio_reqs.php 3.0KB

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