Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Add) Count of applicable torrents per bon earning #4464

Merged
merged 1 commit into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion app/Http/Livewire/UserEarnings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,56 @@ final public function updatingSearch(): void
#[Computed]
final public function bonEarnings(): \Illuminate\Database\Eloquent\Collection
{
return BonEarning::query()->orderBy('position')->get()->keyBy('id');
$query = '';

foreach (BonEarning::with('conditions')->orderBy('position')->get() as $bonEarning) {
// Raw bindings are fine since all database values are either enums or numeric
$conditionQuery = '1=1';

foreach ($bonEarning->conditions as $condition) {
$conditionQuery .= ' AND '.match ($condition->operand1) {
'1' => '1',
'age' => 'TIMESTAMPDIFF(SECOND, torrents.created_at, NOW())',
'size' => 'torrents.size',
'seeders' => 'torrents.seeders',
'leechers' => 'torrents.leechers',
'times_completed' => 'torrents.times_completed',
'internal' => 'torrents.internal',
'personal_release' => 'torrents.personal_release',
'type_id' => 'torrents.type_id',
'seedtime' => 'history.seedtime',
'connectable' => 'peers.connectable',
}.' '.$condition->operator.' '.$condition->operand2;
}

$query .= " WHEN bon_earnings.id = {$bonEarning->id} THEN MAX({$conditionQuery})";
}

return BonEarning::query()
->addSelect([
'torrents_count' => DB::query()->fromSub(
DB::table('peers')
->select([
'peers.torrent_id',
DB::raw("CASE {$query} END AS torrent_count"),
])
->join('history', fn ($join) => $join->on('history.torrent_id', '=', 'peers.torrent_id')->on('history.user_id', '=', 'peers.user_id'))
->join('torrents', 'peers.torrent_id', '=', 'torrents.id')
->where('peers.seeder', '=', true)
->where('peers.active', '=', true)
->where('peers.user_id', '=', $this->user->id)
->where('peers.created_at', '<', now()->subMinutes(30))
->where('torrents.name', 'LIKE', '%'.str_replace(' ', '%', $this->torrentName).'%')
->groupBy(['peers.user_id', 'peers.torrent_id']),
'peers_per_torrent'
)
->select([
DB::raw('SUM(torrent_count)')
])
])
->orderBy('position')
->get()
->keyBy('id');
}

/**
Expand Down
7 changes: 6 additions & 1 deletion resources/views/livewire/user-earnings.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<th>{{ __('common.name') }}</th>
<th>{{ __('common.description') }}</th>
<th>Per torrent per hour</th>
<th>{{ __('torrent.torrents') }}</th>
</tr>
</thead>
<tbody>
Expand All @@ -23,9 +24,13 @@
&plus;
@endif

{{ $bonEarning->variable }} &times;
{{ preg_replace('/(\.\d+?)0+$/', '$1', $bonEarning->multiplier) }}

@if ($bonEarning->variable != 1)
&times; {{ $bonEarning->variable }}
@endif
</td>
<td>{{ $bonEarning->torrents_count }}</td>
</tr>
@endforeach
</tbody>
Expand Down