| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269 |
- <?php
- namespace App\Module\Pet\Logic;
- use App\Module\Pet\Models\PetLevelConfig;
- use App\Module\Pet\Models\PetUser;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物战斗逻辑类
- *
- * 处理宠物战斗相关的业务逻辑,包括战力计算、战斗结果计算、
- * 奖励发放等功能。该类是宠物战斗模块内部使用的核心逻辑类,
- * 不对外提供服务,由PetStealService调用。
- */
- class PetBattleLogic
- {
- /**
- * 获取操作体力消耗
- *
- * @param int $actionType 操作类型
- * @return int 体力消耗
- */
- public function getActionStaminaCost(int $actionType): int
- {
- // 根据操作类型返回不同的体力消耗
- switch ($actionType) {
- case 1: // 偷菜操作
- return config('pet.action_stamina_cost.steal', 10);
- case 2: // 防御操作
- return config('pet.action_stamina_cost.defend', 15);
- default:
- return 10; // 默认消耗10点体力
- }
- }
- /**
- * 执行操作
- *
- * @param int $petId 宠物ID
- * @param int $actionType 操作类型
- * @param int $targetId 目标ID
- * @return array 操作结果
- * @throws Exception
- */
- public function executeAction(int $petId, int $actionType, int $targetId): array
- {
- // 获取宠物信息
- $pet = PetUser::findOrFail($petId);
- // 根据操作类型执行不同的逻辑
- switch ($actionType) {
- case 1: // 偷菜操作
- return $this->executeStealAction($pet, $targetId);
- case 2: // 防御操作
- return $this->executeDefendAction($pet, $targetId);
- default:
- throw new Exception("未知的操作类型");
- }
- }
- /**
- * 执行偷菜操作
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标用户ID
- * @return array 操作结果
- */
- protected function executeStealAction(PetUser $pet, int $targetId): array
- {
- // 简化的偷菜逻辑,不包含战斗
- // 这里可以根据实际需求实现偷菜的基本逻辑
- // 模拟偷菜成功率(基于宠物等级)
- $baseSuccessRate = 70; // 基础成功率70%
- $levelBonus = $pet->level * 2; // 每级增加2%成功率
- $successRate = min(90, $baseSuccessRate + $levelBonus); // 最高90%
- // 随机决定是否成功
- $random = mt_rand(1, 100);
- $success = $random <= $successRate;
- // 计算奖励
- $rewards = [];
- if ($success) {
- // 成功偷菜,获得简单奖励
- $rewards = [
- 'exp' => mt_rand(5, 15),
- 'items' => [
- [
- 'item_id' => 1001,
- 'name' => '作物',
- 'amount' => mt_rand(1, 3)
- ]
- ]
- ];
- }
- return [
- 'success' => $success,
- 'target_id' => $targetId,
- 'success_rate' => $successRate,
- 'rewards' => $rewards
- ];
- }
- /**
- * 执行防御操作
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标用户ID
- * @return array 操作结果
- */
- protected function executeDefendAction(PetUser $pet, int $targetId): array
- {
- // 计算宠物战力
- $petPower = $this->calculatePetPower($pet->id);
- // 获取目标用户的偷菜宠物
- // 这里需要根据实际项目中的农场模块接口进行实现
- // 暂时使用模拟数据
- $targetPetPower = mt_rand(800, 1500); // 模拟目标宠物战力
- // 计算成功率
- $successRate = $this->calculateDefendSuccessRate($petPower, $targetPetPower);
- // 随机决定是否成功
- $random = mt_rand(1, 100);
- $success = $random <= $successRate;
- // 计算奖励
- $rewards = [];
- if ($success) {
- // 成功守护,获得奖励
- $rewards = $this->calculateDefendRewards($pet);
- }
- // 记录战斗详情
- $details = [
- 'pet_power' => $petPower,
- 'target_pet_power' => $targetPetPower,
- 'success_rate' => $successRate,
- 'random_roll' => $random,
- 'success' => $success
- ];
- // 返回战斗结果
- return [
- 'success' => $success,
- 'opponent_id' => $targetId,
- 'opponent' => [
- 'user_id' => $targetId,
- 'pet_power' => $targetPetPower
- ],
- 'rewards' => $rewards,
- 'details' => $details
- ];
- }
- /**
- * 计算宠物战力
- *
- * @param int $petId 宠物ID
- * @return int 战力值
- */
- public function calculatePetPower(int $petId): int
- {
- // 获取宠物信息
- $pet = PetUser::findOrFail($petId);
- // 获取宠物等级配置
- $levelConfig = PetLevelConfig::where('level', $pet->level)->first();
- // 基础战力
- $basePower = $levelConfig ? ($levelConfig->numeric_attributes['base_power'] ?? 100) : 100;
- // 记录日志
- Log::info('计算宠物战力', [
- 'pet_id' => $petId,
- 'level' => $pet->level,
- 'base_power' => $basePower,
- 'final_power' => $basePower
- ]);
- return (int)$basePower;
- }
- /**
- * 计算守护成功率
- *
- * @param int $petPower 宠物战力
- * @param int $targetPetPower 目标宠物战力
- * @return int 成功率(百分比)
- */
- protected function calculateDefendSuccessRate(int $petPower, int $targetPetPower): int
- {
- // 基础成功率
- $baseRate = 60;
- // 根据战力差计算成功率
- $powerDiff = $petPower - $targetPetPower;
- $rateAdjustment = $powerDiff / 100; // 每100点战力差调整1%成功率
- // 最终成功率,限制在20%-95%之间
- $finalRate = max(20, min(95, $baseRate + $rateAdjustment));
- return (int)$finalRate;
- }
- /**
- * 计算守护奖励
- *
- * @param PetUser $pet 宠物对象
- * @return array 奖励列表
- */
- protected function calculateDefendRewards(PetUser $pet): array
- {
- // 这里需要根据实际项目中的奖励系统进行实现
- // 暂时使用模拟数据
- // 计算守护奖励和经验值
- $rewards = [
- 'exp' => mt_rand(10, 20), // 获得10-20点经验
- 'items' => []
- ];
- // 随机获得守护奖励物品
- $rewardItems = [
- ['item_id' => 2001, 'name' => '守护徽章', 'probability' => 0.3, 'amount' => 1],
- ['item_id' => 2002, 'name' => '防御药水', 'probability' => 0.5, 'amount' => 1],
- ['item_id' => 2003, 'name' => '农场币', 'probability' => 1.0, 'amount_min' => 10, 'amount_max' => 50]
- ];
- foreach ($rewardItems as $item) {
- $random = mt_rand(1, 100) / 100;
- if ($random <= $item['probability']) {
- $amount = isset($item['amount_min']) ?
- mt_rand($item['amount_min'], $item['amount_max']) :
- $item['amount'];
- $rewards['items'][] = [
- 'item_id' => $item['item_id'],
- 'name' => $item['name'],
- 'amount' => $amount
- ];
- }
- }
- return $rewards;
- }
- }
|