ChestService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace App\Module\GameItems\Services;
  3. use App\Module\Game\Services\ConsumeService;
  4. use App\Module\Game\Services\RewardService;
  5. use App\Module\GameItems\Enums\ITEM_TYPE;
  6. use App\Module\GameItems\Models\Item;
  7. use App\Module\GameItems\Models\ItemChestOpenLog;
  8. use App\Module\GameItems\Models\ItemChestConfig;
  9. use Exception;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. use UCore\Dto\Res;
  13. /**
  14. * 宝箱服务类 - 使用消耗组/奖励组系统
  15. *
  16. * 提供基于消耗组和奖励组的宝箱开启服务,替代原有的宝箱内容配置系统。
  17. * 该版本使用统一的组系统架构,提供更灵活的宝箱配置和管理功能。
  18. */
  19. class ChestService
  20. {
  21. /**
  22. * 开启宝箱
  23. *
  24. * @param int $userId 用户ID
  25. * @param int $chestId 宝箱ID
  26. * @param int $quantity 开启数量
  27. * @param array $options 选项
  28. * @return Res 开启结果
  29. * @throws Exception
  30. */
  31. public static function openChest(int $userId, int $chestId, int $quantity = 1, array $options = []): Res
  32. {
  33. // 获取宝箱信息
  34. $chest = Item::findOrFail($chestId);
  35. // 检查是否为宝箱类型
  36. if ($chest->type != ITEM_TYPE::CHEST) {
  37. throw new Exception("物品 {$chestId} 不是宝箱类型");
  38. }
  39. // 获取宝箱配置
  40. $chestConfig = ItemChestConfig::with([ 'consumeGroup', 'rewardGroup', 'conditionGroup' ])
  41. ->where('item_id', $chestId)
  42. ->where('is_active', true)
  43. ->first();
  44. if (!$chestConfig) {
  45. throw new Exception("宝箱 {$chestId} 没有配置新系统或配置未激活");
  46. }
  47. if (!$chestConfig->reward_group_id) {
  48. throw new Exception("宝箱 {$chestId} 没有配置奖励组");
  49. }
  50. $allResults = [];
  51. // 第一步:先扣除消耗组(如果有消耗组配置,支持倍率)
  52. if ($chestConfig->consume_group_id) {
  53. $consumeResult = ConsumeService::executeConsume(
  54. $userId,
  55. $chestConfig->consume_group_id,
  56. "开启宝箱",
  57. $chestId,
  58. true, // 检查消耗条件
  59. $quantity // 使用开启数量作为倍率
  60. );
  61. if (!$consumeResult->success) {
  62. throw new Exception("消耗验证失败: " . $consumeResult->message);
  63. }
  64. }
  65. // 第二步:再扣除宝箱本身(一次性扣除所有宝箱)
  66. $chestConsumeResult = ItemService::consumeItem(
  67. $userId,
  68. $chestId,
  69. null,
  70. $quantity,
  71. [
  72. 'source_type' => 'chest_open',
  73. 'source_id' => $chestId,
  74. 'details' => [ 'quantity' => $quantity ],
  75. 'ip_address' => $options['ip_address'] ?? null,
  76. 'device_info' => $options['device_info'] ?? null,
  77. ]
  78. );
  79. if (!$chestConsumeResult['success']) {
  80. throw new Exception("消耗宝箱失败: " . $chestConsumeResult['message']);
  81. }
  82. // 第三步:最后循环产出奖励
  83. for ($i = 0; $i < $quantity; $i++) {
  84. // 发放奖励(支持保底机制)
  85. $rewardResult = RewardService::grantRewardWithPity(
  86. $userId,
  87. $chestConfig->reward_group_id,
  88. 'chest_open',
  89. $chestId,
  90. true // 启用保底机制
  91. );
  92. if (!$rewardResult->success) {
  93. throw new Exception("发放奖励失败: " . ($rewardResult->errorMessage ?? '未知错误'));
  94. }
  95. // 记录本次开启结果
  96. $allResults[] = $rewardResult->items;
  97. }
  98. // 记录宝箱开启日志
  99. $openLog = ItemChestOpenLog::create([
  100. 'user_id' => $userId,
  101. 'chest_id' => $chestId,
  102. 'open_quantity' => $quantity,
  103. 'result_items' => $allResults,
  104. 'pity_triggered' => false, // V2版本暂不支持保底机制
  105. 'pity_content_id' => null,
  106. 'open_time' => now(),
  107. 'ip_address' => $options['ip_address'] ?? null,
  108. 'device_info' => $options['device_info'] ?? null,
  109. ]);
  110. return Res::success('成功', [
  111. 'openLog' => $openLog->id
  112. ]);
  113. }
  114. /**
  115. * 获取宝箱内容预览
  116. *
  117. * @param int $chestId 宝箱ID
  118. * @return array 宝箱内容预览
  119. */
  120. public static function getChestContentPreview(int $chestId): array
  121. {
  122. // 获取宝箱信息
  123. $chest = Item::findOrFail($chestId);
  124. // 检查是否为宝箱类型
  125. if ($chest->type != ITEM_TYPE::CHEST) {
  126. throw new Exception("物品 {$chestId} 不是宝箱类型");
  127. }
  128. // 获取宝箱配置
  129. $chestConfig = ItemChestConfig::with([
  130. 'consumeGroup.consumeItems', 'rewardGroup.rewardItems',
  131. 'conditionGroup'
  132. ])
  133. ->where('item_id', $chestId)
  134. ->where('is_active', true)
  135. ->first();
  136. $preview = [
  137. 'chest_id' => $chestId,
  138. 'chest_name' => $chest->name,
  139. 'version' => 'new',
  140. 'consume_group' => null,
  141. 'reward_group' => null,
  142. 'condition_group' => null,
  143. 'config_status' => $chestConfig ? 'active' : 'not_configured',
  144. ];
  145. if (!$chestConfig) {
  146. return $preview;
  147. }
  148. // 获取消耗组信息
  149. if ($chestConfig->consumeGroup) {
  150. $preview['consume_group'] = [
  151. 'id' => $chestConfig->consumeGroup->id,
  152. 'name' => $chestConfig->consumeGroup->name,
  153. 'code' => $chestConfig->consumeGroup->code,
  154. 'description' => $chestConfig->consumeGroup->description,
  155. 'items' => $chestConfig->consumeGroup->consumeItems->map(function ($item) {
  156. return [
  157. 'consume_type' => $item->consume_type,
  158. 'target_id' => $item->target_id,
  159. 'quantity' => $item->quantity,
  160. ];
  161. })->toArray(),
  162. ];
  163. }
  164. // 获取奖励组信息
  165. if ($chestConfig->rewardGroup) {
  166. $preview['reward_group'] = [
  167. 'id' => $chestConfig->rewardGroup->id,
  168. 'name' => $chestConfig->rewardGroup->name,
  169. 'code' => $chestConfig->rewardGroup->code,
  170. 'description' => $chestConfig->rewardGroup->description,
  171. 'is_random' => $chestConfig->rewardGroup->is_random,
  172. 'random_count' => $chestConfig->rewardGroup->random_count,
  173. 'reward_mode' => $chestConfig->rewardGroup->reward_mode,
  174. 'items' => $chestConfig->rewardGroup->rewardItems->map(function ($item) {
  175. return [
  176. 'reward_type' => $item->reward_type,
  177. 'target_id' => $item->target_id,
  178. 'quantity' => $item->quantity,
  179. 'weight' => $item->weight,
  180. 'is_guaranteed' => $item->is_guaranteed,
  181. ];
  182. })->toArray(),
  183. ];
  184. }
  185. // 获取条件组信息
  186. if ($chestConfig->conditionGroup) {
  187. $preview['condition_group'] = [
  188. 'id' => $chestConfig->conditionGroup->id,
  189. 'name' => $chestConfig->conditionGroup->name,
  190. 'code' => $chestConfig->conditionGroup->code,
  191. 'description' => $chestConfig->conditionGroup->description,
  192. ];
  193. }
  194. return $preview;
  195. }
  196. /**
  197. * 检查宝箱是否可以开启
  198. *
  199. * @param int $userId 用户ID
  200. * @param int $chestId 宝箱ID
  201. * @param int $quantity 开启数量
  202. * @return array 检查结果
  203. */
  204. public static function canOpenChest(int $userId, int $chestId, int $quantity = 1): array
  205. {
  206. try {
  207. // 获取宝箱信息
  208. $chest = Item::findOrFail($chestId);
  209. // 检查是否为宝箱类型
  210. if ($chest->type != ITEM_TYPE::CHEST) {
  211. return [
  212. 'can_open' => false,
  213. 'message' => "物品 {$chestId} 不是宝箱类型",
  214. ];
  215. }
  216. // 获取宝箱配置
  217. $chestConfig = ItemChestConfig::with([ 'consumeGroup' ])
  218. ->where('item_id', $chestId)
  219. ->where('is_active', true)
  220. ->first();
  221. if (!$chestConfig) {
  222. return [
  223. 'can_open' => false,
  224. 'message' => "宝箱 {$chestId} 没有配置新系统或配置未激活",
  225. ];
  226. }
  227. // 检查是否配置了奖励组
  228. if (!$chestConfig->reward_group_id) {
  229. return [
  230. 'can_open' => false,
  231. 'message' => "宝箱 {$chestId} 没有配置奖励组",
  232. ];
  233. }
  234. // 检查用户是否拥有足够的宝箱
  235. $checkResult = ItemService::checkItemQuantity($userId, $chestId, $quantity);
  236. if (!$checkResult->success) {
  237. return [
  238. 'can_open' => false,
  239. 'message' => $checkResult->message,
  240. ];
  241. }
  242. // 检查消耗组(如果有,支持倍率检查)
  243. if ($chestConfig->consume_group_id) {
  244. $canConsume = ConsumeService::checkConsume($userId, $chestConfig->consume_group_id, $quantity);
  245. if (!$canConsume->success) {
  246. return [
  247. 'can_open' => false,
  248. 'message' => "消耗验证失败: " . $canConsume->message,
  249. ];
  250. }
  251. }
  252. return [
  253. 'can_open' => true,
  254. 'message' => '可以开启',
  255. ];
  256. } catch (Exception $e) {
  257. return [
  258. 'can_open' => false,
  259. 'message' => "检查失败: " . $e->getMessage(),
  260. ];
  261. }
  262. }
  263. }