| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\GameItems\Repositorys\ItemDismantleRuleRepository;
- use App\Module\GameItems\Repositorys\ItemCategoryRepository;
- 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 UCore\DcatAdmin\FormHelper;
- use UCore\DcatAdmin\GridHelper;
- use UCore\DcatAdmin\ShowHelper;
- #[Resource('game-items-dismantle-rules', names: 'dcat.admin.game-items-dismantle-rules')]
- class DismantleRuleController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '物品分解规则管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemDismantleRuleRepository(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- $helper->columnId();
- $grid->column('rule_type', '规则类型')->display(function () {
- if (!empty($this->item_id)) {
- return '物品规则';
- } elseif (!empty($this->category_id)) {
- return '分类规则';
- } else {
- return '未知';
- }
- });
- $grid->column('item.name', '物品名称');
- $grid->column('category.name', '分类名称');
- $grid->column('priority', '优先级')->sortable();
- $grid->column('coin_return_rate', '金币返还率')->display(function ($value) {
- return $value * 100 . '%';
- });
- $grid->column('results', '结果数量')->display(function ($results) {
- return count($results);
- });
- $grid->column('is_active', '是否启用')->switch();
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $filter->equal('item_id', '物品')->select(
- (new ItemRepository())->pluck('name', 'id')
- );
- $filter->equal('category_id', '分类')->select(
- (new ItemCategoryRepository())->pluck('name', 'id')
- );
- $filter->where('rule_type', function ($query) {
- if ($this->input == 'item') {
- $query->whereNotNull('item_id')->whereNull('category_id');
- } elseif ($this->input == 'category') {
- $query->whereNotNull('category_id')->whereNull('item_id');
- }
- }, '规则类型')->select([
- 'item' => '物品规则',
- 'category' => '分类规则',
- ]);
- $filter->equal('is_active', '是否启用')->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 ItemDismantleRuleRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- // 规则类型
- $show->field('rule_type', '规则类型')->as(function () {
- if (!empty($this->item_id)) {
- return '物品规则';
- } elseif (!empty($this->category_id)) {
- return '分类规则';
- } else {
- return '未知';
- }
- });
- // 根据规则类型显示不同字段
- if ($show->getModel()->item_id) {
- $show->field('item.name', '物品名称');
- } elseif ($show->getModel()->category_id) {
- $show->field('category.name', '分类名称');
- }
- $helper->field('priority', '优先级');
- $show->field('coin_return_rate', '金币返还率')->as(function ($value) {
- return $value * 100 . '%';
- });
- $show->field('is_active', '是否启用')->as(function ($value) {
- return $value ? '是' : '否';
- });
- $helper->field('created_at', '创建时间');
- $helper->field('updated_at', '更新时间');
- // 显示分解结果
- $show->divider('分解结果');
- $show->field('results', '结果列表')->as(function ($results) {
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>物品名称</th><th>最小数量</th><th>最大数量</th><th>概率</th></tr></thead>';
- $html .= '<tbody>';
- foreach ($results as $result) {
- $html .= '<tr>';
- $html .= '<td>' . $result->resultItem->name . '</td>';
- $html .= '<td>' . $result->min_quantity . '</td>';
- $html .= '<td>' . $result->max_quantity . '</td>';
- $html .= '<td>' . ($result->chance * 100) . '%</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 ItemDismantleRuleRepository(), function (Form $form) {
- $helper = new FormHelper($form, $this);
- // 规则类型
- $form->radio('rule_type', '规则类型')
- ->options(['item' => '物品规则', 'category' => '分类规则'])
- ->default('item')
- ->when('item', function (Form $form) {
- $form->select('item_id', '物品')
- ->options(ItemItem::pluck('name', 'id'))
- ->required();
- })
- ->when('category', function (Form $form) {
- $form->select('category_id', '分类')
- ->options(ItemCategory::pluck('name', 'id'))
- ->required();
- });
- $helper->number('priority')
- ->default(0)
- ->help('数字越大优先级越高,当物品同时匹配多个规则时,使用优先级最高的规则');
- $form->rate('coin_return_rate', '金币返还率')
- ->default(0.5)
- ->help('分解时返还的金币比例,基于物品的sell_price计算');
- $form->switch('is_active', '是否启用')
- ->default(true);
- // 分解结果
- $form->hasMany('results', '分解结果', function (Form\NestedForm $form) {
- $form->select('result_item_id', '物品')
- ->options(ItemItem::pluck('name', 'id'))
- ->required();
- $form->number('min_quantity', '最小数量')
- ->default(1)
- ->min(0)
- ->required();
- $form->number('max_quantity', '最大数量')
- ->default(1)
- ->min(0)
- ->required();
- $form->rate('chance', '概率')
- ->default(1)
- ->help('获得该物品的概率,1表示100%');
- });
- // 保存前回调
- $form->saving(function (Form $form) {
- // 根据规则类型设置对应的字段
- if ($form->rule_type == 'item') {
- $form->category_id = null;
- } else {
- $form->item_id = null;
- }
- });
- });
- }
- }
|