RecipeController.php 8.9 KB

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