| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229 |
- <?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 \App\Module\GameItems\Casts\RecipeDisplayAttributesCast $display_attributes 展示属性,以JSON格式存储键值对,用于界面展示和描述的属性
- * @property float $success_rate 成功率(0-100之间的小数,100 = 100%)
- * @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',
- 'display_attributes',
- 'success_rate',
- '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',
- 'display_attributes' => \App\Module\GameItems\Casts\RecipeDisplayAttributesCast::class,
- '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;
- }
- /**
- * 格式化消耗组详情用于显示
- *
- * @return string
- */
- public function formatConsumeDetails(): string
- {
- if (!$this->consumeGroup) {
- return '<span class="text-muted">无消耗组</span>';
- }
- return $this->consumeGroup->formatConsumeDetails();
- }
- /**
- * 格式化奖励组详情用于显示
- *
- * @return string
- */
- public function formatRewardDetails(): string
- {
- if (!$this->rewardGroup) {
- return '<span class="text-muted">无奖励组</span>';
- }
- return $this->rewardGroup->formatRewardDetails();
- }
- /**
- * 格式化条件组详情用于显示
- *
- * @return string
- */
- public function formatConditionDetails(): string
- {
- if (!$this->conditionGroup) {
- return '<span class="text-muted">无条件组</span>';
- }
- return $this->conditionGroup->formatConditionDetails();
- }
- /**
- * 检查用户是否可以合成该配方
- *
- * @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->checkCondition($userId, $this->condition_group_id);
- if (!$conditionResult['success']) {
- return [
- 'can_craft' => false,
- 'reason' => '条件不满足: ' . $conditionResult['message'],
- ];
- }
- }
- // 检查消耗组是否满足
- if ($this->consume_group_id && $this->consumeGroup) {
- $consumeService = app(\App\Module\Game\Services\ConsumeService::class);
- $consumeResult = $consumeService->checkConsume($userId, $this->consume_group_id);
- if (!$consumeResult->success) {
- return [
- 'can_craft' => false,
- 'reason' => '材料不足: ' . $consumeResult->message,
- ];
- }
- }
- return [
- 'can_craft' => true,
- ];
- }
- }
|