131 lines
3.1 KiB
PHP
131 lines
3.1 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Game\GameSettings;
|
|
|
|
use Kirschbaum\PowerJoins\PowerJoins;
|
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
class Card extends Model
|
|
{
|
|
use HasFactory, SoftDeletes, PowerJoins;
|
|
|
|
public $timestamps = false;
|
|
|
|
protected $appends = ['subject', 'subject_type'];
|
|
|
|
public $fillable = [
|
|
'body',
|
|
'character_id',
|
|
'raid_id',
|
|
'class',
|
|
'role'
|
|
];
|
|
|
|
/**
|
|
* Get the character that's associated with this card.
|
|
*/
|
|
public function character()
|
|
{
|
|
return $this->belongsTo(Character::class);
|
|
}
|
|
|
|
/**
|
|
* Get the raid that's associated with this card.
|
|
*/
|
|
public function raid()
|
|
{
|
|
return $this->belongsTo(Raid::class);
|
|
}
|
|
|
|
/**
|
|
* Who, Where or What this card belongs to.
|
|
*/
|
|
public function getSubjectAttribute()
|
|
{
|
|
if ($this->character) {
|
|
return $this->character->name;
|
|
} else if ($this->class) {
|
|
return Str::ucfirst($this->class);
|
|
} else if ($this->role) {
|
|
return Str::ucfirst($this->role);
|
|
}
|
|
return "Somebody";
|
|
}
|
|
|
|
/**
|
|
* Who, Where or What this card belongs to.
|
|
*/
|
|
public function getSubjectTypeAttribute()
|
|
{
|
|
if ($this->character) {
|
|
return "Character";
|
|
} else if ($this->class) {
|
|
return "Class";
|
|
} else if ($this->role) {
|
|
return "Roll";
|
|
}
|
|
return "Anyone";
|
|
}
|
|
|
|
/**
|
|
* Get cards depending on settings.
|
|
*/
|
|
public static function getBySettings(GameSettings $settings, ?int $max = null, int $num_jackpot = 0)
|
|
{
|
|
$query = self::settingsQuery($settings)
|
|
->where('jackpot', '=', 0);
|
|
|
|
$jitems = \collect([]);
|
|
if ($num_jackpot > 0) {
|
|
$jitems = self::settingsQuery($settings)
|
|
->where('jackpot', '!=', 0)
|
|
->limit($num_jackpot)
|
|
->get();
|
|
}
|
|
|
|
if ($max !== null) {
|
|
$query->limit($max - count($jitems));
|
|
}
|
|
|
|
if (count($jitems) > 0) {
|
|
$items = $query->get()->merge($jitems)->shuffle();
|
|
} else {
|
|
$items = $query->inRandomOrder()->get();
|
|
}
|
|
|
|
return $items->makeHidden(['character', 'class', 'deleted_at']);
|
|
}
|
|
|
|
protected static function settingsQuery(GameSettings $settings): Builder
|
|
{
|
|
$query = self::query();
|
|
|
|
// Map type of setting to database column.
|
|
$type_map = [
|
|
'characters' => 'character_id',
|
|
'raids' => 'raid_id',
|
|
'classes' => 'class',
|
|
'roles' => 'role'
|
|
];
|
|
|
|
// Run through all settings and apply filter.
|
|
foreach ($settings->all() as $type => $values) {
|
|
|
|
$column = $type_map[$type];
|
|
|
|
$query->where(function ($q) use ($column, $values) {
|
|
$q->whereIn($column, $values)->orWhereNull($column);
|
|
});
|
|
}
|
|
|
|
return $query;
|
|
}
|
|
}
|