GameRewardGroupController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. <?php
  2. namespace App\Module\Game\AdminControllers;
  3. use App\Module\Game\AdminControllers\Actions\DuplicateRewardGroupAction;
  4. use App\Module\Game\AdminControllers\Actions\RandomRewardAction;
  5. use App\Module\Game\AdminControllers\Actions\BatchRandomRewardAction;
  6. use App\Module\Game\Enums\REWARD_TYPE;
  7. use App\Module\Game\Enums\REWARD_MODE;
  8. use App\Module\Game\Models\GameRewardGroup;
  9. use App\Module\Game\Models\GameRewardItem;
  10. use App\Module\Game\Repositorys\GameRewardGroupRepository;
  11. use App\Module\GameItems\Models\Item;
  12. use App\Module\Fund\Models\FundCurrencyModel;
  13. use Dcat\Admin\Form;
  14. use Dcat\Admin\Grid;
  15. use Dcat\Admin\Show;
  16. use Dcat\Admin\Http\Controllers\AdminController;
  17. use Spatie\RouteAttributes\Attributes\Resource;
  18. /**
  19. * 奖励组管理控制器
  20. */
  21. #[Resource('game-reward-groups', names: 'dcat.admin.game-reward-groups')]
  22. class GameRewardGroupController extends AdminController
  23. {
  24. /**
  25. * 标题
  26. *
  27. * @return string
  28. */
  29. protected function title()
  30. {
  31. return '奖励组管理';
  32. }
  33. /**
  34. * 创建表格
  35. *
  36. * @return Grid
  37. */
  38. protected function grid()
  39. {
  40. return Grid::make(new GameRewardGroupRepository(), function (Grid $grid) {
  41. // 预加载奖励项和标签关联数据
  42. $grid->model()->with(['rewardItems', 'tags']);
  43. $grid->column('id', 'ID')->sortable();
  44. $grid->column('name', '名称');
  45. $grid->column('code', '编码');
  46. $grid->column('description', '描述')->limit(30);
  47. $grid->column('is_random', '随机发放')->bool();
  48. $grid->column('random_count', '随机数量');
  49. $grid->column('reward_mode', '奖励模式')->display(function ($value) {
  50. return REWARD_MODE::getName($value ?? 1);
  51. });
  52. // 标签列
  53. $grid->column('tags', '标签')->display(function () {
  54. return $this->formatTags();
  55. })->width(200);
  56. // 奖励详情列
  57. $grid->column('reward_details', '奖励详情')->display(function () {
  58. return $this->formatRewardDetails();
  59. })->width(300);
  60. $grid->column('created_at', '创建时间');
  61. $grid->column('updated_at', '更新时间');
  62. $grid->filter(function (Grid\Filter $filter) {
  63. $filter->equal('id', 'ID');
  64. $filter->like('name', '名称');
  65. $filter->like('code', '编码');
  66. $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
  67. $filter->equal('reward_mode', '奖励模式')->select(REWARD_MODE::getAll());
  68. // 标签筛选
  69. $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
  70. $filter->whereHas('tags', '标签', function ($query) {
  71. $query->where('id', request('tags'));
  72. })->select($tagRepository->getActiveTagOptions());
  73. });
  74. // 使用 RowAction 类添加行操作按钮
  75. $grid->actions(function (Grid\Displayers\Actions $actions) {
  76. // 添加复制按钮
  77. $actions->append(new DuplicateRewardGroupAction());
  78. // 添加随机奖励按钮
  79. $actions->append(new RandomRewardAction());
  80. $actions->append(new BatchRandomRewardAction(10));
  81. $actions->append(new BatchRandomRewardAction(100));
  82. $actions->disableDelete();
  83. });
  84. });
  85. }
  86. /**
  87. * 创建详情页
  88. *
  89. * @param mixed $id
  90. * @return Show
  91. */
  92. protected function detail($id)
  93. {
  94. return Show::make($id, new GameRewardGroupRepository(['tags']), function (Show $show) use ($id) {
  95. $show->field('id', 'ID');
  96. $show->field('name', '名称');
  97. $show->field('code', '编码');
  98. $show->field('description', '描述');
  99. $show->field('is_random', '随机发放')->using([0 => '否', 1 => '是']);
  100. $show->field('random_count', '随机数量');
  101. $show->field('reward_mode', '奖励模式')->as(function ($value) {
  102. return REWARD_MODE::getName($value ?? 1);
  103. });
  104. // 标签显示
  105. $show->field('tags', '标签')->as(function ($tags) {
  106. if (empty($tags)) {
  107. return '<span class="text-muted">无标签</span>';
  108. }
  109. $tagHtml = [];
  110. foreach ($tags as $tag) {
  111. $tagHtml[] = sprintf(
  112. '<span class="badge" style="background-color: %s; color: %s;">%s</span>',
  113. $tag['color'],
  114. $this->getContrastColor($tag['color']),
  115. $tag['name']
  116. );
  117. }
  118. return implode(' ', $tagHtml);
  119. });
  120. $show->field('created_at', '创建时间');
  121. $show->field('updated_at', '更新时间');
  122. // 显示奖励项
  123. $show->divider();
  124. $show->field('reward_items', '奖励项')->unescape()->as(function () use ($id) {
  125. // 获取奖励组及其奖励项
  126. $group = GameRewardGroup::with('rewardItems')->find($id);
  127. if (!$group || $group->rewardItems->isEmpty()) {
  128. return '<span class="text-muted">暂无奖励项</span>';
  129. }
  130. $html = '<div class="reward-items-list">';
  131. foreach ($group->rewardItems as $item) {
  132. // 使用统一的奖励类型描述器
  133. $displayText = \App\Module\Game\Services\RewardTypeDescriptor::formatRewardDisplayFromModel($item, true);
  134. $weight = $item->weight;
  135. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  136. $html .= '<div class="reward-item mb-2 p-2 border rounded">';
  137. $html .= $displayText;
  138. $html .= '<br><small class="text-muted">';
  139. $html .= '权重: ' . $weight . ' | ' . $guaranteed;
  140. // 显示概率信息(如果有)
  141. if ($item->probability !== null) {
  142. $html .= ' | 概率: ' . $item->probability . '%';
  143. }
  144. // 显示随机数量范围信息
  145. if ($item->min_quantity !== null || $item->max_quantity !== null) {
  146. $html .= ' | 随机数量: ';
  147. if ($item->min_quantity !== null && $item->max_quantity !== null) {
  148. $html .= $item->min_quantity . '-' . $item->max_quantity;
  149. } elseif ($item->min_quantity !== null) {
  150. $html .= $item->min_quantity . '+';
  151. } elseif ($item->max_quantity !== null) {
  152. $html .= '1-' . $item->max_quantity;
  153. }
  154. }
  155. if ($item->param1 || $item->param2) {
  156. $html .= ' | 参数: ' . $item->param1 . ',' . $item->param2;
  157. }
  158. $html .= '</small>';
  159. $html .= '</div>';
  160. }
  161. $html .= '</div>';
  162. // 添加管理链接
  163. $html .= '<div class="mt-3">';
  164. $html .= '<a href="' . admin_url('game-reward-items?group_id=' . $id) . '" target="_blank" class="btn btn-sm btn-primary">';
  165. $html .= '<i class="fa fa-list"></i> 管理奖励项';
  166. $html .= '</a>';
  167. $html .= '</div>';
  168. return $html;
  169. });
  170. });
  171. }
  172. /**
  173. * 获取奖励类型名称
  174. *
  175. * @param int $rewardType 奖励类型
  176. * @return string 奖励类型名称
  177. */
  178. public function getRewardTypeName(int $rewardType): string
  179. {
  180. switch ($rewardType) {
  181. case REWARD_TYPE::ITEM->value:
  182. return '物品';
  183. case REWARD_TYPE::CURRENCY->value:
  184. return '货币';
  185. case REWARD_TYPE::PET_EXP->value:
  186. return '宠物经验';
  187. case REWARD_TYPE::PET_ENERGY->value:
  188. return '宠物体力';
  189. case REWARD_TYPE::OTHER->value:
  190. return '其他';
  191. default:
  192. return '未知';
  193. }
  194. }
  195. /**
  196. * 根据奖励项获取实际内容描述
  197. *
  198. * @param GameRewardItem $item 奖励项
  199. * @return string 奖励内容描述
  200. */
  201. public function getRewardContent(GameRewardItem $item): string
  202. {
  203. switch ($item->reward_type) {
  204. case REWARD_TYPE::ITEM->value:
  205. // 物品奖励
  206. try {
  207. $gameItem = Item::find($item->target_id);
  208. if ($gameItem) {
  209. $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
  210. // 添加参数说明
  211. $params = [];
  212. if ($item->param1 > 0) {
  213. $params[] = "品质: {$item->param1}";
  214. }
  215. if ($item->param2 > 0) {
  216. $params[] = "绑定: {$item->param2}";
  217. }
  218. if (!empty($params)) {
  219. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  220. }
  221. return $content;
  222. }
  223. } catch (\Exception $e) {
  224. // 忽略异常,返回默认内容
  225. }
  226. return "物品 (ID: {$item->target_id})";
  227. case REWARD_TYPE::CURRENCY->value:
  228. // 货币奖励
  229. try {
  230. $currency = FundCurrencyModel::find($item->target_id);
  231. if ($currency) {
  232. $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
  233. // 添加参数说明
  234. $params = [];
  235. if ($item->param1 > 0) {
  236. $params[] = "来源: {$item->param1}";
  237. }
  238. if (!empty($params)) {
  239. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  240. }
  241. return $content;
  242. }
  243. } catch (\Exception $e) {
  244. // 忽略异常,返回默认内容
  245. }
  246. return "货币 (ID: {$item->target_id})";
  247. case REWARD_TYPE::PET_EXP->value:
  248. // 宠物经验奖励
  249. return "宠物经验 (宠物ID: {$item->target_id})";
  250. case REWARD_TYPE::PET_ENERGY->value:
  251. // 宠物体力奖励
  252. return "宠物体力 (宠物ID: {$item->target_id})";
  253. case REWARD_TYPE::FARM_SHRINE->value:
  254. // 神像奖励(农场buff)
  255. try {
  256. $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
  257. $durationHours = $item->param2 > 0 ? $item->param2 : 24;
  258. $content = "<strong>{$shrineName}</strong> (类型: {$item->target_id})";
  259. $content .= "<br><small>持续时间: {$durationHours}小时</small>";
  260. // 添加参数说明
  261. $params = [];
  262. if ($item->param1 > 0) {
  263. $params[] = "神像类型: {$item->param1}";
  264. }
  265. if ($item->param2 > 0) {
  266. $params[] = "持续时间: {$item->param2}小时";
  267. }
  268. if (!empty($params)) {
  269. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  270. }
  271. return $content;
  272. } catch (\Exception $e) {
  273. return "神像 (类型: {$item->target_id})";
  274. }
  275. case REWARD_TYPE::OTHER->value:
  276. // 其他奖励
  277. return "其他奖励 (ID: {$item->target_id})";
  278. default:
  279. return "未知奖励类型 (ID: {$item->target_id})";
  280. }
  281. }
  282. /**
  283. * 创建表单
  284. *
  285. * @return Form
  286. */
  287. protected function form()
  288. {
  289. return Form::make(new GameRewardGroupRepository(['tags']), function (Form $form) {
  290. $form->display('id', 'ID');
  291. $form->text('name', '名称')->required();
  292. $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
  293. $form->textarea('description', '描述');
  294. $form->switch('is_random', '随机发放')->default(0);
  295. $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
  296. $form->select('reward_mode', '奖励模式')
  297. ->options(REWARD_MODE::getAll())
  298. ->default(REWARD_MODE::WEIGHT_SELECTION->value)
  299. ->help('权重选择模式:传统的权重选择机制;独立概率模式:每个奖励项独立判断是否获得');
  300. // 标签选择
  301. $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
  302. $form->multipleSelect('tags', '标签')
  303. ->options($tagRepository->getActiveTagOptions())
  304. ->help('可以选择多个标签来分类管理奖励组');
  305. $form->display('created_at', '创建时间');
  306. $form->display('updated_at', '更新时间');
  307. // 添加提示信息,告知用户如何管理奖励项
  308. if ($form->isEditing()) {
  309. $id = $form->getKey();
  310. $form->html('<div class="alert alert-info">
  311. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
  312. <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
  313. <i class="fa fa-list"></i> 管理奖励项
  314. </a>
  315. </div>');
  316. } else {
  317. $form->html('<div class="alert alert-info">
  318. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
  319. </div>');
  320. }
  321. });
  322. }
  323. /**
  324. * 格式化数量文本
  325. *
  326. * @param GameRewardItem $item
  327. * @return string
  328. */
  329. private function formatQuantityText(GameRewardItem $item): string
  330. {
  331. // 如果设置了最小和最大数量,显示范围
  332. if ($item->min_quantity !== null && $item->max_quantity !== null) {
  333. if ($item->min_quantity == $item->max_quantity) {
  334. return (string)$item->min_quantity;
  335. } else {
  336. return $item->min_quantity . '-' . $item->max_quantity;
  337. }
  338. }
  339. // 如果只设置了最小数量
  340. if ($item->min_quantity !== null) {
  341. return $item->min_quantity . '+';
  342. }
  343. // 如果只设置了最大数量
  344. if ($item->max_quantity !== null) {
  345. return '1-' . $item->max_quantity;
  346. }
  347. // 默认使用固定数量
  348. return (string)$item->quantity;
  349. }
  350. /**
  351. * 根据背景色获取对比色
  352. *
  353. * @param string $hexColor
  354. * @return string
  355. */
  356. private function getContrastColor(string $hexColor): string
  357. {
  358. $hexColor = ltrim($hexColor, '#');
  359. $r = hexdec(substr($hexColor, 0, 2));
  360. $g = hexdec(substr($hexColor, 2, 2));
  361. $b = hexdec(substr($hexColor, 4, 2));
  362. $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
  363. return $brightness > 128 ? '#000000' : '#ffffff';
  364. }
  365. }