| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- namespace App\Module\Task\Handlers;
- use App\Module\Task\Contracts\ConditionHandlerInterface;
- use Illuminate\Support\Facades\Log;
- /**
- * 宠物经验累计条件处理器
- *
- * 处理宠物经验累计相关的任务条件验证和进度计算
- */
- class PetExpCumulativeHandler implements ConditionHandlerInterface
- {
- /**
- * 验证用户是否满足条件(用于前置条件检查)
- *
- * @param int $userId 用户ID
- * @param array $params 条件参数
- * @return bool 是否满足条件
- */
- public function validate(int $userId, array $params): bool
- {
- // 对于宠物经验累计条件,前置条件检查通常返回true
- // 因为这是一个累计型条件,不需要特殊的前置条件
- return true;
- }
- /**
- * 检查事件参数是否匹配条件(用于进度更新)
- *
- * @param array $eventParams 事件参数
- * @param array $conditionParams 条件参数
- * @return bool 是否匹配
- */
- public function isMatch(array $eventParams, array $conditionParams): bool
- {
- try {
- // 获取条件参数
- $petId = $conditionParams['pet_id'] ?? 0;
- $sourceType = $conditionParams['source_type'] ?? '';
- $minExp = $conditionParams['min_exp'] ?? 1;
-
- // 获取事件参数
- $eventPetId = $eventParams['pet_id'] ?? 0;
- $eventSourceType = $eventParams['source_type'] ?? '';
- $expGained = $eventParams['exp_gained'] ?? 0;
-
- // 检查经验值是否满足最小要求
- if ($expGained < $minExp) {
- Log::info('宠物经验不满足最小要求', [
- 'exp_gained' => $expGained,
- 'min_exp' => $minExp
- ]);
- return false;
- }
-
- // 检查宠物ID过滤(0表示任意宠物)
- if ($petId > 0 && $eventPetId != $petId) {
- Log::info('宠物ID不匹配', [
- 'event_pet_id' => $eventPetId,
- 'condition_pet_id' => $petId
- ]);
- return false;
- }
-
- // 检查来源类型过滤(空表示任意来源)
- if (!empty($sourceType) && $eventSourceType != $sourceType) {
- Log::info('经验来源类型不匹配', [
- 'event_source_type' => $eventSourceType,
- 'condition_source_type' => $sourceType
- ]);
- return false;
- }
-
- return true;
- } catch (\Exception $e) {
- Log::error('宠物经验累计条件匹配检查失败', [
- 'event_params' => $eventParams,
- 'condition_params' => $conditionParams,
- 'error' => $e->getMessage()
- ]);
-
- return false;
- }
- }
- /**
- * 计算事件对条件进度的贡献值
- *
- * @param array $eventParams 事件参数
- * @param array $conditionParams 条件参数
- * @return int 进度增量
- */
- public function calculateProgress(array $eventParams, array $conditionParams): int
- {
- try {
- // 先检查是否匹配
- if (!$this->isMatch($eventParams, $conditionParams)) {
- return 0;
- }
-
- // 返回实际获得的经验值作为进度增量
- return $eventParams['exp_gained'] ?? 0;
- } catch (\Exception $e) {
- Log::error('宠物经验累计进度计算失败', [
- 'event_params' => $eventParams,
- 'condition_params' => $conditionParams,
- 'error' => $e->getMessage()
- ]);
-
- return 0;
- }
- }
- /**
- * 检查条件是否已完成
- *
- * @param int $currentValue 当前值
- * @param int $targetValue 目标值
- * @param string $operator 运算符
- * @return bool 是否已完成
- */
- public function isCompleted(int $currentValue, int $targetValue, string $operator = '>='): bool
- {
- switch ($operator) {
- case '=':
- return $currentValue == $targetValue;
- case '>=':
- return $currentValue >= $targetValue;
- case '>':
- return $currentValue > $targetValue;
- case '<=':
- return $currentValue <= $targetValue;
- case '<':
- return $currentValue < $targetValue;
- default:
- return false;
- }
- }
- }
|