ChestContentController.php 5.5 KB

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