GameRewardGroup.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. namespace App\Module\Game\Models;
  3. use Illuminate\Database\Eloquent\Relations\HasMany;
  4. use UCore\ModelCore;
  5. /**
  6. * 奖励组
  7. *
  8. * field start
  9. * @property int $id 主键
  10. * @property string $name 奖励组名称
  11. * @property string $code 奖励组编码(唯一)
  12. * @property string $description 奖励组描述
  13. * @property bool $is_random 是否随机发放(0:全部发放, 1:随机发放)
  14. * @property int $random_count 随机发放时的奖励数量
  15. * @property int $reward_mode 奖励模式(1:权重选择模式, 2:独立概率模式)
  16. * @property \Carbon\Carbon $created_at 创建时间
  17. * @property \Carbon\Carbon $updated_at 更新时间
  18. * field end
  19. */
  20. class GameRewardGroup extends ModelCore
  21. {
  22. /**
  23. * 与模型关联的表名
  24. *
  25. * @var string
  26. */
  27. protected $table = 'game_reward_groups';
  28. // attrlist start
  29. protected $fillable = [
  30. 'id',
  31. 'name',
  32. 'code',
  33. 'description',
  34. 'is_random',
  35. 'random_count',
  36. 'reward_mode',
  37. ];
  38. // attrlist end
  39. /**
  40. * 应该被转换为原生类型的属性
  41. *
  42. * @var array
  43. */
  44. protected $casts = [
  45. 'is_random' => 'boolean',
  46. 'random_count' => 'integer',
  47. 'reward_mode' => 'integer',
  48. ];
  49. /**
  50. * 获取奖励组中的所有奖励项
  51. *
  52. * @return HasMany
  53. */
  54. public function rewardItems(): HasMany
  55. {
  56. return $this->hasMany(GameRewardItem::class, 'group_id', 'id');
  57. }
  58. /**
  59. * 获取关联的标签(多态关联)
  60. *
  61. * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
  62. */
  63. public function tags()
  64. {
  65. return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id');
  66. }
  67. /**
  68. * 格式化奖励详情用于显示
  69. *
  70. * @return string
  71. */
  72. public function formatRewardDetails(): string
  73. {
  74. if ($this->rewardItems->isEmpty()) {
  75. return '<span class="text-muted">暂无奖励项</span>';
  76. }
  77. $details = [];
  78. foreach ($this->rewardItems as $item) {
  79. $detail = $this->formatSingleRewardItem($item);
  80. $details[] = $detail;
  81. }
  82. return '<div class="reward-details">' . implode('<br>', $details) . '</div>';
  83. }
  84. /**
  85. * 格式化标签用于显示
  86. *
  87. * @return string
  88. */
  89. public function formatTags(): string
  90. {
  91. if ($this->tags->isEmpty()) {
  92. return '<span class="text-muted">无标签</span>';
  93. }
  94. $tagHtml = [];
  95. foreach ($this->tags as $tag) {
  96. $tagHtml[] = $tag->formatTag();
  97. }
  98. return implode(' ', $tagHtml);
  99. }
  100. /**
  101. * 格式化单个奖励项
  102. *
  103. * @param GameRewardItem $item
  104. * @return string
  105. */
  106. private function formatSingleRewardItem(GameRewardItem $item): string
  107. {
  108. // 使用统一的奖励类型描述器
  109. $displayText = \App\Module\Game\Services\RewardTypeDescriptor::formatRewardDisplayFromModel($item, true);
  110. $weight = $item->weight;
  111. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  112. return sprintf(
  113. '%s (权重: %.2f, %s)',
  114. $displayText,
  115. $weight,
  116. $guaranteed
  117. );
  118. }
  119. }