1
0
forked from metin2/web

Shop improvements, renamed "shop" to "mall", support for Argon2ID

This commit is contained in:
2024-06-02 22:09:52 +03:00
parent 83f4f72b6e
commit 63d0d2ac79
1932 changed files with 729 additions and 514 deletions

View File

@ -2,7 +2,7 @@
namespace App\Models\Enums;
enum ShopItemPricingEnum: string
enum MallItemPricingEnum: string
{
case CASH = 'CASH';
case MILEAGE = 'MILEAGE';
@ -10,8 +10,8 @@ enum ShopItemPricingEnum: string
public function description(): string
{
return match($this) {
self::CASH => __('shop/main.currency.cash'),
self::MILEAGE => __('shop/main.currency.mileage')
self::CASH => __('mall/main.currency.cash'),
self::MILEAGE => __('mall/main.currency.mileage')
};
}
}

View File

@ -0,0 +1,58 @@
<?php
namespace App\Models\Game;
use App\Models\Mall\MallItem;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ItemProto extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'player';
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'item_proto';
/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'vnum';
/**
* The attributes that are mass assignable.
*
* @var array<int, string>
*/
protected $fillable = [
'name',
];
/**
* 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 = [
];
}

View File

@ -1,11 +1,11 @@
<?php
namespace App\Models\Shop;
namespace App\Models\Mall;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class ShopCategory extends Model
class MallCategory extends Model
{
/**
* The connection name for the model.
@ -48,6 +48,6 @@ class ShopCategory extends Model
*/
public function items(): HasMany
{
return $this->hasMany(ShopItem::class, 'category_id', 'id');
return $this->hasMany(MallItem::class, 'category_id', 'id');
}
}

View File

@ -0,0 +1,100 @@
<?php
namespace App\Models\Mall;
use App\Models\Enums\MallItemPricingEnum;
use App\Models\Game\ItemProto;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Support\Facades\Auth;
class MallItem extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'website';
/**
* 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
*
* @return HasOne
*/
public function proto(): HasOne
{
return $this->hasOne(ItemProto::class, 'vnum', 'vnum');
}
/**
* @return bool
*/
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);
}
}

View File

@ -1,67 +0,0 @@
<?php
namespace App\Models\Shop;
use App\Models\Enums\ShopItemPricingEnum;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
class ShopItem extends Model
{
/**
* The connection name for the model.
*
* @var string|null
*/
protected $connection = 'website';
/**
* 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' => ShopItemPricingEnum::class,
];
/**
* @return bool
*/
public function userCanBuy(): bool
{
$user = Auth::user();
if ($this->pricing == ShopItemPricingEnum::CASH)
return $user->cash >= $this->price;
elseif ($this->pricing == ShopItemPricingEnum::MILEAGE)
return $user->mileage >= $this->price;
return false;
}
}