ChestService.php 11 KB

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