Initial Commit
This commit is contained in:
commit
ddf09fe00c
113 changed files with 187148 additions and 0 deletions
93
app/Models/Card.php
Normal file
93
app/Models/Card.php
Normal file
|
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Game\GameSettings;
|
||||
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class Card extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected $appends = ['subject'];
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
/**
|
||||
* Get cards depending on settings.
|
||||
*/
|
||||
public static function getBySettings(GameSettings $settings, ?int $max = null)
|
||||
{
|
||||
// Map type of setting to database column.
|
||||
$type_map = [
|
||||
'characters' => 'character_id',
|
||||
'raids' => 'raid_id',
|
||||
'classes' => 'class',
|
||||
'roles' => 'role'
|
||||
];
|
||||
|
||||
$query = self::query()
|
||||
->inRandomOrder();
|
||||
|
||||
// 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);
|
||||
});
|
||||
}
|
||||
|
||||
if ($max !== null) {
|
||||
$query->limit($max);
|
||||
}
|
||||
|
||||
return $query->get()
|
||||
->makeHidden(['character', 'class', 'deleted_at']);
|
||||
}
|
||||
}
|
||||
Reference in a new issue