GameRewardGroup.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item->reward_type);
  109. $targetName = $this->getTargetName($item);
  110. $quantity = $item->quantity;
  111. $weight = $item->weight;
  112. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  113. return sprintf(
  114. '<span class="badge badge-info">%s</span> %s × %d (权重: %.2f, %s)',
  115. $rewardTypeName,
  116. $targetName,
  117. $quantity,
  118. $weight,
  119. $guaranteed
  120. );
  121. }
  122. /**
  123. * 获取目标名称
  124. *
  125. * @param GameRewardItem $item
  126. * @return string
  127. */
  128. private function getTargetName(GameRewardItem $item): string
  129. {
  130. // dump($item);
  131. switch ($item->reward_type) {
  132. case \App\Module\Game\Enums\REWARD_TYPE::ITEM->value:
  133. try {
  134. $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
  135. return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
  136. } catch (\Exception $e) {
  137. return "物品 (ID: {$item->target_id})";
  138. }
  139. case \App\Module\Game\Enums\REWARD_TYPE::CURRENCY->value:
  140. try {
  141. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  142. return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
  143. } catch (\Exception $e) {
  144. return "货币 (ID: {$item->target_id})";
  145. }
  146. case \App\Module\Game\Enums\REWARD_TYPE::FUND_CONFIG->value:
  147. try {
  148. $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  149. return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
  150. } catch (\Exception $e) {
  151. return "账户种类 (ID: {$item->target_id})";
  152. }
  153. case \App\Module\Game\Enums\REWARD_TYPE::PET_EXP->value:
  154. return "宠物经验 (宠物ID: {$item->target_id})";
  155. case \App\Module\Game\Enums\REWARD_TYPE::PET_ENERGY->value:
  156. return "宠物体力 (宠物ID: {$item->target_id})";
  157. case \App\Module\Game\Enums\REWARD_TYPE::PET->value:
  158. $pet = \App\Module\Pet\Models\PetConfig::find($item->target_id);
  159. return "宠物 (宠物: {$pet->name})";
  160. case \App\Module\Game\Enums\REWARD_TYPE::FARM_SHRINE->value:
  161. try {
  162. $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
  163. $durationHours = $item->param2 > 0 ? $item->param2 : 24;
  164. return "{$shrineName} ({$durationHours}小时)";
  165. } catch (\Exception $e) {
  166. return "神像 (类型: {$item->target_id})";
  167. }
  168. case \App\Module\Game\Enums\REWARD_TYPE::OTHER->value:
  169. return "其他奖励 (ID: {$item->target_id})";
  170. default:
  171. return "未知奖励类型 (ID: {$item->target_id})";
  172. }
  173. }
  174. }