Skip to content

Commit

Permalink
feat(auto unlocking): make auto unlocking optional
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Oct 6, 2024
1 parent f5cd0f0 commit 17b0f29
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 18 deletions.
24 changes: 23 additions & 1 deletion app/Http/Controllers/Admin/LimitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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();
}
}
2 changes: 1 addition & 1 deletion app/Models/Limit/Limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
];

/**
Expand Down
12 changes: 11 additions & 1 deletion app/Services/LimitManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
46 changes: 39 additions & 7 deletions app/Services/LimitService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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()) {
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('limits', function (Blueprint $table) {
//
$table->boolean('is_auto_unlocked')->default(true);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('limits', function (Blueprint $table) {
//
$table->dropColumn('is_auto_unlocked');
});
}
};
29 changes: 21 additions & 8 deletions resources/views/widgets/_add_limits.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,27 @@
@if ($limits)
<h5>Limits for {!! $limits->first()->object->displayName !!}</h5>
@endif
<div class="form-group">
{!! Form::label('is_unlocked', 'Is Unlocked?') !!}
<p>
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.
<br />
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.
</p>
{!! Form::select('is_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!}
<div class="row">
<div class="col-md form-group">
{!! Form::label('is_unlocked', 'Is Unlocked?', ['class' => 'form-label font-weight-bold']) !!}
<p>
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.
<br />
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.
</p>
{!! Form::select('is_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!}
</div>
<div class="col-md form-group border-left">
{!! 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'.") !!}
<p>
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.
<br />
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.
<br />
This setting is good to prevent users from being debitted before being certain they want to interact with the object.
</p>
{!! Form::select('is_auto_unlocked', ['yes' => 'Yes', 'no' => 'No'], $limits?->first()->is_auto_unlocked ? 'yes' : 'no', ['class' => 'form-control']) !!}
</div>
</div>
@if ($limits)
@foreach ($limits as $limit)
Expand Down
16 changes: 16 additions & 0 deletions resources/views/widgets/_limits.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@
@endforeach
</tbody>
</table>
@if (!$limits->first()->isUnlocked(Auth::user() ?? null) && !$limits->first()->is_auto_unlocked)
<div class="alert alert-secondary p-0 mt-2 mb-0">
{!! Form::open(['url' => 'limits/unlock/'.$limits->first()->id]) !!}
{!! Form::submit('Unlock', ['class' => 'btn btn-sm btn-secondary']) !!}
{!! Form::close() !!}
</div>
@endif
@else
<div class="alert alert-{{ $limits->first()->isUnlocked(Auth::user() ?? null) ? 'info' : 'danger' }} p-0 mt-2">
<small>
Expand All @@ -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)
<div class="alert alert-secondary p-0 mt-2 mb-0">
<small>
{!! Form::open(['url' => 'limits/unlock/'.$limits->first()->id]) !!}
{!! Form::submit('Unlock', ['class' => 'btn btn-sm btn-secondary']) !!}
{!! Form::close() !!}
</small>
</div>
@endif
</small>
</div>
@endif
Expand Down
7 changes: 7 additions & 0 deletions routes/lorekeeper/members.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit 17b0f29

Please sign in to comment.