Skip to content

Commit

Permalink
add element rewards to prompts
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Nov 17, 2023
1 parent ecbbc12 commit 8c33cc6
Show file tree
Hide file tree
Showing 14 changed files with 138 additions and 126 deletions.
19 changes: 17 additions & 2 deletions app/Helpers/AssetHelpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function getAssetKeys($isCharacter = false) {
if (!$isCharacter) {
return ['items', 'currencies', 'raffle_tickets', 'loot_tables', 'user_items', 'characters'];
} else {
return ['currencies', 'items', 'character_items', 'loot_tables'];
return ['currencies', 'items', 'character_items', 'loot_tables', 'elements'];
}
}

Expand Down Expand Up @@ -144,6 +144,14 @@ function getAssetModelString($type, $namespaced = true) {
return 'CharacterItem';
}
break;

case 'elements':
if ($namespaced) {
return '\App\Models\Element\Element';
} else {
return 'Element';
}
break;
}

return null;
Expand Down Expand Up @@ -300,7 +308,7 @@ function fillUserAssets($assets, $sender, $recipient, $logType, $data) {
} elseif ($key == 'user_items' && count($contents)) {
$service = new \App\Services\InventoryManager;
foreach ($contents as $asset) {
if (!$service->moveStack($sender, $recipient, $logType, $data, $asset['asset'])) {
if (!$service->moveStack($sender, $recipient, $logType, $data, $asset['asset'], $asset['quantity'])) {
return false;
}
}
Expand Down Expand Up @@ -360,6 +368,13 @@ function fillCharacterAssets($assets, $sender, $recipient, $logType, $data, $sub
return false;
}
}
} elseif ($key == 'elements' && count($contents)) {
$service = new \App\Services\TypingManager;
foreach ($contents as $asset) {
if (!$service->creditTyping($recipient, $asset['asset'], $sender, $logType)) {
return false;
}
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/Admin/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Currency\Currency;
use App\Models\Item\Item;
use App\Models\Loot\LootTable;
use App\Models\Raffle\Raffle;
use App\Models\Element\Element;
use App\Models\Prompt\PromptCategory;
use App\Models\Submission\Submission;
use App\Services\SubmissionManager;
Expand Down Expand Up @@ -75,6 +79,7 @@ public function getSubmission($id) {
'tables' => LootTable::orderBy('name')->pluck('name', 'id'),
'raffles' => Raffle::where('rolled_at', null)->where('is_active', 1)->orderBy('name')->pluck('name', 'id'),
'count' => Submission::where('prompt_id', $submission->prompt_id)->where('status', 'Approved')->where('user_id', $submission->user_id)->count(),
'elements' => Element::orderBy('name')->pluck('name', 'id'),
] : []));
}

Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/Users/SubmissionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\Controller;
use App\Models\Character\Character;
use App\Models\Currency\Currency;
use App\Models\Element\Element;
use App\Models\Item\Item;
use App\Models\Item\ItemCategory;
use App\Models\Prompt\Prompt;
Expand Down Expand Up @@ -101,6 +102,7 @@ public function getNewSubmission(Request $request) {
'inventory' => $inventory,
'page' => 'submission',
'expanded_rewards' => Config::get('lorekeeper.extensions.character_reward_expansion.expanded'),
'elements' => Element::orderBy('name')->pluck('name', 'id')
]));
}

