| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- <?php
- namespace App\Module\GameItems\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 物品分解结果
- *
- * field start
- * @property int $id 记录ID,主键
- * @property int $rule_id 分解规则ID,外键关联kku_item_dismantle_rules表
- * @property int $result_item_id 结果物品ID,外键关联kku_item_items表
- * @property int $min_quantity 最小数量
- * @property int $max_quantity 最大数量
- * @property float $base_chance 基础获取概率(百分比,最大100)
- * @property float $rarity_factor 稀有度影响因子
- * @property float $quality_factor 品质影响因子
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemDismantleResult extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_dismantle_results';
- // attrlist start
- protected $fillable = [
- 'id',
- 'rule_id',
- 'result_item_id',
- 'min_quantity',
- 'max_quantity',
- 'base_chance',
- 'rarity_factor',
- 'quality_factor',
- ];
- // attrlist end
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'rule_id',
- 'result_item_id',
- 'min_quantity',
- 'max_quantity',
- 'chance',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'min_quantity' => 'integer',
- 'max_quantity' => 'integer',
- 'chance' => 'float',
- ];
- /**
- * 获取关联的分解规则
- *
- * @return BelongsTo
- */
- public function rule(): BelongsTo
- {
- return $this->belongsTo(ItemDismantleRule::class, 'rule_id');
- }
- /**
- * 获取关联的结果物品
- *
- * @return BelongsTo
- */
- public function resultItem(): BelongsTo
- {
- return $this->belongsTo(ItemItem::class, 'result_item_id');
- }
- /**
- * 获取随机数量
- *
- * @return int
- */
- public function getRandomQuantity(): int
- {
- if ($this->min_quantity == $this->max_quantity) {
- return $this->min_quantity;
- }
- return mt_rand($this->min_quantity, $this->max_quantity);
- }
- /**
- * 检查是否命中概率
- *
- * @return bool
- */
- public function isChanceHit(): bool
- {
- if ($this->chance >= 1.0) {
- return true;
- }
- return mt_rand(1, 10000) <= $this->chance * 10000;
- }
- }
|