|
|
@@ -520,10 +520,45 @@ class PetLogic
|
|
|
|
|
|
if ($lastUsed) {
|
|
|
$cooldownSeconds = $skill->cool_down;
|
|
|
- $secondsSinceLastUse = now()->diffInSeconds($lastUsed->used_at);
|
|
|
+ $now = now();
|
|
|
+ $lastUsedTime = $lastUsed->used_at;
|
|
|
+
|
|
|
+ // 确保时间计算的正确性
|
|
|
+ if ($lastUsedTime instanceof \Carbon\Carbon) {
|
|
|
+ $secondsSinceLastUse = $now->diffInSeconds($lastUsedTime, false);
|
|
|
+ } else {
|
|
|
+ // 如果不是Carbon对象,尝试解析
|
|
|
+ $lastUsedTime = \Carbon\Carbon::parse($lastUsedTime);
|
|
|
+ $secondsSinceLastUse = $now->diffInSeconds($lastUsedTime, false);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果secondsSinceLastUse为负数,说明lastUsed时间在未来,这是异常情况
|
|
|
+ if ($secondsSinceLastUse < 0) {
|
|
|
+ Log::warning('检测到异常的技能使用时间', [
|
|
|
+ 'pet_id' => $petId,
|
|
|
+ 'skill_id' => $skillId,
|
|
|
+ 'last_used_at' => $lastUsedTime->toDateTimeString(),
|
|
|
+ 'current_time' => $now->toDateTimeString(),
|
|
|
+ 'seconds_diff' => $secondsSinceLastUse
|
|
|
+ ]);
|
|
|
+ // 异常情况下,重置为0,允许技能使用
|
|
|
+ $secondsSinceLastUse = 0;
|
|
|
+ }
|
|
|
|
|
|
if ($secondsSinceLastUse < $cooldownSeconds) {
|
|
|
$remainingCooldown = $cooldownSeconds - $secondsSinceLastUse;
|
|
|
+
|
|
|
+ // 记录详细的冷却信息用于调试
|
|
|
+ Log::info('技能冷却检查', [
|
|
|
+ 'pet_id' => $petId,
|
|
|
+ 'skill_id' => $skillId,
|
|
|
+ 'cooldown_seconds' => $cooldownSeconds,
|
|
|
+ 'seconds_since_last_use' => $secondsSinceLastUse,
|
|
|
+ 'remaining_cooldown' => $remainingCooldown,
|
|
|
+ 'last_used_at' => $lastUsedTime->toDateTimeString(),
|
|
|
+ 'current_time' => $now->toDateTimeString()
|
|
|
+ ]);
|
|
|
+
|
|
|
throw new Exception("技能冷却中,还需等待 {$remainingCooldown} 秒");
|
|
|
}
|
|
|
}
|
|
|
@@ -533,14 +568,28 @@ class PetLogic
|
|
|
throw new Exception("体力不足,无法使用技能");
|
|
|
}
|
|
|
|
|
|
- // 消耗体力
|
|
|
+ // 先执行技能效果,确保技能能够成功激活
|
|
|
+ $effectResult = $this->executeSkillEffect($pet, $skill, $params);
|
|
|
+
|
|
|
+ // 检查技能效果是否成功
|
|
|
+ if (isset($effectResult['success']) && $effectResult['success'] === false) {
|
|
|
+ // 技能效果执行失败,记录失败日志但不消耗体力,不进入冷却
|
|
|
+ Log::warning('宠物技能使用失败', [
|
|
|
+ 'pet_id' => $petId,
|
|
|
+ 'skill_id' => $skillId,
|
|
|
+ 'params' => $params,
|
|
|
+ 'effect_result' => $effectResult,
|
|
|
+ 'reason' => $effectResult['message'] ?? '技能使用失败'
|
|
|
+ ]);
|
|
|
+
|
|
|
+ throw new Exception($effectResult['message'] ?? '技能使用失败');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 技能效果成功,消耗体力
|
|
|
$pet->stamina -= $skill->stamina_cost;
|
|
|
$pet->save();
|
|
|
|
|
|
- // 执行技能效果
|
|
|
- $effectResult = $this->executeSkillEffect($pet, $skill, $params);
|
|
|
-
|
|
|
- // 记录技能使用日志
|
|
|
+ // 记录技能使用日志(只有成功时才记录)
|
|
|
$skillLog = PetSkillLog::create([
|
|
|
'pet_id' => $petId,
|
|
|
'skill_id' => $skillId,
|
|
|
@@ -580,15 +629,17 @@ class PetLogic
|
|
|
{
|
|
|
// 根据技能名称执行不同的效果
|
|
|
switch ($skill->skill_name) {
|
|
|
- case '自动收菜':
|
|
|
+ case '自动收获':
|
|
|
return $this->activateAutoHarvestSkill($pet, $skill, $params);
|
|
|
|
|
|
- case '自动播种':
|
|
|
+ case '自动种植':
|
|
|
return $this->activateAutoPlantSkill($pet, $skill, $params);
|
|
|
|
|
|
case '灾害防护':
|
|
|
return $this->activateDisasterProtectionSkill($pet, $skill, $params);
|
|
|
-
|
|
|
+ //自动除草
|
|
|
+ //自动浇水
|
|
|
+ //自动杀虫
|
|
|
default:
|
|
|
return [
|
|
|
'success' => false,
|
|
|
@@ -607,6 +658,20 @@ class PetLogic
|
|
|
*/
|
|
|
protected function activateAutoHarvestSkill(PetUser $pet, PetSkill $skill, array $params): array
|
|
|
{
|
|
|
+ // 检查是否已有相同技能在激活中
|
|
|
+ $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id)
|
|
|
+ ->where('skill_name', $skill->skill_name)
|
|
|
+ ->where('status', 'active')
|
|
|
+ ->where('end_time', '>', now())
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($existingActiveSkill) {
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '该技能已经在激活中,无法重复激活'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
// 技能持续时间(默认2小时)
|
|
|
$duration = $params['duration'] ?? 7200; // 2小时
|
|
|
$endTime = now()->addSeconds($duration);
|
|
|
@@ -653,6 +718,20 @@ class PetLogic
|
|
|
*/
|
|
|
protected function activateAutoPlantSkill(PetUser $pet, PetSkill $skill, array $params): array
|
|
|
{
|
|
|
+ // 检查是否已有相同技能在激活中
|
|
|
+ $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id)
|
|
|
+ ->where('skill_name', $skill->skill_name)
|
|
|
+ ->where('status', 'active')
|
|
|
+ ->where('end_time', '>', now())
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($existingActiveSkill) {
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '该技能已经在激活中,无法重复激活'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
// 技能持续时间(默认4小时)
|
|
|
$duration = $params['duration'] ?? 14400; // 4小时
|
|
|
$endTime = now()->addSeconds($duration);
|
|
|
@@ -700,6 +779,20 @@ class PetLogic
|
|
|
*/
|
|
|
protected function activateDisasterProtectionSkill(PetUser $pet, PetSkill $skill, array $params): array
|
|
|
{
|
|
|
+ // 检查是否已有相同技能在激活中
|
|
|
+ $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id)
|
|
|
+ ->where('skill_name', $skill->skill_name)
|
|
|
+ ->where('status', 'active')
|
|
|
+ ->where('end_time', '>', now())
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if ($existingActiveSkill) {
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'message' => '该技能已经在激活中,无法重复激活'
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
// 技能持续时间(默认6小时)
|
|
|
$duration = $params['duration'] ?? 21600; // 6小时
|
|
|
$endTime = now()->addSeconds($duration);
|