ItemController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\Game\DCache\ItemJsonConfig;
  4. use App\Module\GameItems\Enums\ITEM_TYPE;
  5. use App\Module\GameItems\Models\ItemCategory;
  6. use App\Module\GameItems\Models\ItemChestOpenCost;
  7. use App\Module\GameItems\Repositorys\ItemChestContentRepository;
  8. use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
  9. use App\Module\GameItems\Repositorys\ItemRepository;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use UCore\DcatAdmin\AdminController;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. use Spatie\RouteAttributes\Attributes\Get;
  16. use Illuminate\Support\Facades\Artisan;
  17. use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
  18. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  19. use App\Module\GameItems\AdminControllers\Helper\GridHelper;
  20. use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
  21. use App\Module\GameItems\AdminControllers\Actions\ChestManageAction;
  22. use App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction;
  23. /**
  24. * 物品管理控制器
  25. *
  26. * @package App\Module\GameItems\AdminControllers
  27. */
  28. #[Resource('game-items', names: 'dcat.admin.game-items')]
  29. class ItemController extends AdminController
  30. {
  31. /**
  32. * 标题
  33. *
  34. * @var string
  35. */
  36. protected $title = '物品管理';
  37. /**
  38. * 列表页
  39. *
  40. * @return Grid
  41. */
  42. protected function grid()
  43. {
  44. return Grid::make(new ItemRepository([ 'category' ]), function (Grid $grid) {
  45. $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
  46. if ($status['is_synced']) {
  47. admin_success('JSON配置表状态', $status['message']);
  48. } else {
  49. admin_warning('JSON配置表状态', $status['message']);
  50. }
  51. $grid->tools([
  52. new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
  53. new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
  54. ]);
  55. $helper = new GridHelper($grid, $this);
  56. $grid->column('id', 'ID')->sortable();
  57. $grid->column('name', '名称');
  58. $grid->column('category.name', '分类');
  59. $helper->columnModelCats('type');
  60. $grid->column('is_unique', '单独属性')->bool();
  61. $grid->column('max_stack', '最大堆叠');
  62. $grid->column('tradable', '可交易')->bool();
  63. $grid->column('dismantlable', '可分解')->bool();
  64. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  65. $grid->column('global_expire_at', '全局过期时间');
  66. $grid->column('created_at', '创建时间');
  67. $grid->column('updated_at', '更新时间');
  68. // 添加行操作
  69. $grid->actions(function (Grid\Displayers\Actions $actions) {
  70. $actions->disableDelete();
  71. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
  72. // 如果是宝箱类型,添加宝箱管理按钮
  73. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestManageAction());
  74. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestCostAction());
  75. });
  76. // 筛选
  77. $grid->filter(function ($filter) {
  78. $helper = new FilterHelper($filter, $this);
  79. $helper->equal('id', 'ID');
  80. $filter->like('name', '名称');
  81. $filter->equal('category_id', '分类')->select(
  82. ItemCategory::pluck('name', 'id')
  83. );
  84. $helper->equalRadioModelCats('type', '类型');
  85. $filter->equal('is_unique', '单独属性')->radio([
  86. 1 => '是',
  87. 0 => '否',
  88. ]);
  89. $filter->equal('tradable', '可交易')->radio([
  90. 1 => '是',
  91. 0 => '否',
  92. ]);
  93. $filter->equal('dismantlable', '可分解')->radio([
  94. 1 => '是',
  95. 0 => '否',
  96. ]);
  97. });
  98. return $grid;
  99. });
  100. }
  101. /**
  102. * 详情页
  103. *
  104. * @param mixed $id
  105. * @return Show
  106. */
  107. protected function detail($id)
  108. {
  109. return Show::make($id, new ItemRepository(), function (Show $show) {
  110. $helper = new ShowHelper($show, $this);
  111. $helper->field('id', 'ID');
  112. $show->field('name', '名称');
  113. $show->field('description', '描述');
  114. $show->field('category.name', '分类');
  115. $helper->fieldModelCats('type');
  116. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  117. return $isUnique ? '是' : '否';
  118. });
  119. $show->field('max_stack', '最大堆叠');
  120. $show->field('sell_price', '出售价格');
  121. $show->field('tradable', '可交易')->as(function ($tradable) {
  122. return $tradable ? '是' : '否';
  123. });
  124. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  125. return $dismantlable ? '是' : '否';
  126. });
  127. $show->field('default_expire_seconds', '默认过期时间(秒)');
  128. $helper->fieldModelCatsJson('display_attributes', '显示属性');
  129. $helper->fieldModelCatsJson('numeric_attributes', '数值属性');
  130. $show->field('global_expire_at', '全局过期时间');
  131. $show->field('created_at', '创建时间');
  132. $show->field('updated_at', '更新时间');
  133. $show->divider();
  134. // 如果是宝箱类型,显示宝箱内容
  135. $show->relation('chest_contents', '宝箱内容',
  136. function (\App\Module\GameItems\Models\Item $item) {
  137. // dd($item);
  138. $grid = new Grid(new ItemChestContentRepository([ 'chest', 'item', 'group' ]));
  139. $grid->model()->where('chest_id', $item->id);
  140. // 设置路由
  141. $grid->setResource('game-items-chest-contents');
  142. $grid->id();
  143. $grid->column('chest.name', '宝箱名称');
  144. $grid->column('item.name', '物品名称');
  145. $grid->column('group.name', '物品组名称');
  146. $grid->column('min_quantity', '最小数量');
  147. $grid->column('max_quantity', '最大数量');
  148. $grid->column('weight', '权重');
  149. $grid->disableActions();
  150. return $grid;
  151. });
  152. $show->relation('chest_costs', '宝箱消耗',
  153. function (\App\Module\GameItems\Models\Item $item) {
  154. // dd($item);
  155. $grid = new Grid(new ItemChestOpenCostRepository(['costItem']));
  156. $grid->model()->where('chest_id', $item->id);
  157. // 设置路由
  158. $grid->setResource('game-items-chest-costs');
  159. $grid->id();
  160. $grid->column('costItem.name', '物品名称');
  161. $grid->disableActions();
  162. return $grid;
  163. });
  164. return $show;
  165. });
  166. }
  167. /**
  168. * 表单
  169. *
  170. * @return Form
  171. */
  172. protected function form()
  173. {
  174. return Form::make(new ItemRepository(), function (Form $form) {
  175. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  176. $helper->text('name')->required();
  177. $form->textarea('description', '描述');
  178. $form->select('category_id', '分类')
  179. ->options(ItemCategory::pluck('name', 'id'))
  180. ->required();
  181. $helper->selectOptionCast('type', '类型');
  182. $form->switch('is_unique', '单独属性')
  183. ->default(false);
  184. $form->number('max_stack', '最大堆叠')
  185. ->default(1)
  186. ->min(1)
  187. ->help('0表示无限堆叠');
  188. $form->number('sell_price', '出售价格')
  189. ->default(0)
  190. ->min(0);
  191. $form->switch('tradable', '可交易')
  192. ->default(true);
  193. $form->switch('dismantlable', '可分解')
  194. ->default(true);
  195. $form->number('default_expire_seconds', '默认过期时间(秒)')
  196. ->default(0)
  197. ->help('0表示永不过期');
  198. $helper->embedsCats('display_attributes', '显示属性')
  199. ->help('用于显示的属性,如:攻击力、防御力等');
  200. $helper->embedsCats('numeric_attributes', '数值属性')
  201. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  202. $form->datetime('global_expire_at', '全局过期时间')
  203. ->help('所有该物品的全局过期时间,为空表示永不过期');
  204. // 保存前回调
  205. $form->saving(function (Form $form) {
  206. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  207. if ($form->type == ITEM_TYPE::CHEST) {
  208. $numericAttributes = $form->numeric_attributes ?: [];
  209. if (!isset($numericAttributes['min_drop_count'])) {
  210. $numericAttributes['min_drop_count'] = 1;
  211. }
  212. if (!isset($numericAttributes['max_drop_count'])) {
  213. $numericAttributes['max_drop_count'] = 1;
  214. }
  215. $form->numeric_attributes = $numericAttributes;
  216. }
  217. });
  218. return $form;
  219. });
  220. }
  221. }