RecipeController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Module\GameItems\AdminControllers;
  3. use App\Module\GameItems\Repositorys\ItemRecipeRepository;
  4. use App\Module\GameItems\Repositorys\ItemRepository;
  5. use Dcat\Admin\Form;
  6. use Dcat\Admin\Grid;
  7. use Dcat\Admin\Show;
  8. use UCore\DcatAdmin\AdminController;
  9. use Dcat\Admin\Layout\Content;
  10. use Spatie\RouteAttributes\Attributes\Resource;
  11. use UCore\DcatAdmin\FilterHelper;
  12. use App\Module\GameItems\AdminControllers\Helper\FormHelper;
  13. use UCore\DcatAdmin\GridHelper;
  14. use UCore\DcatAdmin\ShowHelper;
  15. /**
  16. * 合成配方管理控制器
  17. *
  18. * @package App\Module\GameItems\AdminControllers
  19. */
  20. #[Resource('game-items-recipes', names: 'dcat.admin.game-items-recipes')]
  21. class RecipeController extends AdminController
  22. {
  23. /**
  24. * 标题
  25. *
  26. * @var string
  27. */
  28. protected $title = '合成配方管理';
  29. /**
  30. * 列表页
  31. *
  32. * @return Grid
  33. */
  34. protected function grid()
  35. {
  36. return Grid::make(new ItemRecipeRepository(), function (Grid $grid) {
  37. $helper = new GridHelper($grid, $this);
  38. $helper->columnId();
  39. $grid->column('name', '配方名称');
  40. $grid->column('resultItem.name', '产出物品');
  41. $grid->column('result_quantity', '产出数量');
  42. $grid->column('success_rate', '成功率')->display(function ($value) {
  43. return $value * 100 . '%';
  44. });
  45. $grid->column('materials', '材料数量')->display(function ($materials) {
  46. return count($materials);
  47. });
  48. $grid->column('coin_cost', '金币消耗')->display(function ($value) {
  49. if (empty($value)) {
  50. return '0';
  51. }
  52. if (is_string($value)) {
  53. $value = json_decode($value, true);
  54. }
  55. if (is_array($value)) {
  56. $result = [];
  57. foreach ($value as $currency => $amount) {
  58. $result[] = $currency . ': ' . $amount;
  59. }
  60. return implode(', ', $result);
  61. }
  62. return $value;
  63. });
  64. $grid->column('cooldown_seconds', '冷却时间(秒)');
  65. $grid->column('is_visible', '是否可见')->switch();
  66. $grid->column('created_at', '创建时间');
  67. $grid->column('updated_at', '更新时间');
  68. // 筛选
  69. $grid->filter(function ($filter) {
  70. $helper = new FilterHelper($filter, $this);
  71. $helper->equal('id', 'ID');
  72. $helper->like('name', '配方名称');
  73. $filter->equal('result_item_id', '产出物品')->select(
  74. (new ItemRepository())->pluck('name', 'id')
  75. );
  76. $filter->equal('is_visible', '是否可见')->radio([
  77. 1 => '是',
  78. 0 => '否',
  79. ]);
  80. });
  81. });
  82. }
  83. /**
  84. * 详情页
  85. *
  86. * @param mixed $id
  87. * @param Content $content
  88. * @return Content
  89. */
  90. public function show($id, Content $content)
  91. {
  92. return $content
  93. ->header($this->title)
  94. ->description('详情')
  95. ->body($this->detail($id));
  96. }
  97. /**
  98. * 详情页
  99. *
  100. * @param mixed $id
  101. * @return Show
  102. */
  103. protected function detail($id)
  104. {
  105. return Show::make($id, new ItemRecipeRepository(), function (Show $show) {
  106. $helper = new ShowHelper($show, $this);
  107. $helper->field('id', 'ID');
  108. $helper->field('name', '配方名称');
  109. $show->field('resultItem.name', '产出物品');
  110. $helper->field('result_quantity', '产出数量');
  111. $show->field('success_rate', '成功率')->as(function ($value) {
  112. return $value * 100 . '%';
  113. });
  114. // 显示金币消耗
  115. $show->field('coin_cost', '金币消耗')->as(function ($value) {
  116. if (empty($value)) {
  117. return '0';
  118. }
  119. if (is_string($value)) {
  120. $value = json_decode($value, true);
  121. }
  122. if (is_array($value)) {
  123. $result = [];
  124. foreach ($value as $currency => $amount) {
  125. $result[] = $currency . ': ' . $amount;
  126. }
  127. return implode('<br>', $result);
  128. }
  129. return $value;
  130. })->unescape();
  131. $helper->field('cooldown_seconds', '冷却时间(秒)');
  132. $show->field('is_visible', '是否可见')->as(function ($value) {
  133. return $value ? '是' : '否';
  134. });
  135. // 显示解锁条件
  136. $show->field('unlock_condition', '解锁条件')->as(function ($value) {
  137. if (empty($value)) {
  138. return '无';
  139. }
  140. if (is_string($value)) {
  141. $value = json_decode($value, true);
  142. }
  143. if (is_array($value)) {
  144. $result = [];
  145. foreach ($value as $condition => $requirement) {
  146. $result[] = $condition . ': ' . $requirement;
  147. }
  148. return implode('<br>', $result);
  149. }
  150. return $value;
  151. })->unescape();
  152. $helper->field('created_at', '创建时间');
  153. $helper->field('updated_at', '更新时间');
  154. // 显示配方材料
  155. $show->divider('配方材料');
  156. $show->field('materials', '材料列表')->as(function ($materials) {
  157. $html = '<table class="table table-bordered">';
  158. $html .= '<thead><tr><th>物品名称</th><th>数量</th><th>是否消耗</th></tr></thead>';
  159. $html .= '<tbody>';
  160. foreach ($materials as $material) {
  161. $html .= '<tr>';
  162. $html .= '<td>' . $material->item->name . '</td>';
  163. $html .= '<td>' . $material->quantity . '</td>';
  164. $html .= '<td>' . ($material->is_consumed ? '是' : '否') . '</td>';
  165. $html .= '</tr>';
  166. }
  167. $html .= '</tbody></table>';
  168. return $html;
  169. })->unescape();
  170. });
  171. }
  172. /**
  173. * 创建页
  174. *
  175. * @param Content $content
  176. * @return Content
  177. */
  178. public function create(Content $content)
  179. {
  180. return $content
  181. ->header($this->title)
  182. ->description('创建')
  183. ->body($this->form());
  184. }
  185. /**
  186. * 编辑页
  187. *
  188. * @param mixed $id
  189. * @param Content $content
  190. * @return Content
  191. */
  192. public function edit($id, Content $content)
  193. {
  194. return $content
  195. ->header($this->title)
  196. ->description('编辑')
  197. ->body($this->form()->edit($id));
  198. }
  199. /**
  200. * 表单
  201. *
  202. * @return Form
  203. */
  204. protected function form()
  205. {
  206. return Form::make(new ItemRecipeRepository(), function (Form $form) {
  207. $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
  208. $helper->text('name', '配方名称')->required();
  209. $form->select('result_item_id', '产出物品')
  210. ->options((new ItemRepository())->pluck('name', 'id'))
  211. ->required();
  212. $form->number('result_quantity', '产出数量')
  213. ->default(1)
  214. ->min(1)
  215. ->required();
  216. $form->rate('success_rate', '成功率')
  217. ->default(1)
  218. ->help('合成成功的概率,1表示100%');
  219. // 金币消耗
  220. $form->keyValue('coin_cost', '金币消耗')
  221. ->help('可以设置多种货币类型的消耗,如:gold:100表示消耗100金币');
  222. $form->number('cooldown_seconds', '冷却时间(秒)')
  223. ->default(0)
  224. ->min(0)
  225. ->help('两次合成之间的冷却时间,0表示无冷却');
  226. $form->switch('is_visible', '是否可见')
  227. ->default(true)
  228. ->help('是否在游戏中对玩家可见');
  229. // 解锁条件
  230. $form->keyValue('unlock_condition', '解锁条件')
  231. ->help('设置解锁该配方的条件,如:level:10表示玩家等级达到10级');
  232. // 配方材料
  233. $form->hasMany('materials', '配方材料', function (Form\NestedForm $form) {
  234. $form->select('item_id', '物品')
  235. ->options((new ItemRepository())->pluck('name', 'id'))
  236. ->required();
  237. $form->number('quantity', '数量')
  238. ->default(1)
  239. ->min(1)
  240. ->required();
  241. $form->switch('is_consumed', '是否消耗')
  242. ->default(true)
  243. ->help('合成时是否消耗该材料,否则只需要拥有但不会减少');
  244. });
  245. });
  246. }
  247. }