1
0
Fork 0

Initial Commit

This commit is contained in:
Henrik Hautakoski 2021-10-18 11:53:33 +02:00
commit ddf09fe00c
113 changed files with 187148 additions and 0 deletions

93
app/Models/Card.php Normal file
View 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']);
}
}

View file

@ -0,0 +1,45 @@
<?php
namespace App\Models\Casts;
use App\Game\GameSettings;
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
use InvalidArgumentException;
class GameSettingsCaster implements CastsAttributes
{
/**
* Cast the given value.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param mixed $value
* @param array $attributes
* @return GameSettings
*/
public function get($model, $key, $value, $attributes)
{
$value = json_decode($value, true);
return new GameSettings($value);
}
/**
* Prepare the given value for storage.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $key
* @param GameSettings|array $value
* @param array $attributes
* @return string
*/
public function set($model, $key, $value, $attributes)
{
if (is_array($value)) {
$value = new GameSettings($value);
} else if (!($value instanceof GameSettings)) {
throw new InvalidArgumentException('The given value must be an GameSettings instance or array.');
}
return json_encode($value->toArray());
}
}

23
app/Models/Character.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Character extends Model
{
use HasFactory, SoftDeletes;
public $timestamps = false;
public $fillable = [
'name',
];
public function cards()
{
return $this->hasMany(Card::class);
}
}

23
app/Models/Raid.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Raid extends Model
{
use HasFactory, SoftDeletes;
public $timestamps = false;
public $fillable = [
'name',
];
public function cards()
{
return $this->hasMany(Card::class);
}
}

60
app/Models/Setting.php Normal file
View file

@ -0,0 +1,60 @@
<?php
namespace App\Models;
use Vinkla\Hashids\Facades\Hashids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Prunable;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;
class Setting extends Model
{
use HasFactory, Prunable;
public $timestamps = false;
public $fillable = [
'value',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'value' => Casts\GameSettingsCaster::class,
];
/**
* Get the prunable model query.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function prunable()
{
return static::where('created_at', '<', now()->subDay(1));
}
public function getHashAttribute()
{
return Hashids::encode($this->id);
}
public function scopeHash($q, $hash)
{
return $q->where('id', Hashids::decode($hash));
}
/*
public function __call($method, $parameters)
{
if ($method == 'find' && is_string($parameters[0])) {
$parameters[0] = Hashids::decode($$parameters[0]);
}
return parent::__call($method, $parameters);
} */
}