GameRewardGroup.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. *
  10. * @property int $id 主键
  11. * @property string $name 奖励组名称
  12. * @property string $code 奖励组编码(唯一)
  13. * @property string $description 奖励组描述
  14. * @property bool $is_random 是否随机发放(0:全部发放, 1:随机发放)
  15. * @property int $random_count 随机发放时的奖励数量
  16. * @property int $reward_mode 奖励模式(1:权重选择模式, 2:独立概率模式)
  17. * @property \Carbon\Carbon $created_at 创建时间
  18. * @property \Carbon\Carbon $updated_at 更新时间
  19. * field end
  20. */
  21. class GameRewardGroup extends ModelCore
  22. {
  23. /**
  24. * 与模型关联的表名
  25. *
  26. * @var string
  27. */
  28. protected $table = 'game_reward_groups';
  29. // attrlist start
  30. protected $fillable = [
  31. 'id',
  32. 'name',
  33. 'code',
  34. 'description',
  35. 'is_random',
  36. 'random_count',
  37. 'reward_mode',
  38. ];
  39. // attrlist end
  40. /**
  41. * 应该被转换为原生类型的属性
  42. *
  43. * @var array
  44. */
  45. protected $casts = [
  46. 'is_random' => 'boolean',
  47. 'random_count' => 'integer',
  48. 'reward_mode' => 'integer',
  49. ];
  50. /**
  51. * 获取奖励组中的所有奖励项
  52. *
  53. * @return HasMany
  54. */
  55. public function rewardItems(): HasMany
  56. {
  57. return $this->hasMany(GameRewardItem::class, 'group_id', 'id');
  58. }
  59. /**
  60. * 获取关联的标签(多态关联)
  61. *
  62. * @return \Illuminate\Database\Eloquent\Relations\MorphToMany
  63. */
  64. public function tags()
  65. {
  66. return $this->morphToMany(GameTag::class, 'taggable', 'game_tag_relations', 'taggable_id', 'tag_id');
  67. }
  68. /**
  69. * 格式化奖励详情用于显示
  70. *
  71. * @return string
  72. */
  73. public function formatRewardDetails(): string
  74. {
  75. if ($this->rewardItems->isEmpty()) {
  76. return '<span class="text-muted">暂无奖励项</span>';
  77. }
  78. $details = [];
  79. foreach ($this->rewardItems as $item) {
  80. $detail = $this->formatSingleRewardItem($item);
  81. $details[] = $detail;
  82. }
  83. return '<div class="reward-details">' . implode('<br>', $details) . '</div>';
  84. }
  85. /**
  86. * 格式化标签用于显示
  87. *
  88. * @return string
  89. */
  90. public function formatTags(): string
  91. {
  92. if ($this->tags->isEmpty()) {
  93. return '<span class="text-muted">无标签</span>';
  94. }
  95. $tagHtml = [];
  96. foreach ($this->tags as $tag) {
  97. $tagHtml[] = $tag->formatTag();
  98. }
  99. return implode(' ', $tagHtml);
  100. }
  101. /**
  102. * 格式化单个奖励项
  103. *
  104. * @param GameRewardItem $item
  105. * @return string
  106. */
  107. private function formatSingleRewardItem(GameRewardItem $item): string
  108. {
  109. $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item->reward_type);
  110. $targetName = $this->getTargetName($item);
  111. // 处理随机数量显示
  112. $quantityText = $this->formatQuantityText($item);
  113. $weight = $item->weight;
  114. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  115. return sprintf(
  116. '<span class="badge badge-info">%s</span> %s × %s (权重: %.2f, %s)',
  117. $rewardTypeName,
  118. $targetName,
  119. $quantityText,
  120. $weight,
  121. $guaranteed
  122. );
  123. }
  124. /**
  125. * 格式化数量文本
  126. *
  127. * @param GameRewardItem $item
  128. * @return string
  129. */
  130. private function formatQuantityText(GameRewardItem $item): string
  131. {
  132. // 如果设置了最小和最大数量,显示范围
  133. if ($item->min_quantity > 0 && $item->max_quantity > 0) {
  134. if ($item->min_quantity == $item->max_quantity) {
  135. return (string)$item->min_quantity;
  136. } else {
  137. return $item->min_quantity . '-' . $item->max_quantity;
  138. }
  139. }
  140. if( $item->min_quantity >0 || $item->max_quantity > 0){
  141. // 如果只设置了最小数量
  142. if ($item->min_quantity == 0) {
  143. return ' 数量错误 ,缺少 最大值';
  144. }
  145. // 如果只设置了最大数量
  146. if ($item->max_quantity == 0) {
  147. return ' 数量错误 ,缺少 最小值';
  148. }
  149. }
  150. // 默认使用固定数量
  151. return (string)$item->quantity;
  152. }
  153. /**
  154. * 获取目标名称
  155. *
  156. * @param GameRewardItem $item
  157. * @return string
  158. */
  159. private function getTargetName(GameRewardItem $item): string
  160. {
  161. // dump($item);
  162. switch ($item->reward_type) {
  163. case \App\Module\Game\Enums\REWARD_TYPE::ITEM->value:
  164. try {
  165. $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
  166. return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
  167. } catch (\Exception $e) {
  168. return "物品 (ID: {$item->target_id})";
  169. }
  170. case \App\Module\Game\Enums\REWARD_TYPE::CURRENCY->value:
  171. try {
  172. $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
  173. return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
  174. } catch (\Exception $e) {
  175. return "货币 (ID: {$item->target_id})";
  176. }
  177. case \App\Module\Game\Enums\REWARD_TYPE::FUND_CONFIG->value:
  178. try {
  179. $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
  180. return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
  181. } catch (\Exception $e) {
  182. return "账户种类 (ID: {$item->target_id})";
  183. }
  184. case \App\Module\Game\Enums\REWARD_TYPE::PET_EXP->value:
  185. return "宠物经验 (宠物ID: {$item->target_id})";
  186. case \App\Module\Game\Enums\REWARD_TYPE::PET_ENERGY->value:
  187. return "宠物体力 (宠物ID: {$item->target_id})";
  188. case \App\Module\Game\Enums\REWARD_TYPE::PET->value:
  189. $pet = \App\Module\Pet\Models\PetConfig::find($item->target_id);
  190. return "宠物 (宠物: {$pet->name})";
  191. case \App\Module\Game\Enums\REWARD_TYPE::FARM_SHRINE->value:
  192. try {
  193. $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
  194. $durationHours = $item->param2 > 0 ? $item->param2 : 24;
  195. return "{$shrineName} ({$durationHours}小时)";
  196. } catch (\Exception $e) {
  197. return "神像 (类型: {$item->target_id})";
  198. }
  199. case \App\Module\Game\Enums\REWARD_TYPE::OTHER->value:
  200. return "其他奖励 (ID: {$item->target_id})";
  201. default:
  202. return "未知奖励类型 (ID: {$item->target_id})";
  203. }
  204. }
  205. }