| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?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 int $item_id 物品ID,外键关联kku_item_items表
- * @property int $category_id 分类ID,外键关联kku_item_categories表
- * @property int $min_rarity 最小适用稀有度
- * @property int $max_rarity 最大适用稀有度
- * @property int $priority 规则优先级
- * @property int $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',
- 'item_id',
- 'category_id',
- 'min_rarity',
- 'max_rarity',
- 'priority',
- 'is_active',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'priority' => 'integer',
- 'coin_return_rate' => 'float',
- '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 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);
- }
- /**
- * 获取分解结果
- *
- * @return array
- */
- public function getDismantleResults(): array
- {
- $results = [];
- $dismantleResults = $this->results()->with('resultItem')->get();
- foreach ($dismantleResults as $result) {
- // 根据概率决定是否获得该物品
- if (mt_rand(1, 10000) <= $result->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;
- }
- }
|