I apologize in advance for not writing a better bug report but I forgot the details and have no time to check everything again.
The following code in bt_tracker_cron should update the number of seeders:
function bt_tracker_cron() {
// Update the statistics for the torrent based off of the active users table
// Update the seeders first
$result = db_query("SELECT bt.info_hash, COUNT(btau.ip) AS seeders FROM {bt_torrents} bt INNER JOIN {bt_tracker_active_users} btau ON btau.info_hash = bt.info_hash WHERE btau.bytes_left = 0 GROUP BY bt.info_hash");
while ($row = db_fetch_array($result)) {
db_query("UPDATE {bt_torrents} bt SET seeders = %d WHERE info_hash = %b", $row['seeders'], $row['info_hash']);
}
$result = db_query("SELECT bt.info_hash, COUNT(btau.ip) AS seeders FROM {bt_torrents} bt LEFT JOIN {bt_tracker_active_users} btau ON btau.info_hash = bt.info_hash WHERE NOT btau.bytes_left = 0 OR btau.ip IS NULL GROUP BY bt.info_hash");
while ($row = db_fetch_array($result)) {
db_query("UPDATE {bt_torrents} bt SET seeders = %d WHERE info_hash = %b", $row['seeders'], $row['info_hash']);
}
This does not work correctly on my system and apparently caused users to be unable to download torrents using certain clients. Surprisingly enough it did work for other clients, but with a delay in starting the updates. Perhaps this was caused by DHT networks or some other means of finding peers without the help of the server.
I fixed this a month ago and unfortunately I don't remember the details, but if I remember correctly the first query works as it should. For the second one, suppose there is a torrent with a leecher but no seeder. Since there is one leecher (one active user), there will be no row where btau.ip IS NULL for this torrent. There will only be a row where btau.bytes_left is not 0. This row satisfies the WHERE requirement (NOT btau.bytes_left = 0 OR btau.ip IS NULL) and therefore the single leecher counts as a seeder -- if I remember correctly.
Initially I replaced it with this nested query, which works correctly for me:
$result = db_query("SELECT bt.info_hash, COUNT(btau.ip) AS seeders FROM {bt_torrents} bt LEFT JOIN (SELECT * FROM {bt_tracker_active_users} WHERE bytes_left = 0) btau ON btau.info_hash = bt.info_hash GROUP BY bt.info_hash");
while ($row = db_fetch_array($result)) {
db_query("UPDATE {bt_torrents} bt SET seeders = %d WHERE info_hash = %b", $row['seeders'], $row['info_hash']);
}
But I'm not sure nested queries are really supposed to be supported by Drupal 5.
Perhaps one could use a single UPDATE query in some way, if nested COUNT queries are allowed?
$result = db_query("UPDATE {bt_torrents} bt SET seeders = COUNT(SELECT * FROM {bt_tracker_active_users} btau WHERE btau.bytes_left = 0 AND btau.info_hash = bt.info_hash");
This is completely untested, though.