ChestContentController.php 5.1 KB

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