RecipeController.php 8.5 KB

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