ChestContentController.php 6.3 KB

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