Expand Down
9 changes: 9 additions & 0 deletions app/Models/Character/CharacterImage.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,15 @@ public function scopeImages($query, $user = null) {
**********************************************************************************************/

/**
* Displays the character's name, linked to their character page.
*
* @return string
*/
public function getDisplayNameAttribute() {
return '<a href="'.$this->character->url.'" class="display-character">'.$this->character->fullName.'</a>';
}

/**
* Gets the file directory containing the model's image.
*
Expand Down
9 changes: 9 additions & 0 deletions app/Models/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ public function getImageUrlAttribute() {
return asset($this->imageDirectory.'/'.$this->imageFileName);
}

/**
* Gets the currency's asset type for asset management.
*
* @return string
*/
public function getAssetTypeAttribute() {
return 'elements';
}

/**
* Gets the URL of the model's encyclopedia page.
*
Expand Down
17 changes: 16 additions & 1 deletion app/Services/SubmissionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Character\Character;
use App\Models\Currency\Currency;
use App\Models\Element\Element;
use App\Models\Item\Item;
use App\Models\Loot\LootTable;
use App\Models\Prompt\Prompt;
Expand Down Expand Up @@ -380,6 +381,7 @@ public function approveSubmission($data, $user) {
$currencyIds = [];
$itemIds = [];
$tableIds = [];
$elementIds = [];
if (isset($data['character_currency_id'])) {
foreach ($data['character_currency_id'] as $c) {
foreach ($c as $currencyId) {
Expand All @@ -397,24 +399,34 @@ public function approveSubmission($data, $user) {
break;
case 'LootTable': $tableIds[] = $id;
break;
case 'Element': $elementIds[] = $id;
break;
}
}
} // Expanded character rewards
}
array_unique($currencyIds);
array_unique($itemIds);
array_unique($tableIds);
array_unique($elementIds);
$currencies = Currency::whereIn('id', $currencyIds)->where('is_character_owned', 1)->get()->keyBy('id');
$items = Item::whereIn('id', $itemIds)->get()->keyBy('id');
$tables = LootTable::whereIn('id', $tableIds)->get()->keyBy('id');
$elements = Element::whereIn('id', $elementIds)->get()->keyBy('id');

// We're going to remove all characters from the submission and reattach them with the updated data
$submission->characters()->delete();

// Distribute character rewards
foreach ($characters as $c) {
// Users might not pass in clean arrays (may contain redundant data) so we need to clean that up
$assets = $this->processRewards($data + ['character_id' => $c->id, 'currencies' => $currencies, 'items' => $items, 'tables' => $tables], true);
$assets = $this->processRewards($data + [
'character_id' => $c->id,
'currencies' => $currencies,
'items' => $items,
'tables' => $tables,
'elements' => $elements,
], true);

if (!$assets = fillCharacterAssets($assets, $user, $c, $promptLogType, $promptData, $submission->user)) {
throw new \Exception('Failed to distribute rewards to character.');
Expand Down Expand Up @@ -526,6 +538,9 @@ private function processRewards($data, $isCharacter, $isStaff = false, $isClaim
case 'LootTable': if ($data['character_rewardable_quantity'][$data['character_id']][$key]) {
addAsset($assets, $data['tables'][$reward], $data['character_rewardable_quantity'][$data['character_id']][$key]);
} break;
case 'Element': // we don't check for quanity here
addAsset($assets, $data['elements'][$reward], 1);
break;
}
}
}
Expand Down
52 changes: 47 additions & 5 deletions app/Services/TypingManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Models\Element\Typing;
use Auth;
use DB;

use Log;
class TypingManager extends Service {
/*
|--------------------------------------------------------------------------
Expand All @@ -29,7 +29,7 @@ class TypingManager extends Service {
* @param mixed $typing_id
* @param mixed|null $element_ids
*/
public function createTyping($typing_model, $typing_id, $element_ids = null) {
public function createTyping($typing_model, $typing_id, $element_ids = null, $log = true) {
DB::beginTransaction();

try {
Expand Down Expand Up @@ -57,7 +57,7 @@ public function createTyping($typing_model, $typing_id, $element_ids = null) {
]);

// log the action
if (!$this->logAdminAction(Auth::user(), 'Created Typing', 'Created '.$typing->object->displayName.' typing')) {
if ($log && !$this->logAdminAction(Auth::user(), 'Created Typing', 'Created '.$typing->object->displayName.' typing')) {
throw new \Exception('Failed to log admin action.');
}

Expand All @@ -75,7 +75,7 @@ public function createTyping($typing_model, $typing_id, $element_ids = null) {
* @param mixed $typing
* @param mixed|null $element_ids
*/
public function editTyping($typing, $element_ids = null) {
public function editTyping($typing, $element_ids = null, $log = true) {
DB::beginTransaction();

try {
Expand All @@ -101,7 +101,7 @@ public function editTyping($typing, $element_ids = null) {
]);

// log the action
if (!$this->logAdminAction(Auth::user(), 'Edited Typing', 'Edited '.$typing->object->displayName.' typing')) {
if ($log && !$this->logAdminAction(Auth::user(), 'Edited Typing', 'Edited '.$typing->object->displayName.' typing')) {
throw new \Exception('Failed to log admin action.');
}

Expand Down Expand Up @@ -137,4 +137,46 @@ public function deleteTyping($typing) {

return $this->rollbackReturn(false);
}

/**
* Credits an element, for usage with the asset helper vs widget.
*
* @param mixed $element
*/
public function creditTyping($recipient, $element, $sender = null, $origin = null)
{
DB::beginTransaction();

try {
$model = get_class($recipient->image);
$id = $recipient->image->id;

// log the action
if (!$this->logAdminAction(Auth::user(), 'Credited Typing', 'Credited '.$element->displayName.' to '.$recipient->displayName.''.
($sender ? ' from '.$sender->displayName : '') .
($origin ? ' ('.$origin.')' : '')
)) {
throw new \Exception('Failed to log admin action.');
}

$typing = Typing::where('typing_model', $model)->where('typing_id', $id)->first();

if ($typing) {
if (!$this->editTyping($typing, [$element->id], false)) {
throw new \Exception('Failed to edit typing.');
}
}
else {
if (!$this->createTyping($model, $id, [$element->id], false)) {
throw new \Exception('Failed to create typing.');
}
}

return $this->commitReturn(true);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
}

return $this->rollbackReturn(false);
}
}
15 changes: 13 additions & 2 deletions resources/views/admin/submissions/submission.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,14 @@
<h2>Characters</h2>
<div id="characters" class="mb-3">
@foreach ($submission->characters as $character)
@include('widgets._character_select_entry', ['characterCurrencies' => $characterCurrencies, 'items' => $items, 'tables' => $tables, 'character' => $character, 'expanded_rewards' => $expanded_rewards])
@include('widgets._character_select_entry', [
'characterCurrencies' => $characterCurrencies,
'items' => $items,
'tables' => $tables,
'character' => $character,
'expanded_rewards' => $expanded_rewards,
'elements' => $elements,
])
@endforeach
</div>
<div class="text-right mb-3">
Expand Down Expand Up @@ -196,12 +203,16 @@

@if ($expanded_rewards)
<td>
{!! Form::select('character_rewardable_type[]', ['Item' => 'Item', 'Currency' => 'Currency', 'LootTable' => 'Loot Table'], null, ['class' => 'form-control character-rewardable-type', 'placeholder' => 'Select Reward Type']) !!}
{!! Form::select('character_rewardable_type[]', ['Item' => 'Item', 'Currency' => 'Currency', 'LootTable' => 'Loot Table', 'Element' => 'Element'], null, [
'class' => 'form-control character-rewardable-type',
'placeholder' => 'Select Reward Type',
]) !!}
</td>
<td class="lootDivs">
<div class="character-currencies hide">{!! Form::select('character_rewardable_id[]', $characterCurrencies, 0, ['class' => 'form-control character-currency-id', 'placeholder' => 'Select Currency']) !!}</div>
<div class="character-items hide">{!! Form::select('character_rewardable_id[]', $items, 0, ['class' => 'form-control character-item-id', 'placeholder' => 'Select Item']) !!}</div>
<div class="character-tables hide">{!! Form::select('character_rewardable_id[]', $tables, 0, ['class' => 'form-control character-table-id', 'placeholder' => 'Select Loot Table']) !!}</div>
<div class="character-elements hide">{!! Form::select('character_rewardable_id[]', $elements, 0, ['class' => 'form-control character-element-id', 'placeholder' => 'Select Element']) !!}</div>
</td>
@else
<td class="lootDivs">
Expand Down
8 changes: 5 additions & 3 deletions resources/views/character/_image_info.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@
}
$type = $type ?? null;
@endphp
@if ($type)
@if ($type || (Auth::check() && Auth::user()->hasPower('manage_characters')))
<div class="row">
<div class="col-lg-4 col-md-6 col-4">
<h5>Typing</h5>
</div>
<div class="col-lg-8 col-md-6 col-8 row">
<h5>{!! $type->displayElements !!}</h5>
<h5>{!! $type?->displayElements !!}</h5>
@if (Auth::check() && Auth::user()->hasPower('manage_characters'))
{!! add_help('Typing is assigned on an image basis') !!}
<div class="ml-auto">
<a href="#" class="btn btn-outline-info btn-sm edit-typing" data-id="{{ $image->id }}"><i class="fas fa-cog"></i> Edit</a>
<a href="#" class="btn btn-outline-info btn-sm edit-typing" data-id="{{ $image->id }}">
<i class="fas fa-cog"></i> {{ $type ? 'Edit' : 'Create' }}
</a>
</div>
@endif
</div>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/home/create_submission.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@
</div>
{!! Form::close() !!}

@include('widgets._character_select', ['characterCurrencies' => $characterCurrencies, 'showLootTables' => false])
@include('widgets._character_select', ['characterCurrencies' => $characterCurrencies, 'showLootTables' => false, 'showElements' => false])
@if ($isClaim)
@include('widgets._loot_select_row', ['showLootTables' => false, 'showRaffles' => true])
@else
Expand Down
Loading

0 comments on commit 8c33cc6

Please sign in to comment.