|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * NOTICE OF LICENSE. |
| 7 | + * |
| 8 | + * UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0 |
| 9 | + * The details is bundled with this project in the file LICENSE.txt. |
| 10 | + * |
| 11 | + * @project UNIT3D Community Edition |
| 12 | + * |
| 13 | + * @author Obi-Wana |
| 14 | + * @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0 |
| 15 | + */ |
| 16 | + |
| 17 | +namespace App\Http\Controllers\Collectible; |
| 18 | + |
| 19 | +use App\Http\Controllers\Controller; |
| 20 | +use App\Http\Requests\StoreCollectibleOfferRequest; |
| 21 | +use App\Models\Collectible; |
| 22 | +use App\Models\CollectibleOffer; |
| 23 | +use App\Models\CollectibleTransaction; |
| 24 | +use App\Models\User; |
| 25 | +use Illuminate\Http\Request; |
| 26 | +use Illuminate\Support\Facades\DB; |
| 27 | + |
| 28 | +class CollectibleOfferController extends Controller |
| 29 | +{ |
| 30 | + /** |
| 31 | + * Create A Collectible Offer. |
| 32 | + * |
| 33 | + * User creates a new offer. |
| 34 | + */ |
| 35 | + public function create(Collectible $collectible): \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
| 36 | + { |
| 37 | + return view('collectible.offer.create', [ |
| 38 | + 'collectibleItem' => $collectible->items()->where('user_id', '=', auth()->user()->id)->first(), |
| 39 | + ]); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Store A New Collectible Offer. |
| 44 | + * |
| 45 | + * Store User creates a new offer. |
| 46 | + */ |
| 47 | + public function store(Collectible $collectible, StoreCollectibleOfferRequest $request): \Illuminate\Http\RedirectResponse |
| 48 | + { |
| 49 | + $request->validated('offer'); |
| 50 | + |
| 51 | + $user = auth()->user(); |
| 52 | + |
| 53 | + $userOwns = $collectible->items()->where('user_id', '=', $user->id)->exists(); |
| 54 | + $userIsSelling = $collectible->offers()->where('user_id', $user->id)->whereNull('filled_at')->exists(); |
| 55 | + |
| 56 | + if (! $collectible->resell) { |
| 57 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 58 | + ->withErrors("Reselling is disabled for this collectible."); |
| 59 | + } |
| 60 | + |
| 61 | + if (! $userOwns) { |
| 62 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 63 | + ->withErrors("You can not sell this item, you do not own it."); |
| 64 | + } |
| 65 | + |
| 66 | + if ($collectible->in_stock) { |
| 67 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 68 | + ->withErrors("You can not sell this item, its still in stock at the regular market."); |
| 69 | + } |
| 70 | + |
| 71 | + if ($userIsSelling) { |
| 72 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 73 | + ->withErrors("You can not sell this item, you are already selling it."); |
| 74 | + } |
| 75 | + |
| 76 | + if ($request->offer['price'] > $collectible->price * 1.4) { |
| 77 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 78 | + ->withErrors("The highest selling price for this item is 1.2 times its original price."); |
| 79 | + } |
| 80 | + |
| 81 | + $offer = CollectibleOffer::create([ |
| 82 | + 'collectible_id' => $collectible->id, |
| 83 | + 'user_id' => $user->id, |
| 84 | + 'price' => $request->offer['price'], |
| 85 | + ]); |
| 86 | + |
| 87 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 88 | + ->with('success', "Offer created."); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Store An Collectible Offer Accept. |
| 93 | + * |
| 94 | + * User accepts an existing offer. |
| 95 | + */ |
| 96 | + public function update(CollectibleOffer $collectibleOffer): \Illuminate\Http\RedirectResponse |
| 97 | + { |
| 98 | + $user = auth()->user(); |
| 99 | + $userAvgSeedtime = DB::table('history')->where('user_id', '=', $user->id)->avg('seedtime'); |
| 100 | + $userSeedSize = $user->seedingTorrents()->sum('size'); |
| 101 | + |
| 102 | + $collectible = $collectibleOffer->collectible; |
| 103 | + $userOwns = $collectible->items()->where('user_id', '=', $user->id)->exists(); |
| 104 | + |
| 105 | + if ($user->id === $collectibleOffer->user_id) { |
| 106 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 107 | + ->withErrors("You can not accept your own offer."); |
| 108 | + } |
| 109 | + |
| 110 | + if ($userOwns) { |
| 111 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 112 | + ->withErrors("You already own this item."); |
| 113 | + } |
| 114 | + |
| 115 | + if ($collectibleOffer->price > $user->seedbonus) { |
| 116 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 117 | + ->withErrors("Not enough BON."); |
| 118 | + } |
| 119 | + |
| 120 | + $requirements = $collectible->requirements; |
| 121 | + $userMeetsAllRequirements = ( |
| 122 | + ($requirements->min_uploaded === null || $user->uploaded >= $requirements->min_uploaded) |
| 123 | + && ($requirements->min_seedsize === null || $userSeedSize >= $requirements->min_seedsize) |
| 124 | + && ($requirements->min_avg_seedtime === null || $userAvgSeedtime >= $requirements->min_avg_seedtime) |
| 125 | + && ($requirements->min_ratio === null || $user->ratio >= $requirements->min_ratio) |
| 126 | + && ($requirements->min_age === null || $user->created_at->diffInSeconds(now()) >= $requirements->min_age) |
| 127 | + ); |
| 128 | + |
| 129 | + if (! $userMeetsAllRequirements) { |
| 130 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 131 | + ->withErrors("You do not meet all requirements to buy this item!"); |
| 132 | + } |
| 133 | + |
| 134 | + // Get the offered item |
| 135 | + $collectibleItem = $collectible->items()->where('user_id', '=', $collectibleOffer->seller->id)->first(); |
| 136 | + |
| 137 | + DB::transaction(static function () use ($collectible, $collectibleItem, $collectibleOffer, $user): void { |
| 138 | + $transaction = CollectibleTransaction::create([ |
| 139 | + 'collectible_id' => $collectible->id, |
| 140 | + 'seller_id' => $collectibleOffer->seller->id, |
| 141 | + 'buyer_id' => $user->id, |
| 142 | + 'price' => $collectibleOffer->price, |
| 143 | + ]); |
| 144 | + |
| 145 | + $collectibleItem->update([ |
| 146 | + 'user_id' => $user->id, |
| 147 | + ]); |
| 148 | + |
| 149 | + $collectibleOffer->update([ |
| 150 | + 'filled_at' => now(), |
| 151 | + ]); |
| 152 | + |
| 153 | + User::whereKey($transaction->seller_id)->increment('seedbonus', (float) $transaction->price); |
| 154 | + User::whereKey($transaction->buyer_id)->decrement('seedbonus', (float) $transaction->price); |
| 155 | + }); |
| 156 | + |
| 157 | + return to_route('collectibles.show', ['collectible' => $collectible]) |
| 158 | + ->with('success', "Offer accepted."); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Destroy An Collectible Offer. |
| 163 | + * |
| 164 | + * User deletes his existing offer. |
| 165 | + */ |
| 166 | + public function destroy(Request $request, CollectibleOffer $collectibleOffer): \Illuminate\Http\RedirectResponse |
| 167 | + { |
| 168 | + abort_unless($request->user()->is($collectibleOffer->seller) || $request->user()->group->is_modo, 403); |
| 169 | + |
| 170 | + $collectibleOffer->delete(); |
| 171 | + |
| 172 | + return to_route('collectibles.show', ['collectible' => $collectibleOffer->collectible]) |
| 173 | + ->with('success', 'Offer deleted.'); |
| 174 | + } |
| 175 | +} |
0 commit comments