GameRewardGroupController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\AdminControllers\Actions\DuplicateRewardGroupAction;
  4. use App\Module\Game\Enums\REWARD_TYPE;
  5. use App\Module\Game\Models\GameRewardGroup;
  6. use App\Module\Game\Models\GameRewardItem;
  7. use App\Module\Game\Repositorys\GameRewardGroupRepository;
  8. use App\Module\GameItems\Models\Item;
  9. use App\Module\Fund\Models\FundCurrencyModel;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Show;
  13. use Dcat\Admin\Http\Controllers\AdminController;
  14. use Spatie\RouteAttributes\Attributes\Resource;
  15. /**
  16. * 奖励组管理控制器
  17. */
  18. #[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
  19. class GameRewardGroupController extends AdminController
  20. {
  21. /**
  22. * 标题
  23. *
  24. * @return string
  25. */
  26. protected function title()
  27. {
  28. return '奖励组管理';
  29. }
  30. /**
  31. * 创建表格
  32. *
  33. * @return Grid
  34. */
  35. protected function grid()
  36. {
  37. return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
  38. // 预加载奖励项关联数据
  39. $grid->model()->with('rewardItems');
  40. $grid->column('id', 'ID')->sortable();
  41. $grid->column('name', '名称');
  42. $grid->column('code', '编码');
  43. $grid->column('description', '描述')->limit(30);
  44. $grid->column('is_random', '随机发放')->bool(['否', '是']);
  45. $grid->column('random_count', '随机数量');
  46. // 奖励详情列
  47. $grid->column('reward_details', '奖励详情')->display(function () {
  48. return $this->formatRewardDetails();
  49. })->width(300);
  50. $grid->column('created_at', '创建时间');
  51. $grid->column('updated_at', '更新时间');
  52. $grid->filter(function (Grid\Filter $filter) {
  53. $filter->equal('id', 'ID');
  54. $filter->like('name', '名称');
  55. $filter->like('code', '编码');
  56. $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
  57. });
  58. // 使用 RowAction 类添加行操作按钮
  59. $grid->actions(function (Grid\Displayers\Actions $actions) {
  60. // 添加复制按钮
  61. $actions->append(new DuplicateRewardGroupAction());
  62. $actions->disableDelete();
  63. });
  64. });
  65. }
  66. /**
  67. * 创建详情页
  68. *
  69. * @param mixed $id
  70. * @return Show
  71. */
  72. protected function detail($id)
  73. {
  74. return Show::make($id, new GameRewardGroupRepository(), function (Show $show) use ($id) {
  75. $show->field('id', 'ID');
  76. $show->field('name', '名称');
  77. $show->field('code', '编码');
  78. $show->field('description', '描述');
  79. $show->field('is_random', '随机发放')->using([0 => '否', 1 => '是']);
  80. $show->field('random_count', '随机数量');
  81. $show->field('created_at', '创建时间');
  82. $show->field('updated_at', '更新时间');
  83. // 显示奖励项
  84. $show->divider();
  85. $show->field('reward_items', '奖励项')->unescape()->as(function () use ($id) {
  86. // 获取奖励组及其奖励项
  87. $group = GameRewardGroup::with('rewardItems')->find($id);
  88. if (!$group || $group->rewardItems->isEmpty()) {
  89. return '<span class="text-muted">暂无奖励项</span>';
  90. }
  91. $html = '<div class="reward-items-list">';
  92. foreach ($group->rewardItems as $item) {
  93. $rewardTypeName = REWARD_TYPE::getName($item->reward_type);
  94. $targetName = $group->getTargetName($item);
  95. $quantity = $item->quantity;
  96. $weight = $item->weight;
  97. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  98. $html .= '<div class="reward-item mb-2 p-2 border rounded">';
  99. $html .= '<span class="badge badge-info mr-2">' . $rewardTypeName . '</span>';
  100. $html .= '<strong>' . $targetName . '</strong>';
  101. $html .= ' × ' . $quantity;
  102. $html .= '<br><small class="text-muted">';
  103. $html .= '权重: ' . $weight . ' | ' . $guaranteed;
  104. if ($item->param1 || $item->param2) {
  105. $html .= ' | 参数: ' . $item->param1 . ',' . $item->param2;
  106. }
  107. $html .= '</small>';
  108. $html .= '</div>';
  109. }
  110. $html .= '</div>';
  111. // 添加管理链接
  112. $html .= '<div class="mt-3">';
  113. $html .= '<a href="' . admin_url('game-reward-items?group_id=' . $id) . '" target="_blank" class="btn btn-sm btn-primary">';
  114. $html .= '<i class="fa fa-list"></i> 管理奖励项';
  115. $html .= '</a>';
  116. $html .= '</div>';
  117. return $html;
  118. });
  119. });
  120. }
  121. /**
  122. * 获取奖励类型名称
  123. *
  124. * @param int $rewardType 奖励类型
  125. * @return string 奖励类型名称
  126. */
  127. public function getRewardTypeName(int $rewardType): string
  128. {
  129. switch ($rewardType) {
  130. case REWARD_TYPE::ITEM->value:
  131. return '物品';
  132. case REWARD_TYPE::CURRENCY->value:
  133. return '货币';
  134. case REWARD_TYPE::PET_EXP->value:
  135. return '宠物经验';
  136. case REWARD_TYPE::PET_ENERGY->value:
  137. return '宠物体力';
  138. case REWARD_TYPE::OTHER->value:
  139. return '其他';
  140. default:
  141. return '未知';
  142. }
  143. }
  144. /**
  145. * 根据奖励项获取实际内容描述
  146. *
  147. * @param GameRewardItem $item 奖励项
  148. * @return string 奖励内容描述
  149. */
  150. public function getRewardContent(GameRewardItem $item): string
  151. {
  152. switch ($item->reward_type) {
  153. case REWARD_TYPE::ITEM->value:
  154. // 物品奖励
  155. try {
  156. $gameItem = Item::find($item->target_id);
  157. if ($gameItem) {
  158. $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
  159. // 添加参数说明
  160. $params = [];
  161. if ($item->param1 > 0) {
  162. $params[] = "品质: {$item->param1}";
  163. }
  164. if ($item->param2 > 0) {
  165. $params[] = "绑定: {$item->param2}";
  166. }
  167. if (!empty($params)) {
  168. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  169. }
  170. return $content;
  171. }
  172. } catch (\Exception $e) {
  173. // 忽略异常,返回默认内容
  174. }
  175. return "物品 (ID: {$item->target_id})";
  176. case REWARD_TYPE::CURRENCY->value:
  177. // 货币奖励
  178. try {
  179. $currency = FundCurrencyModel::find($item->target_id);
  180. if ($currency) {
  181. $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
  182. // 添加参数说明
  183. $params = [];
  184. if ($item->param1 > 0) {
  185. $params[] = "来源: {$item->param1}";
  186. }
  187. if (!empty($params)) {
  188. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  189. }
  190. return $content;
  191. }
  192. } catch (\Exception $e) {
  193. // 忽略异常,返回默认内容
  194. }
  195. return "货币 (ID: {$item->target_id})";
  196. case REWARD_TYPE::PET_EXP->value:
  197. // 宠物经验奖励
  198. return "宠物经验 (宠物ID: {$item->target_id})";
  199. case REWARD_TYPE::PET_ENERGY->value:
  200. // 宠物体力奖励
  201. return "宠物体力 (宠物ID: {$item->target_id})";
  202. case REWARD_TYPE::FARM_SHRINE->value:
  203. // 神像奖励(农场buff)
  204. try {
  205. $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
  206. $durationHours = $item->param2 > 0 ? $item->param2 : 24;
  207. $content = "<strong>{$shrineName}</strong> (类型: {$item->target_id})";
  208. $content .= "<br><small>持续时间: {$durationHours}小时</small>";
  209. // 添加参数说明
  210. $params = [];
  211. if ($item->param1 > 0) {
  212. $params[] = "神像类型: {$item->param1}";
  213. }
  214. if ($item->param2 > 0) {
  215. $params[] = "持续时间: {$item->param2}小时";
  216. }
  217. if (!empty($params)) {
  218. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  219. }
  220. return $content;
  221. } catch (\Exception $e) {
  222. return "神像 (类型: {$item->target_id})";
  223. }
  224. case REWARD_TYPE::OTHER->value:
  225. // 其他奖励
  226. return "其他奖励 (ID: {$item->target_id})";
  227. default:
  228. return "未知奖励类型 (ID: {$item->target_id})";
  229. }
  230. }
  231. /**
  232. * 创建表单
  233. *
  234. * @return Form
  235. */
  236. protected function form()
  237. {
  238. return Form::make(new GameRewardGroupRepository(), function (Form $form) {
  239. $form->display('id', 'ID');
  240. $form->text('name', '名称')->required();
  241. $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
  242. $form->textarea('description', '描述');
  243. $form->switch('is_random', '随机发放')->default(0);
  244. $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
  245. $form->display('created_at', '创建时间');
  246. $form->display('updated_at', '更新时间');
  247. // 添加提示信息,告知用户如何管理奖励项
  248. if ($form->isEditing()) {
  249. $id = $form->getKey();
  250. $form->html('<div class="alert alert-info">
  251. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
  252. <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
  253. <i class="fa fa-list"></i> 管理奖励项
  254. </a>
  255. </div>');
  256. } else {
  257. $form->html('<div class="alert alert-info">
  258. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
  259. </div>');
  260. }
  261. });
  262. }
  263. }