ChestContentController.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Enums\ITEM_TYPE;
  4. use App\Module\GameItems\Models\ItemChestContent;
  5. use App\Module\GameItems\Models\ItemGroup;
  6. use Dcat\Admin\Form;
  7. use Dcat\Admin\Grid;
  8. use Dcat\Admin\Show;
  9. use UCore\DcatAdmin\AdminController;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\FilterHelper;
  12. use UCore\DcatAdmin\FormHelper;
  13. use UCore\DcatAdmin\GridHelper;
  14. use UCore\DcatAdmin\ShowHelper;
  15. #[Resource('game-items-chest-contents', names: 'dcat.admin.game-items-chest-contents')]
  16. class ChestContentController extends AdminController
  17. {
  18. /**
  19. * 标题
  20. *
  21. * @var string
  22. */
  23. protected $title = '宝箱内容管理';
  24. /**
  25. * 列表页
  26. *
  27. * @return Grid
  28. */
  29. protected function grid()
  30. {
  31. return Grid::make(new ItemChestContent(), function (Grid $grid) {
  32. $helper = new GridHelper($grid, $this);
  33. $grid->column('id', 'ID')->sortable();
  34. $grid->column('chest.name', '宝箱名称');
  35. $grid->column('item.name', '物品名称');
  36. $grid->column('group.name', '物品组名称');
  37. $grid->column('min_quantity', '最小数量');
  38. $grid->column('max_quantity', '最大数量');
  39. $grid->column('weight', '权重');
  40. $grid->column('allow_duplicate', '允许重复')->bool();
  41. $grid->column('pity_count', '保底次数');
  42. $grid->column('pity_weight_factor', '保底权重因子');
  43. $grid->column('created_at', '创建时间');
  44. $grid->column('updated_at', '更新时间');
  45. // 筛选
  46. $grid->filter(function ($filter) {
  47. $helper = new FilterHelper($filter, $this);
  48. $helper->equal('id','ID');
  49. $filter->equal('chest_id', '宝箱')->select(
  50. ItemItem::where('type', ITEM_TYPE::OPENABLE)->pluck('name', 'id')
  51. );
  52. $filter->equal('item_id', '物品')->select(
  53. ItemItem::pluck('name', 'id')
  54. );
  55. $filter->equal('group_id', '物品组')->select(
  56. ItemGroup::pluck('name', 'id')
  57. );
  58. });
  59. return $grid;
  60. });
  61. }
  62. /**
  63. * 详情页
  64. *
  65. * @param mixed $id
  66. * @return Show
  67. */
  68. protected function detail($id)
  69. {
  70. return Show::make($id, new ItemChestContent(), function (Show $show) {
  71. $helper = new ShowHelper($show, $this);
  72. $helper->field('id','ID');
  73. $show->field('chest.name', '宝箱名称');
  74. $show->field('item.name', '物品名称');
  75. $show->field('group.name', '物品组名称');
  76. $show->field('min_quantity', '最小数量');
  77. $show->field('max_quantity', '最大数量');
  78. $show->field('weight', '权重');
  79. $show->field('allow_duplicate', '允许重复')->as(function ($allowDuplicate) {
  80. return $allowDuplicate ? '是' : '否';
  81. });
  82. $show->field('pity_count', '保底次数');
  83. $show->field('pity_weight_factor', '保底权重因子');
  84. $show->field('created_at', '创建时间');
  85. $show->field('updated_at', '更新时间');
  86. return $show;
  87. });
  88. }
  89. /**
  90. * 表单
  91. *
  92. * @return Form
  93. */
  94. protected function form()
  95. {
  96. return Form::make(new ItemChestContent(), function (Form $form) {
  97. $helper = new FormHelper($form, $this);
  98. $form->select('chest_id', '宝箱')
  99. ->options(ItemItem::where('type', ITEM_TYPE::OPENABLE)->pluck('name', 'id'))
  100. ->required();
  101. // 物品和物品组二选一
  102. $form->radio('content_type', '内容类型')
  103. ->options(['item' => '物品', 'group' => '物品组'])
  104. ->default('item')
  105. ->when('item', function (Form $form) {
  106. $form->select('item_id', '物品')
  107. ->options(ItemItem::pluck('name', 'id'))
  108. ->required();
  109. })
  110. ->when('group', function (Form $form) {
  111. $form->select('group_id', '物品组')
  112. ->options(ItemGroup::pluck('name', 'id'))
  113. ->required();
  114. });
  115. $form->number('min_quantity', '最小数量')
  116. ->default(1)
  117. ->min(1)
  118. ->required();
  119. $form->number('max_quantity', '最大数量')
  120. ->default(1)
  121. ->min(1)
  122. ->required();
  123. $form->number('weight', '权重')
  124. ->default(1.0)
  125. ->min(0.001)
  126. ->step(0.001)
  127. ->required()
  128. ->help('权重越高,掉落概率越大,所有内容权重总和为100');
  129. $form->switch('allow_duplicate', '允许重复')
  130. ->default(false)
  131. ->help('是否允许在一次开箱中重复获得该内容');
  132. $form->number('pity_count', '保底次数')
  133. ->default(0)
  134. ->min(0)
  135. ->help('连续未获得该内容的次数达到此值时,必定获得该内容,0表示不启用保底机制');
  136. $form->number('pity_weight_factor', '保底权重因子')
  137. ->default(1.0)
  138. ->min(0)
  139. ->step(0.1)
  140. ->help('每次未获得该内容时,权重增加的倍数,默认为1.0');
  141. // 保存前回调
  142. $form->saving(function (Form $form) {
  143. // 根据内容类型设置对应的字段
  144. if ($form->content_type == 'item') {
  145. $form->group_id = null;
  146. } else {
  147. $form->item_id = null;
  148. }
  149. });
  150. return $form;
  151. });
  152. }
  153. }