PetExpCumulativeHandler.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. namespace App\Module\Task\Handlers;
  3. use App\Module\Task\Contracts\ConditionHandlerInterface;
  4. use Illuminate\Support\Facades\Log;
  5. /**
  6. * 宠物经验累计条件处理器
  7. *
  8. * 处理宠物经验累计相关的任务条件验证和进度计算
  9. */
  10. class PetExpCumulativeHandler implements ConditionHandlerInterface
  11. {
  12. /**
  13. * 验证用户是否满足条件(用于前置条件检查)
  14. *
  15. * @param int $userId 用户ID
  16. * @param array $params 条件参数
  17. * @return bool 是否满足条件
  18. */
  19. public function validate(int $userId, array $params): bool
  20. {
  21. // 对于宠物经验累计条件,前置条件检查通常返回true
  22. // 因为这是一个累计型条件,不需要特殊的前置条件
  23. return true;
  24. }
  25. /**
  26. * 检查事件参数是否匹配条件(用于进度更新)
  27. *
  28. * @param array $eventParams 事件参数
  29. * @param array $conditionParams 条件参数
  30. * @return bool 是否匹配
  31. */
  32. public function isMatch(array $eventParams, array $conditionParams): bool
  33. {
  34. try {
  35. // 获取条件参数
  36. $petId = $conditionParams['pet_id'] ?? 0;
  37. $sourceType = $conditionParams['source_type'] ?? '';
  38. $minExp = $conditionParams['min_exp'] ?? 1;
  39. // 获取事件参数
  40. $eventPetId = $eventParams['pet_id'] ?? 0;
  41. $eventSourceType = $eventParams['source_type'] ?? '';
  42. $expGained = $eventParams['exp_gained'] ?? 0;
  43. // 检查经验值是否满足最小要求
  44. if ($expGained < $minExp) {
  45. Log::info('宠物经验不满足最小要求', [
  46. 'exp_gained' => $expGained,
  47. 'min_exp' => $minExp
  48. ]);
  49. return false;
  50. }
  51. // 检查宠物ID过滤(0表示任意宠物)
  52. if ($petId > 0 && $eventPetId != $petId) {
  53. Log::info('宠物ID不匹配', [
  54. 'event_pet_id' => $eventPetId,
  55. 'condition_pet_id' => $petId
  56. ]);
  57. return false;
  58. }
  59. // 检查来源类型过滤(空表示任意来源)
  60. if (!empty($sourceType) && $eventSourceType != $sourceType) {
  61. Log::info('经验来源类型不匹配', [
  62. 'event_source_type' => $eventSourceType,
  63. 'condition_source_type' => $sourceType
  64. ]);
  65. return false;
  66. }
  67. return true;
  68. } catch (\Exception $e) {
  69. Log::error('宠物经验累计条件匹配检查失败', [
  70. 'event_params' => $eventParams,
  71. 'condition_params' => $conditionParams,
  72. 'error' => $e->getMessage()
  73. ]);
  74. return false;
  75. }
  76. }
  77. /**
  78. * 计算事件对条件进度的贡献值
  79. *
  80. * @param array $eventParams 事件参数
  81. * @param array $conditionParams 条件参数
  82. * @return int 进度增量
  83. */
  84. public function calculateProgress(array $eventParams, array $conditionParams): int
  85. {
  86. try {
  87. // 先检查是否匹配
  88. if (!$this->isMatch($eventParams, $conditionParams)) {
  89. return 0;
  90. }
  91. // 返回实际获得的经验值作为进度增量
  92. return $eventParams['exp_gained'] ?? 0;
  93. } catch (\Exception $e) {
  94. Log::error('宠物经验累计进度计算失败', [
  95. 'event_params' => $eventParams,
  96. 'condition_params' => $conditionParams,
  97. 'error' => $e->getMessage()
  98. ]);
  99. return 0;
  100. }
  101. }
  102. /**
  103. * 检查条件是否已完成
  104. *
  105. * @param int $currentValue 当前值
  106. * @param int $targetValue 目标值
  107. * @param string $operator 运算符
  108. * @return bool 是否已完成
  109. */
  110. public function isCompleted(int $currentValue, int $targetValue, string $operator = '>='): bool
  111. {
  112. switch ($operator) {
  113. case '=':
  114. return $currentValue == $targetValue;
  115. case '>=':
  116. return $currentValue >= $targetValue;
  117. case '>':
  118. return $currentValue > $targetValue;
  119. case '<=':
  120. return $currentValue <= $targetValue;
  121. case '<':
  122. return $currentValue < $targetValue;
  123. default:
  124. return false;
  125. }
  126. }
  127. }