<?
	function bytesToGiB($bytes) {
		if ($bytes > 1048576) {
			$GiB = $bytes / 1073741824;
		} else {
			$GiB = 0;
		}
		return $GiB;
	}

	$Username = $LoggedUser['Username'];
	View::show_header($Username." &gt; Bonus Rates");

	$activeTorrents = array();
	$DB->query("
		SELECT x.timespent,
				t.GroupID,
				t.Media,
				t.Container,
				t.Codec,
				t.Resolution,
				t.AudioFormat,
				t.Size,
				t.Seeders,
				t.Recorded,
				us.SeedTime,
				(us.SeedTime/60/60) AS hours,
				(us.SeedTime/60/60/24) AS days,
				tg.Name AS Album,
				ag.Name AS Artist
		FROM xbt_files_users AS x
		LEFT JOIN torrents AS t ON t.ID=x.fid
		LEFT JOIN users_seedtime AS us ON x.fid=us.TorrentID AND x.uid=us.UserID
		LEFT JOIN torrents_group AS tg ON t.GroupID=tg.ID
		LEFT JOIN torrents_artists AS ta ON t.GroupID=ta.GroupID
		LEFT JOIN artists_group AS ag ON ta.ArtistID=ag.ArtistID
		WHERE x.uid = $UserID AND x.active=1 AND x.remaining=0
		GROUP BY x.fid, x.ip");
	while ($result = $DB->next_record()) {
		$activeTorrents[] = $result;
	}
	$totals = [
		'torrents' => 0,
		'size' => 0,
		'points' => 0,
	];
?>
<div class="thin">
	<div class="header">
		<h2><?=Users::format_username($UserID, false, false, false)?> &gt; Bonus Rates</h2>
	</div>
	<div class="main_column" style="width: 100%; margin-top: 18px">
		<table class="box torrent_table cats no_grouping" id="torrent_table">
			<tr class="colhead">
				<td>Torrent</td>
				<td>Size</td>
				<td><svg width="11" height="15" fill="white" class="tooltip" alt="Seeders" title="Seeders"><polygon points="0,7 5.5,0 11,7 8,7 8,15 3,15 3,7"></polygon></svg>
	          </td>
				<td>Seed Time</td>
				<td>BP/hour</td>
			</tr>
<?php foreach ($activeTorrents as $torrent) { 
	$points = bytesToGiB($torrent['Size']) * ( 0.25 + ( 0.6 * log(1+$torrent['days']) / pow($torrent['Seeders'],0.6) ) );
	$totals['torrents']++;
	$totals['size'] = $totals['size'] + $torrent['Size'];
	$totals['points'] = $totals['points'] + $points;
?>
			<tr id="site_style_tr">
				<td><?=$torrent['Artist'].' - '.$torrent['Album'].' - '.$torrent['Recorded'].' | '.$torrent['Codec'].' / '.$torrent['Media'].' / '.$torrent['Container'].' / '.$torrent['Resolution'].' / '.$torrent['AudioFormat']; ?></td>
				<td><?=Format::get_size($torrent['Size'])?></td>
				<td><?php echo $torrent['Seeders']; ?></td>
				<td><?php echo round($torrent['days'], 2); ?> days</td>
				<td><?=round($points, 4)?></td>
			</tr>
<?php } ?>
		</table>
		<br />
		<table class="box torrent_table cats no_grouping" id="torrent_table">
			<tr class="colhead">
				<td>Total Torrents</td>
				<td>Seeding Size</td>
				<td>BP/hour</td>
				<td>BP/day</td>
				<td>BP/month</td>
			</tr>
			<tr>
				<td><?=$totals['torrents']?></td>
				<td><?=Format::get_size($totals['size'])?></td>
				<td><?=round($totals['points'], 4)?></td>
				<td><?=round($totals['points']*24, 4)?></td>
				<td><?=round($totals['points']*24*30, 4)?></td>
			</tr>
		</table>
	</div>
</div>
<? View::show_footer(); ?>
