From 17b0f295c79ab791c65acf6c4b1d6bbba6b3a6c7 Mon Sep 17 00:00:00 2001 From: ScuffedNewt Date: Sun, 6 Oct 2024 17:32:37 +0100 Subject: [PATCH] feat(auto unlocking): make auto unlocking optional --- .../Controllers/Admin/LimitController.php | 24 +++++++++- app/Models/Limit/Limit.php | 2 +- app/Services/LimitManager.php | 12 ++++- app/Services/LimitService.php | 46 ++++++++++++++++--- ...50833_add_auto_unlock_option_to_limits.php | 30 ++++++++++++ resources/views/widgets/_add_limits.blade.php | 29 ++++++++---- resources/views/widgets/_limits.blade.php | 16 +++++++ routes/lorekeeper/members.php | 7 +++ 8 files changed, 148 insertions(+), 18 deletions(-) create mode 100644 database/migrations/2024_10_06_150833_add_auto_unlock_option_to_limits.php diff --git a/app/Http/Controllers/Admin/LimitController.php b/app/Http/Controllers/Admin/LimitController.php index 955670a20..0c4efebec 100644 --- a/app/Http/Controllers/Admin/LimitController.php +++ b/app/Http/Controllers/Admin/LimitController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; +use App\Models\Limit\Limit; use App\Services\LimitService; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; @@ -17,7 +18,7 @@ class LimitController extends Controller { */ public function postCreateEditLimits(Request $request, LimitService $service) { $data = $request->only([ - 'object_model', 'object_id', 'limit_type', 'limit_id', 'quantity', 'debit', 'is_unlocked', + 'object_model', 'object_id', 'limit_type', 'limit_id', 'quantity', 'debit', 'is_unlocked', 'is_auto_unlocked', ]); if ($service->editLimits($data['object_model'], $data['object_id'], $data, Auth::user())) { flash('Limits updated successfully.')->success(); @@ -29,4 +30,25 @@ public function postCreateEditLimits(Request $request, LimitService $service) { return redirect()->back(); } + + /** + * Unlocks limits for an object for a user. + * + * @param App\Services\LimitService $service + * @param int $id + * + * @return \Illuminate\Http\RedirectResponse + */ + public function postUnlockLimits(LimitService $service, $id) { + $limit = Limit::find($id); + if ($service->unlockLimits($limit->object, Auth::user())) { + flash(($limit->object->displayName ?? $limit->object->name) . ' unlocked successfully.')->success(); + } else { + foreach ($service->errors()->getMessages()['error'] as $error) { + flash($error)->error(); + } + } + + return redirect()->back(); + } } diff --git a/app/Models/Limit/Limit.php b/app/Models/Limit/Limit.php index f75cb6e18..422fb243e 100644 --- a/app/Models/Limit/Limit.php +++ b/app/Models/Limit/Limit.php @@ -14,7 +14,7 @@ class Limit extends Model { * @var array */ protected $fillable = [ - 'object_model', 'object_id', 'limit_type', 'limit_id', 'quantity', 'debit', 'is_unlocked', + 'object_model', 'object_id', 'limit_type', 'limit_id', 'quantity', 'debit', 'is_unlocked', 'is_auto_unlocked', ]; /** diff --git a/app/Services/LimitManager.php b/app/Services/LimitManager.php index e4f97df1c..194956071 100644 --- a/app/Services/LimitManager.php +++ b/app/Services/LimitManager.php @@ -29,7 +29,7 @@ class LimitManager extends Service { * * @param mixed $object */ - public function checkLimits($object) { + public function checkLimits($object, $is_unlock = false) { try { $user = Auth::user(); @@ -59,6 +59,11 @@ public function checkLimits($object) { } if ($limit->debit) { + // if the limit is not unlocked, check if it is auto unlocked + if (!$is_unlock && $limits->first()->is_unlocked && !$limits->first()->is_auto_unlocked) { + throw new \Exception(($limits->first()->object->displayName ?? $limits->first()->object->name).' requires manual unlocking!'); + } + $stacks = UserItem::where('user_id', $user->id)->where('item_id', $limit->limit_id)->orderBy('count', 'asc')->get(); // asc because pop() removes from the end $count = $limit->quantity; @@ -76,6 +81,11 @@ public function checkLimits($object) { } if ($limit->debit) { + // if the limit is not unlocked, check if it is auto unlocked + if (!$is_unlock && $limits->first()->is_unlocked && !$limits->first()->is_auto_unlocked) { + throw new \Exception(($limits->first()->object->displayName ?? $limits->first()->object->name).' requires manual unlocking!'); + } + $service = new CurrencyManager; if (!$service->debitCurrency($user, null, 'Limit Requirements', 'Used in '.$limit->object->displayName.' limit requirements.', $limit->limit, $limit->quantity)) { foreach ($service->errors()->getMessages()['error'] as $error) { diff --git a/app/Services/LimitService.php b/app/Services/LimitService.php index 3912731a0..5a9b6482e 100644 --- a/app/Services/LimitService.php +++ b/app/Services/LimitService.php @@ -4,6 +4,7 @@ use App\Models\Limit\DynamicLimit; use App\Models\Limit\Limit; +use App\Services\LimitManager; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; @@ -54,13 +55,14 @@ public function editLimits($object_model, $object_id, $data, $log = true) { if (isset($data['limit_type'])) { foreach ($data['limit_type'] as $key => $type) { $limit = new Limit([ - 'object_model' => $object_model, - 'object_id' => $object_id, - 'limit_type' => $data['limit_type'][$key], - 'limit_id' => $data['limit_id'][$key], - 'quantity' => $data['quantity'][$key], - 'debit' => $data['debit'][$key] == 'no' ? 0 : 1, - 'is_unlocked' => $data['is_unlocked'] == 'no' ? 0 : 1, + 'object_model' => $object_model, + 'object_id' => $object_id, + 'limit_type' => $data['limit_type'][$key], + 'limit_id' => $data['limit_id'][$key], + 'quantity' => $data['quantity'][$key], + 'debit' => $data['debit'][$key] == 'no' ? 0 : 1, + 'is_unlocked' => $data['is_unlocked'] == 'no' ? 0 : 1, + 'is_auto_unlocked' => $data['is_auto_unlocked'] == 'no' ? 0 : 1, ]); if (!$limit->save()) { @@ -82,6 +84,36 @@ public function editLimits($object_model, $object_id, $data, $log = true) { return $this->rollbackReturn(false); } + /** + * Unlocks the limits for an object. + * + * @param mixed $object_model + * @param mixed $object_id + * + * @return bool + */ + public function unlockLimits($object) { + DB::beginTransaction(); + + try { + + $service = new LimitManager; + if (!$service->checkLimits($object, true)) { + foreach ($service->errors()->getMessages()['error'] as $error) { + flash($error)->error(); + } + throw new \Exception('Failed to unlock limits.'); + } + + return $this->commitReturn(true); + } catch (\Exception $e) { + $this->setError('error', $e->getMessage()); + } + + return $this->rollbackReturn(false); + } + + /********************************************************************************************** DYNAMIC LIMITS diff --git a/database/migrations/2024_10_06_150833_add_auto_unlock_option_to_limits.php b/database/migrations/2024_10_06_150833_add_auto_unlock_option_to_limits.php new file mode 100644 index 000000000..297e7f8f1 --- /dev/null +++ b/database/migrations/2024_10_06_150833_add_auto_unlock_option_to_limits.php @@ -0,0 +1,30 @@ +boolean('is_auto_unlocked')->default(true); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('limits', function (Blueprint $table) { + // + $table->dropColumn('is_auto_unlocked'); + }); + } +}; diff --git a/resources/views/widgets/_add_limits.blade.php b/resources/views/widgets/_add_limits.blade.php index b92a6e465..dce918b28 100644 --- a/resources/views/widgets/_add_limits.blade.php +++ b/resources/views/widgets/_add_limits.blade.php @@ -33,14 +33,27 @@ @if ($limits)
Limits for {!! $limits->first()->object->displayName !!}
@endif -
- {!! Form::label('is_unlocked', 'Is Unlocked?') !!} -

- If this is set to "No", the object will continue to be locked until all requirements are met, every time the user attempts to interact with it. -
- If this is set to "Yes", the object will be unlocked for the user to interact with after the requirements are met once. This option is good for one-time unlocks such as shops, locations, certain prompts, etc. -

- {!! Form::select('is_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!} +
+
+ {!! Form::label('is_unlocked', 'Is Unlocked?', ['class' => 'form-label font-weight-bold']) !!} +

+ If this is set to "No", the object will continue to be locked until all requirements are met, every time the user attempts to interact with it. +
+ If this is set to "Yes", the object will be unlocked for the user to interact with indefinitely after the requirements are met once. This option is good for one-time unlocks such as shops, locations, certain prompts, etc. +

+ {!! Form::select('is_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!} +
+
+ {!! Form::label('is_auto_unlocked', 'Automatically Unlock?', ['class' => 'form-label font-weight-bold']) !!} {!! add_help("This only affects objects that have 'Is Unlocked?' set to 'Yes'.") !!} +

+ If this is set to "No", the user will have to manually unlock the object by interacting with it - ex. clicking on the "Unlock" button. +
+ If this is set to "Yes", the object will be automatically unlocked when the user attempts to access them - ex. when a user enters a shop. +
+ This setting is good to prevent users from being debitted before being certain they want to interact with the object. +

+ {!! Form::select('is_auto_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_auto_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!} +
@if ($limits) @foreach ($limits as $limit) diff --git a/resources/views/widgets/_limits.blade.php b/resources/views/widgets/_limits.blade.php index 256a545bf..e2dcae4ec 100644 --- a/resources/views/widgets/_limits.blade.php +++ b/resources/views/widgets/_limits.blade.php @@ -37,6 +37,13 @@ @endforeach + @if (!$limits->first()->isUnlocked(Auth::user() ?? null) && !$limits->first()->is_auto_unlocked) +
+ {!! Form::open(['url' => 'limits/unlock/'.$limits->first()->id]) !!} + {!! Form::submit('Unlock', ['class' => 'btn btn-sm btn-secondary']) !!} + {!! Form::close() !!} +
+ @endif @else
@@ -46,6 +53,15 @@ return ($limit->quantity ? $limit->quantity . ' ' : '') . $limit->limit->displayName; })->toArray(), ) !!}) + @if (!$limits->first()->isUnlocked(Auth::user() ?? null) && !$limits->first()->is_auto_unlocked) +
+ + {!! Form::open(['url' => 'limits/unlock/'.$limits->first()->id]) !!} + {!! Form::submit('Unlock', ['class' => 'btn btn-sm btn-secondary']) !!} + {!! Form::close() !!} + +
+ @endif
@endif diff --git a/routes/lorekeeper/members.php b/routes/lorekeeper/members.php index 2817418f6..a4cde27d4 100644 --- a/routes/lorekeeper/members.php +++ b/routes/lorekeeper/members.php @@ -232,3 +232,10 @@ Route::post('/{id}/like/{action}', 'CommentController@like')->name('comments.like'); Route::get('/liked', 'CommentController@getLikedComments'); }); + +/************************************************************************************************** + Comments +**************************************************************************************************/ +Route::group(['prefix' => 'limits'], function () { + Route::post('unlock/{id}', 'Admin\LimitController@postUnlockLimits'); +});