| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- <?php
- namespace App\Module\Game\AdminControllers;
- use App\Module\Game\AdminControllers\Actions\DuplicateRewardGroupAction;
- use App\Module\Game\AdminControllers\Actions\RandomRewardAction;
- use App\Module\Game\AdminControllers\Actions\BatchRandomRewardAction;
- use App\Module\Game\Enums\REWARD_TYPE;
- use App\Module\Game\Enums\REWARD_MODE;
- use App\Module\Game\Models\GameRewardGroup;
- use App\Module\Game\Models\GameRewardItem;
- use App\Module\Game\Repositorys\GameRewardGroupRepository;
- use App\Module\GameItems\Models\Item;
- use App\Module\Fund\Models\FundCurrencyModel;
- use Dcat\Admin\Form;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Dcat\Admin\Http\Controllers\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\Helper\Color;
- /**
- * 奖励组管理控制器
- */
- #[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
- class GameRewardGroupController extends AdminController
- {
- /**
- * 标题
- *
- * @return string
- */
- protected function title()
- {
- return '奖励组管理';
- }
- /**
- * 创建表格
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
- // 预加载奖励项和标签关联数据
- $grid->model()->with(['rewardItems', 'tags']);
- $grid->column('id', 'ID')->sortable();
- $grid->column('name', '名称');
- $grid->column('code', '编码');
- $grid->column('description', '描述')->limit(30);
- $grid->column('is_random', '随机发放')->bool();
- $grid->column('random_count', '随机数量');
- $grid->column('reward_mode', '奖励模式')->display(function ($value) {
- return REWARD_MODE::getName($value ?? 1);
- });
- // 标签列
- $grid->column('tags', '标签')->display(function () {
- return $this->formatTags();
- })->width(200);
- // 奖励详情列
- $grid->column('reward_details', '奖励详情')->display(function () {
- return $this->formatRewardDetails();
- })->width(300);
- $grid->column('created_at', '创建时间');
- $grid->column('updated_at', '更新时间');
- $grid->filter(function (Grid\Filter $filter) {
- $filter->equal('id', 'ID');
- $filter->like('name', '名称');
- $filter->like('code', '编码');
- $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
- $filter->equal('reward_mode', '奖励模式')->select(REWARD_MODE::getAll());
- // 标签筛选
- $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
- $filter->whereHas('tags', '标签', function ($query) {
- $query->where('id', request('tags'));
- })->select($tagRepository->getActiveTagOptions());
- });
- // 使用 RowAction 类添加行操作按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- // 添加复制按钮
- $actions->append(new DuplicateRewardGroupAction());
- // 添加随机奖励按钮
- $actions->append(new RandomRewardAction());
- $actions->append(new BatchRandomRewardAction(10));
- $actions->append(new BatchRandomRewardAction(100));
- $actions->disableDelete();
- });
- });
- }
- /**
- * 创建详情页
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new GameRewardGroupRepository(['tags']), function (Show $show) use ($id) {
- $show->field('id', 'ID');
- $show->field('name', '名称');
- $show->field('code', '编码');
- $show->field('description', '描述');
- $show->field('is_random', '随机发放')->using([0 => '否', 1 => '是']);
- $show->field('random_count', '随机数量');
- $show->field('reward_mode', '奖励模式')->as(function ($value) {
- return REWARD_MODE::getName($value ?? 1);
- });
- // 标签显示
- $show->field('tags', '标签')->as(function ($tags) {
- if (empty($tags)) {
- return '<span class="text-muted">无标签</span>';
- }
- $tagHtml = [];
- foreach ($tags as $tag) {
- $tagHtml[] = sprintf(
- '<span class="badge" style="background-color: %s; color: %s;">%s</span>',
- $tag['color'],
- Color::getContrastColor($tag['coler']),
- $tag['name']
- );
- }
- return implode(' ', $tagHtml);
- });
- $show->field('created_at', '创建时间');
- $show->field('updated_at', '更新时间');
- // 显示奖励项
- $show->divider();
- $show->field('reward_items', '奖励项')->unescape()->as(function () use ($id) {
- // 获取奖励组及其奖励项
- $group = GameRewardGroup::with('rewardItems')->find($id);
- if (!$group || $group->rewardItems->isEmpty()) {
- return '<span class="text-muted">暂无奖励项</span>';
- }
- $html = '<div class="reward-items-list">';
- foreach ($group->rewardItems as $item) {
- // 使用统一的奖励类型描述器
- $displayText = \App\Module\Game\Services\RewardTypeDescriptor::formatRewardDisplayFromModel($item, true);
- $weight = $item->weight;
- $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
- $html .= '<div class="reward-item mb-2 p-2 border rounded">';
- $html .= $displayText;
- $html .= '<br><small class="text-muted">';
- $html .= '权重: ' . $weight . ' | ' . $guaranteed;
- // 显示概率信息(如果有)
- if ($item->probability !== null) {
- $html .= ' | 概率: ' . $item->probability . '%';
- }
- // 显示随机数量范围信息
- if ($item->min_quantity !== null || $item->max_quantity !== null) {
- $html .= ' | 随机数量: ';
- if ($item->min_quantity !== null && $item->max_quantity !== null) {
- $html .= $item->min_quantity . '-' . $item->max_quantity;
- } elseif ($item->min_quantity !== null) {
- $html .= $item->min_quantity . '+';
- } elseif ($item->max_quantity !== null) {
- $html .= '1-' . $item->max_quantity;
- }
- }
- if ($item->param1 || $item->param2) {
- $html .= ' | 参数: ' . $item->param1 . ',' . $item->param2;
- }
- $html .= '</small>';
- $html .= '</div>';
- }
- $html .= '</div>';
- // 添加管理链接
- $html .= '<div class="mt-3">';
- $html .= '<a href="' . admin_url('game-reward-items?group_id=' . $id) . '" target="_blank" class="btn btn-sm btn-primary">';
- $html .= '<i class="fa fa-list"></i> 管理奖励项';
- $html .= '</a>';
- $html .= '</div>';
- return $html;
- });
- });
- }
- /**
- * 获取奖励类型名称
- *
- * @param int $rewardType 奖励类型
- * @return string 奖励类型名称
- */
- public function getRewardTypeName(int $rewardType): string
- {
- switch ($rewardType) {
- case REWARD_TYPE::ITEM->value:
- return '物品';
- case REWARD_TYPE::CURRENCY->value:
- return '货币';
- case REWARD_TYPE::PET_EXP->value:
- return '宠物经验';
- case REWARD_TYPE::PET_ENERGY->value:
- return '宠物体力';
- case REWARD_TYPE::OTHER->value:
- return '其他';
- default:
- return '未知';
- }
- }
- /**
- * 根据奖励项获取实际内容描述
- *
- * @param GameRewardItem $item 奖励项
- * @return string 奖励内容描述
- */
- public function getRewardContent(GameRewardItem $item): string
- {
- switch ($item->reward_type) {
- case REWARD_TYPE::ITEM->value:
- // 物品奖励
- try {
- $gameItem = Item::find($item->target_id);
- if ($gameItem) {
- $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
- // 添加参数说明
- $params = [];
- if ($item->param1 > 0) {
- $params[] = "品质: {$item->param1}";
- }
- if ($item->param2 > 0) {
- $params[] = "绑定: {$item->param2}";
- }
- if (!empty($params)) {
- $content .= "<br><small>" . implode(', ', $params) . "</small>";
- }
- return $content;
- }
- } catch (\Exception $e) {
- // 忽略异常,返回默认内容
- }
- return "物品 (ID: {$item->target_id})";
- case REWARD_TYPE::CURRENCY->value:
- // 货币奖励
- try {
- $currency = FundCurrencyModel::find($item->target_id);
- if ($currency) {
- $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
- // 添加参数说明
- $params = [];
- if ($item->param1 > 0) {
- $params[] = "来源: {$item->param1}";
- }
- if (!empty($params)) {
- $content .= "<br><small>" . implode(', ', $params) . "</small>";
- }
- return $content;
- }
- } catch (\Exception $e) {
- // 忽略异常,返回默认内容
- }
- return "货币 (ID: {$item->target_id})";
- case REWARD_TYPE::PET_EXP->value:
- // 宠物经验奖励
- return "宠物经验 (宠物ID: {$item->target_id})";
- case REWARD_TYPE::PET_ENERGY->value:
- // 宠物体力奖励
- return "宠物体力 (宠物ID: {$item->target_id})";
- case REWARD_TYPE::FARM_SHRINE->value:
- // 神像奖励(农场buff)
- try {
- $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
- $durationHours = $item->param2 > 0 ? $item->param2 : 24;
- $content = "<strong>{$shrineName}</strong> (类型: {$item->target_id})";
- $content .= "<br><small>持续时间: {$durationHours}小时</small>";
- // 添加参数说明
- $params = [];
- if ($item->param1 > 0) {
- $params[] = "神像类型: {$item->param1}";
- }
- if ($item->param2 > 0) {
- $params[] = "持续时间: {$item->param2}小时";
- }
- if (!empty($params)) {
- $content .= "<br><small>" . implode(', ', $params) . "</small>";
- }
- return $content;
- } catch (\Exception $e) {
- return "神像 (类型: {$item->target_id})";
- }
- case REWARD_TYPE::OTHER->value:
- // 其他奖励
- return "其他奖励 (ID: {$item->target_id})";
- default:
- return "未知奖励类型 (ID: {$item->target_id})";
- }
- }
- /**
- * 创建表单
- *
- * @return Form
- */
- protected function form()
- {
- return Form::make(new GameRewardGroupRepository(['tags']), function (Form $form) {
- $form->display('id', 'ID');
- $form->text('name', '名称')->required();
- $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
- $form->textarea('description', '描述');
- $form->switch('is_random', '随机发放')->default(0);
- $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
- $form->select('reward_mode', '奖励模式')
- ->options(REWARD_MODE::getAll())
- ->default(REWARD_MODE::WEIGHT_SELECTION->value)
- ->help('权重选择模式:传统的权重选择机制;独立概率模式:每个奖励项独立判断是否获得');
- // 标签选择
- $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
- $form->multipleSelect('tags', '标签')
- ->options($tagRepository->getActiveTagOptions())
- ->help('可以选择多个标签来分类管理奖励组');
- $form->display('created_at', '创建时间');
- $form->display('updated_at', '更新时间');
- // 添加提示信息,告知用户如何管理奖励项
- if ($form->isEditing()) {
- $id = $form->getKey();
- $form->html('<div class="alert alert-info">
- <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
- <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
- <i class="fa fa-list"></i> 管理奖励项
- </a>
- </div>');
- } else {
- $form->html('<div class="alert alert-info">
- <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
- </div>');
- }
- });
- }
- /**
- * 格式化数量文本
- *
- * @param GameRewardItem $item
- * @return string
- */
- private function formatQuantityText(GameRewardItem $item): string
- {
- // 如果设置了最小和最大数量,显示范围
- if ($item->min_quantity !== null && $item->max_quantity !== null) {
- if ($item->min_quantity == $item->max_quantity) {
- return (string)$item->min_quantity;
- } else {
- return $item->min_quantity . '-' . $item->max_quantity;
- }
- }
- // 如果只设置了最小数量
- if ($item->min_quantity !== null) {
- return $item->min_quantity . '+';
- }
- // 如果只设置了最大数量
- if ($item->max_quantity !== null) {
- return '1-' . $item->max_quantity;
- }
- // 默认使用固定数量
- return (string)$item->quantity;
- }
- }
|