| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- <?php
- namespace App\Module\Game\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 奖励组保底计数
- *
- * field start
- * @property int $id
- * @property int $user_id 用户ID
- * @property int $reward_group_id 奖励组ID
- * @property int $reward_item_id 奖励项ID
- * @property int $count 当前计数
- * @property int $pity_threshold 保底阈值
- * @property \Carbon\Carbon $last_attempt_at 最后尝试时间
- * @property \Carbon\Carbon $last_hit_at 最后命中时间
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * field end
- */
- class GameRewardGroupPityCount extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'game_reward_group_pity_counts';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'reward_group_id',
- 'reward_item_id',
- 'count',
- 'pity_threshold',
- 'last_attempt_at',
- 'last_hit_at',
- ];
- // attrlist end
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'count' => 'integer',
- 'pity_threshold' => 'integer',
- 'last_attempt_at' => 'datetime',
- 'last_hit_at' => 'datetime',
- ];
- /**
- * 获取关联的用户
- *
- * @return BelongsTo
- */
- public function user(): BelongsTo
- {
- return $this->belongsTo(\App\Models\User::class, 'user_id');
- }
- /**
- * 获取关联的奖励组
- *
- * @return BelongsTo
- */
- public function rewardGroup(): BelongsTo
- {
- return $this->belongsTo(GameRewardGroup::class, 'reward_group_id');
- }
- /**
- * 获取关联的奖励项
- *
- * @return BelongsTo
- */
- public function rewardItem(): BelongsTo
- {
- return $this->belongsTo(GameRewardItem::class, 'reward_item_id');
- }
- /**
- * 检查是否达到保底阈值
- *
- * @return bool
- */
- public function isAtPityThreshold(): bool
- {
- return $this->count >= $this->pity_threshold && $this->pity_threshold > 0;
- }
- /**
- * 增加计数
- *
- * @return void
- */
- public function incrementCount(): void
- {
- $this->increment('count');
- $this->update(['last_attempt_at' => now()]);
- }
- /**
- * 重置计数
- *
- * @return void
- */
- public function resetCount(): void
- {
- $this->update([
- 'count' => 0,
- 'last_hit_at' => now(),
- ]);
- }
- /**
- * 获取调整后的权重
- *
- * @param float $baseWeight 基础权重
- * @param float $weightFactor 权重因子
- * @return float
- */
- public function getAdjustedWeight(float $baseWeight, float $weightFactor): float
- {
- if ($this->pity_threshold <= 0) {
- return $baseWeight;
- }
- // 如果达到保底阈值,权重设为极大值确保必中
- if ($this->isAtPityThreshold()) {
- return 999999.0;
- }
- // 根据当前计数调整权重
- $progressRatio = $this->count / $this->pity_threshold;
- $adjustedWeight = $baseWeight * (1 + $progressRatio * $weightFactor);
- return $adjustedWeight;
- }
- }
|