web/app/Models/Mall/MallItem.php

127 lines
3.8 KiB
PHP

<?php
namespace App\Models\Mall;
use App\Models\Enums\MallItemPricingEnum;
use App\Models\Game\Player\ItemProto;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;
/**
* @property int $id
* @property int $vnum
* @property int $category_id
* @property int|null $old_price
* @property int $price
* @property MallItemPricingEnum $pricing
* @property int $quantity
* @property string|null $image
* @property string|null $description
* @property string|null $other
* @property-read ItemProto|null $proto
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem query()
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereCategoryId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereImage($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereOldPrice($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereOther($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem wherePrice($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem wherePricing($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereQuantity($value)
* @method static \Illuminate\Database\Eloquent\Builder<static>|MallItem whereVnum($value)
* @mixin \Eloquent
*/
class MallItem extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'id',
'category_id',
'old_price',
'price',
'pricing',
'quantity',
'image',
'description',
'other',
];
/**
* The attributes that should be hidden for serialization.
*
* @var array<int, string>
*/
protected $hidden = [
];
/**
* The attributes that should be cast.
*
* @var array<string, string>
*/
protected $casts = [
'pricing' => MallItemPricingEnum::class,
];
/**
* Get the associated item_proto entry
*/
public function proto(): HasOne
{
return $this->hasOne(ItemProto::class, 'vnum', 'vnum');
}
public function userCanBuy(): bool
{
$user = Auth::user();
if ($this->pricing == MallItemPricingEnum::CASH) {
return $user->cash >= $this->price;
} elseif ($this->pricing == MallItemPricingEnum::MILEAGE) {
return $user->mileage >= $this->price;
}
return false;
}
public static function getSuggestions(int $maxCount, ?MallItem $forItem = null, ?string $frontpageDisplay = null)
{
$query = MallItem::query();
// Ignore the current item if specified
if ($forItem) {
$query = $query->whereNotIn('vnum', [$forItem->vnum]);
}
// Select items that are to be shown on the frontpage
if ($frontpageDisplay) {
$query = $query->where('other', $frontpageDisplay);
}
$items = $query->get();
// Just return what we selected if we don't have enough items in the database
if ($items->count() <= $maxCount) {
return $items;
}
return $items->random($maxCount);
}
}