Skip to content

Commit

Permalink
feat: extension
Browse files Browse the repository at this point in the history
  • Loading branch information
ScuffedNewt committed Aug 26, 2024
1 parent 2257e5e commit c203054
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/Http/Controllers/Admin/Data/FeatureController.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ public function postCreateEditFeature(Request $request, FeatureService $service,
$id ? $request->validate(Feature::$updateRules) : $request->validate(Feature::$createRules);
$data = $request->only([
'name', 'species_id', 'subtype_id', 'rarity_id', 'feature_category_id', 'description', 'image', 'remove_image', 'is_visible',
'alternative_rarities',
]);
if ($id && $service->updateFeature(Feature::find($id), $data, Auth::user())) {
flash('Trait updated successfully.')->success();
Expand Down
5 changes: 4 additions & 1 deletion app/Http/Middleware/PostRequestThrottleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ class PostRequestThrottleMiddleware {
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response {
if ($request->isMethod('get')) {
$allowedRoutes = [
'admin/*',
];
if ($request->isMethod('get') || $request->is(...$allowedRoutes)) {
return $next($request);
}

Expand Down
64 changes: 63 additions & 1 deletion app/Models/Feature/Feature.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Feature extends Model {
*/
protected $fillable = [
'feature_category_id', 'species_id', 'subtype_id', 'rarity_id', 'name', 'has_image', 'description', 'parsed_description', 'is_visible', 'hash',
'alternative_rarities',
];

/**
Expand All @@ -24,6 +25,16 @@ class Feature extends Model {
* @var string
*/
protected $table = 'features';

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'alternative_rarities' => 'array',
];

/**
* Validation rules for creation.
*
Expand Down Expand Up @@ -214,6 +225,13 @@ public function getDisplayNameAttribute() {
return '<a href="'.$this->url.'" class="display-trait">'.$this->name.'</a>'.($this->rarity ? ' ('.$this->rarity->displayName.')' : '');
}

/**
* Get's the display name without the rarity.
*/
public function getDisplayNameWithoutRarityAttribute() {
return '<a href="'.$this->url.'" class="display-trait">'.$this->name.'</a>';
}

/**
* Gets the file directory containing the model's image.
*
Expand Down Expand Up @@ -269,7 +287,7 @@ public function getUrlAttribute() {
* @return string
*/
public function getSearchUrlAttribute() {
return url('masterlist?feature_ids[]='.$this->id);
return url('masterlist?feature_id[]='.$this->id);
}

/**
Expand Down Expand Up @@ -331,4 +349,48 @@ public static function getDropdownItems($withHidden = 0) {
return self::where('is_visible', '>=', $visibleOnly)->orderBy('name')->pluck('name', 'id')->toArray();
}
}

/**
* Displays alternative rarities for a feature.
*/
public function displayAlternativeRarities() {
if (!$this->alternative_rarities) {
return null;
}

$altRarities = [];
foreach ($this->alternative_rarities as $species_id => $valuesArray) {
foreach ($valuesArray as $values) {
$altRarities[] = [
'species' => Species::find($species_id),
'rarity' => Rarity::find($values['rarity_id']),
'subtype' => Subtype::find($values['subtype_id']),
];
}
}

return $altRarities;
}

/**
* Gets the display name of the feature when on a character's page.
*/
public function displayName($species_id = null, $subtype_id = null) {
if ($this->alternative_rarities) {
foreach ($this->alternative_rarities as $species => $valuesArray) {
if ($species == $species_id) {
foreach ($valuesArray as $values) {
if ($values['subtype_id'] == $subtype_id || (!$values['subtype_id'] && !$subtype_id)) {
return $this->getDisplayNameWithoutRarityAttribute().' ('.Rarity::find($values['rarity_id'])->displayName
.'<i class="fas fa-info-circle ml-1" data-toggle="tooltip" title="This trait is different for this species'.
($values['subtype_id'] ? ' and subtype.' : '.').'
."></i>)';
}
}
}
}
}

return $this->getDisplayNameAttribute();
}
}
24 changes: 24 additions & 0 deletions app/Services/FeatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ public function updateFeature($feature, $data, $user) {
$this->handleImage($image, $feature->imagePath, $feature->imageFileName);
}

if (isset($data['alternative_rarities'])) {
$this->updateAlternativeFeatureRarities($feature, $data['alternative_rarities']);
}

return $this->commitReturn($feature);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
Expand Down Expand Up @@ -414,4 +418,24 @@ private function populateData($data, $feature = null) {

return $data;
}

/**
* Updates the alternative feature rarities.
*
* @param Feature $feature
* @param array $data
*
* @return void
*/
private function updateAlternativeFeatureRarities($feature, $data) {
$altRarityData = [];
foreach ($data['species_id'] as $key => $value) {
$altRarityData[$value][] = [
'subtype_id' => $data['subtype_id'][$key],
'rarity_id' => $data['rarity_id'][$key]
];
}

$feature->update(['alternative_rarities' => $altRarityData]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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('features', function (Blueprint $table) {
$table->json('alternative_rarities')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('features', function (Blueprint $table) {
$table->dropColumn('alternative_rarities');
});
}
};
80 changes: 80 additions & 0 deletions resources/views/admin/features/create_edit_feature.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,37 @@
{!! Form::select('subtype_id', $subtypes, $feature->subtype_id, ['class' => 'form-control', 'id' => 'subtype']) !!}
</div>
</div>

@if ($feature->id)
<hr />
<h4>Alternative Rarities</h4>
<p>If you want a trait to have different rarities depending on species / subtype set them here.</p>
<div class="text-right">
<div class="btn btn-primary" id="addAlternativeRarity">Add Alternative Rarity</div>
</div>
<div id="alternativeRarities">
@foreach ($feature->alternative_rarities ?? [] as $species_id=>$valuesArray)
@foreach ($valuesArray as $values)
<div class="row">
<div class="col-md-4 form-group">
{!! Form::label('Species') !!}
{!! Form::select('alternative_rarities[species_id][]', $specieses, $species_id, ['class' => 'form-control selectize species']) !!}
</div>
<div class="col-md-4 form-group subtype">
{!! Form::label('Subtype (Optional)') !!}
{!! Form::select('alternative_rarities[subtype_id][]', $subtypes, (!isset($values['subtype_id']) || !$values['subtype_id']) ? 'none' : $values['subtype_id'], ['class' => 'form-control', 'placeholder' => 'Select Subtype']) !!}
</div>
<div class="col-md-4 form-group">
{!! Form::label('Rarity') !!}
{!! Form::select('alternative_rarities[rarity_id][]', $rarities, $values['rarity_id'], ['class' => 'form-control selectize']) !!}
</div>
</div>
@endforeach
@endforeach
</div>
<hr />
@endif

<div class="form-group">
{!! Form::label('Description (Optional)') !!}
{!! Form::textarea('description', $feature->description, ['class' => 'form-control wysiwyg']) !!}
Expand All @@ -78,6 +109,23 @@
</div>
</div>
@endif

<div class="hide alt-rarity-row">
<div class="row">
<div class="col-md-4 form-group">
{!! Form::label('Species') !!}
{!! Form::select('alternative_rarities[species_id][]', $specieses, null, ['class' => 'form-control selectize species']) !!}
</div>
<div class="col-md-4 form-group subtype">
{!! Form::label('Subtype (Optional)') !!}
{!! Form::select('alternative_rarities[subtype_id][]', [], null, ['class' => 'form-control', 'placeholder' => 'Select Subtype']) !!}
</div>
<div class="col-md-4 form-group">
{!! Form::label('Rarity') !!}
{!! Form::select('alternative_rarities[rarity_id][]', $rarities, null, ['class' => 'form-control selectize']) !!}
</div>
</div>
</div>
@endsection

@section('scripts')
Expand All @@ -89,12 +137,41 @@
loadModal("{{ url('admin/data/traits/delete') }}/{{ $feature->id }}", 'Delete Trait');
});
refreshSubtype();
$('#addAlternativeRarity').on('click', function() {
var row = $('.alt-rarity-row').clone();
row.removeClass('hide').removeClass('alt-rarity-row');
row.find('.selectize').selectize();
row.find('.species').on('change', function() {
changeSubtype(this);
});
$('#alternativeRarities').append(row);
});
});
$("#species").change(function() {
refreshSubtype();
});
function changeSubtype(node) {
var species = node.value;
var row = $(node).closest('.row');
var subtype = row.find('.subtype');
$.ajax({
type: "GET",
url: "{{ url('admin/data/traits/check-subtype') }}?species=" + species,
dataType: "text"
}).done(function(res) {
subtype.html(res);
subtype.find('select').attr('name', 'alternative_rarities[subtype_id][]');
$('[data-toggle="tooltip"]').tooltip({
html: true
});
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
});
}
function refreshSubtype() {
var species = $('#species').val();
var subtype_id = {{ $feature->subtype_id ?: 'null' }};
Expand All @@ -104,6 +181,9 @@ function refreshSubtype() {
dataType: "text"
}).done(function(res) {
$("#subtypes").html(res);
$('[data-toggle="tooltip"]').tooltip({
html: true
});
}).fail(function(jqXHR, textStatus, errorThrown) {
alert("AJAX call failed: " + textStatus + ", " + errorThrown);
});
Expand Down
4 changes: 2 additions & 2 deletions resources/views/character/_image_info.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
<strong>Miscellaneous:</strong>
@endif
@foreach ($group as $feature)
<div class="ml-md-2">{!! $feature->feature->displayName !!} @if ($feature->data)
<div class="ml-md-2">{!! $feature->feature->displayName($image->species_id, $image->subtype_id) !!} @if ($feature->data)
({{ $feature->data }})
@endif
</div>
Expand All @@ -94,7 +94,7 @@
<div>
@if ($feature->feature->feature_category_id)
<strong>{!! $feature->feature->category->displayName !!}:</strong>
@endif {!! $feature->feature->displayName !!} @if ($feature->data)
@endif {!! $feature->feature->displayName($image->species_id, $image->subtype_id) !!} @if ($feature->data)
({{ $feature->data }})
@endif
</div>
Expand Down
19 changes: 19 additions & 0 deletions resources/views/world/_feature_entry.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,25 @@
@endif
<div class="world-entry-text parsed-text">
{!! $feature->parsed_description !!}
@if (count($feature->alternative_rarities ?? []))
<hr />
<h5>Alternative Rarities</h5>
<div class="row">
@foreach ($feature->displayAlternativeRarities() ?? [] as $data)
<div class="col-md-3">
@if ($data['subtype'])
<div>
<strong>{!! $data['subtype']->displayName !!} ({!! $data['species']->displayName !!} Subtype):</strong> {!! $data['rarity']->displayName !!}
</div>
@else
<div>
<strong>{!! $data['species']->displayName !!}:</strong> {!! $data['rarity']->displayName !!}
</div>
@endif
</div>
@endforeach
</div>
@endif
</div>
</div>
</div>

0 comments on commit c203054

Please sign in to comment.