|
17 | 17 | namespace App\Console\Commands;
|
18 | 18 |
|
19 | 19 | use App\Helpers\ByteUnits;
|
| 20 | +use App\Models\BonEarning; |
20 | 21 | use App\Models\User;
|
21 | 22 | use Illuminate\Console\Command;
|
22 | 23 | use Illuminate\Support\Facades\DB;
|
@@ -47,230 +48,80 @@ public function handle(ByteUnits $byteUnits): void
|
47 | 48 | {
|
48 | 49 | $now = now();
|
49 | 50 |
|
50 |
| - $dyingTorrent = DB::table('peers') |
51 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
52 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
53 |
| - ->where('torrents.seeders', 1) |
54 |
| - ->where('torrents.times_completed', '>', 2) |
55 |
| - ->where('peers.seeder', 1) |
56 |
| - ->where('peers.active', 1) |
57 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
58 |
| - ->groupBy('peers.user_id') |
59 |
| - ->get() |
60 |
| - ->toArray(); |
61 |
| - |
62 |
| - $legendaryTorrent = DB::table('peers') |
63 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
64 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
65 |
| - ->where('peers.seeder', 1) |
66 |
| - ->where('peers.active', 1) |
67 |
| - ->where('torrents.created_at', '<', $now->copy()->subMonths(12)) |
68 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
69 |
| - ->groupBy('peers.user_id') |
70 |
| - ->get() |
71 |
| - ->toArray(); |
72 |
| - |
73 |
| - $oldTorrent = DB::table('peers') |
74 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
75 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
76 |
| - ->where('peers.seeder', 1) |
77 |
| - ->where('peers.active', 1) |
78 |
| - ->where('torrents.created_at', '<', $now->copy()->subMonths(6)) |
79 |
| - ->where('torrents.created_at', '>', $now->copy()->subMonths(12)) |
80 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
81 |
| - ->groupBy('peers.user_id') |
82 |
| - ->get() |
83 |
| - ->toArray(); |
84 |
| - |
85 |
| - $hugeTorrent = DB::table('peers') |
86 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
87 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
88 |
| - ->where('peers.seeder', 1) |
89 |
| - ->where('peers.active', 1) |
90 |
| - ->where('torrents.size', '>=', $byteUnits->bytesFromUnit('100GiB')) |
91 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
92 |
| - ->groupBy('peers.user_id') |
93 |
| - ->get() |
94 |
| - ->toArray(); |
95 |
| - |
96 |
| - $largeTorrent = DB::table('peers') |
97 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
98 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
99 |
| - ->where('peers.seeder', 1) |
100 |
| - ->where('peers.active', 1) |
101 |
| - ->where('torrents.size', '>=', $byteUnits->bytesFromUnit('25GiB')) |
102 |
| - ->where('torrents.size', '<', $byteUnits->bytesFromUnit('100GiB')) |
103 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
104 |
| - ->groupBy('peers.user_id') |
105 |
| - ->get() |
106 |
| - ->toArray(); |
107 |
| - |
108 |
| - $regularTorrent = DB::table('peers') |
109 |
| - ->select(DB::raw('count(DISTINCT(peers.torrent_id)) as value'), 'peers.user_id') |
110 |
| - ->join('torrents', 'torrents.id', 'peers.torrent_id') |
111 |
| - ->where('peers.seeder', 1) |
112 |
| - ->where('peers.active', 1) |
113 |
| - ->where('torrents.size', '>=', $byteUnits->bytesFromUnit('1GiB')) |
114 |
| - ->where('torrents.size', '<', $byteUnits->bytesFromUnit('25GiB')) |
115 |
| - ->where('peers.created_at', '<', $now->copy()->subMinutes(30)) |
116 |
| - ->groupBy('peers.user_id') |
117 |
| - ->get() |
118 |
| - ->toArray(); |
119 |
| - |
120 |
| - $participaintSeeder = DB::table('history') |
121 |
| - ->select(DB::raw('count(*) as value'), 'history.user_id') |
122 |
| - ->where('history.active', 1) |
123 |
| - ->where('history.seedtime', '>=', 2_592_000) |
124 |
| - ->where('history.seedtime', '<', 2_592_000 * 2) |
125 |
| - ->groupBy('history.user_id') |
126 |
| - ->get() |
127 |
| - ->toArray(); |
128 |
| - |
129 |
| - $teamplayerSeeder = DB::table('history') |
130 |
| - ->select(DB::raw('count(*) as value'), 'history.user_id') |
131 |
| - ->where('history.active', 1) |
132 |
| - ->where('history.seedtime', '>=', 2_592_000 * 2) |
133 |
| - ->where('history.seedtime', '<', 2_592_000 * 3) |
134 |
| - ->groupBy('history.user_id') |
135 |
| - ->get() |
136 |
| - ->toArray(); |
137 |
| - |
138 |
| - $commitedSeeder = DB::table('history') |
139 |
| - ->select(DB::raw('count(*) as value'), 'history.user_id') |
140 |
| - ->where('history.active', 1) |
141 |
| - ->where('history.seedtime', '>=', 2_592_000 * 3) |
142 |
| - ->where('history.seedtime', '<', 2_592_000 * 6) |
143 |
| - ->groupBy('history.user_id') |
144 |
| - ->get() |
145 |
| - ->toArray(); |
146 |
| - |
147 |
| - $mvpSeeder = DB::table('history') |
148 |
| - ->select(DB::raw('count(*) as value'), 'history.user_id') |
149 |
| - ->where('history.active', 1) |
150 |
| - ->where('history.seedtime', '>=', 2_592_000 * 6) |
151 |
| - ->where('history.seedtime', '<', 2_592_000 * 12) |
152 |
| - ->groupBy('history.user_id') |
153 |
| - ->get() |
154 |
| - ->toArray(); |
155 |
| - |
156 |
| - $legendarySeeder = DB::table('history') |
157 |
| - ->select(DB::raw('count(*) as value'), 'history.user_id') |
158 |
| - ->where('history.active', 1) |
159 |
| - ->where('history.seedtime', '>=', 2_592_000 * 12) |
160 |
| - ->groupBy('history.user_id') |
161 |
| - ->get() |
162 |
| - ->toArray(); |
163 |
| - |
164 |
| - //Move data from SQL to array |
165 |
| - |
166 |
| - $array = []; |
167 |
| - |
168 |
| - foreach ($dyingTorrent as $value) { |
169 |
| - if (\array_key_exists($value->user_id, $array)) { |
170 |
| - $array[$value->user_id] += $value->value * 2; |
171 |
| - } else { |
172 |
| - $array[$value->user_id] = $value->value * 2; |
173 |
| - } |
174 |
| - } |
175 |
| - |
176 |
| - foreach ($legendaryTorrent as $value) { |
177 |
| - if (\array_key_exists($value->user_id, $array)) { |
178 |
| - $array[$value->user_id] += $value->value * 1.5; |
179 |
| - } else { |
180 |
| - $array[$value->user_id] = $value->value * 1.5; |
| 51 | + $earningsQuery = '0'; |
| 52 | + |
| 53 | + foreach (BonEarning::with('conditions')->orderBy('position')->get() as $bonEarning) { |
| 54 | + // Raw bindings are fine since all database values are either enums or numeric |
| 55 | + $conditionQuery = '1=1'; |
| 56 | + |
| 57 | + foreach ($bonEarning->conditions as $condition) { |
| 58 | + $conditionQuery .= ' AND '.match ($condition->operand1) { |
| 59 | + '1' => '1', |
| 60 | + 'age' => 'TIMESTAMPDIFF(SECOND, torrents.created_at, NOW())', |
| 61 | + 'size' => 'torrents.size', |
| 62 | + 'seeders' => 'torrents.seeders', |
| 63 | + 'leechers' => 'torrents.leechers', |
| 64 | + 'times_completed' => 'torrents.times_completed', |
| 65 | + 'internal' => 'torrents.internal', |
| 66 | + 'personal_release' => 'torrents.personal_release', |
| 67 | + 'type_id' => 'torrents.type_id', |
| 68 | + 'seedtime' => 'history.seedtime', |
| 69 | + 'connectable' => 'MAX(peers.connectable)', |
| 70 | + }.' '.$condition->operator.' '.$condition->operand2; |
181 | 71 | }
|
182 |
| - } |
183 |
| - |
184 |
| - foreach ($oldTorrent as $value) { |
185 |
| - if (\array_key_exists($value->user_id, $array)) { |
186 |
| - $array[$value->user_id] += $value->value * 1; |
187 |
| - } else { |
188 |
| - $array[$value->user_id] = $value->value * 1; |
189 |
| - } |
190 |
| - } |
191 |
| - |
192 |
| - foreach ($hugeTorrent as $value) { |
193 |
| - if (\array_key_exists($value->user_id, $array)) { |
194 |
| - $array[$value->user_id] += $value->value * 0.75; |
195 |
| - } else { |
196 |
| - $array[$value->user_id] = $value->value * 0.75; |
197 |
| - } |
198 |
| - } |
199 |
| - |
200 |
| - foreach ($largeTorrent as $value) { |
201 |
| - if (\array_key_exists($value->user_id, $array)) { |
202 |
| - $array[$value->user_id] += $value->value * 0.50; |
203 |
| - } else { |
204 |
| - $array[$value->user_id] = $value->value * 0.50; |
205 |
| - } |
206 |
| - } |
207 |
| - |
208 |
| - foreach ($regularTorrent as $value) { |
209 |
| - if (\array_key_exists($value->user_id, $array)) { |
210 |
| - $array[$value->user_id] += $value->value * 0.25; |
211 |
| - } else { |
212 |
| - $array[$value->user_id] = $value->value * 0.25; |
213 |
| - } |
214 |
| - } |
215 |
| - |
216 |
| - foreach ($participaintSeeder as $value) { |
217 |
| - if (\array_key_exists($value->user_id, $array)) { |
218 |
| - $array[$value->user_id] += $value->value * 0.25; |
219 |
| - } else { |
220 |
| - $array[$value->user_id] = $value->value * 0.25; |
221 |
| - } |
222 |
| - } |
223 |
| - |
224 |
| - foreach ($teamplayerSeeder as $value) { |
225 |
| - if (\array_key_exists($value->user_id, $array)) { |
226 |
| - $array[$value->user_id] += $value->value * 0.50; |
227 |
| - } else { |
228 |
| - $array[$value->user_id] = $value->value * 0.50; |
229 |
| - } |
230 |
| - } |
231 |
| - |
232 |
| - foreach ($commitedSeeder as $value) { |
233 |
| - if (\array_key_exists($value->user_id, $array)) { |
234 |
| - $array[$value->user_id] += $value->value * 0.75; |
235 |
| - } else { |
236 |
| - $array[$value->user_id] = $value->value * 0.75; |
237 |
| - } |
238 |
| - } |
239 |
| - |
240 |
| - foreach ($mvpSeeder as $value) { |
241 |
| - if (\array_key_exists($value->user_id, $array)) { |
242 |
| - $array[$value->user_id] += $value->value * 1; |
243 |
| - } else { |
244 |
| - $array[$value->user_id] = $value->value * 1; |
245 |
| - } |
246 |
| - } |
247 |
| - |
248 |
| - foreach ($legendarySeeder as $value) { |
249 |
| - if (\array_key_exists($value->user_id, $array)) { |
250 |
| - $array[$value->user_id] += $value->value * 2; |
251 |
| - } else { |
252 |
| - $array[$value->user_id] = $value->value * 2; |
253 |
| - } |
254 |
| - } |
255 |
| - |
256 |
| - //Move data from array to BonTransactions table |
257 |
| - /*foreach ($array as $key => $value) { |
258 |
| - $log = new BonTransactions(); |
259 |
| - $log->bon_exchange_id = 0; |
260 |
| - $log->name = "Seeding Award"; |
261 |
| - $log->cost = $value; |
262 |
| - $log->receiver_id = $key; |
263 |
| - $log->comment = "Seeding Award"; |
264 |
| - $log->save(); |
265 |
| - }*/ |
266 | 72 |
|
267 |
| - //Move data from array to Users table |
268 |
| - foreach ($array as $key => $value) { |
269 |
| - User::whereKey($key)->update([ |
270 |
| - 'seedbonus' => DB::raw('seedbonus + '.$value), |
271 |
| - ]); |
| 73 | + $variable = match ($bonEarning->variable) { |
| 74 | + '1' => '1', |
| 75 | + 'age' => 'TIMESTAMPDIFF(SECOND, torrents.created_at, NOW())', |
| 76 | + 'size' => 'torrents.size', |
| 77 | + 'seeders' => 'torrents.seeders', |
| 78 | + 'leechers' => 'torrents.leechers', |
| 79 | + 'times_completed' => 'torrents.times_completed', |
| 80 | + 'internal' => 'torrents.internal', |
| 81 | + 'personal_release' => 'torrents.personal_release', |
| 82 | + 'seedtime' => 'history.seedtime', |
| 83 | + 'connectable' => 'MAX(peers.connectable)', |
| 84 | + }; |
| 85 | + |
| 86 | + $earningsQuery .= match ($bonEarning->operation) { |
| 87 | + 'append' => " + CASE WHEN ({$conditionQuery}) THEN {$variable} * {$bonEarning->multiplier} ELSE 0 END", |
| 88 | + 'multiply' => " * CASE WHEN ({$conditionQuery}) THEN {$variable} * {$bonEarning->multiplier} ELSE 1 END", |
| 89 | + }; |
272 | 90 | }
|
273 | 91 |
|
274 |
| - $this->comment('Automated BON Allocation Command Complete'); |
| 92 | + DB::transaction(function () use ($earningsQuery): void { |
| 93 | + User::withoutTimestamps(function () use ($earningsQuery): void { |
| 94 | + DB::table('users') |
| 95 | + ->joinSub( |
| 96 | + DB::query()->fromSub( |
| 97 | + DB::table('peers') |
| 98 | + ->select([ |
| 99 | + 'peers.user_id', |
| 100 | + 'peers.torrent_id', |
| 101 | + DB::raw("({$earningsQuery}) AS hourly_earnings"), |
| 102 | + ]) |
| 103 | + ->join('history', fn ($join) => $join->on('history.torrent_id', '=', 'peers.torrent_id')->on('history.user_id', '=', 'peers.user_id')) |
| 104 | + ->join('torrents', 'peers.torrent_id', '=', 'torrents.id') |
| 105 | + ->where('peers.seeder', '=', true) |
| 106 | + ->where('peers.active', '=', true) |
| 107 | + ->where('peers.created_at', '<', now()->subMinutes(30)) |
| 108 | + ->groupBy(['peers.user_id', 'peers.torrent_id']), |
| 109 | + 'earnings_per_user_per_torrent', |
| 110 | + ) |
| 111 | + ->select([ |
| 112 | + 'user_id', |
| 113 | + DB::raw('SUM(hourly_earnings) AS hourly_earnings_sum'), |
| 114 | + ]) |
| 115 | + ->groupBy('user_id'), |
| 116 | + 'earnings_per_user', |
| 117 | + fn ($join) => $join->on('users.id', '=', 'earnings_per_user.user_id'), |
| 118 | + ) |
| 119 | + ->update([ |
| 120 | + 'seedbonus' => DB::raw('seedbonus + hourly_earnings_sum'), |
| 121 | + ]); |
| 122 | + }); |
| 123 | + }, 25); |
| 124 | + |
| 125 | + $this->comment('Automated BON Allocation Command Complete in '.now()->diffInMilliseconds($now).' ms'); |
275 | 126 | }
|
276 | 127 | }
|
0 commit comments