| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Repositorys\ItemRecipeRepository;
- use App\Module\GameItems\Repositorys\ItemRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Dcat\Admin\Layout\Content;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\FormHelper;
- use App\Module\GameItems\AdminControllers\Helper\GridHelper;
- use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
- /**
- * 合成配方管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items-recipes', names: 'dcat.admin.game-items-recipes')]
- class RecipeController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '合成配方管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemRecipeRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('name', '配方名称');
- $grid->column('resultItem.name', '产出物品');
- $grid->column('result_quantity', '产出数量');
- $grid->column('success_rate', '成功率')->display(function ($value) {
- return $value * 100 . '%';
- });
- $grid->column('materials', '材料数量')->display(function ($materials) {
- return count($materials);
- });
- $grid->column('coin_cost', '金币消耗')->display(function ($value) {
- if (empty($value)) {
- return '0';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- if (is_array($value)) {
- $result = [];
- foreach ($value as $currency => $amount) {
- $result[] = $currency . ': ' . $amount;
- }
- return implode(', ', $result);
- }
- return $value;
- });
- $grid->column('cooldown_seconds', '冷却时间(秒)');
- $grid->column('is_visible', '是否可见')->switch();
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $helper->like('name', '配方名称');
- $filter->equal('result_item_id', '产出物品')->select(
- (new ItemRepository())->pluck('name', 'id')
- );
- $filter->equal('is_visible', '是否可见')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- });
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ItemRecipeRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $helper->field('name', '配方名称');
- $show->field('resultItem.name', '产出物品');
- $helper->field('result_quantity', '产出数量');
- $show->field('success_rate', '成功率')->as(function ($value) {
- return $value * 100 . '%';
- });
- // 显示金币消耗
- $show->field('coin_cost', '金币消耗')->as(function ($value) {
- if (empty($value)) {
- return '0';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- if (is_array($value)) {
- $result = [];
- foreach ($value as $currency => $amount) {
- $result[] = $currency . ': ' . $amount;
- }
- return implode('<br>', $result);
- }
- return $value;
- })->unescape();
- $helper->field('cooldown_seconds', '冷却时间(秒)');
- $show->field('is_visible', '是否可见')->as(function ($value) {
- return $value ? '是' : '否';
- });
- // 显示解锁条件
- $show->field('unlock_condition', '解锁条件')->as(function ($value) {
- if (empty($value)) {
- return '无';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- if (is_array($value)) {
- $result = [];
- foreach ($value as $condition => $requirement) {
- $result[] = $condition . ': ' . $requirement;
- }
- return implode('<br>', $result);
- }
- return $value;
- })->unescape();
- $helper->field('created_at', '创建时间');
- $helper->field('updated_at', '更新时间');
- // 显示配方材料
- $show->divider('配方材料');
- $show->field('materials', '材料列表')->as(function ($materials) {
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>物品名称</th><th>数量</th><th>是否消耗</th></tr></thead>';
- $html .= '<tbody>';
- foreach ($materials as $material) {
- $html .= '<tr>';
- $html .= '<td>' . $material->item->name . '</td>';
- $html .= '<td>' . $material->quantity . '</td>';
- $html .= '<td>' . ($material->is_consumed ? '是' : '否') . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- return $html;
- })->unescape();
- });
- }
- /**
- * 创建页
- *
- * @param Content $content
- * @return Content
- */
- public function create(Content $content)
- {
- return $content
- ->header($this->title)
- ->description('创建')
- ->body($this->form());
- }
- /**
- * 编辑页
- *
- * @param mixed $id
- * @param Content $content
- * @return Content
- */
- public function edit($id, Content $content)
- {
- return $content
- ->header($this->title)
- ->description('编辑')
- ->body($this->form()->edit($id));
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ItemRecipeRepository(), function (Form $form) {
- $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
- $helper->text('name', '配方名称')->required();
- $form->select('result_item_id', '产出物品')
- ->options((new ItemRepository())->pluck('name', 'id'))
- ->required();
- $form->number('result_quantity', '产出数量')
- ->default(1)
- ->min(1)
- ->required();
- $form->rate('success_rate', '成功率')
- ->default(1)
- ->help('合成成功的概率,1表示100%');
- // 金币消耗
- $form->keyValue('coin_cost', '金币消耗')
- ->help('可以设置多种货币类型的消耗,如:gold:100表示消耗100金币');
- $form->number('cooldown_seconds', '冷却时间(秒)')
- ->default(0)
- ->min(0)
- ->help('两次合成之间的冷却时间,0表示无冷却');
- $form->switch('is_visible', '是否可见')
- ->default(true)
- ->help('是否在游戏中对玩家可见');
- // 解锁条件
- $form->keyValue('unlock_condition', '解锁条件')
- ->help('设置解锁该配方的条件,如:level:10表示玩家等级达到10级');
- // 配方材料
- $form->hasMany('materials', '配方材料', function (Form\NestedForm $form) {
- $form->select('item_id', '物品')
- ->options((new ItemRepository())->pluck('name', 'id'))
- ->required();
- $form->number('quantity', '数量')
- ->default(1)
- ->min(1)
- ->required();
- $form->switch('is_consumed', '是否消耗')
- ->default(true)
- ->help('合成时是否消耗该材料,否则只需要拥有但不会减少');
- });
- });
- }
- }
|