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

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