ItemController.php 11 KB

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