PetBattleLogic.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. namespace App\Module\Pet\Logic;
  3. use App\Module\Pet\Models\PetLevelConfig;
  4. use App\Module\Pet\Models\PetUser;
  5. use Exception;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 宠物战斗逻辑类
  9. *
  10. * 处理宠物战斗相关的业务逻辑,包括战力计算、战斗结果计算、
  11. * 奖励发放等功能。该类是宠物战斗模块内部使用的核心逻辑类,
  12. * 不对外提供服务,由PetStealService调用。
  13. */
  14. class PetBattleLogic
  15. {
  16. /**
  17. * 获取操作体力消耗
  18. *
  19. * @param int $actionType 操作类型
  20. * @return int 体力消耗
  21. */
  22. public function getActionStaminaCost(int $actionType): int
  23. {
  24. // 根据操作类型返回不同的体力消耗
  25. switch ($actionType) {
  26. case 1: // 偷菜操作
  27. return config('pet.action_stamina_cost.steal', 10);
  28. case 2: // 防御操作
  29. return config('pet.action_stamina_cost.defend', 15);
  30. default:
  31. return 10; // 默认消耗10点体力
  32. }
  33. }
  34. /**
  35. * 执行操作
  36. *
  37. * @param int $petId 宠物ID
  38. * @param int $actionType 操作类型
  39. * @param int $targetId 目标ID
  40. * @return array 操作结果
  41. * @throws Exception
  42. */
  43. public function executeAction(int $petId, int $actionType, int $targetId): array
  44. {
  45. // 获取宠物信息
  46. $pet = PetUser::findOrFail($petId);
  47. // 根据操作类型执行不同的逻辑
  48. switch ($actionType) {
  49. case 1: // 偷菜操作
  50. return $this->executeStealAction($pet, $targetId);
  51. case 2: // 防御操作
  52. return $this->executeDefendAction($pet, $targetId);
  53. default:
  54. throw new Exception("未知的操作类型");
  55. }
  56. }
  57. /**
  58. * 执行偷菜操作
  59. *
  60. * @param PetUser $pet 宠物对象
  61. * @param int $targetId 目标用户ID
  62. * @return array 操作结果
  63. */
  64. protected function executeStealAction(PetUser $pet, int $targetId): array
  65. {
  66. // 简化的偷菜逻辑,不包含战斗
  67. // 这里可以根据实际需求实现偷菜的基本逻辑
  68. // 模拟偷菜成功率(基于宠物等级)
  69. $baseSuccessRate = 70; // 基础成功率70%
  70. $levelBonus = $pet->level * 2; // 每级增加2%成功率
  71. $successRate = min(90, $baseSuccessRate + $levelBonus); // 最高90%
  72. // 随机决定是否成功
  73. $random = mt_rand(1, 100);
  74. $success = $random <= $successRate;
  75. // 计算奖励
  76. $rewards = [];
  77. if ($success) {
  78. // 成功偷菜,获得简单奖励
  79. $rewards = [
  80. 'exp' => mt_rand(5, 15),
  81. 'items' => [
  82. [
  83. 'item_id' => 1001,
  84. 'name' => '作物',
  85. 'amount' => mt_rand(1, 3)
  86. ]
  87. ]
  88. ];
  89. }
  90. return [
  91. 'success' => $success,
  92. 'target_id' => $targetId,
  93. 'success_rate' => $successRate,
  94. 'rewards' => $rewards
  95. ];
  96. }
  97. /**
  98. * 执行防御操作
  99. *
  100. * @param PetUser $pet 宠物对象
  101. * @param int $targetId 目标用户ID
  102. * @return array 操作结果
  103. */
  104. protected function executeDefendAction(PetUser $pet, int $targetId): array
  105. {
  106. // 计算宠物战力
  107. $petPower = $this->calculatePetPower($pet->id);
  108. // 获取目标用户的偷菜宠物
  109. // 这里需要根据实际项目中的农场模块接口进行实现
  110. // 暂时使用模拟数据
  111. $targetPetPower = mt_rand(800, 1500); // 模拟目标宠物战力
  112. // 计算成功率
  113. $successRate = $this->calculateDefendSuccessRate($petPower, $targetPetPower);
  114. // 随机决定是否成功
  115. $random = mt_rand(1, 100);
  116. $success = $random <= $successRate;
  117. // 计算奖励
  118. $rewards = [];
  119. if ($success) {
  120. // 成功守护,获得奖励
  121. $rewards = $this->calculateDefendRewards($pet);
  122. }
  123. // 记录战斗详情
  124. $details = [
  125. 'pet_power' => $petPower,
  126. 'target_pet_power' => $targetPetPower,
  127. 'success_rate' => $successRate,
  128. 'random_roll' => $random,
  129. 'success' => $success
  130. ];
  131. // 返回战斗结果
  132. return [
  133. 'success' => $success,
  134. 'opponent_id' => $targetId,
  135. 'opponent' => [
  136. 'user_id' => $targetId,
  137. 'pet_power' => $targetPetPower
  138. ],
  139. 'rewards' => $rewards,
  140. 'details' => $details
  141. ];
  142. }
  143. /**
  144. * 计算宠物战力
  145. *
  146. * @param int $petId 宠物ID
  147. * @return int 战力值
  148. */
  149. public function calculatePetPower(int $petId): int
  150. {
  151. // 获取宠物信息
  152. $pet = PetUser::findOrFail($petId);
  153. // 获取宠物等级配置
  154. $levelConfig = PetLevelConfig::where('level', $pet->level)->first();
  155. // 基础战力
  156. $basePower = $levelConfig ? ($levelConfig->numeric_attributes['base_power'] ?? 100) : 100;
  157. // 记录日志
  158. Log::info('计算宠物战力', [
  159. 'pet_id' => $petId,
  160. 'level' => $pet->level,
  161. 'base_power' => $basePower,
  162. 'final_power' => $basePower
  163. ]);
  164. return (int)$basePower;
  165. }
  166. /**
  167. * 计算守护成功率
  168. *
  169. * @param int $petPower 宠物战力
  170. * @param int $targetPetPower 目标宠物战力
  171. * @return int 成功率(百分比)
  172. */
  173. protected function calculateDefendSuccessRate(int $petPower, int $targetPetPower): int
  174. {
  175. // 基础成功率
  176. $baseRate = 60;
  177. // 根据战力差计算成功率
  178. $powerDiff = $petPower - $targetPetPower;
  179. $rateAdjustment = $powerDiff / 100; // 每100点战力差调整1%成功率
  180. // 最终成功率,限制在20%-95%之间
  181. $finalRate = max(20, min(95, $baseRate + $rateAdjustment));
  182. return (int)$finalRate;
  183. }
  184. /**
  185. * 计算守护奖励
  186. *
  187. * @param PetUser $pet 宠物对象
  188. * @return array 奖励列表
  189. */
  190. protected function calculateDefendRewards(PetUser $pet): array
  191. {
  192. // 这里需要根据实际项目中的奖励系统进行实现
  193. // 暂时使用模拟数据
  194. // 计算守护奖励和经验值
  195. $rewards = [
  196. 'exp' => mt_rand(10, 20), // 获得10-20点经验
  197. 'items' => []
  198. ];
  199. // 随机获得守护奖励物品
  200. $rewardItems = [
  201. ['item_id' => 2001, 'name' => '守护徽章', 'probability' => 0.3, 'amount' => 1],
  202. ['item_id' => 2002, 'name' => '防御药水', 'probability' => 0.5, 'amount' => 1],
  203. ['item_id' => 2003, 'name' => '农场币', 'probability' => 1.0, 'amount_min' => 10, 'amount_max' => 50]
  204. ];
  205. foreach ($rewardItems as $item) {
  206. $random = mt_rand(1, 100) / 100;
  207. if ($random <= $item['probability']) {
  208. $amount = isset($item['amount_min']) ?
  209. mt_rand($item['amount_min'], $item['amount_max']) :
  210. $item['amount'];
  211. $rewards['items'][] = [
  212. 'item_id' => $item['item_id'],
  213. 'name' => $item['name'],
  214. 'amount' => $amount
  215. ];
  216. }
  217. }
  218. return $rewards;
  219. }
  220. }