| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196 |
- <?php
- namespace App\Module\GameItems\Models;
- use App\Module\GameItems\Casts\TransactionDetailsCast;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 物品合成配方
- *
- * field start
- * @property int $id 配方ID,主键
- * @property string $name 配方名称
- * @property int $result_item_id 产出物品ID,外键关联kku_item_items表
- * @property int $result_min_quantity 最小产出数量
- * @property int $result_max_quantity 最大产出数量
- * @property float $success_rate 成功率(百分比,最大100)
- * @property object|array $coin_cost 货币成本,以JSON格式存储多种货币类型和数量
- * @property int $level_required 所需等级
- * @property int $is_default_unlocked 是否默认解锁(0:否, 1:是)
- * @property object|array $unlock_condition 解锁条件,以JSON格式存储
- * @property int $cooldown_seconds 冷却时间(秒)
- * @property int $category_id 配方分类ID
- * @property int $sort_order 排序权重
- * @property int $is_active 是否激活(0:否, 1:是)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemRecipe extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_recipes';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'result_item_id',
- 'result_min_quantity',
- 'result_max_quantity',
- 'success_rate',
- 'coin_cost',
- 'level_required',
- 'is_default_unlocked',
- 'unlock_condition',
- 'cooldown_seconds',
- 'category_id',
- 'sort_order',
- 'is_active',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'result_quantity' => 'integer',
- 'success_rate' => 'float',
- 'coin_cost' => TransactionDetailsCast::class,
- 'cooldown_seconds' => 'integer',
- 'is_visible' => 'boolean',
- 'unlock_condition' => TransactionDetailsCast::class,
- ];
- /**
- * 获取合成结果物品
- *
- * @return BelongsTo
- */
- public function resultItem(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'result_item_id');
- }
- /**
- * 获取配方材料
- *
- * @return HasMany
- */
- public function materials(): HasMany
- {
- return $this->hasMany(ItemRecipeMaterial::class, 'recipe_id');
- }
- /**
- * 获取用户配方解锁状态
- *
- * @return HasMany
- */
- public function userRecipes(): HasMany
- {
- return $this->hasMany(ItemUserRecipe::class, 'recipe_id');
- }
- /**
- * 获取合成记录
- *
- * @return HasMany
- */
- public function craftLogs(): HasMany
- {
- return $this->hasMany(ItemCraftLog::class, 'recipe_id');
- }
- /**
- * 检查用户是否已解锁该配方
- *
- * @param int $userId 用户ID
- * @return bool
- */
- public function isUnlockedByUser(int $userId): bool
- {
- $userRecipe = $this->userRecipes()
- ->where('user_id', $userId)
- ->first();
- return $userRecipe && $userRecipe->is_unlocked;
- }
- /**
- * 检查用户是否可以合成该配方
- *
- * @param int $userId 用户ID
- * @return array 检查结果,包含是否可合成和原因
- */
- public function canCraftByUser(int $userId): array
- {
- // 检查配方是否已解锁
- if (!$this->isUnlockedByUser($userId)) {
- return [
- 'can_craft' => false,
- 'reason' => '配方未解锁',
- ];
- }
- // 检查冷却时间
- if ($this->cooldown_seconds > 0) {
- $lastCraft = $this->craftLogs()
- ->where('user_id', $userId)
- ->where('created_at', '>', now()->subSeconds($this->cooldown_seconds))
- ->first();
- if ($lastCraft) {
- $remainingSeconds = $this->cooldown_seconds - now()->diffInSeconds($lastCraft->created_at);
- return [
- 'can_craft' => false,
- 'reason' => '冷却中',
- 'remaining_seconds' => $remainingSeconds,
- ];
- }
- }
- // 检查材料是否足够
- $materials = $this->materials;
- $missingMaterials = [];
- foreach ($materials as $material) {
- $userItem = ItemUser::where('user_id', $userId)
- ->where('item_id', $material->item_id)
- ->whereNull('instance_id')
- ->sum('quantity');
- if ($userItem < $material->quantity) {
- $missingMaterials[] = [
- 'item_id' => $material->item_id,
- 'item_name' => $material->item->name,
- 'required' => $material->quantity,
- 'available' => $userItem,
- ];
- }
- }
- if (!empty($missingMaterials)) {
- return [
- 'can_craft' => false,
- 'reason' => '材料不足',
- 'missing_materials' => $missingMaterials,
- ];
- }
- return [
- 'can_craft' => true,
- ];
- }
- }
|