Archived
1
0
Fork 0
This repository has been archived on 2026-06-16. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
wow-raid-bingo/app/Models/Card.php

108 lines
2.5 KiB
PHP

<?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', '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)
{
// 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']);
}
}