Skip to content
47 changes: 47 additions & 0 deletions app/Console/Commands/FixPetDropIds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Console\Commands;

use App\Models\Pet\PetDrop;
use Illuminate\Console\Command;

class FixPetDropIds extends Command {
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'fix-pet-drop-ids';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Fixes inconsistent pet drop data IDs';

/**
* Execute the console command.
*/
public function handle() {
$drops = PetDrop::all();
foreach ($drops as $drop) {
if ($drop->user_pet !== null) {
if (isset($drop->user_pet->pet->dropData) && ($drop->drop_id !== $drop->user_pet->pet->dropData->id)) {
$drop->update([
'drop_id' => $drop->user_pet->pet->dropData->id,
]);
$this->line('Corrected pet ID #'.$drop->user_pet->id."\n");
} elseif (!isset($drop->user_pet->pet->dropData)) {
// the pet has no drop data and the PetDrop can be deleted
$this->line('Deleted drop data for pet #'.$drop->user_pet_id." that has no drops\n");
$drop->delete();
}
} else {
// the pet is deleted and the PetDrop is no longer needed
$this->line('Deleted drop data for deleted pet #'.$drop->user_pet_id."\n");
$drop->delete();
}
}
}
}
31 changes: 26 additions & 5 deletions app/Services/PetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,15 +459,21 @@ public function editVariant($id, $pet, $stack_id, $isStaff = false) {
DB::beginTransaction();

try {
// find parent if id is default
if ($id == 0) {
$pet_type = Pet::find($pet->pet_id);
$id = $pet_type->parent == null ? null : $pet_type->parent->id;
}

if ($id == null) {
throw new \Exception('Pet is already the default variant.');
}

if (!$isStaff || !Auth::user()->isStaff) {
if (!$stack_id) {
throw new \Exception('No item selected.');
}

if ($id == 0) {
$id = 'default';
}

// check if user has item
$item = UserItem::find($stack_id);
$tag = $item->item->tags->where('tag', 'splice')->first();
Expand All @@ -492,9 +498,23 @@ public function editVariant($id, $pet, $stack_id, $isStaff = false) {
$this->logAdminAction($pet->user, 'Pet Variant Changed', 'Changed pet id #'.$pet->id.'/'.$pet->pet->name.' to variant '.Pet::find($id)->name);
}

$pet->pet_id = $id == 'default' ? null : $id;
$pet->pet_id = $id;
$pet->save();

// update pet drop, if relevant
if (isset($pet->drops)) {
$newPet = Pet::find($id);
if (isset($newPet->dropData)) {
if ($pet->drops->drop_id !== $newPet->dropData->id) {
$pet->drops->drop_id = $newPet->dropData->id;
$pet->drops->save();
}
} else {
// the new variant does not have drops, so we discard the old drops row
$pet->drops()->delete();
}
}

return $this->commitReturn(true);
} catch (\Exception $e) {
$this->setError('error', $e->getMessage());
Expand Down Expand Up @@ -738,6 +758,7 @@ public function debitStack($user, $type, $data, $stack) {
DB::beginTransaction();

try {
$stack->drops()->delete();
$stack->delete();

if ($type && !$this->createLog($user ? $user->id : null, null, $stack->id, $type, $data['data'], $stack->pet_id, 1)) {
Expand Down
2 changes: 1 addition & 1 deletion resources/views/admin/pets/create_edit_drop.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
</div>
<div class="form-group">
{!! Form::label('cap', 'Drop Cap (Optional)', ['class' => 'form-label ml-3']) !!} {!! add_help('How many batches of drops are allowed to accumulate. Either set to 0 or unset to allow unlimited accumulation.') !!}
{!! Form::number('cap', $drop->id ? $drop->cap : null, ['class' => 'form-control mr-2', 'placeholder' => 'Drop Cap']) !!}
{!! Form::number('cap', $drop->cap ?? null, ['class' => 'form-control mr-2', 'placeholder' => 'Drop Cap']) !!}
</div>
<div class="form-group">
{!! Form::checkbox('is_active', 1, $drop->id ? $drop->isActive : 1, ['class' => 'form-check-input', 'data-toggle' => 'toggle']) !!}
Expand Down
22 changes: 18 additions & 4 deletions resources/views/home/_pet_form.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,25 @@
<li class="list-group-item">
<a class="card-title h5 collapse-title" data-toggle="collapse" href="#userVariantForm">Change Pet Variant</a>
{!! Form::open(['url' => 'pets/variant/' . $pet->id, 'id' => 'userVariantForm', 'class' => 'collapse']) !!}
@if (isset($pet->drops) && $pet->drops->drops_available > 0)
<div class="alert alert-danger">
This pet currently has available drops. Changing its variant may alter or remove their currently available drops.
<br>
<b>Please collect your drops before changing this pet's variant.</b>
</div>
@endif
<p>
This will use a splice item!
@if ($pet->pet->isVariant)
<br>Current Variant: {{ $pet->pet->name }}
<br><b>Current Variant:</b> {{ $pet->pet->name }}
@endif
</p>
<div class="form-group">
{!! Form::select('stack_id', $splices, null, ['class' => 'form-control', 'placeholder' => 'Select Item']) !!}
</div>
<div class="form-group">
@php
$variants = ['0' => 'Default'] + ($pet->pet->isVariant ? $pet->pet->parent->variants()->pluck('name', 'id')->toArray() : $pet->pet->variants()->pluck('name', 'id')->toArray());
$variants = [0 => 'Default'] + ($pet->pet->isVariant ? $pet->pet->parent->variants()->pluck('name', 'id')->toArray() : $pet->pet->variants()->pluck('name', 'id')->toArray());
@endphp
{!! Form::select('variant_id', $variants, $pet->variant_id, ['class' => 'form-control']) !!}
</div>
Expand All @@ -107,9 +114,16 @@
<a class="card-title h5 collapse-title" data-toggle="collapse" href="#variantForm">[ADMIN] Change Pet Variant</a>
{!! Form::open(['url' => 'pets/variant/' . $pet->id, 'id' => 'variantForm', 'class' => 'collapse']) !!}
{!! Form::hidden('is_staff', 1) !!}
@if (isset($pet->drops) && $pet->drops->drops_available > 0)
<div class="alert alert-danger">
This pet currently has available drops. Changing its variant may alter or remove their currently available drops.
<br>
<b>Inform the user to collect their drops before changing this pet's variant.</b>
</div>
@endif
<div class="form-group">
@php
$variants = ['0' => 'Default'] + ($pet->pet->isVariant ? $pet->pet->parent->variants()->pluck('name', 'id')->toArray() : $pet->pet->variants()->pluck('name', 'id')->toArray());
$variants = [0 => 'Default'] + ($pet->pet->isVariant ? $pet->pet->parent->variants()->pluck('name', 'id')->toArray() : $pet->pet->variants()->pluck('name', 'id')->toArray());
@endphp
{!! Form::select('variant_id', $variants, $pet->variant_id, ['class' => 'form-control mt-2']) !!}
</div>
Expand All @@ -126,7 +140,7 @@
{!! Form::hidden('is_staff', 1) !!}
<div class="form-group">
@php
$evolutions = ['0' => 'Default'] + $pet->pet->evolutions()->pluck('evolution_name', 'id')->toArray();
$evolutions = [0 => 'Default'] + $pet->pet->evolutions()->pluck('evolution_name', 'id')->toArray();
@endphp
{!! Form::select('evolution_id', $evolutions, $pet->evolution_id, ['class' => 'form-control mt-2']) !!}
</div>
Expand Down
26 changes: 20 additions & 6 deletions resources/views/home/_pet_stack.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,25 @@
<li class="list-group-item">
<a class="card-title h5 collapse-title" data-toggle="collapse" href="#userVariantForm">Change Pet Variant</a>
{!! Form::open(['url' => 'pets/variant/' . $stack->id, 'id' => 'userVariantForm', 'class' => 'collapse']) !!}
@if (isset($stack->drops) && $stack->drops->drops_available > 0)
<div class="alert alert-danger">
This pet currently has available drops. Changing its variant may alter or remove their currently available drops.
<br>
<b>Please collect your drops before changing this pet's variant.</b>
</div>
@endif
<p>
This will use a splice item!
@if ($stack->pet->isVariant)
<br><b>Current variant:</b> {{ $stack->pet->name }}
<br><b>Current Variant:</b> {{ $stack->pet->name }}
@endif
</p>
<div class="form-group">
{!! Form::select('stack_id', $splices, null, ['class' => 'form-control', 'placeholder' => 'Select Item']) !!}
</div>
<div class="form-group">
@php
$variants = ['0' => 'Default'] + ($stack->pet->isVariant ? $stack->pet->parent->variants()->pluck('name', 'id')->toArray() : $stack->pet->variants()->pluck('name', 'id')->toArray());
$variants = [0 => 'Default'] + ($stack->pet->isVariant ? $stack->pet->parent->variants()->pluck('name', 'id')->toArray() : $stack->pet->variants()->pluck('name', 'id')->toArray());
@endphp
{!! Form::select('variant_id', $variants, $stack->pet->parent_id, ['class' => 'form-control']) !!}
</div>
Expand All @@ -138,14 +145,21 @@
<a class="card-title h5 collapse-title" data-toggle="collapse" href="#variantForm">[ADMIN] Change Pet Variant</a>
{!! Form::open(['url' => 'pets/variant/' . $stack->id, 'id' => 'variantForm', 'class' => 'collapse']) !!}
{!! Form::hidden('is_staff', 1) !!}
@if (isset($stack->drops) && $stack->drops->drops_available > 0)
<div class="alert alert-danger">
This pet currently has available drops. Changing its variant may alter or remove their currently available drops.
<br>
<b>Inform the user to collect their drops before changing this pet's variant.</b>
</div>
@endif
<p>
@if ($stack->variant_id)
<br><b>Current variant:</b> {{ $stack->variant->name }}
<br><b>Current Variant:</b> {{ $stack->variant->name }}
@endif
</p>
<div class="form-group">
@php
$variants = ['0' => 'Default'] + ($stack->pet->isVariant ? $stack->pet->parent->variants()->pluck('name', 'id')->toArray() : $stack->pet->variants()->pluck('name', 'id')->toArray());
$variants = [0 => 'Default'] + ($stack->pet->isVariant ? $stack->pet->parent->variants()->pluck('name', 'id')->toArray() : $stack->pet->variants()->pluck('name', 'id')->toArray());
@endphp
{!! Form::select('variant_id', $variants, $stack->pet->isVariant ? $stack->pet_id : 0, ['class' => 'form-control mt-2']) !!}
</div>
Expand All @@ -161,12 +175,12 @@
{!! Form::hidden('is_staff', 1) !!}
<p>
@if ($stack->evolution_id)
<br><b>Current evolution:</b> {{ $stack->evolution->evolution_name }} (Stage {{ $stack->evolution->evolution_stage }})
<br><b>Current Evolution:</b> {{ $stack->evolution->evolution_name }} (Stage {{ $stack->evolution->evolution_stage }})
@endif
</p>
<div class="form-group">
@php
$evolutions = ['0' => 'Default'] + $stack->pet->evolutions()->pluck('evolution_name', 'id')->toArray();
$evolutions = [0 => 'Default'] + $stack->pet->evolutions()->pluck('evolution_name', 'id')->toArray();
@endphp
{!! Form::select('evolution_id', $evolutions, $stack->evolution_id, ['class' => 'form-control mt-2']) !!}
</div>
Expand Down