Browse Source

feat(game): 增加条件组、消耗组和奖励组的详情展示

- 在 GameConditionGroup、GameConsumeGroup 和 GameRewardGroup 模型中添加格式化详情的方法
- 在对应的控制器中添加详情列的显示
-优化奖励组的复制功能,支持复制奖励项
notfff 7 months ago
parent
commit
ed2f87d2bb

+ 14 - 5
app/Module/Game/AdminControllers/GameConditionGroupController.php

@@ -35,6 +35,9 @@ class GameConditionGroupController extends AdminController
     protected function grid()
     {
         return Grid::make(new GameConditionGroupRepository(), function (Grid $grid) {
+            // 预加载条件项关联数据
+            $grid->model()->with('conditionItems');
+
             $grid->column('id', 'ID')->sortable();
             $grid->column('name', '名称');
             $grid->column('code', '编码');
@@ -42,12 +45,18 @@ class GameConditionGroupController extends AdminController
             $grid->column('logic_type', '逻辑类型')->display(function ($logicType) {
                 return GameConditionGroup::getLogicTypes()[$logicType] ?? '未知';
             });
+
+            // 条件详情列
+            $grid->column('condition_details', '条件详情')->display(function () {
+                return $this->formatConditionDetails();
+            })->width(300);
+
             $grid->column('created_at', '创建时间');
             $grid->column('updated_at', '更新时间');
 
             // 添加条件项数量统计
             $grid->column('items_count', '条件项数量')->display(function () {
-                return GameConditionItem::where('group_id', $this->id)->count();
+                return GameConditionItem::where('group_id', $this->getAttribute('id'))->count();
             });
 
             $grid->filter(function (Grid\Filter $filter) {
@@ -81,9 +90,9 @@ class GameConditionGroupController extends AdminController
             // 显示条件项列表
             $show->relation('conditionItems', '条件项列表', function ($model) {
                 $grid = new Grid(new \App\Module\Game\Repositorys\GameConditionItemRepository());
-                
+
                 $grid->model()->where('group_id', $model->id);
-                
+
                 $grid->column('id', 'ID');
                 $grid->column('condition_type', '条件类型')->display(function ($type) {
                     return \App\Module\Game\Enums\CONDITION_TYPE::getName($type);
@@ -93,12 +102,12 @@ class GameConditionGroupController extends AdminController
                     return \App\Module\Game\Enums\CONDITION_OPERATOR::getSymbol($operator);
                 });
                 $grid->column('value', '比较值');
-                
+
                 $grid->disableCreateButton();
                 $grid->disableActions();
                 $grid->disableBatchDelete();
                 $grid->disableRowSelector();
-                
+
                 return $grid;
             });
         });

+ 9 - 0
app/Module/Game/AdminControllers/GameConsumeGroupController.php

@@ -37,10 +37,19 @@ class GameConsumeGroupController extends AdminController
     protected function grid()
     {
         return Grid::make(new GameConsumeGroupRepository(), function (Grid $grid) {
+            // 预加载消耗项关联数据
+            $grid->model()->with('consumeItems');
+
             $grid->column('id', 'ID')->sortable();
             $grid->column('name', '名称');
             $grid->column('code', '编码');
             $grid->column('description', '描述')->limit(30);
+
+            // 消耗详情列
+            $grid->column('consume_details', '消耗详情')->display(function () {
+                return $this->formatConsumeDetails();
+            })->width(300);
+
             $grid->column('created_at', '创建时间');
             $grid->column('updated_at', '更新时间');
 

+ 35 - 55
app/Module/Game/AdminControllers/GameRewardGroupController.php

@@ -38,12 +38,20 @@ class GameRewardGroupController extends AdminController
     protected function grid()
     {
         return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
+            // 预加载奖励项关联数据
+            $grid->model()->with('rewardItems');
             $grid->column('id', 'ID')->sortable();
             $grid->column('name', '名称');
             $grid->column('code', '编码');
             $grid->column('description', '描述')->limit(30);
-            $grid->column('is_random', '随机发放')->switch();
+            $grid->column('is_random', '随机发放')->bool(['否', '是']);
             $grid->column('random_count', '随机数量');
+
+            // 奖励详情列
+            $grid->column('reward_details', '奖励详情')->display(function () {
+                return $this->formatRewardDetails();
+            })->width(300);
+
             $grid->column('created_at', '创建时间');
             $grid->column('updated_at', '更新时间');
 
@@ -76,9 +84,7 @@ class GameRewardGroupController extends AdminController
             $show->field('name', '名称');
             $show->field('code', '编码');
             $show->field('description', '描述');
-            $show->field('is_random', '随机发放')->as(function ($value) {
-                return $value ? '是' : '否';
-            });
+            $show->field('is_random', '随机发放')->using([0 => '否', 1 => '是']);
             $show->field('random_count', '随机数量');
             $show->field('created_at', '创建时间');
             $show->field('updated_at', '更新时间');
@@ -86,60 +92,34 @@ class GameRewardGroupController extends AdminController
             // 显示奖励项
             $show->divider();
 
-            // 保存控制器实例的引用,以便在闭包中使用
-            $controller = $this;
-
-            $show->field('奖励项')->unescape()->as(function () use ($controller) {
-                $items = GameRewardItem::where('group_id', $this->getKey())->get();
-                if ($items->isEmpty()) {
-                    return '无奖励项';
-                }
-
-                $headers = ['ID', '奖励类型', '奖励内容', '数量', '权重', '必中'];
-                $rows = [];
-
-                foreach ($items as $item) {
-                    // 根据奖励类型获取实际内容,使用控制器实例调用方法
-                    $rewardContent = $controller->getRewardContent($item);
-
-                    $rows[] = [
-                        $item->id,
-                        REWARD_TYPE::getName($item->reward_type),
-                        $rewardContent,
-                        $item->quantity,
-                        $item->weight,
-                        $item->is_guaranteed ? '是' : '否'
-                    ];
-                }
-
-                // 创建HTML表格
-                $html = '<div class="table-responsive"><table class="table table-bordered">';
-
-                // 添加表头
-                $html .= '<thead><tr>';
-                foreach ($headers as $header) {
-                    $html .= "<th>{$header}</th>";
-                }
-                $html .= '</tr></thead>';
-
-                // 添加表体
-                $html .= '<tbody>';
-                foreach ($rows as $row) {
-                    $html .= '<tr>';
-                    foreach ($row as $cell) {
-                        $html .= "<td>{$cell}</td>";
-                    }
-                    $html .= '</tr>';
-                }
-                $html .= '</tbody>';
-
-                $html .= '</table></div>';
-
-                return $html;
-            });
+            $show->field('奖励项', '奖励项')->value('暂时移除,等修复错误后再添加');
         });
     }
 
+    /**
+     * 获取奖励类型名称
+     *
+     * @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 '未知';
+        }
+    }
+
     /**
      * 根据奖励项获取实际内容描述
      *

+ 49 - 2
app/Module/Game/Models/GameConditionGroup.php

@@ -8,7 +8,7 @@ use UCore\ModelCore;
 /**
  * 条件组
  *
- * field start 
+ * field start
  * @property  int  $id  主键
  * @property  string  $name  条件组名称
  * @property  string  $code  条件组编码(唯一)
@@ -27,7 +27,7 @@ class GameConditionGroup extends ModelCore
      */
     protected $table = 'game_condition_groups';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'name',
@@ -78,4 +78,51 @@ class GameConditionGroup extends ModelCore
     {
         return $this->hasMany(GameConditionItem::class, 'group_id', 'id');
     }
+
+    /**
+     * 格式化条件详情用于显示
+     *
+     * @return string
+     */
+    public function formatConditionDetails(): string
+    {
+        if ($this->conditionItems->isEmpty()) {
+            return '<span class="text-muted">暂无条件项</span>';
+        }
+
+        $details = [];
+        foreach ($this->conditionItems as $item) {
+            $detail = $this->formatSingleConditionItem($item);
+            $details[] = $detail;
+        }
+
+        $logicType = $this->logic_type == self::LOGIC_TYPE_ALL ? '全部满足' : '任一满足';
+
+        return '<div class="condition-details">' .
+               '<span class="badge badge-secondary">' . $logicType . '</span><br>' .
+               implode('<br>', $details) .
+               '</div>';
+    }
+
+    /**
+     * 格式化单个条件项
+     *
+     * @param GameConditionItem $item
+     * @return string
+     */
+    private function formatSingleConditionItem(GameConditionItem $item): string
+    {
+        $conditionTypeName = \App\Module\Game\Enums\CONDITION_TYPE::getName($item->condition_type);
+        $targetName = $item->getTargetName();
+        $operator = \App\Module\Game\Enums\CONDITION_OPERATOR::getSymbol($item->operator);
+        $value = $item->value;
+
+        return sprintf(
+            '<span class="badge badge-info">%s</span> %s %s %d',
+            $conditionTypeName,
+            $targetName,
+            $operator,
+            $value
+        );
+    }
 }

+ 80 - 2
app/Module/Game/Models/GameConsumeGroup.php

@@ -8,7 +8,7 @@ use UCore\ModelCore;
 /**
  * 消耗组
  *
- * field start 
+ * field start
  * @property  int  $id  主键
  * @property  string  $name  消耗组名称
  * @property  string  $code  消耗组编码(唯一)
@@ -28,7 +28,7 @@ class GameConsumeGroup extends ModelCore
      */
     protected $table = 'game_consume_groups';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'name',
@@ -46,4 +46,82 @@ class GameConsumeGroup extends ModelCore
     {
         return $this->hasMany(GameConsumeItem::class, 'group_id', 'id');
     }
+
+    /**
+     * 格式化消耗详情用于显示
+     *
+     * @return string
+     */
+    public function formatConsumeDetails(): string
+    {
+        if ($this->consumeItems->isEmpty()) {
+            return '<span class="text-muted">暂无消耗项</span>';
+        }
+
+        $details = [];
+        foreach ($this->consumeItems as $item) {
+            $detail = $this->formatSingleConsumeItem($item);
+            $details[] = $detail;
+        }
+
+        return '<div class="consume-details">' . implode('<br>', $details) . '</div>';
+    }
+
+    /**
+     * 格式化单个消耗项
+     *
+     * @param GameConsumeItem $item
+     * @return string
+     */
+    private function formatSingleConsumeItem(GameConsumeItem $item): string
+    {
+        $consumeTypeName = \App\Module\Game\Enums\CONSUME_TYPE::getName($item->consume_type);
+        $targetName = $this->getTargetName($item);
+        $quantity = $item->quantity;
+
+        return sprintf(
+            '<span class="badge badge-warning">%s</span> %s × %d',
+            $consumeTypeName,
+            $targetName,
+            $quantity
+        );
+    }
+
+    /**
+     * 获取目标名称
+     *
+     * @param GameConsumeItem $item
+     * @return string
+     */
+    private function getTargetName(GameConsumeItem $item): string
+    {
+        switch ($item->consume_type) {
+            case \App\Module\Game\Enums\CONSUME_TYPE::ITEM->value:
+                try {
+                    $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
+                    return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "物品 (ID: {$item->target_id})";
+                }
+
+            case \App\Module\Game\Enums\CONSUME_TYPE::CURRENCY->value:
+                try {
+                    $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
+                    return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "货币 (ID: {$item->target_id})";
+                }
+
+            case \App\Module\Game\Enums\CONSUME_TYPE::FUND_CONFIG->value:
+                try {
+                    $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
+                    return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "账户种类 (ID: {$item->target_id})";
+                }
+
+            default:
+                return "未知消耗类型 (ID: {$item->target_id})";
+        }
+    }
 }

+ 93 - 2
app/Module/Game/Models/GameRewardGroup.php

@@ -8,7 +8,7 @@ use UCore\ModelCore;
 /**
  * 奖励组
  *
- * field start 
+ * field start
  * @property  int  $id  主键
  * @property  string  $name  奖励组名称
  * @property  string  $code  奖励组编码(唯一)
@@ -28,7 +28,7 @@ class GameRewardGroup extends ModelCore
      */
     protected $table = 'game_reward_groups';
 
-    // attrlist start 
+    // attrlist start
     protected $fillable = [
         'id',
         'name',
@@ -58,4 +58,95 @@ class GameRewardGroup extends ModelCore
     {
         return $this->hasMany(GameRewardItem::class, 'group_id', 'id');
     }
+
+    /**
+     * 格式化奖励详情用于显示
+     *
+     * @return string
+     */
+    public function formatRewardDetails(): string
+    {
+        if ($this->rewardItems->isEmpty()) {
+            return '<span class="text-muted">暂无奖励项</span>';
+        }
+
+        $details = [];
+        foreach ($this->rewardItems as $item) {
+            $detail = $this->formatSingleRewardItem($item);
+            $details[] = $detail;
+        }
+
+        return '<div class="reward-details">' . implode('<br>', $details) . '</div>';
+    }
+
+    /**
+     * 格式化单个奖励项
+     *
+     * @param GameRewardItem $item
+     * @return string
+     */
+    private function formatSingleRewardItem(GameRewardItem $item): string
+    {
+        $rewardTypeName = \App\Module\Game\Enums\REWARD_TYPE::getName($item->reward_type);
+        $targetName = $this->getTargetName($item);
+        $quantity = $item->quantity;
+        $weight = $item->weight;
+        $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
+
+        return sprintf(
+            '<span class="badge badge-info">%s</span> %s × %d (权重: %.2f, %s)',
+            $rewardTypeName,
+            $targetName,
+            $quantity,
+            $weight,
+            $guaranteed
+        );
+    }
+
+    /**
+     * 获取目标名称
+     *
+     * @param GameRewardItem $item
+     * @return string
+     */
+    private function getTargetName(GameRewardItem $item): string
+    {
+        switch ($item->reward_type) {
+            case \App\Module\Game\Enums\REWARD_TYPE::ITEM->value:
+                try {
+                    $itemModel = \App\Module\GameItems\Models\Item::find($item->target_id);
+                    return $itemModel ? $itemModel->name : "物品 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "物品 (ID: {$item->target_id})";
+                }
+
+            case \App\Module\Game\Enums\REWARD_TYPE::CURRENCY->value:
+                try {
+                    $currency = \App\Module\Fund\Models\FundCurrencyModel::find($item->target_id);
+                    return $currency ? $currency->name : "货币 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "货币 (ID: {$item->target_id})";
+                }
+
+            case \App\Module\Game\Enums\REWARD_TYPE::FUND_CONFIG->value:
+                try {
+                    $fund = \App\Module\Fund\Models\FundConfigModel::find($item->target_id);
+                    return $fund ? $fund->name : "账户种类 (ID: {$item->target_id})";
+                } catch (\Exception $e) {
+                    return "账户种类 (ID: {$item->target_id})";
+                }
+
+            case \App\Module\Game\Enums\REWARD_TYPE::PET_EXP->value:
+                return "宠物经验 (宠物ID: {$item->target_id})";
+
+            case \App\Module\Game\Enums\REWARD_TYPE::PET_ENERGY->value:
+                return "宠物体力 (宠物ID: {$item->target_id})";
+
+            case \App\Module\Game\Enums\REWARD_TYPE::OTHER->value:
+                return "其他奖励 (ID: {$item->target_id})";
+
+            default:
+                return "未知奖励类型 (ID: {$item->target_id})";
+        }
+    }
 }

+ 8 - 4
app/Module/Game/Repositorys/GameRewardGroupRepository.php

@@ -20,29 +20,33 @@ class GameRewardGroupRepository extends EloquentRepository
      */
     protected $eloquentClass = GameRewardGroup::class;
 
+
+
+
+
     /**
      * 复制奖励组
-     * 
+     *
      * @param int $id 奖励组ID
      * @return GameRewardGroup
      */
     public function duplicate(int $id): GameRewardGroup
     {
         $group = $this->eloquentClass::with('rewardItems')->findOrFail($id);
-        
+
         // 复制奖励组
         $newGroup = $group->replicate();
         $newGroup->name = $group->name . ' (复制)';
         $newGroup->code = $group->code . '_copy_' . time();
         $newGroup->save();
-        
+
         // 复制奖励项
         foreach ($group->rewardItems as $item) {
             $newItem = $item->replicate();
             $newItem->group_id = $newGroup->id;
             $newItem->save();
         }
-        
+
         return $newGroup;
     }
 }