forked from corowne/lorekeeper
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
be5f733
commit 54ba07d
Showing
5 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
<?php | ||
|
||
namespace App\Services\Item; | ||
|
||
use App\Models\Character\Character; | ||
use App\Models\Element\Element; | ||
use App\Models\Element\Typing; | ||
use App\Services\Service; | ||
use App\Services\InventoryManager; | ||
use App\Services\TypingManager; | ||
use DB; | ||
|
||
class ElementalPotionService extends Service { | ||
/* | ||
|-------------------------------------------------------------------------- | ||
| Elemental Potion Service | ||
|-------------------------------------------------------------------------- | ||
| | ||
| Handles the application of elements to characters / other things. | ||
| | ||
*/ | ||
|
||
/** | ||
* Retrieves any data that should be used in the item tag editing form. | ||
* | ||
* @return array | ||
*/ | ||
public function getEditData() { | ||
return [ | ||
'elements' => Element::orderBy('name')->pluck('name', 'id'), | ||
]; | ||
} | ||
|
||
/** | ||
* Processes the data attribute of the tag and returns it in the preferred format. | ||
* | ||
* @param mixed $tag | ||
* @return mixed | ||
*/ | ||
public function getTagData($tag) { | ||
$data['element_id'] = $tag->data['element_id'] ?? null; | ||
|
||
return $data; | ||
} | ||
|
||
/** | ||
* Processes the data attribute of the tag and returns it in the preferred format. | ||
* | ||
* @param mixed $tag | ||
* @param array $data | ||
* | ||
* @return bool | ||
*/ | ||
public function updateData($tag, $data) { | ||
|
||
$potionData['element_id'] = $data['element_id']; | ||
|
||
DB::beginTransaction(); | ||
|
||
try { | ||
|
||
$tag->update(['data' => json_encode($potionData)]); | ||
|
||
return $this->commitReturn(true); | ||
} catch (\Exception $e) { | ||
$this->setError('error', $e->getMessage()); | ||
} | ||
|
||
return $this->rollbackReturn(false); | ||
} | ||
|
||
/** | ||
* Acts upon the item when used from the inventory. | ||
* | ||
* @param \App\Models\User\UserItem $stacks | ||
* @param \App\Models\User\User $user | ||
* @param array $data | ||
* | ||
* @return bool | ||
*/ | ||
public function act($stacks, $user, $data) { | ||
DB::beginTransaction(); | ||
|
||
try { | ||
$character = Character::find($data['character_id']); | ||
|
||
foreach ($stacks as $key=> $stack) { | ||
if ($stack->user_id != $user->id) { | ||
throw new \Exception('This item does not belong to you.'); | ||
} | ||
if ($data['quantities'][$key] > 1) { | ||
throw new \Exception('You can only apply one element at a time.'); | ||
} | ||
|
||
// Next, try to delete the box item. If successful, we can start distributing rewards. | ||
if ((new InventoryManager)->debitStack($stack->user, 'Potion Consumed', ['data' => 'Potion used on ' . $character->displayName], $stack, $data['quantities'][$key])) { | ||
for ($q = 0; $q < $data['quantities'][$key]; $q++) { | ||
$service = new TypingManager; | ||
// check if typing exists on character | ||
$typing = Typing::where('typing_model', get_class($character->image))->where('typing_id', $character->image->id)->first(); | ||
if (!$typing) { | ||
if(!$service->createTyping(get_class($character->image), $character->image->id, [$stack->item->tag($data['tag'])->data['element_id']])) { | ||
foreach ($service->errors()->getMessages()['error'] as $error) { | ||
flash($error)->error(); | ||
} | ||
|
||
throw new \Exception('Failed to create typing.'); | ||
} | ||
} | ||
else { | ||
if(!$service->editTyping($typing, array_merge(json_decode($typing->element_ids), [$stack->item->tag($data['tag'])->data['element_id']]))) { | ||
foreach ($service->errors()->getMessages()['error'] as $error) { | ||
flash($error)->error(); | ||
} | ||
|
||
throw new \Exception('Failed to edit typing.'); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
flash('You have successfully applied the element to your character, ' . $character->displayName . '.')->success(); | ||
|
||
return $this->commitReturn(true); | ||
} catch (\Exception $e) { | ||
$this->setError('error', $e->getMessage()); | ||
} | ||
|
||
return $this->rollbackReturn(false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
resources/views/admin/items/tags/elemental_potion.blade.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
|
||
<p> | ||
Potions by default can only apply one element a specific object, and by default can only apply to characters. | ||
<br /> | ||
Potions can only apply elements to characters that have less than 2 elements. | ||
</p> | ||
|
||
<div class="form-group"> | ||
{!! Form::label('Element') !!} | ||
{!! Form::select('element_id', $elements, $tag->getData()['element_id'], ['class' => 'form-control']) !!} | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
@if($stack->first()->user_id == Auth::user()->id) | ||
<li class="list-group-item"> | ||
<a class="card-title h5 collapse-title" data-toggle="collapse" href="#openBoxForm"> Consume Potion</a> | ||
<div id="openBoxForm" class="collapse"> | ||
{!! Form::hidden('tag', $tag->tag) !!} | ||
<p><b>Select a Character to apply this potion to:</b></p> | ||
<div class="form-group"> | ||
{!! Form::select('character_id', Auth::user()->characters()->get()->pluck('fullName', 'id'), null, ['class' => 'form-control']) !!} | ||
</div> | ||
<p>This action is not reversible. Are you sure you want to use this potion?</p> | ||
<div class="text-right"> | ||
{!! Form::button('Open', ['class' => 'btn btn-primary', 'name' => 'action', 'value' => 'act', 'type' => 'submit']) !!} | ||
</div> | ||
</div> | ||
</li> | ||
@else | ||
<div class="alert alert-info"> | ||
Elemental Potions can only be applied by the owner of the item. | ||
</div> | ||
@endif |