GameRewardGroupPityCount.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace App\Module\Game\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 奖励组保底计数
  7. *
  8. * field start
  9. * @property int $id
  10. * @property int $user_id 用户ID
  11. * @property int $reward_group_id 奖励组ID
  12. * @property int $reward_item_id 奖励项ID
  13. * @property int $count 当前计数
  14. * @property int $pity_threshold 保底阈值
  15. * @property \Carbon\Carbon $last_attempt_at 最后尝试时间
  16. * @property \Carbon\Carbon $last_hit_at 最后命中时间
  17. * @property \Carbon\Carbon $created_at
  18. * @property \Carbon\Carbon $updated_at
  19. * field end
  20. */
  21. class GameRewardGroupPityCount extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'game_reward_group_pity_counts';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'user_id',
  33. 'reward_group_id',
  34. 'reward_item_id',
  35. 'count',
  36. 'pity_threshold',
  37. 'last_attempt_at',
  38. 'last_hit_at',
  39. ];
  40. // attrlist end
  41. /**
  42. * 应该被转换为原生类型的属性
  43. *
  44. * @var array
  45. */
  46. protected $casts = [
  47. 'count' => 'integer',
  48. 'pity_threshold' => 'integer',
  49. 'last_attempt_at' => 'datetime',
  50. 'last_hit_at' => 'datetime',
  51. ];
  52. /**
  53. * 获取关联的用户
  54. *
  55. * @return BelongsTo
  56. */
  57. public function user(): BelongsTo
  58. {
  59. return $this->belongsTo(\App\Models\User::class, 'user_id');
  60. }
  61. /**
  62. * 获取关联的奖励组
  63. *
  64. * @return BelongsTo
  65. */
  66. public function rewardGroup(): BelongsTo
  67. {
  68. return $this->belongsTo(GameRewardGroup::class, 'reward_group_id');
  69. }
  70. /**
  71. * 获取关联的奖励项
  72. *
  73. * @return BelongsTo
  74. */
  75. public function rewardItem(): BelongsTo
  76. {
  77. return $this->belongsTo(GameRewardItem::class, 'reward_item_id');
  78. }
  79. /**
  80. * 检查是否达到保底阈值
  81. *
  82. * @return bool
  83. */
  84. public function isAtPityThreshold(): bool
  85. {
  86. return $this->count >= $this->pity_threshold && $this->pity_threshold > 0;
  87. }
  88. /**
  89. * 增加计数
  90. *
  91. * @return void
  92. */
  93. public function incrementCount(): void
  94. {
  95. $this->increment('count');
  96. $this->update(['last_attempt_at' => now()]);
  97. }
  98. /**
  99. * 重置计数
  100. *
  101. * @return void
  102. */
  103. public function resetCount(): void
  104. {
  105. $this->update([
  106. 'count' => 0,
  107. 'last_hit_at' => now(),
  108. ]);
  109. }
  110. /**
  111. * 获取调整后的权重
  112. *
  113. * @param float $baseWeight 基础权重
  114. * @param float $weightFactor 权重因子
  115. * @return float
  116. */
  117. public function getAdjustedWeight(float $baseWeight, float $weightFactor): float
  118. {
  119. if ($this->pity_threshold <= 0) {
  120. return $baseWeight;
  121. }
  122. // 如果达到保底阈值,权重设为极大值确保必中
  123. if ($this->isAtPityThreshold()) {
  124. return 999999.0;
  125. }
  126. // 根据当前计数调整权重
  127. $progressRatio = $this->count / $this->pity_threshold;
  128. $adjustedWeight = $baseWeight * (1 + $progressRatio * $weightFactor);
  129. return $adjustedWeight;
  130. }
  131. }