| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- namespace App\Module\GameItems\AdminControllers;
- use App\Module\Game\DCache\ItemJsonConfig;
- use App\Module\GameItems\Enums\ITEM_TYPE;
- use App\Module\GameItems\Models\ItemCategory;
- use App\Module\GameItems\Models\ItemChestOpenCost;
- use App\Module\GameItems\Repositorys\ItemChestContentRepository;
- use App\Module\GameItems\Repositorys\ItemChestOpenCostRepository;
- use App\Module\GameItems\Repositorys\ItemRepository;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use Spatie\RouteAttributes\Attributes\Get;
- use Illuminate\Support\Facades\Artisan;
- use App\Module\GameItems\AdminControllers\Helper\FilterHelper;
- use App\Module\GameItems\AdminControllers\Helper\FormHelper;
- use App\Module\GameItems\AdminControllers\Helper\GridHelper;
- use App\Module\GameItems\AdminControllers\Helper\ShowHelper;
- use App\Module\GameItems\AdminControllers\Actions\ChestManageAction;
- use App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction;
- /**
- * 物品管理控制器
- *
- * @package App\Module\GameItems\AdminControllers
- */
- #[Resource('game-items', names: 'dcat.admin.game-items')]
- class ItemController extends AdminController
- {
- /**
- * 标题
- *
- * @var string
- */
- protected $title = '物品管理';
- /**
- * 列表页
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new ItemRepository([ 'category' ]), function (Grid $grid) {
- $status = \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool::checkSyncStatus();
- if ($status['is_synced']) {
- admin_success('JSON配置表状态', $status['message']);
- } else {
- admin_warning('JSON配置表状态', $status['message']);
- }
- $grid->tools([
- new \App\Module\GameItems\AdminControllers\Tools\RefreshCheckTool($status['should_display']),
- new \App\Module\GameItems\AdminControllers\Tools\SyncItemsJsonTool($status['should_display'])
- ]);
- $helper = new GridHelper($grid, $this);
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '名称');
- $grid->column('category.name', '分类');
- $helper->columnModelCats('type');
- $grid->column('display_attributes', '展示属性')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
- foreach ((array)$value as $key => $val) {
- if (empty($val)) continue;
- $html .= "<div><strong>{$key}</strong>: {$val}</div>";
- }
- $html .= '</div>';
- return $html;
- });
- // 添加数值属性列,只显示有效的属性(非0值),并使用注释作为键名
- $grid->column('numeric_attributes', '数值属性')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- // 属性名到注释的映射
- $attributeNames = [
- 'min_drop_count' => '宝箱最小数量',
- 'max_drop_count' => '宝箱最大数量',
- 'crop_growth_time' => '减少作物生长时间',
- 'pet_power' => '增加宠物体力',
- 'reward_group_id' => '随机奖励物品组',
- 'pet_exp' => '增加宠物经验',
- 'fram_pesticide_rate' => '除虫概率(%)',
- 'fram_drought_rate' => '解决干旱概率(%)',
- 'fram_weedicide_rate' => '除草概率(%)'
- ];
- $html = '<div style="max-width: 250px; max-height: 150px; overflow: auto;">';
- foreach ((array)$value as $key => $val) {
- if (empty($val) || $val === 0) continue; // 过滤掉空值和0值
- $name = $attributeNames[$key] ?? $key;
- $html .= "<div><strong>{$name}</strong>: {$val}</div>";
- }
- $html .= '</div>';
- return $html;
- });
- $grid->column('is_unique', '单独属性')->bool();
- $grid->column('max_stack', '最大堆叠');
- $grid->column('tradable', '可交易')->bool();
- $grid->column('dismantlable', '可分解')->bool();
- $grid->column('default_expire_seconds', '默认过期时间(秒)');
- $grid->column('global_expire_at', '全局过期时间');
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- // 添加行操作
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableDelete();
- $actions->append(new \App\Module\GameItems\AdminControllers\Actions\DuplicateRowAction());
- // 如果是宝箱类型,添加宝箱管理按钮
- $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestManageAction());
- $actions->append(new \App\Module\GameItems\AdminControllers\Actions\ChestCostAction());
- });
- // 筛选
- $grid->filter(function ($filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equal('id', 'ID');
- $filter->like('name', '名称');
- $filter->equal('category_id', '分类')->select(
- ItemCategory::pluck('name', 'id')
- );
- $helper->equalRadioModelCats('type', '类型');
- $filter->equal('is_unique', '单独属性')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- $filter->equal('tradable', '可交易')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- $filter->equal('dismantlable', '可分解')->radio([
- 1 => '是',
- 0 => '否',
- ]);
- });
- return $grid;
- });
- }
- /**
- * 详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new ItemRepository(), function (Show $show) {
- $helper = new ShowHelper($show, $this);
- $helper->field('id', 'ID');
- $show->field('name', '名称');
- $show->field('description', '描述');
- $show->field('category.name', '分类');
- $helper->fieldModelCats('type');
- $show->field('display_attributes', '展示属性')->unescape()->as(function ($value) {
- if (empty($value)) {
- return '无';
- }
- if (is_string($value)) {
- $value = json_decode($value, true);
- }
- $html = '<table class="table table-bordered">';
- $html .= '<thead><tr><th>属性名</th><th>属性值</th></tr></thead>';
- $html .= '<tbody>';
- foreach ((array)$value as $key => $val) {
- if (empty($val)) continue;
- $html .= '<tr>';
- $html .= '<td>' . htmlspecialchars($key) . '</td>';
- $html .= '<td>' . htmlspecialchars((string)$val) . '</td>';
- $html .= '</tr>';
- }
- $html .= '</tbody></table>';
- return $html;
- });
- $show->field('is_unique', '单独属性')->as(function ($isUnique) {
- return $isUnique ? '是' : '否';
- });
- $show->field('max_stack', '最大堆叠');
- $show->field('sell_price', '出售价格');
- $show->field('tradable', '可交易')->as(function ($tradable) {
- return $tradable ? '是' : '否';
- });
- $show->field('dismantlable', '可分解')->as(function ($dismantlable) {
- return $dismantlable ? '是' : '否';
- });
- $show->field('default_expire_seconds', '默认过期时间(秒)');
- $helper->fieldModelCatsJson('display_attributes', '显示属性');
- $helper->fieldModelCatsJson('numeric_attributes', '数值属性');
- $show->field('global_expire_at', '全局过期时间');
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- $show->divider();
- // 如果是宝箱类型,显示宝箱内容
- $show->relation('chest_contents', '宝箱内容',
- function (\App\Module\GameItems\Models\Item $item) {
- // dd($item);
- $grid = new Grid(new ItemChestContentRepository([ 'chest', 'item', 'group' ]));
- $grid->model()->where('chest_id', $item->id);
- // 设置路由
- $grid->setResource('game-items-chest-contents');
- $grid->id();
- $grid->column('chest.name', '宝箱名称');
- $grid->column('item.name', '物品名称');
- $grid->column('group.name', '物品组名称');
- $grid->column('min_quantity', '最小数量');
- $grid->column('max_quantity', '最大数量');
- $grid->column('weight', '权重');
- $grid->disableActions();
- return $grid;
- });
- $show->relation('chest_costs', '宝箱消耗',
- function (\App\Module\GameItems\Models\Item $item) {
- // dd($item);
- $grid = new Grid(new ItemChestOpenCostRepository(['costItem']));
- $grid->model()->where('chest_id', $item->id);
- // 设置路由
- $grid->setResource('game-items-chest-costs');
- $grid->id();
- $grid->column('costItem.name', '物品名称');
- $grid->disableActions();
- return $grid;
- });
- return $show;
- });
- }
- /**
- * 表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new ItemRepository(), function (Form $form) {
- $helper = new \App\Module\GameItems\AdminControllers\Helper\FormHelper($form, $this);
- $helper->text('name')->required();
- $form->textarea('description', '描述');
- $form->select('category_id', '分类')
- ->options(ItemCategory::pluck('name', 'id'))
- ->required();
- $helper->selectOptionCast('type', '类型');
- $form->switch('is_unique', '单独属性')
- ->default(false);
- $form->number('max_stack', '最大堆叠')
- ->default(1)
- ->min(1)
- ->help('0表示无限堆叠');
- $form->number('sell_price', '出售价格')
- ->default(0)
- ->min(0);
- $form->switch('tradable', '可交易')
- ->default(true);
- $form->switch('dismantlable', '可分解')
- ->default(true);
- $form->number('default_expire_seconds', '默认过期时间(秒)')
- ->default(0)
- ->help('0表示永不过期');
- $helper->embedsCats('display_attributes', '显示属性')
- ->help('用于显示的属性,如:攻击力、防御力等');
- $helper->embedsCats('numeric_attributes', '数值属性')
- ->help('用于计算的属性,如:宝箱掉落物品数量范围等');
- $form->datetime('global_expire_at', '全局过期时间')
- ->help('所有该物品的全局过期时间,为空表示永不过期');
- // 保存前回调
- $form->saving(function (Form $form) {
- // 如果是宝箱类型,确保有min_drop_count和max_drop_count属性
- if ($form->type == ITEM_TYPE::CHEST) {
- $numericAttributes = $form->numeric_attributes ?: [];
- if (!isset($numericAttributes['min_drop_count'])) {
- $numericAttributes['min_drop_count'] = 1;
- }
- if (!isset($numericAttributes['max_drop_count'])) {
- $numericAttributes['max_drop_count'] = 1;
- }
- $form->numeric_attributes = $numericAttributes;
- }
- });
- return $form;
- });
- }
- }
|