| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?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 $chest_id 宝箱物品ID,外键关联kku_item_items表
- * @property int $item_id 可能获得的物品ID,外键关联kku_item_items表(与group_id二选一)
- * @property int $group_id 物品组ID,外键关联kku_item_groups表(与item_id二选一)
- * @property int $min_quantity 最小数量
- * @property int $max_quantity 最大数量
- * @property float $weight 权重,决定获取概率
- * @property int $allow_duplicate 是否允许在同一宝箱中重复掉落(0:不允许, 1:允许)
- * @property int $pity_count 保底次数,当玩家连续未获得该内容达到次数后必定获得(0表示不启用保底)
- * @property float $pity_weight_factor 保底权重因子,用于递增概率计算(默认1.0)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemChestContent extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_chest_contents';
- // attrlist start
- protected $fillable = [
- 'id',
- 'chest_id',
- 'item_id',
- 'group_id',
- 'min_quantity',
- 'max_quantity',
- 'weight',
- 'allow_duplicate',
- 'pity_count',
- 'pity_weight_factor',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'weight' => 'float',
- 'min_quantity' => 'integer',
- 'max_quantity' => 'integer',
- 'allow_duplicate' => 'boolean',
- 'pity_count' => 'integer',
- 'pity_weight_factor' => 'float',
- ];
- /**
- * 获取关联的宝箱物品
- *
- * @return BelongsTo
- */
- public function chest(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'chest_id');
- }
- /**
- * 获取关联的物品(如果有)
- *
- * @return BelongsTo
- */
- public function item(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'item_id');
- }
- /**
- * 获取关联的物品组(如果有)
- *
- * @return BelongsTo
- */
- public function group(): BelongsTo
- {
- return $this->belongsTo(ItemGroup::class, 'group_id');
- }
- /**
- * 获取用户对该宝箱内容的保底计数
- *
- * @return HasMany
- */
- public function pityTimes(): HasMany
- {
- return $this->hasMany(ItemPityTime::class, 'chest_content_id');
- }
- /**
- * 判断是否为物品组内容
- *
- * @return bool
- */
- public function isGroupContent(): bool
- {
- return !empty($this->group_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);
- }
- /**
- * 计算调整后的权重(考虑保底机制)
- *
- * @param int $currentPityCount 当前保底计数
- * @return float
- */
- public function getAdjustedWeight(int $currentPityCount = 0): float
- {
- if (empty($this->pity_count) || $currentPityCount <= 0) {
- return $this->weight;
- }
- // 如果达到保底次数,返回极大值确保必定获得
- if ($currentPityCount >= $this->pity_count) {
- return 999999.999;
- }
- // 计算权重调整因子
- $pityFactor = ($currentPityCount / $this->pity_count) * $this->pity_weight_factor;
- // 返回调整后的权重
- return $this->weight * (1 + $pityFactor);
- }
- }
|