GameRewardGroupController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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', 'tags']);
  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('tags', '标签')->display(function () {
  48. return $this->formatTags();
  49. })->width(200);
  50. // 奖励详情列
  51. $grid->column('reward_details', '奖励详情')->display(function () {
  52. return $this->formatRewardDetails();
  53. })->width(300);
  54. $grid->column('created_at', '创建时间');
  55. $grid->column('updated_at', '更新时间');
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $filter->equal('id', 'ID');
  58. $filter->like('name', '名称');
  59. $filter->like('code', '编码');
  60. $filter->equal('is_random', '随机发放')->select([0 => '否', 1 => '是']);
  61. // 标签筛选
  62. $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
  63. $filter->whereHas('tags', '标签', function ($query) {
  64. $query->where('id', request('tags'));
  65. })->select($tagRepository->getActiveTagOptions());
  66. });
  67. // 使用 RowAction 类添加行操作按钮
  68. $grid->actions(function (Grid\Displayers\Actions $actions) {
  69. // 添加复制按钮
  70. $actions->append(new DuplicateRewardGroupAction());
  71. $actions->disableDelete();
  72. });
  73. });
  74. }
  75. /**
  76. * 创建详情页
  77. *
  78. * @param mixed $id
  79. * @return Show
  80. */
  81. protected function detail($id)
  82. {
  83. return Show::make($id, new GameRewardGroupRepository(['tags']), function (Show $show) use ($id) {
  84. $show->field('id', 'ID');
  85. $show->field('name', '名称');
  86. $show->field('code', '编码');
  87. $show->field('description', '描述');
  88. $show->field('is_random', '随机发放')->using([0 => '否', 1 => '是']);
  89. $show->field('random_count', '随机数量');
  90. // 标签显示
  91. $show->field('tags', '标签')->as(function ($tags) {
  92. if (empty($tags)) {
  93. return '<span class="text-muted">无标签</span>';
  94. }
  95. $tagHtml = [];
  96. foreach ($tags as $tag) {
  97. $tagHtml[] = sprintf(
  98. '<span class="badge" style="background-color: %s; color: %s;">%s</span>',
  99. $tag['color'],
  100. $this->getContrastColor($tag['color']),
  101. $tag['name']
  102. );
  103. }
  104. return implode(' ', $tagHtml);
  105. });
  106. $show->field('created_at', '创建时间');
  107. $show->field('updated_at', '更新时间');
  108. // 显示奖励项
  109. $show->divider();
  110. $show->field('reward_items', '奖励项')->unescape()->as(function () use ($id) {
  111. // 获取奖励组及其奖励项
  112. $group = GameRewardGroup::with('rewardItems')->find($id);
  113. if (!$group || $group->rewardItems->isEmpty()) {
  114. return '<span class="text-muted">暂无奖励项</span>';
  115. }
  116. $html = '<div class="reward-items-list">';
  117. foreach ($group->rewardItems as $item) {
  118. $rewardTypeName = REWARD_TYPE::getName($item->reward_type);
  119. $targetName = $group->getTargetName($item);
  120. $quantity = $item->quantity;
  121. $weight = $item->weight;
  122. $guaranteed = $item->is_guaranteed ? '必中' : '非必中';
  123. $html .= '<div class="reward-item mb-2 p-2 border rounded">';
  124. $html .= '<span class="badge badge-info mr-2">' . $rewardTypeName . '</span>';
  125. $html .= '<strong>' . $targetName . '</strong>';
  126. $html .= ' × ' . $quantity;
  127. $html .= '<br><small class="text-muted">';
  128. $html .= '权重: ' . $weight . ' | ' . $guaranteed;
  129. if ($item->param1 || $item->param2) {
  130. $html .= ' | 参数: ' . $item->param1 . ',' . $item->param2;
  131. }
  132. $html .= '</small>';
  133. $html .= '</div>';
  134. }
  135. $html .= '</div>';
  136. // 添加管理链接
  137. $html .= '<div class="mt-3">';
  138. $html .= '<a href="' . admin_url('game-reward-items?group_id=' . $id) . '" target="_blank" class="btn btn-sm btn-primary">';
  139. $html .= '<i class="fa fa-list"></i> 管理奖励项';
  140. $html .= '</a>';
  141. $html .= '</div>';
  142. return $html;
  143. });
  144. });
  145. }
  146. /**
  147. * 获取奖励类型名称
  148. *
  149. * @param int $rewardType 奖励类型
  150. * @return string 奖励类型名称
  151. */
  152. public function getRewardTypeName(int $rewardType): string
  153. {
  154. switch ($rewardType) {
  155. case REWARD_TYPE::ITEM->value:
  156. return '物品';
  157. case REWARD_TYPE::CURRENCY->value:
  158. return '货币';
  159. case REWARD_TYPE::PET_EXP->value:
  160. return '宠物经验';
  161. case REWARD_TYPE::PET_ENERGY->value:
  162. return '宠物体力';
  163. case REWARD_TYPE::OTHER->value:
  164. return '其他';
  165. default:
  166. return '未知';
  167. }
  168. }
  169. /**
  170. * 根据奖励项获取实际内容描述
  171. *
  172. * @param GameRewardItem $item 奖励项
  173. * @return string 奖励内容描述
  174. */
  175. public function getRewardContent(GameRewardItem $item): string
  176. {
  177. switch ($item->reward_type) {
  178. case REWARD_TYPE::ITEM->value:
  179. // 物品奖励
  180. try {
  181. $gameItem = Item::find($item->target_id);
  182. if ($gameItem) {
  183. $content = "<strong>{$gameItem->name}</strong> (ID: {$item->target_id})";
  184. // 添加参数说明
  185. $params = [];
  186. if ($item->param1 > 0) {
  187. $params[] = "品质: {$item->param1}";
  188. }
  189. if ($item->param2 > 0) {
  190. $params[] = "绑定: {$item->param2}";
  191. }
  192. if (!empty($params)) {
  193. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  194. }
  195. return $content;
  196. }
  197. } catch (\Exception $e) {
  198. // 忽略异常,返回默认内容
  199. }
  200. return "物品 (ID: {$item->target_id})";
  201. case REWARD_TYPE::CURRENCY->value:
  202. // 货币奖励
  203. try {
  204. $currency = FundCurrencyModel::find($item->target_id);
  205. if ($currency) {
  206. $content = "<strong>{$currency->name}</strong> (ID: {$item->target_id})";
  207. // 添加参数说明
  208. $params = [];
  209. if ($item->param1 > 0) {
  210. $params[] = "来源: {$item->param1}";
  211. }
  212. if (!empty($params)) {
  213. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  214. }
  215. return $content;
  216. }
  217. } catch (\Exception $e) {
  218. // 忽略异常,返回默认内容
  219. }
  220. return "货币 (ID: {$item->target_id})";
  221. case REWARD_TYPE::PET_EXP->value:
  222. // 宠物经验奖励
  223. return "宠物经验 (宠物ID: {$item->target_id})";
  224. case REWARD_TYPE::PET_ENERGY->value:
  225. // 宠物体力奖励
  226. return "宠物体力 (宠物ID: {$item->target_id})";
  227. case REWARD_TYPE::FARM_SHRINE->value:
  228. // 神像奖励(农场buff)
  229. try {
  230. $shrineName = \App\Module\Farm\Enums\BUFF_TYPE::getName($item->target_id);
  231. $durationHours = $item->param2 > 0 ? $item->param2 : 24;
  232. $content = "<strong>{$shrineName}</strong> (类型: {$item->target_id})";
  233. $content .= "<br><small>持续时间: {$durationHours}小时</small>";
  234. // 添加参数说明
  235. $params = [];
  236. if ($item->param1 > 0) {
  237. $params[] = "神像类型: {$item->param1}";
  238. }
  239. if ($item->param2 > 0) {
  240. $params[] = "持续时间: {$item->param2}小时";
  241. }
  242. if (!empty($params)) {
  243. $content .= "<br><small>" . implode(', ', $params) . "</small>";
  244. }
  245. return $content;
  246. } catch (\Exception $e) {
  247. return "神像 (类型: {$item->target_id})";
  248. }
  249. case REWARD_TYPE::OTHER->value:
  250. // 其他奖励
  251. return "其他奖励 (ID: {$item->target_id})";
  252. default:
  253. return "未知奖励类型 (ID: {$item->target_id})";
  254. }
  255. }
  256. /**
  257. * 创建表单
  258. *
  259. * @return Form
  260. */
  261. protected function form()
  262. {
  263. return Form::make(new GameRewardGroupRepository(['tags']), function (Form $form) {
  264. $form->display('id', 'ID');
  265. $form->text('name', '名称')->required();
  266. $form->text('code', '编码')->required()->rules('required|alpha_dash|unique:game_reward_groups,code,' . $form->getKey());
  267. $form->textarea('description', '描述');
  268. $form->switch('is_random', '随机发放')->default(0);
  269. $form->number('random_count', '随机数量')->default(1)->min(1)->help('随机发放时的奖励数量,仅当随机发放开启时有效');
  270. // 标签选择
  271. $tagRepository = new \App\Module\Game\Repositorys\GameTagRepository();
  272. $form->multipleSelect('tags', '标签')
  273. ->options($tagRepository->getActiveTagOptions())
  274. ->help('可以选择多个标签来分类管理奖励组');
  275. $form->display('created_at', '创建时间');
  276. $form->display('updated_at', '更新时间');
  277. // 添加提示信息,告知用户如何管理奖励项
  278. if ($form->isEditing()) {
  279. $id = $form->getKey();
  280. $form->html('<div class="alert alert-info">
  281. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请保存当前表单后使用以下链接管理奖励项:</p>
  282. <a href="'.admin_url('game-reward-items?group_id='.$id).'" class="btn btn-primary" target="_blank">
  283. <i class="fa fa-list"></i> 管理奖励项
  284. </a>
  285. </div>');
  286. } else {
  287. $form->html('<div class="alert alert-info">
  288. <p><i class="fa fa-info-circle"></i> 奖励项管理已移至单独页面,请先保存当前表单,然后才能添加奖励项。</p>
  289. </div>');
  290. }
  291. });
  292. }
  293. /**
  294. * 根据背景色获取对比色
  295. *
  296. * @param string $hexColor
  297. * @return string
  298. */
  299. private function getContrastColor(string $hexColor): string
  300. {
  301. $hexColor = ltrim($hexColor, '#');
  302. $r = hexdec(substr($hexColor, 0, 2));
  303. $g = hexdec(substr($hexColor, 2, 2));
  304. $b = hexdec(substr($hexColor, 4, 2));
  305. $brightness = ($r * 299 + $g * 587 + $b * 114) / 1000;
  306. return $brightness > 128 ? '#000000' : '#ffffff';
  307. }
  308. }