| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552 |
- <?php
- namespace App\Module\Pet\Logic;
- use App\Module\Pet\Enums\PetGrade;
- use App\Module\Pet\Models\PetLevelConfig;
- use App\Module\Pet\Models\PetUser;
- use Exception;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物战斗逻辑类
- *
- * 处理宠物战斗相关的业务逻辑,包括战力计算、战斗结果计算、
- * 奖励发放等功能。该类是宠物战斗模块内部使用的核心逻辑类,
- * 不对外提供服务,由PetBattleService调用。
- */
- class PetBattleLogic
- {
- /**
- * 获取战斗体力消耗
- *
- * @param int $battleType 战斗类型
- * @return int 体力消耗
- */
- public function getBattleStaminaCost(int $battleType): int
- {
- // 根据战斗类型返回不同的体力消耗
- switch ($battleType) {
- case 1: // 偷菜战斗
- return config('pet.battle_stamina_cost.steal', 10);
-
- case 2: // 守护战斗
- return config('pet.battle_stamina_cost.defend', 15);
-
- case 3: // 争霸赛战斗
- return config('pet.battle_stamina_cost.royale', 20);
-
- default:
- return 10; // 默认消耗10点体力
- }
- }
-
- /**
- * 执行战斗
- *
- * @param int $petId 宠物ID
- * @param int $battleType 战斗类型
- * @param int $targetId 目标ID
- * @return array 战斗结果
- * @throws Exception
- */
- public function executeBattle(int $petId, int $battleType, int $targetId): array
- {
- // 获取宠物信息
- $pet = PetUser::findOrFail($petId);
-
- // 根据战斗类型执行不同的战斗逻辑
- switch ($battleType) {
- case 1: // 偷菜战斗
- return $this->executeStealBattle($pet, $targetId);
-
- case 2: // 守护战斗
- return $this->executeDefendBattle($pet, $targetId);
-
- case 3: // 争霸赛战斗
- return $this->executeRoyaleBattle($pet, $targetId);
-
- default:
- throw new Exception("未知的战斗类型");
- }
- }
-
- /**
- * 执行偷菜战斗
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标用户ID
- * @return array 战斗结果
- */
- protected function executeStealBattle(PetUser $pet, int $targetId): array
- {
- // 计算宠物战力
- $petPower = $this->calculatePetPower($pet->id);
-
- // 获取目标用户的守护宠物
- // 这里需要根据实际项目中的农场模块接口进行实现
- // 暂时使用模拟数据
- $targetPetPower = 0;
- $targetPet = null;
-
- // 模拟获取目标用户的守护宠物
- $targetPets = PetUser::where('user_id', $targetId)
- ->where('status', 1) // 正常状态
- ->get();
-
- if ($targetPets->isNotEmpty()) {
- // 找出战力最高的宠物作为守护宠物
- foreach ($targetPets as $tp) {
- $tp_power = $this->calculatePetPower($tp->id);
- if ($tp_power > $targetPetPower) {
- $targetPetPower = $tp_power;
- $targetPet = $tp;
- }
- }
- }
-
- // 计算成功率
- $successRate = $this->calculateStealSuccessRate($petPower, $targetPetPower);
-
- // 随机决定是否成功
- $random = mt_rand(1, 100);
- $success = $random <= $successRate;
-
- // 计算奖励
- $rewards = [];
- if ($success) {
- // 成功偷菜,获得奖励
- $rewards = $this->calculateStealRewards($pet, $targetId);
- }
-
- // 记录战斗详情
- $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_id' => $targetPet ? $targetPet->id : null,
- 'pet_name' => $targetPet ? $targetPet->name : '无守护宠物',
- 'pet_power' => $targetPetPower
- ],
- 'rewards' => $rewards,
- 'details' => $details
- ];
- }
-
- /**
- * 执行守护战斗
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标用户ID
- * @return array 战斗结果
- */
- protected function executeDefendBattle(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 PetUser $pet 宠物对象
- * @param int $targetId 目标Boss ID或对手宠物ID
- * @return array 战斗结果
- */
- protected function executeRoyaleBattle(PetUser $pet, int $targetId): array
- {
- // 计算宠物战力
- $petPower = $this->calculatePetPower($pet->id);
-
- // 获取目标Boss或对手宠物信息
- // 这里需要根据实际项目中的争霸赛系统进行实现
- // 暂时使用模拟数据
- $targetPetPower = 0;
- $targetPet = null;
-
- if ($targetId > 0) {
- // 对战其他宠物
- $targetPet = PetUser::find($targetId);
- if ($targetPet) {
- $targetPetPower = $this->calculatePetPower($targetId);
- }
- } else {
- // 对战Boss
- $targetPetPower = 5000; // 模拟Boss战力
- }
-
- // 计算成功率
- $successRate = $this->calculateRoyaleSuccessRate($petPower, $targetPetPower);
-
- // 随机决定是否成功
- $random = mt_rand(1, 100);
- $success = $random <= $successRate;
-
- // 计算奖励
- $rewards = [];
- if ($success) {
- // 战斗胜利,获得奖励
- $rewards = $this->calculateRoyaleRewards($pet, $targetId);
- }
-
- // 计算造成的伤害(用于争霸赛排名)
- $damage = $this->calculateRoyaleDamage($petPower, $targetPetPower, $success);
-
- // 记录战斗详情
- $details = [
- 'pet_power' => $petPower,
- 'target_pet_power' => $targetPetPower,
- 'success_rate' => $successRate,
- 'random_roll' => $random,
- 'success' => $success,
- 'damage' => $damage
- ];
-
- // 返回战斗结果
- return [
- 'success' => $success,
- 'opponent_id' => $targetId,
- 'opponent' => $targetPet ? [
- 'pet_id' => $targetPet->id,
- 'pet_name' => $targetPet->name,
- 'pet_grade' => $targetPet->grade->value,
- 'pet_level' => $targetPet->level,
- 'pet_power' => $targetPetPower
- ] : [
- 'boss_id' => 0,
- 'boss_name' => 'Boss',
- 'boss_power' => $targetPetPower
- ],
- 'rewards' => $rewards,
- 'damage' => $damage,
- '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;
-
- // 品阶加成
- $gradeBonus = config('pet.grade_attribute_bonus.' . $pet->grade->value, 0);
-
- // 计算最终战力
- $power = $basePower * (1 + $gradeBonus);
-
- // 记录日志
- Log::info('计算宠物战力', [
- 'pet_id' => $petId,
- 'level' => $pet->level,
- 'grade' => $pet->grade->value,
- 'base_power' => $basePower,
- 'grade_bonus' => $gradeBonus,
- 'final_power' => $power
- ]);
-
- return (int)$power;
- }
-
- /**
- * 计算偷菜成功率
- *
- * @param int $petPower 宠物战力
- * @param int $targetPetPower 目标宠物战力
- * @return int 成功率(百分比)
- */
- protected function calculateStealSuccessRate(int $petPower, int $targetPetPower): int
- {
- // 如果目标没有守护宠物,成功率较高
- if ($targetPetPower <= 0) {
- return 80;
- }
-
- // 基础成功率
- $baseRate = 50;
-
- // 根据战力差计算成功率
- $powerDiff = $petPower - $targetPetPower;
- $rateAdjustment = $powerDiff / 100; // 每100点战力差调整1%成功率
-
- // 最终成功率,限制在10%-90%之间
- $finalRate = max(10, min(90, $baseRate + $rateAdjustment));
-
- return (int)$finalRate;
- }
-
- /**
- * 计算守护成功率
- *
- * @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 int $petPower 宠物战力
- * @param int $targetPetPower 目标宠物战力
- * @return int 成功率(百分比)
- */
- protected function calculateRoyaleSuccessRate(int $petPower, int $targetPetPower): int
- {
- // 基础成功率
- $baseRate = 40;
-
- // 根据战力差计算成功率
- $powerDiff = $petPower - $targetPetPower;
- $rateAdjustment = $powerDiff / 150; // 每150点战力差调整1%成功率
-
- // 最终成功率,限制在5%-85%之间
- $finalRate = max(5, min(85, $baseRate + $rateAdjustment));
-
- return (int)$finalRate;
- }
-
- /**
- * 计算偷菜奖励
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标用户ID
- * @return array 奖励列表
- */
- protected function calculateStealRewards(PetUser $pet, int $targetId): array
- {
- // 这里需要根据实际项目中的农场模块接口进行实现
- // 暂时使用模拟数据
-
- // 模拟获取可偷取的作物
- $availableCrops = [
- ['item_id' => 1001, 'name' => '小麦', 'max_amount' => 5],
- ['item_id' => 1002, 'name' => '胡萝卜', 'max_amount' => 3],
- ['item_id' => 1003, 'name' => '土豆', 'max_amount' => 2]
- ];
-
- // 随机选择1-2种作物
- $cropCount = mt_rand(1, min(2, count($availableCrops)));
- $selectedCrops = array_rand($availableCrops, $cropCount);
-
- if (!is_array($selectedCrops)) {
- $selectedCrops = [$selectedCrops];
- }
-
- // 计算偷取数量和经验值
- $rewards = [
- 'exp' => mt_rand(5, 15), // 获得5-15点经验
- 'items' => []
- ];
-
- foreach ($selectedCrops as $index) {
- $crop = $availableCrops[$index];
- $amount = mt_rand(1, $crop['max_amount']);
-
- $rewards['items'][] = [
- 'item_id' => $crop['item_id'],
- 'name' => $crop['name'],
- 'amount' => $amount
- ];
- }
-
- return $rewards;
- }
-
- /**
- * 计算守护奖励
- *
- * @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;
- }
-
- /**
- * 计算争霸赛奖励
- *
- * @param PetUser $pet 宠物对象
- * @param int $targetId 目标ID
- * @return array 奖励列表
- */
- protected function calculateRoyaleRewards(PetUser $pet, int $targetId): array
- {
- // 这里需要根据实际项目中的争霸赛系统进行实现
- // 暂时使用模拟数据
-
- // 计算争霸赛奖励和经验值
- $rewards = [
- 'exp' => mt_rand(20, 40), // 获得20-40点经验
- 'items' => []
- ];
-
- // 如果是击败Boss,获得更多奖励
- if ($targetId <= 0) {
- // Boss战奖励
- $bossRewardItems = [
- ['item_id' => 3001, 'name' => '争霸徽章', 'amount' => 1],
- ['item_id' => 3002, 'name' => '高级狗粮', 'amount' => mt_rand(1, 3)],
- ['item_id' => 3003, 'name' => '钻石', 'amount' => mt_rand(10, 30)]
- ];
-
- foreach ($bossRewardItems as $item) {
- $rewards['items'][] = [
- 'item_id' => $item['item_id'],
- 'name' => $item['name'],
- 'amount' => $item['amount']
- ];
- }
- } else {
- // PVP战奖励
- $pvpRewardItems = [
- ['item_id' => 3001, 'name' => '争霸徽章', 'amount' => 1],
- ['item_id' => 3004, 'name' => '荣誉点数', 'amount' => mt_rand(5, 15)]
- ];
-
- foreach ($pvpRewardItems as $item) {
- $rewards['items'][] = [
- 'item_id' => $item['item_id'],
- 'name' => $item['name'],
- 'amount' => $item['amount']
- ];
- }
- }
-
- return $rewards;
- }
-
- /**
- * 计算争霸赛造成的伤害
- *
- * @param int $petPower 宠物战力
- * @param int $targetPetPower 目标宠物战力
- * @param bool $success 是否成功
- * @return int 造成的伤害
- */
- protected function calculateRoyaleDamage(int $petPower, int $targetPetPower, bool $success): int
- {
- // 基础伤害
- $baseDamage = $petPower * 0.1;
-
- // 如果战斗成功,额外增加伤害
- if ($success) {
- $baseDamage *= 1.5;
- }
-
- // 根据目标战力调整伤害
- $powerRatio = $petPower / max(1, $targetPetPower);
- $damageAdjustment = $powerRatio * 0.5;
-
- // 最终伤害,加入随机因素
- $finalDamage = $baseDamage * (1 + $damageAdjustment) * (0.8 + mt_rand(0, 40) / 100);
-
- return (int)$finalDamage;
- }
- }
|