| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- <?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 $item_id 物品ID,外键关联kku_item_items表
- * @property int $category_id 分类ID,外键关联kku_item_categories表
- * @property int $consume_group_id 消耗组ID
- * @property int $reward_group_id 奖励组ID
- * @property int $condition_group_id 条件组ID
- * @property int $min_rarity 最小适用稀有度
- * @property int $max_rarity 最大适用稀有度
- * @property int $priority 规则优先级
- * @property int $sort_order 排序权重
- * @property bool $is_active 是否激活(0:否, 1:是)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemDismantleRule extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_dismantle_rules';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'code',
- 'description',
- 'item_id',
- 'category_id',
- 'consume_group_id',
- 'reward_group_id',
- 'condition_group_id',
- 'min_rarity',
- 'max_rarity',
- 'priority',
- 'sort_order',
- 'is_active',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'item_id' => 'integer',
- 'category_id' => 'integer',
- 'consume_group_id' => 'integer',
- 'reward_group_id' => 'integer',
- 'condition_group_id' => 'integer',
- 'min_rarity' => 'integer',
- 'max_rarity' => 'integer',
- 'priority' => 'integer',
- 'sort_order' => 'integer',
- 'is_active' => 'boolean',
- ];
- /**
- * 获取关联的物品(如果有)
- *
- * @return BelongsTo
- */
- public function item(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'item_id');
- }
- /**
- * 获取关联的分类(如果有)
- *
- * @return BelongsTo
- */
- public function category(): BelongsTo
- {
- return $this->belongsTo(ItemCategory::class, 'category_id');
- }
- /**
- * 获取消耗组
- *
- * @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 results(): HasMany
- {
- return $this->hasMany(ItemDismantleResult::class, 'rule_id');
- }
- /**
- * 获取分解日志
- *
- * @return HasMany
- */
- public function dismantleLogs(): HasMany
- {
- return $this->hasMany(ItemDismantleLog::class, 'rule_id');
- }
- /**
- * 计算分解返还的金币
- *
- * @param int $itemPrice 物品价格
- * @return int
- */
- public function calculateCoinReturn(int $itemPrice): int
- {
- if ($this->coin_return_rate <= 0) {
- return 0;
- }
- return (int)($itemPrice * $this->coin_return_rate);
- }
- /**
- * 检查用户是否可以使用该分解规则
- *
- * @param int $userId 用户ID
- * @param int $itemId 物品ID
- * @param int $quantity 分解数量
- * @return array 检查结果
- */
- public function canDismantleByUser(int $userId, int $itemId, int $quantity = 1): array
- {
- // 检查规则是否激活
- if (!$this->is_active) {
- return [
- 'can_dismantle' => false,
- 'reason' => '分解规则未激活',
- ];
- }
- // 检查条件组(如果设置了条件组)
- if ($this->condition_group_id && $this->conditionGroup) {
- // TODO: 实现ConditionService::checkConditionGroup方法
- // $conditionService = app(\App\Module\Game\Services\ConditionService::class);
- // $conditionResult = $conditionService->checkConditionGroup($userId, $this->condition_group_id);
- //
- // if (!$conditionResult['is_satisfied']) {
- // return [
- // 'can_dismantle' => false,
- // 'reason' => '条件不满足',
- // 'condition_details' => $conditionResult['details'],
- // ];
- // }
- }
- // 检查消耗组(如果设置了消耗组,用于额外消耗,如分解工具等)
- if ($this->consume_group_id && $this->consumeGroup) {
- // TODO: 实现ConsumeService::checkConsumeGroup方法
- // $consumeService = app(\App\Module\Game\Services\ConsumeService::class);
- // $consumeResult = $consumeService->checkConsumeGroup($userId, $this->consume_group_id);
- //
- // if (!$consumeResult['can_consume']) {
- // return [
- // 'can_dismantle' => false,
- // 'reason' => '分解工具或材料不足',
- // 'consume_details' => $consumeResult['details'],
- // ];
- // }
- }
- return [
- 'can_dismantle' => true,
- ];
- }
- /**
- * 获取分解结果(使用奖励组)
- *
- * @return array
- */
- public function getDismantleResults(): array
- {
- // 如果设置了奖励组,使用奖励组系统
- if ($this->reward_group_id && $this->rewardGroup) {
- // TODO: 实现RewardService::generateRewards方法
- // $rewardService = app(\App\Module\Game\Services\RewardService::class);
- // return $rewardService->generateRewards($this->reward_group_id);
- // 临时返回空数组,等待RewardService实现
- return [];
- }
- // 兼容旧系统:使用原有的分解结果表
- $results = [];
- $dismantleResults = $this->results()->with('resultItem')->get();
- foreach ($dismantleResults as $result) {
- // 根据概率决定是否获得该物品
- if (mt_rand(1, 10000) <= $result->base_chance * 100) {
- // 计算数量
- $quantity = $result->min_quantity;
- if ($result->max_quantity > $result->min_quantity) {
- $quantity = mt_rand($result->min_quantity, $result->max_quantity);
- }
- if ($quantity > 0) {
- $results[] = [
- 'item_id' => $result->result_item_id,
- 'item_name' => $result->resultItem->name,
- 'quantity' => $quantity,
- ];
- }
- }
- }
- return $results;
- }
- }
|