ItemController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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('display_attributes', '展示属性')->display(function ($value) {
  61. if (empty($value)) {
  62. return '-';
  63. }
  64. if (is_string($value)) {
  65. $value = json_decode($value, true);
  66. }
  67. $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
  68. foreach ((array)$value as $key => $val) {
  69. if (empty($val)) continue;
  70. $html .= "<div><strong>{$key}</strong>: {$val}</div>";
  71. }
  72. $html .= '</div>';
  73. return $html;
  74. });
  75. // 添加数值属性列,只显示有效的属性(非0值),并使用注释作为键名
  76. $grid->column('numeric_attributes', '数值属性')->display(function ($value) {
  77. if (empty($value)) {
  78. return '-';
  79. }
  80. if (is_string($value)) {
  81. $value = json_decode($value, true);
  82. }
  83. // 属性名到注释的映射
  84. $attributeNames = [
  85. 'min_drop_count' => '宝箱最小数量',
  86. 'max_drop_count' => '宝箱最大数量',
  87. 'crop_growth_time' => '减少作物生长时间',
  88. 'pet_power' => '增加宠物体力',
  89. 'reward_group_id' => '随机奖励物品组',
  90. 'pet_exp' => '增加宠物经验',
  91. 'fram_pesticide_rate' => '除虫概率(%)',
  92. 'fram_drought_rate' => '解决干旱概率(%)',
  93. 'fram_weedicide_rate' => '除草概率(%)'
  94. ];
  95. $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
  96. foreach ((array)$value as $key => $val) {
  97. if (empty($val) || $val === 0) continue; // 过滤掉空值和0值
  98. $name = $attributeNames[$key] ?? $key;
  99. $html .= "<div><strong>{$name}</strong>: {$val}</div>";
  100. }
  101. $html .= '</div>';
  102. return $html;
  103. });
  104. $grid->column('is_unique', '单独属性')->bool();
  105. $grid->column('max_stack', '最大堆叠');
  106. $grid->column('tradable', '可交易')->bool();
  107. $grid->column('dismantlable', '可分解')->bool();
  108. $grid->column('default_expire_seconds', '默认过期时间(秒)');
  109. $grid->column('global_expire_at', '全局过期时间');
  110. $grid->column('created_at', '创建时间');
  111. $grid->column('updated_at', '更新时间');
  112. // 添加行操作
  113. $grid->actions(function (Grid\Displayers\Actions $actions) {
  114. $actions->disableDelete();
  115. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
  116. // 如果是宝箱类型,添加宝箱管理按钮
  117. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestManageAction());
  118. $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestCostAction());
  119. });
  120. // 筛选
  121. $grid->filter(function ($filter) {
  122. $helper = new FilterHelper($filter, $this);
  123. $helper->equal('id', 'ID');
  124. $filter->like('name', '名称');
  125. $filter->equal('category_id', '分类')->select(
  126. ItemCategory::pluck('name', 'id')
  127. );
  128. $helper->equalRadioModelCats('type', '类型');
  129. $filter->equal('is_unique', '单独属性')->radio([
  130. 1 => '是',
  131. 0 => '否',
  132. ]);
  133. $filter->equal('tradable', '可交易')->radio([
  134. 1 => '是',
  135. 0 => '否',
  136. ]);
  137. $filter->equal('dismantlable', '可分解')->radio([
  138. 1 => '是',
  139. 0 => '否',
  140. ]);
  141. });
  142. return $grid;
  143. });
  144. }
  145. /**
  146. * 详情页
  147. *
  148. * @param mixed $id
  149. * @return Show
  150. */
  151. protected function detail($id)
  152. {
  153. return Show::make($id, new ItemRepository(), function (Show $show) {
  154. $helper = new ShowHelper($show, $this);
  155. $helper->field('id', 'ID');
  156. $show->field('name', '名称');
  157. $show->field('description', '描述');
  158. $show->field('category.name', '分类');
  159. $helper->fieldModelCats('type');
  160. $show->field('display_attributes', '展示属性')->unescape()->as(function ($value) {
  161. if (empty($value)) {
  162. return '无';
  163. }
  164. if (is_string($value)) {
  165. $value = json_decode($value, true);
  166. }
  167. $html = '<table class="table table-bordered">';
  168. $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
  169. $html .= '<tbody>';
  170. foreach ((array)$value as $key => $val) {
  171. if (empty($val)) continue;
  172. $html .= '<tr>';
  173. $html .= '<td>' . htmlspecialchars($key) . '</td>';
  174. $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
  175. $html .= '</tr>';
  176. }
  177. $html .= '</tbody></table>';
  178. return $html;
  179. });
  180. $show->field('is_unique', '单独属性')->as(function ($isUnique) {
  181. return $isUnique ? '是' : '否';
  182. });
  183. $show->field('max_stack', '最大堆叠');
  184. $show->field('sell_price', '出售价格');
  185. $show->field('tradable', '可交易')->as(function ($tradable) {
  186. return $tradable ? '是' : '否';
  187. });
  188. $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
  189. return $dismantlable ? '是' : '否';
  190. });
  191. $show->field('default_expire_seconds', '默认过期时间(秒)');
  192. $helper->fieldModelCatsJson('display_attributes', '显示属性');
  193. $helper->fieldModelCatsJson('numeric_attributes', '数值属性');
  194. $show->field('global_expire_at', '全局过期时间');
  195. $show->field('created_at', '创建时间');
  196. $show->field('updated_at', '更新时间');
  197. $show->divider();
  198. // 如果是宝箱类型,显示宝箱内容
  199. $show->relation('chest_contents', '宝箱内容',
  200. function (\App\Module\GameItems\Models\Item $item) {
  201. // dd($item);
  202. $grid = new Grid(new ItemChestContentRepository([ 'chest', 'item', 'group' ]));
  203. $grid->model()->where('chest_id', $item->id);
  204. // 设置路由
  205. $grid->setResource('game-items-chest-contents');
  206. $grid->id();
  207. $grid->column('chest.name', '宝箱名称');
  208. $grid->column('item.name', '物品名称');
  209. $grid->column('group.name', '物品组名称');
  210. $grid->column('min_quantity', '最小数量');
  211. $grid->column('max_quantity', '最大数量');
  212. $grid->column('weight', '权重');
  213. $grid->disableActions();
  214. return $grid;
  215. });
  216. $show->relation('chest_costs', '宝箱消耗',
  217. function (\App\Module\GameItems\Models\Item $item) {
  218. // dd($item);
  219. $grid = new Grid(new ItemChestOpenCostRepository(['costItem']));
  220. $grid->model()->where('chest_id', $item->id);
  221. // 设置路由
  222. $grid->setResource('game-items-chest-costs');
  223. $grid->id();
  224. $grid->column('costItem.name', '物品名称');
  225. $grid->disableActions();
  226. return $grid;
  227. });
  228. return $show;
  229. });
  230. }
  231. /**
  232. * 表单
  233. *
  234. * @return Form
  235. */
  236. protected function form()
  237. {
  238. return Form::make(new ItemRepository(), function (Form $form) {
  239. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  240. $helper->text('name')->required();
  241. $form->textarea('description', '描述');
  242. $form->select('category_id', '分类')
  243. ->options(ItemCategory::pluck('name', 'id'))
  244. ->required();
  245. $helper->selectOptionCast('type', '类型');
  246. $form->switch('is_unique', '单独属性')
  247. ->default(false);
  248. $form->number('max_stack', '最大堆叠')
  249. ->default(1)
  250. ->min(1)
  251. ->help('0表示无限堆叠');
  252. $form->number('sell_price', '出售价格')
  253. ->default(0)
  254. ->min(0);
  255. $form->switch('tradable', '可交易')
  256. ->default(true);
  257. $form->switch('dismantlable', '可分解')
  258. ->default(true);
  259. $form->number('default_expire_seconds', '默认过期时间(秒)')
  260. ->default(0)
  261. ->help('0表示永不过期');
  262. $helper->embedsCats('display_attributes', '显示属性')
  263. ->help('用于显示的属性,如:攻击力、防御力等');
  264. $helper->embedsCats('numeric_attributes', '数值属性')
  265. ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
  266. $form->datetime('global_expire_at', '全局过期时间')
  267. ->help('所有该物品的全局过期时间,为空表示永不过期');
  268. // 保存前回调
  269. $form->saving(function (Form $form) {
  270. // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
  271. if ($form->type == ITEM_TYPE::CHEST) {
  272. $numericAttributes = $form->numeric_attributes ?: [];
  273. if (!isset($numericAttributes['min_drop_count'])) {
  274. $numericAttributes['min_drop_count'] = 1;
  275. }
  276. if (!isset($numericAttributes['max_drop_count'])) {
  277. $numericAttributes['max_drop_count'] = 1;
  278. }
  279. $form->numeric_attributes = $numericAttributes;
  280. }
  281. });
  282. return $form;
  283. });
  284. }
  285. }