| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- <?php
- namespace App\Module\GameItems\Models;
- 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 string $code 配方编码(唯一)
- * @property string $description 配方描述
- * @property int $consume_group_id 消耗组ID
- * @property int $reward_group_id 奖励组ID
- * @property int $condition_group_id 条件组ID
- * @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 bool $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',
- 'code',
- 'description',
- 'consume_group_id',
- 'reward_group_id',
- 'condition_group_id',
- '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 = [
- 'consume_group_id' => 'integer',
- 'reward_group_id' => 'integer',
- 'condition_group_id' => 'integer',
- 'success_rate' => 'float',
- 'cooldown_seconds' => 'integer',
- 'category_id' => 'integer',
- 'sort_order' => 'integer',
- 'is_active' => 'boolean',
- ];
- /**
- * 获取消耗组
- *
- * @return BelongsTo
- */
- public function consumeGroup(): BelongsTo
- {
- return $this->belongsTo(\App\Module\Game\Models\GameConsumeGroup::class, 'consume_group_id');
- }
- /**
- * 获取奖励组
- *
- * @return BelongsTo
- */
- public function rewardGroup(): BelongsTo
- {
- return $this->belongsTo(\App\Module\Game\Models\GameRewardGroup::class, 'reward_group_id');
- }
- /**
- * 获取条件组
- *
- * @return BelongsTo
- */
- public function conditionGroup(): BelongsTo
- {
- return $this->belongsTo(\App\Module\Game\Models\GameConditionGroup::class, 'condition_group_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->is_active) {
- return [
- 'can_craft' => false,
- 'reason' => '配方未激活',
- ];
- }
- // 检查条件组(如果设置了条件组)
- if ($this->condition_group_id && $this->conditionGroup) {
- $conditionService = app(\App\Module\Game\Services\ConditionService::class);
- $conditionResult = $conditionService->checkConditionGroup($userId, $this->condition_group_id);
- if (!$conditionResult['is_satisfied']) {
- return [
- 'can_craft' => false,
- 'reason' => '条件不满足',
- 'condition_details' => $conditionResult['details'],
- ];
- }
- }
- // 检查冷却时间
- 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,
- ];
- }
- }
- // 检查消耗组是否满足
- if ($this->consume_group_id && $this->consumeGroup) {
- $consumeService = app(\App\Module\Game\Services\ConsumeService::class);
- $consumeResult = $consumeService->checkConsumeGroup($userId, $this->consume_group_id);
- if (!$consumeResult['can_consume']) {
- return [
- 'can_craft' => false,
- 'reason' => '材料不足',
- 'consume_details' => $consumeResult['details'],
- ];
- }
- }
- return [
- 'can_craft' => true,
- ];
- }
- }
|