|
|
@@ -2,7 +2,7 @@
|
|
|
|
|
|
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;
|
|
|
@@ -10,7 +10,7 @@ use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
/**
|
|
|
* 宠物战斗逻辑类
|
|
|
- *
|
|
|
+ *
|
|
|
* 处理宠物战斗相关的业务逻辑,包括战力计算、战斗结果计算、
|
|
|
* 奖励发放等功能。该类是宠物战斗模块内部使用的核心逻辑类,
|
|
|
* 不对外提供服务,由PetBattleService调用。
|
|
|
@@ -19,7 +19,7 @@ class PetBattleLogic
|
|
|
{
|
|
|
/**
|
|
|
* 获取战斗体力消耗
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $battleType 战斗类型
|
|
|
* @return int 体力消耗
|
|
|
*/
|
|
|
@@ -29,21 +29,21 @@ class PetBattleLogic
|
|
|
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
|
|
|
@@ -54,26 +54,26 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 获取宠物信息
|
|
|
$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 战斗结果
|
|
|
@@ -82,18 +82,18 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 计算宠物战力
|
|
|
$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) {
|
|
|
@@ -104,21 +104,21 @@ class PetBattleLogic
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 计算成功率
|
|
|
$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,
|
|
|
@@ -127,7 +127,7 @@ class PetBattleLogic
|
|
|
'random_roll' => $random,
|
|
|
'success' => $success
|
|
|
];
|
|
|
-
|
|
|
+
|
|
|
// 返回战斗结果
|
|
|
return [
|
|
|
'success' => $success,
|
|
|
@@ -142,10 +142,10 @@ class PetBattleLogic
|
|
|
'details' => $details
|
|
|
];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 执行守护战斗
|
|
|
- *
|
|
|
+ *
|
|
|
* @param PetUser $pet 宠物对象
|
|
|
* @param int $targetId 目标用户ID
|
|
|
* @return array 战斗结果
|
|
|
@@ -154,26 +154,26 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 计算宠物战力
|
|
|
$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,
|
|
|
@@ -182,7 +182,7 @@ class PetBattleLogic
|
|
|
'random_roll' => $random,
|
|
|
'success' => $success
|
|
|
];
|
|
|
-
|
|
|
+
|
|
|
// 返回战斗结果
|
|
|
return [
|
|
|
'success' => $success,
|
|
|
@@ -195,10 +195,10 @@ class PetBattleLogic
|
|
|
'details' => $details
|
|
|
];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 执行争霸赛战斗
|
|
|
- *
|
|
|
+ *
|
|
|
* @param PetUser $pet 宠物对象
|
|
|
* @param int $targetId 目标Boss ID或对手宠物ID
|
|
|
* @return array 战斗结果
|
|
|
@@ -207,13 +207,13 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 计算宠物战力
|
|
|
$petPower = $this->calculatePetPower($pet->id);
|
|
|
-
|
|
|
+
|
|
|
// 获取目标Boss或对手宠物信息
|
|
|
// 这里需要根据实际项目中的争霸赛系统进行实现
|
|
|
// 暂时使用模拟数据
|
|
|
$targetPetPower = 0;
|
|
|
$targetPet = null;
|
|
|
-
|
|
|
+
|
|
|
if ($targetId > 0) {
|
|
|
// 对战其他宠物
|
|
|
$targetPet = PetUser::find($targetId);
|
|
|
@@ -224,24 +224,24 @@ class PetBattleLogic
|
|
|
// 对战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,
|
|
|
@@ -251,7 +251,7 @@ class PetBattleLogic
|
|
|
'success' => $success,
|
|
|
'damage' => $damage
|
|
|
];
|
|
|
-
|
|
|
+
|
|
|
// 返回战斗结果
|
|
|
return [
|
|
|
'success' => $success,
|
|
|
@@ -272,10 +272,10 @@ class PetBattleLogic
|
|
|
'details' => $details
|
|
|
];
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 计算宠物战力
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $petId 宠物ID
|
|
|
* @return int 战力值
|
|
|
*/
|
|
|
@@ -283,19 +283,19 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 获取宠物信息
|
|
|
$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,
|
|
|
@@ -305,13 +305,13 @@ class PetBattleLogic
|
|
|
'grade_bonus' => $gradeBonus,
|
|
|
'final_power' => $power
|
|
|
]);
|
|
|
-
|
|
|
+
|
|
|
return (int)$power;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 计算偷菜成功率
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $petPower 宠物战力
|
|
|
* @param int $targetPetPower 目标宠物战力
|
|
|
* @return int 成功率(百分比)
|
|
|
@@ -322,23 +322,23 @@ class PetBattleLogic
|
|
|
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 成功率(百分比)
|
|
|
@@ -347,20 +347,20 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 基础成功率
|
|
|
$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 成功率(百分比)
|
|
|
@@ -369,20 +369,20 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 基础成功率
|
|
|
$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 奖励列表
|
|
|
@@ -391,45 +391,45 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 这里需要根据实际项目中的农场模块接口进行实现
|
|
|
// 暂时使用模拟数据
|
|
|
-
|
|
|
+
|
|
|
// 模拟获取可偷取的作物
|
|
|
$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 奖励列表
|
|
|
*/
|
|
|
@@ -437,27 +437,27 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 这里需要根据实际项目中的奖励系统进行实现
|
|
|
// 暂时使用模拟数据
|
|
|
-
|
|
|
+
|
|
|
// 计算守护奖励和经验值
|
|
|
$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']) :
|
|
|
+ $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'],
|
|
|
@@ -465,13 +465,13 @@ class PetBattleLogic
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $rewards;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 计算争霸赛奖励
|
|
|
- *
|
|
|
+ *
|
|
|
* @param PetUser $pet 宠物对象
|
|
|
* @param int $targetId 目标ID
|
|
|
* @return array 奖励列表
|
|
|
@@ -480,13 +480,13 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 这里需要根据实际项目中的争霸赛系统进行实现
|
|
|
// 暂时使用模拟数据
|
|
|
-
|
|
|
+
|
|
|
// 计算争霸赛奖励和经验值
|
|
|
$rewards = [
|
|
|
'exp' => mt_rand(20, 40), // 获得20-40点经验
|
|
|
'items' => []
|
|
|
];
|
|
|
-
|
|
|
+
|
|
|
// 如果是击败Boss,获得更多奖励
|
|
|
if ($targetId <= 0) {
|
|
|
// Boss战奖励
|
|
|
@@ -495,7 +495,7 @@ class PetBattleLogic
|
|
|
['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'],
|
|
|
@@ -509,7 +509,7 @@ class PetBattleLogic
|
|
|
['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'],
|
|
|
@@ -518,13 +518,13 @@ class PetBattleLogic
|
|
|
];
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
return $rewards;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* 计算争霸赛造成的伤害
|
|
|
- *
|
|
|
+ *
|
|
|
* @param int $petPower 宠物战力
|
|
|
* @param int $targetPetPower 目标宠物战力
|
|
|
* @param bool $success 是否成功
|
|
|
@@ -534,19 +534,19 @@ class PetBattleLogic
|
|
|
{
|
|
|
// 基础伤害
|
|
|
$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;
|
|
|
}
|
|
|
}
|