Skip to content

Commit

Permalink
add item tags for elemental typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Nov 10, 2023
1 parent be5f733 commit 54ba07d
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 0 deletions.
131 changes: 131 additions & 0 deletions app/Services/Item/ElementalPotionService.php
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);
}
}
8 changes: 8 additions & 0 deletions app/Services/TypingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public function createTyping($typing_model, $typing_id, $element_ids = null) {
if (count($element_ids) > 2) {
throw new \Exception('Too many elements provided.');
}
// check that there is not duplicate element ids
if (count($element_ids) != count(array_unique($element_ids))) {
throw new \Exception('Duplicate elements provided.');
}
// check that a typing with this model and id doesn't already exist
if (Typing::where('typing_model', $typing_model)->where('typing_id', $typing_id)->exists()) {
throw new \Exception('A typing with this model and id already exists.');
Expand Down Expand Up @@ -82,6 +86,10 @@ public function editTyping($typing, $element_ids = null) {
if (count($element_ids) > 2) {
throw new \Exception('Too many elements provided.');
}
// check that there is not duplicate element ids
if (count($element_ids) != count(array_unique($element_ids))) {
throw new \Exception('Duplicate elements provided.');
}
// check that a typing with this model and id doesn't already exist
if (Typing::where('typing_model', $typing->typing_model)->where('typing_id', $typing->typing_id)->where('id', '!=', $typing->id)->exists()) {
throw new \Exception('A typing with this model and id already exists.');
Expand Down
6 changes: 6 additions & 0 deletions config/lorekeeper/item_tags.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@
'text_color' => '#ffffff',
'background_color' => '#1fd1a7',
],

'elemental_potion' => [
'name' => 'Elemental Potion',
'text_color' => '#f5f3d3',
'background_color' => '#468a82',
],
];
12 changes: 12 additions & 0 deletions resources/views/admin/items/tags/elemental_potion.blade.php
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>

20 changes: 20 additions & 0 deletions resources/views/inventory/_elemental_potion.blade.php
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

0 comments on commit 54ba07d

Please sign in to comment.